[Fix] Custom Item Drop for a Sign is created before the Blocks are registered (quiqueck/BetterNether#151)

This commit is contained in:
Frank 2023-07-08 13:16:35 +02:00
parent 13cd641ee0
commit 1448ea8467
5 changed files with 46 additions and 12 deletions

View file

@ -147,7 +147,7 @@ public class BCLib implements ModInitializer {
); );
bockReg.registerBlockOnly( bockReg.registerBlockOnly(
makeID("test_wall_sign"), makeID("test_wall_sign"),
TEST_SIGN.wallSign TEST_SIGN.getWallSignBlock()
); );
bockReg.register( bockReg.register(
makeID("test_hanging_sign"), makeID("test_hanging_sign"),
@ -155,7 +155,7 @@ public class BCLib implements ModInitializer {
); );
bockReg.registerBlockOnly( bockReg.registerBlockOnly(
makeID("test_wall_hanging_sign"), makeID("test_wall_hanging_sign"),
TEST_HANGING_SIGN.wallSign TEST_HANGING_SIGN.getWallSignBlock()
); );
} }

View file

@ -25,9 +25,12 @@ import net.minecraft.world.level.block.state.properties.WoodType;
import net.minecraft.world.level.material.MapColor; import net.minecraft.world.level.material.MapColor;
import java.util.List; import java.util.List;
import java.util.function.Supplier;
public abstract class BaseHangingSignBlock extends CeilingHangingSignBlock implements BlockModelProvider, CustomItemProvider, TagProvider { public abstract class BaseHangingSignBlock extends CeilingHangingSignBlock implements BlockModelProvider, CustomItemProvider, TagProvider {
public final BaseWallHangingSignBlock wallSign; protected final Supplier<BaseWallHangingSignBlock> wallSign;
private BlockItem customItem;
private BaseWallHangingSignBlock wallSignBlock;
@FunctionalInterface @FunctionalInterface
public interface WallSignProvider { public interface WallSignProvider {
@ -36,9 +39,15 @@ public abstract class BaseHangingSignBlock extends CeilingHangingSignBlock imple
protected BaseHangingSignBlock(WoodType type, MapColor color, boolean flammable, WallSignProvider provider) { protected BaseHangingSignBlock(WoodType type, MapColor color, boolean flammable, WallSignProvider provider) {
super(BehaviourBuilders.createSign(color, flammable), type); super(BehaviourBuilders.createSign(color, flammable), type);
this.wallSign = provider.create(BehaviourBuilders.createWallSign(color, this, flammable), type); this.wallSign = () -> provider.create(BehaviourBuilders.createWallSign(color, this, flammable), type);
} }
public BaseWallHangingSignBlock getWallSignBlock() {
if (wallSignBlock == null) {
wallSignBlock = wallSign.get();
}
return wallSignBlock;
}
@Override @Override
public float getYRotationDegrees(BlockState blockState) { public float getYRotationDegrees(BlockState blockState) {
@ -47,7 +56,10 @@ public abstract class BaseHangingSignBlock extends CeilingHangingSignBlock imple
@Override @Override
public BlockItem getCustomItem(ResourceLocation blockID, Item.Properties settings) { public BlockItem getCustomItem(ResourceLocation blockID, Item.Properties settings) {
return new HangingSignItem(this, wallSign, settings.stacksTo(16)); if (customItem == null) {
customItem = new HangingSignItem(this, getWallSignBlock(), settings.stacksTo(16));
}
return customItem;
} }
@Override @Override

View file

@ -1,6 +1,8 @@
package org.betterx.bclib.blocks.signs; package org.betterx.bclib.blocks.signs;
import org.betterx.bclib.api.v3.datagen.DropSelfLootProvider;
import org.betterx.bclib.behaviours.BehaviourBuilders; import org.betterx.bclib.behaviours.BehaviourBuilders;
import org.betterx.bclib.behaviours.interfaces.BehaviourExplosionResistant;
import org.betterx.bclib.behaviours.interfaces.BehaviourWood; import org.betterx.bclib.behaviours.interfaces.BehaviourWood;
import org.betterx.bclib.complexmaterials.BCLWoodTypeWrapper; import org.betterx.bclib.complexmaterials.BCLWoodTypeWrapper;
import org.betterx.bclib.interfaces.BlockModelProvider; import org.betterx.bclib.interfaces.BlockModelProvider;
@ -13,6 +15,7 @@ import net.minecraft.tags.ItemTags;
import net.minecraft.tags.TagKey; import net.minecraft.tags.TagKey;
import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.Item; import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.SignItem; import net.minecraft.world.item.SignItem;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.StandingSignBlock; import net.minecraft.world.level.block.StandingSignBlock;
@ -20,12 +23,17 @@ import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.properties.RotationSegment; import net.minecraft.world.level.block.state.properties.RotationSegment;
import net.minecraft.world.level.block.state.properties.WoodType; import net.minecraft.world.level.block.state.properties.WoodType;
import net.minecraft.world.level.material.MapColor; import net.minecraft.world.level.material.MapColor;
import net.minecraft.world.level.storage.loot.LootParams;
import java.util.List; import java.util.List;
import java.util.function.Supplier;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public abstract class BaseSignBlock extends StandingSignBlock implements BlockModelProvider, CustomItemProvider, TagProvider { public abstract class BaseSignBlock extends StandingSignBlock implements BlockModelProvider, CustomItemProvider, TagProvider, DropSelfLootProvider<BaseSignBlock>, BehaviourExplosionResistant {
public final BaseWallSignBlock wallSign; protected final Supplier<BaseWallSignBlock> wallSign;
private BlockItem customItem;
private BaseWallSignBlock wallSignBlock;
@FunctionalInterface @FunctionalInterface
public interface WallSignProvider { public interface WallSignProvider {
@ -34,9 +42,15 @@ public abstract class BaseSignBlock extends StandingSignBlock implements BlockMo
protected BaseSignBlock(WoodType type, MapColor color, boolean flammable, WallSignProvider provider) { protected BaseSignBlock(WoodType type, MapColor color, boolean flammable, WallSignProvider provider) {
super(BehaviourBuilders.createSign(color, flammable), type); super(BehaviourBuilders.createSign(color, flammable), type);
this.wallSign = provider.create(BehaviourBuilders.createWallSign(color, this, flammable), type); this.wallSign = () -> provider.create(BehaviourBuilders.createWallSign(color, this, flammable), type);
}
public BaseWallSignBlock getWallSignBlock() {
if (wallSignBlock == null) {
wallSignBlock = wallSign.get();
}
return wallSignBlock;
} }
@Override @Override
public float getYRotationDegrees(BlockState blockState) { public float getYRotationDegrees(BlockState blockState) {
@ -45,7 +59,10 @@ public abstract class BaseSignBlock extends StandingSignBlock implements BlockMo
@Override @Override
public BlockItem getCustomItem(ResourceLocation blockID, Item.Properties settings) { public BlockItem getCustomItem(ResourceLocation blockID, Item.Properties settings) {
return new SignItem(settings.stacksTo(16), this, wallSign); if (customItem == null) {
customItem = new SignItem(settings, this, getWallSignBlock());
}
return customItem;
} }
@Override @Override
@ -71,4 +88,9 @@ public abstract class BaseSignBlock extends StandingSignBlock implements BlockMo
public static BaseSignBlock from(BCLWoodTypeWrapper type) { public static BaseSignBlock from(BCLWoodTypeWrapper type) {
return new BaseSignBlock.Wood(type); return new BaseSignBlock.Wood(type);
} }
@Override
public List<ItemStack> getDrops(BlockState blockState, LootParams.Builder builder) {
return super.getDrops(blockState, builder);
}
} }

View file

@ -33,7 +33,7 @@ public class HangingSign extends MaterialSlot<WoodenComplexMaterial> {
false, false,
(complexMaterial, settings) -> { (complexMaterial, settings) -> {
if (complexMaterial.getBlock(suffix) instanceof BaseHangingSignBlock sign) { if (complexMaterial.getBlock(suffix) instanceof BaseHangingSignBlock sign) {
return sign.wallSign; return sign.getWallSignBlock();
} }
return null; return null;
} }

View file

@ -33,7 +33,7 @@ public class Sign extends MaterialSlot<WoodenComplexMaterial> {
false, false,
(complexMaterial, settings) -> { (complexMaterial, settings) -> {
if (complexMaterial.getBlock(suffix) instanceof BaseSignBlock sign) { if (complexMaterial.getBlock(suffix) instanceof BaseSignBlock sign) {
return sign.wallSign; return sign.getWallSignBlock();
} }
return null; return null;
} }