From 86c822631c025e9f82771e79e5ceb91d3087f285 Mon Sep 17 00:00:00 2001 From: davchoo Date: Sun, 10 Jul 2022 12:26:52 -0400 Subject: [PATCH] Clean up custom block api a bit --- .../api/block/custom/CustomBlockData.java | 3 +- .../api/block/custom/CustomBlockState.java | 2 ++ .../custom/property/CustomBlockProperty.java | 2 +- .../block/custom/property/PropertyType.java | 15 +++++----- .../level/block/GeyserCustomBlockData.java | 15 ++++++++-- .../block/GeyserCustomBlockProperty.java | 28 ++----------------- .../level/block/GeyserCustomBlockState.java | 13 ++++++--- .../populator/BlockRegistryPopulator.java | 2 +- 8 files changed, 35 insertions(+), 45 deletions(-) diff --git a/api/geyser/src/main/java/org/geysermc/geyser/api/block/custom/CustomBlockData.java b/api/geyser/src/main/java/org/geysermc/geyser/api/block/custom/CustomBlockData.java index 5d8422499..8e2a7886f 100644 --- a/api/geyser/src/main/java/org/geysermc/geyser/api/block/custom/CustomBlockData.java +++ b/api/geyser/src/main/java/org/geysermc/geyser/api/block/custom/CustomBlockData.java @@ -53,13 +53,12 @@ public interface CustomBlockData { Builder components(@NonNull CustomBlockComponents components); - Builder booleanProperty(@NonNull String propertyName, List values); + Builder booleanProperty(@NonNull String propertyName); Builder intProperty(@NonNull String propertyName, List values); Builder stringProperty(@NonNull String propertyName, List values); - Builder permutations(@NonNull List permutations); CustomBlockData build(); diff --git a/api/geyser/src/main/java/org/geysermc/geyser/api/block/custom/CustomBlockState.java b/api/geyser/src/main/java/org/geysermc/geyser/api/block/custom/CustomBlockState.java index a630ecd69..14af446a0 100644 --- a/api/geyser/src/main/java/org/geysermc/geyser/api/block/custom/CustomBlockState.java +++ b/api/geyser/src/main/java/org/geysermc/geyser/api/block/custom/CustomBlockState.java @@ -30,6 +30,8 @@ import org.checkerframework.checker.nullness.qual.NonNull; import java.util.Map; public interface CustomBlockState { + @NonNull CustomBlockData block(); + @NonNull String name(); @NonNull T property(String propertyName); diff --git a/api/geyser/src/main/java/org/geysermc/geyser/api/block/custom/property/CustomBlockProperty.java b/api/geyser/src/main/java/org/geysermc/geyser/api/block/custom/property/CustomBlockProperty.java index 194e2dd7d..697cfe268 100644 --- a/api/geyser/src/main/java/org/geysermc/geyser/api/block/custom/property/CustomBlockProperty.java +++ b/api/geyser/src/main/java/org/geysermc/geyser/api/block/custom/property/CustomBlockProperty.java @@ -34,5 +34,5 @@ public interface CustomBlockProperty { @NonNull List values(); - @NonNull PropertyType type(); + @NonNull PropertyType type(); } diff --git a/api/geyser/src/main/java/org/geysermc/geyser/api/block/custom/property/PropertyType.java b/api/geyser/src/main/java/org/geysermc/geyser/api/block/custom/property/PropertyType.java index d3027f846..e4ac0f339 100644 --- a/api/geyser/src/main/java/org/geysermc/geyser/api/block/custom/property/PropertyType.java +++ b/api/geyser/src/main/java/org/geysermc/geyser/api/block/custom/property/PropertyType.java @@ -25,18 +25,17 @@ package org.geysermc.geyser.api.block.custom.property; -public class PropertyType { - public static final PropertyType BOOLEAN = new PropertyType<>(Boolean.class); - public static final PropertyType INTEGER = new PropertyType<>(Integer.class); - public static final PropertyType STRING = new PropertyType<>(String.class); +import lombok.Getter; +public class PropertyType { + public static final PropertyType BOOLEAN = new PropertyType(Boolean.class); + public static final PropertyType INTEGER = new PropertyType(Integer.class); + public static final PropertyType STRING = new PropertyType(String.class); + + @Getter private final Class typeClass; private PropertyType(Class typeClass) { this.typeClass = typeClass; } - - public Class getTypeClass() { - return typeClass; - } } diff --git a/core/src/main/java/org/geysermc/geyser/level/block/GeyserCustomBlockData.java b/core/src/main/java/org/geysermc/geyser/level/block/GeyserCustomBlockData.java index caf042038..5801305aa 100644 --- a/core/src/main/java/org/geysermc/geyser/level/block/GeyserCustomBlockData.java +++ b/core/src/main/java/org/geysermc/geyser/level/block/GeyserCustomBlockData.java @@ -28,6 +28,8 @@ package org.geysermc.geyser.level.block; import it.unimi.dsi.fastutil.objects.Object2ObjectMaps; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import it.unimi.dsi.fastutil.objects.ObjectLists; +import lombok.AccessLevel; +import lombok.AllArgsConstructor; import lombok.Value; import org.checkerframework.checker.nullness.qual.NonNull; import org.geysermc.geyser.api.block.custom.CustomBlockData; @@ -41,7 +43,11 @@ import java.util.List; import java.util.Map; @Value +@AllArgsConstructor(access = AccessLevel.PRIVATE) public class GeyserCustomBlockData implements CustomBlockData { + + public static final String GEYSER_NAMESPACE = "geyser:"; + String name; CustomBlockComponents components; Map> properties; @@ -54,7 +60,7 @@ public class GeyserCustomBlockData implements CustomBlockData { @Override public @NonNull String identifier() { - return "geyser:" + name; + return GEYSER_NAMESPACE + name; } @Override @@ -96,8 +102,8 @@ public class GeyserCustomBlockData implements CustomBlockData { } @Override - public Builder booleanProperty(@NonNull String propertyName, List values) { - this.properties.put(propertyName, new GeyserCustomBlockProperty<>(propertyName, values, PropertyType.BOOLEAN)); + public Builder booleanProperty(@NonNull String propertyName) { + this.properties.put(propertyName, new GeyserCustomBlockProperty<>(propertyName, List.of(false, true), PropertyType.BOOLEAN)); return this; } @@ -131,6 +137,9 @@ public class GeyserCustomBlockData implements CustomBlockData { if (property.values().isEmpty() || property.values().size() > 16) { throw new IllegalArgumentException(property.name() + " must contain 1 to 16 values."); } + if (property.values().stream().distinct().count() != property.values().size()) { + throw new IllegalArgumentException(property.name() + " has duplicate values."); + } } } if (permutations == null) { diff --git a/core/src/main/java/org/geysermc/geyser/level/block/GeyserCustomBlockProperty.java b/core/src/main/java/org/geysermc/geyser/level/block/GeyserCustomBlockProperty.java index b91e5cf8d..882b8c9f7 100644 --- a/core/src/main/java/org/geysermc/geyser/level/block/GeyserCustomBlockProperty.java +++ b/core/src/main/java/org/geysermc/geyser/level/block/GeyserCustomBlockProperty.java @@ -31,30 +31,6 @@ import org.geysermc.geyser.api.block.custom.property.PropertyType; import java.util.List; -public class GeyserCustomBlockProperty implements CustomBlockProperty { - private final String name; - private final List values; - - private final PropertyType type; - - public GeyserCustomBlockProperty(String name, List values, PropertyType type) { - this.name = name; - this.values = values; - this.type = type; - } - - @Override - public @NonNull String name() { - return name; - } - - @Override - public @NonNull List values(){ - return values; - } - - @Override - public @NonNull PropertyType type() { - return type; - } +public record GeyserCustomBlockProperty(@NonNull String name, @NonNull List values, + @NonNull PropertyType type) implements CustomBlockProperty { } diff --git a/core/src/main/java/org/geysermc/geyser/level/block/GeyserCustomBlockState.java b/core/src/main/java/org/geysermc/geyser/level/block/GeyserCustomBlockState.java index 506a72a93..5e195962a 100644 --- a/core/src/main/java/org/geysermc/geyser/level/block/GeyserCustomBlockState.java +++ b/core/src/main/java/org/geysermc/geyser/level/block/GeyserCustomBlockState.java @@ -39,12 +39,17 @@ import java.util.Map; @Value public class GeyserCustomBlockState implements CustomBlockState { - String name; + CustomBlockData block; Map properties; + @Override + public @NonNull CustomBlockData block() { + return block; + } + @Override public @NonNull String name() { - return name; + return block.name(); } @SuppressWarnings("unchecked") @@ -97,11 +102,11 @@ public class GeyserCustomBlockState implements CustomBlockState { if (property == null) { throw new IllegalArgumentException("Unknown property: " + propertyName); } else if (!property.values().contains(propertyValue)) { - throw new IllegalArgumentException("Invalid value for property: " + propertyName + " Value: " + propertyValue); + throw new IllegalArgumentException("Invalid value: " + propertyValue + " for property: " + propertyName); } } - return new GeyserCustomBlockState(blockData.name(), Object2ObjectMaps.unmodifiable(properties)); + return new GeyserCustomBlockState(blockData, Object2ObjectMaps.unmodifiable(properties)); } } } diff --git a/core/src/main/java/org/geysermc/geyser/registry/populator/BlockRegistryPopulator.java b/core/src/main/java/org/geysermc/geyser/registry/populator/BlockRegistryPopulator.java index 2fbefe03d..e13c65ade 100644 --- a/core/src/main/java/org/geysermc/geyser/registry/populator/BlockRegistryPopulator.java +++ b/core/src/main/java/org/geysermc/geyser/registry/populator/BlockRegistryPopulator.java @@ -135,7 +135,7 @@ public class BlockRegistryPopulator { .putInt("version", stateVersion) .putCompound("states", states) .build()); - customExtBlockStates.add(new GeyserCustomBlockState(customBlock.name(), states)); + customExtBlockStates.add(new GeyserCustomBlockState(customBlock, states)); } }