Skip to content

Commit

Permalink
Remove unnecessary registry supplier redirects (#847)
Browse files Browse the repository at this point in the history
  • Loading branch information
embeddedt authored Apr 27, 2024
1 parent f2ab14e commit e6c1c91
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 277 deletions.
34 changes: 5 additions & 29 deletions patches/net/minecraft/world/item/BucketItem.java.patch
Original file line number Diff line number Diff line change
@@ -1,30 +1,5 @@
--- a/net/minecraft/world/item/BucketItem.java
+++ b/net/minecraft/world/item/BucketItem.java
@@ -29,11 +_,24 @@
import net.minecraft.world.phys.HitResult;

public class BucketItem extends Item implements DispensibleContainerItem {
+ /** Neo: Field accesses are redirected to {@link #getFluid()} with a coremod. */
private final Fluid content;

+ // Forge: Use the other constructor that takes a Supplier
+ @Deprecated
public BucketItem(Fluid p_40689_, Item.Properties p_40690_) {
super(p_40690_);
this.content = p_40689_;
+ this.fluidSupplier = () -> p_40689_;
+ }
+
+ /**
+ * @param supplier A fluid supplier such as {@link net.neoforged.neoforge.registries.DeferredHolder}
+ */
+ public BucketItem(java.util.function.Supplier<? extends Fluid> supplier, Item.Properties builder) {
+ super(builder);
+ this.content = null;
+ this.fluidSupplier = supplier;
}

@Override
@@ -42,6 +_,8 @@
BlockHitResult blockhitresult = getPlayerPOVHitResult(
p_40703_, p_40704_, this.content == Fluids.EMPTY ? ClipContext.Fluid.SOURCE_ONLY : ClipContext.Fluid.NONE
Expand Down Expand Up @@ -89,7 +64,7 @@
liquidblockcontainer1.placeLiquid(p_150717_, p_150718_, blockstate, flowingfluid.getSource(false));
this.playEmptySound(p_150716_, p_150717_, p_150718_);
return true;
@@ -168,8 +_,17 @@
@@ -168,8 +_,18 @@
}

protected void playEmptySound(@Nullable Player p_40696_, LevelAccessor p_40697_, BlockPos p_40698_) {
Expand All @@ -100,11 +75,12 @@
p_40697_.gameEvent(p_40696_, GameEvent.FLUID_PLACE, p_40698_);
+ }
+
+ private final java.util.function.Supplier<? extends Fluid> fluidSupplier;
+ public Fluid getFluid() { return fluidSupplier.get(); }
+
+ protected boolean canBlockContainFluid(@Nullable Player player, Level worldIn, BlockPos posIn, BlockState blockstate)
+ {
+ return blockstate.getBlock() instanceof LiquidBlockContainer && ((LiquidBlockContainer)blockstate.getBlock()).canPlaceLiquid(player, worldIn, posIn, blockstate, this.content);
+ }
+
+ public Fluid getFluid() {
+ return this.content;
}
}
64 changes: 0 additions & 64 deletions patches/net/minecraft/world/item/MobBucketItem.java.patch

This file was deleted.

73 changes: 0 additions & 73 deletions patches/net/minecraft/world/level/block/LiquidBlock.java.patch
Original file line number Diff line number Diff line change
@@ -1,52 +1,5 @@
--- a/net/minecraft/world/level/block/LiquidBlock.java
+++ b/net/minecraft/world/level/block/LiquidBlock.java
@@ -51,7 +_,9 @@
.apply(p_308830_, LiquidBlock::new)
);
public static final IntegerProperty LEVEL = BlockStateProperties.LEVEL;
- protected final FlowingFluid fluid;
+ /** Neo: Field accesses are redirected to {@link #getFluid()} with a coremod. */
+ @Deprecated // Use getFluid
+ private final FlowingFluid fluid;
private final List<FluidState> stateCache;
public static final VoxelShape STABLE_SHAPE = Block.box(0.0, 0.0, 0.0, 16.0, 8.0, 16.0);
public static final ImmutableList<Direction> POSSIBLE_FLOW_DIRECTIONS = ImmutableList.of(
@@ -63,6 +_,7 @@
return CODEC;
}

+ @Deprecated // Forge: Use the constructor that takes a supplier
public LiquidBlock(FlowingFluid p_54694_, BlockBehaviour.Properties p_54695_) {
super(p_54695_);
this.fluid = p_54694_;
@@ -75,6 +_,19 @@

this.stateCache.add(p_54694_.getFlowing(8, true));
this.registerDefaultState(this.stateDefinition.any().setValue(LEVEL, Integer.valueOf(0)));
+ fluidStateCacheInitialized = true;
+ supplier = () -> p_54694_;
+ }
+
+ /**
+ * @param fluid A fluid supplier such as {@link net.neoforged.neoforge.registries.DeferredHolder}
+ */
+ public LiquidBlock(java.util.function.Supplier<? extends FlowingFluid> fluid, BlockBehaviour.Properties p_54695_) {
+ super(p_54695_);
+ this.fluid = null;
+ this.stateCache = Lists.newArrayList();
+ this.registerDefaultState(this.stateDefinition.any().setValue(LEVEL, Integer.valueOf(0)));
+ this.supplier = fluid;
}

@Override
@@ -109,6 +_,7 @@
@Override
protected FluidState getFluidState(BlockState p_54765_) {
int i = p_54765_.getValue(LEVEL);
+ if (!fluidStateCacheInitialized) initFluidStateCache();
return this.stateCache.get(Math.min(i, 8));
}

@@ -134,7 +_,7 @@

@Override
Expand All @@ -70,29 +23,3 @@
private boolean shouldSpreadLiquid(Level p_54697_, BlockPos p_54698_, BlockState p_54699_) {
if (this.fluid.is(FluidTags.LAVA)) {
boolean flag = p_54697_.getBlockState(p_54698_.below()).is(Blocks.SOUL_SOIL);
@@ -195,6 +_,25 @@
return new ItemStack(this.fluid.getBucket());
} else {
return ItemStack.EMPTY;
+ }
+ }
+
+ // Forge start
+ private final java.util.function.Supplier<? extends net.minecraft.world.level.material.Fluid> supplier;
+ public FlowingFluid getFluid() {
+ return (FlowingFluid)supplier.get();
+ }
+
+ private boolean fluidStateCacheInitialized = false;
+ protected synchronized void initFluidStateCache() {
+ if (fluidStateCacheInitialized == false) {
+ this.stateCache.add(getFluid().getSource(false));
+
+ for (int i = 1; i < 8; ++i)
+ this.stateCache.add(getFluid().getFlowing(8 - i, false));
+
+ this.stateCache.add(getFluid().getFlowing(8, true));
+ fluidStateCacheInitialized = true;
}
}

63 changes: 0 additions & 63 deletions patches/net/minecraft/world/level/block/StairBlock.java.patch

This file was deleted.

This file was deleted.

30 changes: 0 additions & 30 deletions src/main/resources/coremods/field_to_method.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,6 @@ function initializeCoreMod() {
return classNode;
}
},
'flowing_fluid_block': {
'target': {
'type': 'CLASS',
'name': 'net.minecraft.world.level.block.LiquidBlock'
},
'transformer': function(classNode) {
ASMAPI.redirectFieldToMethod(classNode, 'fluid', 'getFluid')
return classNode;
}
},
'bucketitem': {
'target': {
'type': 'CLASS',
'name': 'net.minecraft.world.item.BucketItem'
},
'transformer': function(classNode) {
ASMAPI.redirectFieldToMethod(classNode, 'content', 'getFluid')
return classNode;
}
},
'stairsblock': {
'target': {
'type': 'CLASS',
'name': 'net.minecraft.world.level.block.StairBlock'
},
'transformer': function(classNode) {
ASMAPI.redirectFieldToMethod(classNode, 'base', 'getModelBlock')
return classNode;
}
},
'flowerpotblock': {
'target': {
'type': 'CLASS',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public class ItemTests {
})
static void customMobBucket(final DynamicTest test, final RegistrationHelper reg) {
final var cowBucket = reg.items().register("cow_bucket", () -> new MobBucketItem(
() -> EntityType.COW,
() -> Fluids.WATER,
() -> SoundEvents.BUCKET_EMPTY_FISH,
EntityType.COW,
Fluids.WATER,
SoundEvents.BUCKET_EMPTY_FISH,
(new Item.Properties()).stacksTo(1)))
.withLang("Cow bucket");
test.framework().modEventBus().addListener((final FMLCommonSetupEvent event) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ public VertexConsumer color(int r, int g, int b, int a) {
});
private static final DeferredHolder<Fluid, FlowingFluid> TEST_FLUID = FLUIDS.register("test_fluid", () -> new BaseFlowingFluid.Source(fluidProperties()));
private static final DeferredHolder<Fluid, Fluid> TEST_FLUID_FLOWING = FLUIDS.register("test_fluid_flowing", () -> new BaseFlowingFluid.Flowing(fluidProperties()));
private static final DeferredBlock<LiquidBlock> TEST_FLUID_BLOCK = BLOCKS.register("test_fluid_block", () -> new LiquidBlock(TEST_FLUID, BlockBehaviour.Properties.of().noCollission().strength(100.0F).noLootTable()));
private static final DeferredItem<Item> TEST_FLUID_BUCKET = ITEMS.register("test_fluid_bucket", () -> new BucketItem(TEST_FLUID, new Item.Properties().craftRemainder(Items.BUCKET).stacksTo(1)));
private static final DeferredBlock<LiquidBlock> TEST_FLUID_BLOCK = BLOCKS.register("test_fluid_block", () -> new LiquidBlock(TEST_FLUID.get(), BlockBehaviour.Properties.of().noCollission().strength(100.0F).noLootTable()));
private static final DeferredItem<Item> TEST_FLUID_BUCKET = ITEMS.register("test_fluid_bucket", () -> new BucketItem(TEST_FLUID.get(), new Item.Properties().craftRemainder(Items.BUCKET).stacksTo(1)));

public FluidTypeTest(IEventBus modEventBus) {
if (ENABLE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public int getTintColor() {
public static DeferredHolder<Fluid, FlowingFluid> test_fluid = FLUIDS.register("test_fluid", () -> new BaseFlowingFluid.Source(makeProperties()));
public static DeferredHolder<Fluid, FlowingFluid> test_fluid_flowing = FLUIDS.register("test_fluid_flowing", () -> new BaseFlowingFluid.Flowing(makeProperties()));

public static DeferredBlock<LiquidBlock> test_fluid_block = BLOCKS.register("test_fluid_block", () -> new LiquidBlock(test_fluid, Properties.of().noCollission().strength(100.0F).noLootTable()));
public static DeferredItem<Item> TEST_FLUID_BUCKET = ITEMS.register("test_fluid_bucket", () -> new BucketItem(test_fluid, new Item.Properties().craftRemainder(Items.BUCKET).stacksTo(1)));
public static DeferredBlock<LiquidBlock> test_fluid_block = BLOCKS.register("test_fluid_block", () -> new LiquidBlock(test_fluid.value(), Properties.of().noCollission().strength(100.0F).noLootTable()));
public static DeferredItem<Item> TEST_FLUID_BUCKET = ITEMS.register("test_fluid_bucket", () -> new BucketItem(test_fluid.value(), new Item.Properties().craftRemainder(Items.BUCKET).stacksTo(1)));

// WARNING: this doesn't allow "any fluid", only the fluid from this test mod!
public static DeferredBlock<Block> fluidloggable_block = BLOCKS.register("fluidloggable_block", () -> new FluidloggableBlock(Properties.of().mapColor(MapColor.WOOD).noCollission().strength(100.0F).noLootTable()));
Expand Down

0 comments on commit e6c1c91

Please sign in to comment.