Skip to content

Commit

Permalink
Lazy base class and fixed its logic. Removed public setter
Browse files Browse the repository at this point in the history
  • Loading branch information
TelepathicGrunt committed May 31, 2024
1 parent 585cf0d commit a120f8d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
--- a/net/minecraft/world/level/block/entity/BlockEntityType.java
+++ b/net/minecraft/world/level/block/entity/BlockEntityType.java
@@ -301,6 +_,22 @@
@@ -301,6 +_,13 @@
return this.validBlocks.contains(p_155263_.getBlock());
}

Expand All @@ -10,15 +10,6 @@
+ public Set<Block> getValidBlocks() {
+ return java.util.Collections.unmodifiableSet(this.validBlocks);
+ }
+
+ /**
+ * Neo: Add block setter.
+ * Please use {@link net.neoforged.neoforge.event.BlockEntityTypesValidBlocksEvent} to add to the valid blocks for this BlockEntityType.
+ * This setter is only for this event.
+ */
+ public void setValidBlocks(Set<Block> validBlocks) {
+ this.validBlocks = validBlocks;
+ }
+
@Nullable
public Holder.Reference<BlockEntityType<?>> builtInRegistryHolder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package net.neoforged.neoforge.event;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
Expand All @@ -22,14 +24,14 @@
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.fml.event.IModBusEvent;
import net.neoforged.fml.event.lifecycle.FMLCommonSetupEvent;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.jetbrains.annotations.Nullable;

public class BlockEntityTypesValidBlocksEvent extends Event implements IModBusEvent {
private final ResourceKey<BlockEntityType<?>> blockEntityTypeResourceKey;
private final BlockEntityType<?> blockEntityType;
private final Set<Block> currentValidBlocks;
@Nullable
private Class<?> baseClass = null;
private final Supplier<? extends Class<?>> baseClass = Suppliers.memoize(this::setCommonSuperClassForExistingValidBlocks);

protected BlockEntityTypesValidBlocksEvent(ResourceKey<BlockEntityType<?>> blockEntityTypeResourceKey, BlockEntityType<?> blockEntityType) {
this.blockEntityTypeResourceKey = blockEntityTypeResourceKey;
Expand Down Expand Up @@ -68,27 +70,28 @@ public Set<Block> getCurrentValidBlocks() {
* @param block The block to add as a valid block for the current {@link BlockEntityType}
*/
public void addValidBlock(Block block) {
setCommonSuperClassForExistingValidBlocks();

if (this.baseClass == null || this.baseClass.isAssignableFrom(block.getClass())) {
if (this.baseClass.get() == null || this.baseClass.get().isAssignableFrom(block.getClass())) {
this.currentValidBlocks.add(block);
} else {
throw new IllegalArgumentException("Given block " + block + " does not derive from existing valid block's common superclass of " + this.baseClass);
}
}

private void setCommonSuperClassForExistingValidBlocks() {
if (this.baseClass == null) {
for (Block existingBlock : this.currentValidBlocks) {
if (this.baseClass != null) {
if (!existingBlock.getClass().isAssignableFrom(this.baseClass)) {
this.baseClass = findClosestCommonSuper(existingBlock.getClass(), this.baseClass);
}
} else {
this.baseClass = existingBlock.getClass();
@Nullable
private Class<?> setCommonSuperClassForExistingValidBlocks() {
Class<?> calculatedBaseClass = null;

for (Block existingBlock : this.currentValidBlocks) {
if (calculatedBaseClass != null) {
if (!calculatedBaseClass.isAssignableFrom(existingBlock.getClass())) {
calculatedBaseClass = findClosestCommonSuper(existingBlock.getClass(), calculatedBaseClass);
}
} else {
calculatedBaseClass = existingBlock.getClass();
}
}

return calculatedBaseClass;
}

private static Class<?> findClosestCommonSuper(Class<?> a, Class<?> b) {
Expand All @@ -101,11 +104,11 @@ private static Class<?> findClosestCommonSuper(Class<?> a, Class<?> b) {
@EventBusSubscriber(modid = "neoforge", bus = EventBusSubscriber.Bus.MOD)
private static class CommonHandler {
@SubscribeEvent
private static void onCommonSetup(FMLCommonSetupEvent event) {
private static void onCommonSetup(FMLCommonSetupEvent event) throws Throwable {
for (Map.Entry<ResourceKey<BlockEntityType<?>>, BlockEntityType<?>> blockEntityTypeEntry : BuiltInRegistries.BLOCK_ENTITY_TYPE.entrySet()) {
BlockEntityTypesValidBlocksEvent blockEntityTypesValidBlocksEvent = new BlockEntityTypesValidBlocksEvent(blockEntityTypeEntry.getKey(), blockEntityTypeEntry.getValue());
ModLoader.postEventWrapContainerInModOrder(blockEntityTypesValidBlocksEvent); // Allow modders to add to the list in the events.
blockEntityTypeEntry.getValue().setValidBlocks(blockEntityTypesValidBlocksEvent.getCurrentValidBlocks()); // Update the block entity type's valid blocks to the new modified list.
FieldUtils.writeField(blockEntityTypeEntry.getValue(), "validBlocks", blockEntityTypesValidBlocksEvent.getCurrentValidBlocks(), true);
}
}
}
Expand Down

0 comments on commit a120f8d

Please sign in to comment.