Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

throw away while loop in block & biome registry #198

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions core/src/main/java/net/pl3x/map/core/registry/BiomeRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

public class BiomeRegistry extends Registry<@NotNull Biome> {
private static final Gson GSON = new GsonBuilder().create();
private static final int MAX_INDEX = 1023;

private final Map<String, Integer> indexMap;

Expand Down Expand Up @@ -67,21 +68,23 @@ private int getNextIndex(String id) {
if (index > -1) {
return index;
}
int i = 0;
while (true) {
if (!this.indexMap.containsValue(i)) {
this.indexMap.put(id, i);
return i;
}
i++;
}

int i = this.indexMap.size();
if (i > MAX_INDEX) return -1;

this.indexMap.put(id, i);
return i;
}

public @NotNull Biome register(@NotNull String id, int color, int foliage, int grass, int water, Biome.@NotNull GrassModifier grassModifier) {
public Biome register(@NotNull String id, int color, int foliage, int grass, int water, Biome.@NotNull GrassModifier grassModifier) {
if (has(id)) {
throw new KeyAlreadyExistsException("Biome already registered: " + id);
}
return register(id, new Biome(getNextIndex(id), id, color, foliage, grass, water, grassModifier));

int nextIndex = getNextIndex(id);
if (nextIndex == -1) return null;

return register(id, new Biome(nextIndex, id, color, foliage, grass, water, grassModifier));
}

@Override
Expand Down
27 changes: 17 additions & 10 deletions core/src/main/java/net/pl3x/map/core/registry/BlockRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

public class BlockRegistry extends Registry<@NotNull Block> {
private static final Gson GSON = new GsonBuilder().create();
private static final int MAX_INDEX = 1023;

private final Map<String, Integer> indexMap;

Expand All @@ -64,30 +65,36 @@ public void init() {
}
}

public Map<String, Integer> getIndexMap() {
return indexMap;
}

private int getNextIndex(String id) {
int index = this.indexMap.getOrDefault(id, -1);
if (index > -1) {
return index;
}
int i = 0;
while (true) {
if (!this.indexMap.containsValue(i)) {
this.indexMap.put(id, i);
return i;
}
i++;
}

int i = this.indexMap.size();
if (i > MAX_INDEX) return -1;

this.indexMap.put(id, i);
return i;
}

public @NotNull Block register(@NotNull String id, int color) {
public Block register(@NotNull String id, int color) {
Block block = super.get(id);
if (block != null) {
return block; // block already registered
}
if (id.startsWith("minecraft:")) {
Logger.warn("Registering unknown vanilla block " + id);
}
return register(id, new Block(getNextIndex(id), id, color));

int nextIndex = getNextIndex(id);
if (nextIndex == -1) return null;

return register(id, new Block(nextIndex, id, color));
}

@Override
Expand Down
5 changes: 4 additions & 1 deletion core/src/main/java/net/pl3x/map/core/world/Blocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,9 @@ public class Blocks {
}

public static void registerDefaults() {
blocks.forEach((id, block) -> Pl3xMap.api().getBlockRegistry().register(id, block));
blocks.forEach((id, block) -> {
Pl3xMap.api().getBlockRegistry().register(id, block);
Pl3xMap.api().getBlockRegistry().getIndexMap().put(id, block.getIndex());
});
}
}
Loading