Skip to content

Commit

Permalink
Improve registry and tag code
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexProgrammerDE committed Nov 21, 2024
1 parent 5d0e831 commit 5b05cb7
Show file tree
Hide file tree
Showing 20 changed files with 243 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public static JsonObject generateEntity(EntityType<?> entityType) {

entityDesc.addProperty("width", dimensions.width());
entityDesc.addProperty("height", dimensions.height());
entityDesc.addProperty("eyeHeight", dimensions.eyeHeight());
if (dimensions.fixed()) {
entityDesc.addProperty("fixed", true);
}

entityDesc.addProperty("updateInterval", entityType.updateInterval());
entityDesc.addProperty("clientTrackingRange", entityType.clientTrackingRange());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ public boolean equals(Object o) {
public int hashCode() {
return id;
}

@Override
public Registry<AttributeType> registry() {
return REGISTRY;
}
}
5 changes: 5 additions & 0 deletions data-generator/src/main/resources/templates/BlockType.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public int hashCode() {
return id;
}

@Override
public Registry<BlockType> registry() {
return REGISTRY;
}

public record OffsetData(
float maxHorizontalOffset, float maxVerticalOffset, OffsetType offsetType) {
public enum OffsetType {
Expand Down
5 changes: 5 additions & 0 deletions data-generator/src/main/resources/templates/EffectType.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public int hashCode() {
return id;
}

@Override
public Registry<EffectType> registry() {
return REGISTRY;
}

public enum EffectCategory {
BENEFICIAL,
HARMFUL,
Expand Down
7 changes: 7 additions & 0 deletions data-generator/src/main/resources/templates/EntityType.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public record EntityType(
Key key,
float width,
float height,
float eyeHeight,
boolean fixed,
int updateInterval,
int clientTrackingRange,
String category,
Expand Down Expand Up @@ -63,4 +65,9 @@ public boolean equals(Object o) {
public int hashCode() {
return id;
}

@Override
public Registry<EntityType> registry() {
return REGISTRY;
}
}
5 changes: 5 additions & 0 deletions data-generator/src/main/resources/templates/FluidType.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ public boolean equals(Object o) {
public int hashCode() {
return id;
}

@Override
public Registry<FluidType> registry() {
return REGISTRY;
}
}
5 changes: 5 additions & 0 deletions data-generator/src/main/resources/templates/ItemType.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,9 @@ public boolean equals(Object o) {
public int hashCode() {
return id;
}

@Override
public Registry<ItemType> registry() {
return REGISTRY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,9 @@ public boolean equals(Object o) {
public int hashCode() {
return id;
}

@Override
public Registry<AttributeType> registry() {
return REGISTRY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1168,6 +1168,11 @@ public int hashCode() {
return id;
}

@Override
public Registry<BlockType> registry() {
return REGISTRY;
}

public record OffsetData(
float maxHorizontalOffset, float maxVerticalOffset, OffsetType offsetType) {
public enum OffsetType {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ public int hashCode() {
return id;
}

@Override
public Registry<EffectType> registry() {
return REGISTRY;
}

public enum EffectCategory {
BENEFICIAL,
HARMFUL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public record EntityType(
Key key,
float width,
float height,
float eyeHeight,
boolean fixed,
int updateInterval,
int clientTrackingRange,
String category,
Expand Down Expand Up @@ -212,4 +214,9 @@ public boolean equals(Object o) {
public int hashCode() {
return id;
}

@Override
public Registry<EntityType> registry() {
return REGISTRY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,9 @@ public boolean equals(Object o) {
public int hashCode() {
return id;
}

@Override
public Registry<FluidType> registry() {
return REGISTRY;
}
}
5 changes: 5 additions & 0 deletions server/src/main/java/com/soulfiremc/server/data/ItemType.java
Original file line number Diff line number Diff line change
Expand Up @@ -1426,4 +1426,9 @@ public boolean equals(Object o) {
public int hashCode() {
return id;
}

@Override
public Registry<ItemType> registry() {
return REGISTRY;
}
}
4 changes: 2 additions & 2 deletions server/src/main/java/com/soulfiremc/server/data/Registry.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public int size() {
}

public RegistryDataWriter writer(FromRegistryDataFactory<T> factory) {
return (key, id, data) -> register(factory.create(key, id, data));
return (key, id, data) -> register(factory.create(key, id, this, data));
}

public interface RegistryDataWriter {
Expand All @@ -72,6 +72,6 @@ public interface RegistryDataWriter {
}

public interface FromRegistryDataFactory<T extends RegistryValue<T>> {
T create(Key key, int id, NbtMap data);
T create(Key key, int id, Registry<T> registry, NbtMap data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ public interface RegistryValue<T extends RegistryValue<T>> {
int id();

Key key();

Registry<T> registry();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import net.kyori.adventure.key.Key;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Getter
Expand All @@ -39,6 +40,13 @@ public <T extends RegistryValue<T>> boolean is(T value, TagKey<T> tagKey) {
return Arrays.stream(getValuesOfTag(tagKey)).anyMatch(t -> t == value.id());
}

public <T extends RegistryValue<T>> List<TagKey<T>> getTags(T value) {
return tags.getOrDefault(value.registry().registryKey().key(), Map.of()).entrySet().stream()
.filter(entry -> Arrays.stream(entry.getValue()).anyMatch(t -> t == value.id()))
.map(entry -> new TagKey<>(value.registry().registryKey(), entry.getKey()))
.toList();
}

public <T extends RegistryValue<T>> int[] getValuesOfTag(TagKey<T> tagKey) {
return tags.getOrDefault(tagKey.registry().key(), Map.of())
.getOrDefault(tagKey.key(), EMPTY_INT_ARRAY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package com.soulfiremc.server.protocol.bot.state.registry;

import com.soulfiremc.server.data.Registry;
import com.soulfiremc.server.data.RegistryValue;
import lombok.Getter;
import net.kyori.adventure.key.Key;
Expand All @@ -26,12 +27,14 @@
public class Biome implements RegistryValue<Biome> {
private final Key key;
private final int id;
private final Registry<Biome> registry;
private final float temperature;
private final float downfall;

public Biome(Key key, int id, NbtMap biomeData) {
public Biome(Key key, int id, Registry<Biome> registry, NbtMap biomeData) {
this.key = key;
this.id = id;
this.registry = registry;
this.temperature = biomeData.getFloat("temperature");
this.downfall = biomeData.getFloat("downfall");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package com.soulfiremc.server.protocol.bot.state.registry;

import com.soulfiremc.server.data.Registry;
import com.soulfiremc.server.data.RegistryValue;
import lombok.Getter;
import net.kyori.adventure.key.Key;
Expand All @@ -27,6 +28,7 @@
public class DimensionType implements RegistryValue<DimensionType> {
private final Key key;
private final int id;
private final Registry<DimensionType> registry;
private final String infiniburn;
private final String effects;
private final byte ultrawarm;
Expand All @@ -45,9 +47,10 @@ public class DimensionType implements RegistryValue<DimensionType> {
private final byte hasRaids;
private final byte respawnAnchorWorks;

public DimensionType(Key key, int id, NbtMap dimensionTypeData) {
public DimensionType(Key key, int id, Registry<DimensionType> registry, NbtMap dimensionTypeData) {
this.key = key;
this.id = id;
this.registry = registry;
this.infiniburn = dimensionTypeData.getString("infiniburn");
this.effects = dimensionTypeData.getString("effects");
this.ultrawarm = dimensionTypeData.getByte("ultrawarm");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.soulfiremc.server.protocol.bot.state.registry;

import com.google.gson.JsonElement;
import com.soulfiremc.server.data.Registry;
import com.soulfiremc.server.data.RegistryValue;
import lombok.Getter;
import lombok.SneakyThrows;
Expand Down Expand Up @@ -54,11 +55,13 @@ public class SFChatType implements RegistryValue<SFChatType> {
private static final IStyleSerializer<JsonElement> JSON_STYLE_SERIALIZER = CODEC.getJsonSerializer().getStyleSerializer();
private final Key key;
private final int id;
private final Registry<SFChatType> registry;
private final ChatType mcplChatType;

public SFChatType(Key key, int id, NbtMap chatTypeData) {
public SFChatType(Key key, int id, Registry<SFChatType> registry, NbtMap chatTypeData) {
this.key = key;
this.id = id;
this.registry = registry;
this.mcplChatType = new ChatType(
readDecoration(chatTypeData.getCompound("chat")),
readDecoration(chatTypeData.getCompound("narration"))
Expand Down
Loading

0 comments on commit 5b05cb7

Please sign in to comment.