Address @Redned235's review

Signed-off-by: Joshua Castle <26531652+Kas-tle@users.noreply.github.com>
This commit is contained in:
Joshua Castle 2023-05-11 22:40:38 -07:00
parent d6ed896b2d
commit 410712cb78
No known key found for this signature in database
GPG key ID: F674F38216C35D5D
13 changed files with 89 additions and 36 deletions

View file

@ -55,7 +55,7 @@ public interface CustomBlockData {
* *
* @return The components of the custom block. * @return The components of the custom block.
*/ */
CustomBlockComponents components(); @NonNull CustomBlockComponents components();
/** /**
* Gets the custom block's map of block property names to CustomBlockProperty * Gets the custom block's map of block property names to CustomBlockProperty
@ -79,6 +79,11 @@ public interface CustomBlockData {
*/ */
@NonNull CustomBlockState defaultBlockState(); @NonNull CustomBlockState defaultBlockState();
/**
* Gets a builder for a custom block state
*
* @return The builder for a custom block state.
*/
CustomBlockState.@NonNull Builder blockStateBuilder(); CustomBlockState.@NonNull Builder blockStateBuilder();
interface Builder { interface Builder {

View file

@ -56,6 +56,11 @@ public interface CustomBlockState {
*/ */
@NonNull <T> T property(String propertyName); @NonNull <T> T property(String propertyName);
/**
* Gets a map of the properties for the state
*
* @return The properties for the state.
*/
@NonNull Map<String, Object> properties(); @NonNull Map<String, Object> properties();
interface Builder { interface Builder {

View file

@ -26,6 +26,7 @@
package org.geysermc.geyser.api.block.custom.component; package org.geysermc.geyser.api.block.custom.component;
import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -42,14 +43,14 @@ public interface CustomBlockComponents {
* *
* @return The selection box. * @return The selection box.
*/ */
BoxComponent selectionBox(); @Nullable BoxComponent selectionBox();
/** /**
* Gets the collision box component * Gets the collision box component
* Equivalent to "minecraft:collision_box" * Equivalent to "minecraft:collision_box"
* @return The collision box. * @return The collision box.
*/ */
BoxComponent collisionBox(); @Nullable BoxComponent collisionBox();
/** /**
* Gets the display name component * Gets the display name component
@ -57,7 +58,7 @@ public interface CustomBlockComponents {
* *
* @return The display name. * @return The display name.
*/ */
String displayName(); @Nullable String displayName();
/** /**
* Gets the geometry component * Gets the geometry component
@ -65,7 +66,7 @@ public interface CustomBlockComponents {
* *
* @return The geometry. * @return The geometry.
*/ */
String geometry(); @Nullable String geometry();
/** /**
* Gets the material instances component * Gets the material instances component
@ -82,7 +83,7 @@ public interface CustomBlockComponents {
* *
* @return The placement filter. * @return The placement filter.
*/ */
List<PlacementConditions> placementFilter(); @Nullable List<PlacementConditions> placementFilter();
/** /**
* Gets the destructible by mining component * Gets the destructible by mining component
@ -90,7 +91,7 @@ public interface CustomBlockComponents {
* *
* @return The destructible by mining value. * @return The destructible by mining value.
*/ */
Float destructibleByMining(); @Nullable Float destructibleByMining();
/** /**
* Gets the friction component * Gets the friction component
@ -98,7 +99,7 @@ public interface CustomBlockComponents {
* *
* @return The friction value. * @return The friction value.
*/ */
Float friction(); @Nullable Float friction();
/** /**
* Gets the light emission component * Gets the light emission component
@ -106,7 +107,7 @@ public interface CustomBlockComponents {
* *
* @return The light emission value. * @return The light emission value.
*/ */
Integer lightEmission(); @Nullable Integer lightEmission();
/** /**
* Gets the light dampening component * Gets the light dampening component
@ -114,7 +115,7 @@ public interface CustomBlockComponents {
* *
* @return The light dampening value. * @return The light dampening value.
*/ */
Integer lightDampening(); @Nullable Integer lightDampening();
/** /**
* Gets the rotation component * Gets the rotation component
@ -122,7 +123,7 @@ public interface CustomBlockComponents {
* *
* @return The rotation. * @return The rotation.
*/ */
RotationComponent rotation(); @Nullable RotationComponent rotation();
/** /**
* Gets the unit cube component * Gets the unit cube component
@ -130,7 +131,7 @@ public interface CustomBlockComponents {
* *
* @return The rotation. * @return The rotation.
*/ */
boolean unitCube(); @Nullable boolean unitCube();
/** /**
* Gets if the block should place only air * Gets if the block should place only air
@ -138,7 +139,7 @@ public interface CustomBlockComponents {
* *
* @return If the block should place only air. * @return If the block should place only air.
*/ */
boolean placeAir(); @Nullable boolean placeAir();
/** /**
* Gets the set of tags * Gets the set of tags
@ -146,7 +147,7 @@ public interface CustomBlockComponents {
* *
* @return The set of tags. * @return The set of tags.
*/ */
Set<String> tags(); @Nullable Set<String> tags();
interface Builder { interface Builder {
Builder selectionBox(BoxComponent selectionBox); Builder selectionBox(BoxComponent selectionBox);

View file

@ -33,9 +33,24 @@ import java.util.List;
* This class is used to store a property of a custom block of a generic type. * This class is used to store a property of a custom block of a generic type.
*/ */
public interface CustomBlockProperty<T> { public interface CustomBlockProperty<T> {
/**
* Gets the name of the property
*
* @return The name of the property.
*/
@NonNull String name(); @NonNull String name();
/**
* Gets the values of the property
*
* @return The values of the property.
*/
@NonNull List<T> values(); @NonNull List<T> values();
/**
* Gets the type of the property
*
* @return The type of the property.
*/
@NonNull PropertyType type(); @NonNull PropertyType type();
} }

View file

@ -25,6 +25,8 @@
package org.geysermc.geyser.api.block.custom.property; package org.geysermc.geyser.api.block.custom.property;
import org.checkerframework.checker.nullness.qual.NonNull;
/** /**
* This class is used to define a custom block property's type. * This class is used to define a custom block property's type.
*/ */
@ -33,8 +35,13 @@ public class PropertyType {
public static final PropertyType INTEGER = new PropertyType(Integer.class); public static final PropertyType INTEGER = new PropertyType(Integer.class);
public static final PropertyType STRING = new PropertyType(String.class); public static final PropertyType STRING = new PropertyType(String.class);
private final Class<?> typeClass; @NonNull private final Class<?> typeClass;
/**
* Gets the class of the property type
*
* @return The class of the property type.
*/
public Class<?> typeClass() { public Class<?> typeClass() {
return typeClass; return typeClass;
} }

View file

@ -42,7 +42,7 @@ public abstract class GeyserDefineCustomBlocksEvent implements Event {
* *
* @param customBlockData the custom block to register * @param customBlockData the custom block to register
*/ */
public abstract void registerCustomBlock(@NonNull CustomBlockData customBlockData); public abstract void register(@NonNull CustomBlockData customBlockData);
/** /**
* Registers the given {@link CustomBlockState} as an override for the * Registers the given {@link CustomBlockState} as an override for the
@ -53,7 +53,7 @@ public abstract class GeyserDefineCustomBlocksEvent implements Event {
* @param javaIdentifier the java state identifier to override * @param javaIdentifier the java state identifier to override
* @param customBlockState the custom block state with which to override java state identifier * @param customBlockState the custom block state with which to override java state identifier
*/ */
public abstract void registerBlockStateOverride(@NonNull String javaIdentifier, @NonNull CustomBlockState customBlockState); public abstract void registerOverride(@NonNull String javaIdentifier, @NonNull CustomBlockState customBlockState);
/** /**
* Registers the given {@link CustomBlockData} as an override for the * Registers the given {@link CustomBlockData} as an override for the
@ -62,5 +62,5 @@ public abstract class GeyserDefineCustomBlocksEvent implements Event {
* @param javaIdentifier the java item identifier to override * @param javaIdentifier the java item identifier to override
* @param customBlockData the custom block data with which to override java item identifier * @param customBlockData the custom block data with which to override java item identifier
*/ */
public abstract void registerBlockItemOverride(@NonNull String javaIdentifier, @NonNull CustomBlockData customBlockData); public abstract void registerItemOverride(@NonNull String javaIdentifier, @NonNull CustomBlockData customBlockData);
} }

View file

@ -24,5 +24,5 @@ public abstract class GeyserDefineCustomSkullsEvent implements Event {
* @param texture the username, UUID, base64 encoded profile, or skin hash * @param texture the username, UUID, base64 encoded profile, or skin hash
* @param type the type of texture provided * @param type the type of texture provided
*/ */
public abstract void registerCustomSkull(@NonNull String texture, @NonNull SkullTextureType type); public abstract void register(@NonNull String texture, @NonNull SkullTextureType type);
} }

View file

@ -126,11 +126,12 @@ public class BlockRegistries {
static { static {
CustomSkullRegistryPopulator.populate(); CustomSkullRegistryPopulator.populate();
BlockRegistryPopulator.prePopulate(); BlockRegistryPopulator.populate(BlockRegistryPopulator.Stage.PRE_INIT);
BlockRegistryPopulator.registerJavaBlocks(); BlockRegistryPopulator.populate(BlockRegistryPopulator.Stage.INIT_JAVA);
COLLISIONS = IntMappedRegistry.create(Pair.of("org.geysermc.geyser.translator.collision.CollisionRemapper", "mappings/collision.json"), CollisionRegistryLoader::new); COLLISIONS = IntMappedRegistry.create(Pair.of("org.geysermc.geyser.translator.collision.CollisionRemapper", "mappings/collision.json"), CollisionRegistryLoader::new);
CustomBlockRegistryPopulator.registerCustomBedrockBlocks(); CustomBlockRegistryPopulator.populate();
BlockRegistryPopulator.registerBedrockBlocks(); BlockRegistryPopulator.populate(BlockRegistryPopulator.Stage.INIT_BEDROCK);
BlockRegistryPopulator.populate(BlockRegistryPopulator.Stage.POST_INIT);
} }
public static void init() { public static void init() {

View file

@ -183,7 +183,7 @@ public class MappingsReader_v1 extends MappingsReader {
* Read a block mapping entry from a JSON node and Java identifier * Read a block mapping entry from a JSON node and Java identifier
* @param identifier The Java identifier of the block * @param identifier The Java identifier of the block
* @param node The {@link JsonNode} containing the block mapping entry * @param node The {@link JsonNode} containing the block mapping entry
* @return The {@link CustomBlockMapping} record to be read by {@link org.geysermc.geyser.registry.populator.CustomBlockRegistryPopulator#registerCustomBedrockBlocks} * @return The {@link CustomBlockMapping} record to be read by {@link org.geysermc.geyser.registry.populator.CustomBlockRegistryPopulator}
* @throws InvalidCustomMappingsFileException If the JSON node is invalid * @throws InvalidCustomMappingsFileException If the JSON node is invalid
*/ */
@Override @Override

View file

@ -63,16 +63,36 @@ import java.util.zip.GZIPInputStream;
* Populates the block registries. * Populates the block registries.
*/ */
public final class BlockRegistryPopulator { public final class BlockRegistryPopulator {
/**
* The stage of population
*/
public enum Stage {
PRE_INIT,
INIT_JAVA,
INIT_BEDROCK,
POST_INIT;
}
public static void populate(Stage stage) {
switch (stage) {
case PRE_INIT -> { nullifyBlocksNode(); }
case INIT_JAVA -> { registerJavaBlocks(); }
case INIT_BEDROCK -> { registerBedrockBlocks(); }
case POST_INIT -> { nullifyBlocksNode(); }
default -> { throw new IllegalArgumentException("Unknown stage: " + stage); }
}
}
/** /**
* Stores the raw blocks JSON until it is no longer needed. * Stores the raw blocks JSON until it is no longer needed.
*/ */
private static JsonNode BLOCKS_JSON; private static JsonNode BLOCKS_JSON;
public static void prePopulate() { private static void nullifyBlocksNode() {
BLOCKS_JSON = null; BLOCKS_JSON = null;
} }
public static void registerBedrockBlocks() { private static void registerBedrockBlocks() {
BiFunction<String, NbtMapBuilder, String> woolMapper = (bedrockIdentifier, statesBuilder) -> { BiFunction<String, NbtMapBuilder, String> woolMapper = (bedrockIdentifier, statesBuilder) -> {
if (bedrockIdentifier.equals("minecraft:wool")) { if (bedrockIdentifier.equals("minecraft:wool")) {
String color = (String) statesBuilder.remove("color"); String color = (String) statesBuilder.remove("color");
@ -306,11 +326,9 @@ public final class BlockRegistryPopulator {
.extendedCollisionBoxes(extendedCollisionBoxes) .extendedCollisionBoxes(extendedCollisionBoxes)
.build()); .build());
} }
BLOCKS_JSON = null;
} }
public static void registerJavaBlocks() { private static void registerJavaBlocks() {
JsonNode blocksJson; JsonNode blocksJson;
try (InputStream stream = GeyserImpl.getInstance().getBootstrap().getResource("mappings/blocks.json")) { try (InputStream stream = GeyserImpl.getInstance().getBootstrap().getResource("mappings/blocks.json")) {
blocksJson = GeyserImpl.JSON_MAPPER.readTree(stream); blocksJson = GeyserImpl.JSON_MAPPER.readTree(stream);

View file

@ -39,7 +39,7 @@ public class CustomBlockRegistryPopulator {
/** /**
* Registers all custom blocks defined by extensions and user supplied mappings * Registers all custom blocks defined by extensions and user supplied mappings
*/ */
public static void registerCustomBedrockBlocks() { public static void populate() {
if (!GeyserImpl.getInstance().getConfig().isAddNonBedrockItems()) { if (!GeyserImpl.getInstance().getConfig().isAddNonBedrockItems()) {
return; return;
} }
@ -50,7 +50,7 @@ public class CustomBlockRegistryPopulator {
GeyserImpl.getInstance().getEventBus().fire(new GeyserDefineCustomBlocksEvent() { GeyserImpl.getInstance().getEventBus().fire(new GeyserDefineCustomBlocksEvent() {
@Override @Override
public void registerCustomBlock(@NonNull CustomBlockData customBlockData) { public void register(@NonNull CustomBlockData customBlockData) {
if (customBlockData.name().length() == 0) { if (customBlockData.name().length() == 0) {
throw new IllegalArgumentException("Custom block name must have at least 1 character."); throw new IllegalArgumentException("Custom block name must have at least 1 character.");
} }
@ -64,7 +64,7 @@ public class CustomBlockRegistryPopulator {
} }
@Override @Override
public void registerBlockStateOverride(@NonNull String javaIdentifier, @NonNull CustomBlockState customBlockState) { public void registerOverride(@NonNull String javaIdentifier, @NonNull CustomBlockState customBlockState) {
int id = BlockRegistries.JAVA_IDENTIFIER_TO_ID.getOrDefault(javaIdentifier, -1); int id = BlockRegistries.JAVA_IDENTIFIER_TO_ID.getOrDefault(javaIdentifier, -1);
if (id == -1) { if (id == -1) {
throw new IllegalArgumentException("Unknown Java block state. Identifier: " + javaIdentifier); throw new IllegalArgumentException("Unknown Java block state. Identifier: " + javaIdentifier);
@ -80,7 +80,7 @@ public class CustomBlockRegistryPopulator {
} }
@Override @Override
public void registerBlockItemOverride(@NonNull String javaIdentifier, @NonNull CustomBlockData customBlockData) { public void registerItemOverride(@NonNull String javaIdentifier, @NonNull CustomBlockData customBlockData) {
if (!customBlocks.contains(customBlockData)) { if (!customBlocks.contains(customBlockData)) {
throw new IllegalArgumentException("Custom block is unregistered. Name: " + customBlockData.name()); throw new IllegalArgumentException("Custom block is unregistered. Name: " + customBlockData.name());
} }

View file

@ -78,7 +78,7 @@ public class CustomSkullRegistryPopulator {
GeyserImpl.getInstance().getEventBus().fire(new GeyserDefineCustomSkullsEvent() { GeyserImpl.getInstance().getEventBus().fire(new GeyserDefineCustomSkullsEvent() {
@Override @Override
public void registerCustomSkull(@NonNull String texture, @NonNull SkullTextureType type) { public void register(@NonNull String texture, @NonNull SkullTextureType type) {
switch (type) { switch (type) {
case USERNAME -> usernames.add(texture); case USERNAME -> usernames.add(texture);
case UUID -> uuids.add(texture); case UUID -> uuids.add(texture);

View file

@ -113,7 +113,7 @@ public class JavaLevelChunkWithLightTranslator extends PacketTranslator<Clientbo
try { try {
ByteBuf in = Unpooled.wrappedBuffer(packet.getChunkData()); ByteBuf in = Unpooled.wrappedBuffer(packet.getChunkData());
int runningSectionExtendedCollisions[] = new int[BlockStorage.SIZE]; int[] runningSectionExtendedCollisions = new int[BlockStorage.SIZE];
boolean extendedCollisionNextSection = false; boolean extendedCollisionNextSection = false;
for (int sectionY = 0; sectionY < chunkSize; sectionY++) { for (int sectionY = 0; sectionY < chunkSize; sectionY++) {
ChunkSection javaSection = session.getDownstream().getCodecHelper().readChunkSection(in, biomeGlobalPalette); ChunkSection javaSection = session.getDownstream().getCodecHelper().readChunkSection(in, biomeGlobalPalette);
@ -193,7 +193,7 @@ public class JavaLevelChunkWithLightTranslator extends PacketTranslator<Clientbo
continue; continue;
} }
IntList bedrockPalette = new IntArrayList(); IntList bedrockPalette = new IntArrayList(javaPalette.size());
int airPaletteId = -1; int airPaletteId = -1;
waterloggedPaletteIds.clear(); waterloggedPaletteIds.clear();
bedrockOnlyBlockEntityIds.clear(); bedrockOnlyBlockEntityIds.clear();
@ -214,6 +214,7 @@ public class JavaLevelChunkWithLightTranslator extends PacketTranslator<Clientbo
if (!session.getBlockMappings().getExtendedCollisionBoxes().isEmpty() && !extendedCollision) { if (!session.getBlockMappings().getExtendedCollisionBoxes().isEmpty() && !extendedCollision) {
if (session.getBlockMappings().getExtendedCollisionBoxes().get(javaId) != null) { if (session.getBlockMappings().getExtendedCollisionBoxes().get(javaId) != null) {
extendedCollision = true; extendedCollision = true;
bedrockPalette = new IntArrayList();
} }
} }