Skip to content

Instantly share code, notes, and snippets.

@kermandev
Created February 23, 2025 19:35
Show Gist options
  • Select an option

  • Save kermandev/3141a6cc9b9b6e1964ac321574e099e9 to your computer and use it in GitHub Desktop.

Select an option

Save kermandev/3141a6cc9b9b6e1964ac321574e099e9 to your computer and use it in GitHub Desktop.
Index: src/main/java/net/minestom/server/instance/block/BlockImpl.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/net/minestom/server/instance/block/BlockImpl.java b/src/main/java/net/minestom/server/instance/block/BlockImpl.java
--- a/src/main/java/net/minestom/server/instance/block/BlockImpl.java (revision 67d35344a005119a261ae6f60b485c1c8a58af99)
+++ b/src/main/java/net/minestom/server/instance/block/BlockImpl.java (date 1740339146108)
@@ -4,6 +4,7 @@
import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import it.unimi.dsi.fastutil.objects.Object2ObjectMaps;
import net.kyori.adventure.nbt.CompoundBinaryTag;
+import net.minestom.server.MinecraftServer;
import net.minestom.server.registry.Registry;
import net.minestom.server.tag.Tag;
import net.minestom.server.utils.block.BlockUtils;
@@ -18,11 +19,12 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
+import java.util.function.Supplier;
record BlockImpl(@NotNull Registry.BlockEntry registry,
long propertiesArray,
@Nullable CompoundBinaryTag nbt,
- @Nullable BlockHandler handler) implements Block {
+ @Nullable Supplier<BlockHandler> handlerSupplier) implements Block {
/**
* Number of bits used to store the index of a property value.
* <p>
@@ -87,7 +89,7 @@
var mainProperties = Registry.Properties.fromMap(new MergedMap<>(stateOverride, properties.asMap()));
final BlockImpl block = new BlockImpl(Registry.block(namespace, mainProperties),
- propertiesValue, null, null);
+ propertiesValue, null, () -> null); // TODO Server flag?
BLOCK_STATE_MAP.set(block.stateId(), block);
propertiesKeys[propertiesOffset] = propertiesValue;
blocksValues[propertiesOffset++] = block;
@@ -156,17 +158,17 @@
tag.write(builder, value);
final CompoundBinaryTag temporaryNbt = builder.build();
final CompoundBinaryTag finalNbt = temporaryNbt.size() > 0 ? temporaryNbt : null;
- return new BlockImpl(registry, propertiesArray, finalNbt, handler);
+ return new BlockImpl(registry, propertiesArray, finalNbt, handlerSupplier);
}
@Override
public @NotNull Block withNbt(@Nullable CompoundBinaryTag compound) {
- return new BlockImpl(registry, propertiesArray, compound, handler);
+ return new BlockImpl(registry, propertiesArray, compound, handlerSupplier);
}
@Override
public @NotNull Block withHandler(@Nullable BlockHandler handler) {
- return new BlockImpl(registry, propertiesArray, nbt, handler);
+ return new BlockImpl(registry, propertiesArray, nbt, Objects.requireNonNullElse(() -> handler, null));
}
@Override
@@ -207,19 +209,31 @@
@Override
public String toString() {
- return String.format("%s{properties=%s, nbt=%s, handler=%s}", name(), properties(), nbt, handler);
+ return String.format("%s{properties=%s, nbt=%s, handler=%s}", name(), properties(), nbt, handler());
+ }
+
+ @Override
+ public @Nullable BlockHandler handler() {
+ if (handlerSupplier == null) return null;
+
+ final BlockHandler handler = handlerSupplier.get();
+ if (handler == null) { // TODO stable value?
+ return MinecraftServer.getBlockManager().getHandler(name());
+ } else {
+ return handler;
+ }
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof BlockImpl block)) return false;
- return stateId() == block.stateId() && Objects.equals(nbt, block.nbt) && Objects.equals(handler, block.handler);
+ return stateId() == block.stateId() && Objects.equals(nbt, block.nbt) && Objects.equals(handler(), block.handler());
}
@Override
public int hashCode() {
- return Objects.hash(stateId(), nbt, handler);
+ return Objects.hash(stateId(), nbt, handler());
}
private Block compute(long updatedProperties) {
@@ -227,9 +241,9 @@
final BlockImpl block = possibleProperties().get(updatedProperties);
assert block != null;
// Reuse the same block instance if possible
- if (nbt == null && handler == null) return block;
+ if (nbt == null && handler() == null) return block;
// Otherwise copy with the nbt and handler
- return new BlockImpl(block.registry(), block.propertiesArray, nbt, handler);
+ return new BlockImpl(block.registry(), block.propertiesArray, nbt, handlerSupplier);
}
private static byte findKeyIndex(PropertyType[] properties, String key, BlockImpl block) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment