diff --git a/Common/src/main/java/einstein/mendable_anvils/MendableAnvilsCommon.java b/Common/src/main/java/einstein/mendable_anvils/MendableAnvilsCommon.java index d64024b..db0e4d8 100644 --- a/Common/src/main/java/einstein/mendable_anvils/MendableAnvilsCommon.java +++ b/Common/src/main/java/einstein/mendable_anvils/MendableAnvilsCommon.java @@ -2,14 +2,18 @@ import einstein.mendable_anvils.platform.Services; import net.minecraft.core.BlockPos; -import net.minecraft.server.level.ServerLevel; +import net.minecraft.core.dispenser.DispenseItemBehavior; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.core.registries.Registries; +import net.minecraft.resources.ResourceLocation; import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; +import net.minecraft.tags.TagKey; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.Items; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.AnvilBlock; import net.minecraft.world.level.block.Block; @@ -31,16 +35,33 @@ public class MendableAnvilsCommon { Blocks.CHIPPED_ANVIL, Blocks.ANVIL, Blocks.DAMAGED_ANVIL, Blocks.CHIPPED_ANVIL ); + private static final TagKey REPAIR_ITEMS = TagKey.create(Registries.ITEM, new ResourceLocation(MOD_ID, "anvil_repair_items")); public static void init() { - DispenserBlock.registerBehavior(Items.IRON_INGOT, new MendAnvilDispenseItemBehavior()); + } + + public static void onDatapackSync() { + // Clears the registry of mend anvil behaviors incase tags are different between single player worlds + Map registry = Map.copyOf(DispenserBlock.DISPENSER_REGISTRY); // Copy of the registry so there are no issues caused by removing keys while looping + registry.forEach((item, behavior) -> { + if (behavior instanceof MendAnvilDispenseItemBehavior) { + DispenserBlock.DISPENSER_REGISTRY.remove(item); + } + }); + + BuiltInRegistries.ITEM.getTagOrEmpty(REPAIR_ITEMS).forEach(holder -> { + Item item = holder.value(); + if (!DispenserBlock.DISPENSER_REGISTRY.containsKey(item)) { + DispenserBlock.registerBehavior(item, new MendAnvilDispenseItemBehavior()); + } + }); } public static InteractionResult onBlockClick(Player player, Level level, InteractionHand hand, BlockHitResult result) { ItemStack stack = player.getItemInHand(hand); BlockPos pos = result.getBlockPos(); - if (stack.is(Items.IRON_INGOT) && player.isShiftKeyDown()) { + if (stack.is(REPAIR_ITEMS) && player.isShiftKeyDown()) { if (mendAnvil(level, pos)) { if (!player.isCreative()) { stack.shrink(1); diff --git a/Common/src/main/resources/data/mendable_anvils/tags/items/anvil_repair_items.json b/Common/src/main/resources/data/mendable_anvils/tags/items/anvil_repair_items.json new file mode 100644 index 0000000..549adc4 --- /dev/null +++ b/Common/src/main/resources/data/mendable_anvils/tags/items/anvil_repair_items.json @@ -0,0 +1,6 @@ +{ + "replace": false, + "values": [ + "minecraft:iron_ingot" + ] +} \ No newline at end of file diff --git a/Fabric/src/main/java/einstein/mendable_anvils/MendableAnvilsFabric.java b/Fabric/src/main/java/einstein/mendable_anvils/MendableAnvilsFabric.java index 1351fd1..81fc48d 100644 --- a/Fabric/src/main/java/einstein/mendable_anvils/MendableAnvilsFabric.java +++ b/Fabric/src/main/java/einstein/mendable_anvils/MendableAnvilsFabric.java @@ -1,6 +1,7 @@ package einstein.mendable_anvils; import net.fabricmc.api.ModInitializer; +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.fabricmc.fabric.api.event.player.UseBlockCallback; public class MendableAnvilsFabric implements ModInitializer { @@ -9,5 +10,7 @@ public class MendableAnvilsFabric implements ModInitializer { public void onInitialize() { MendableAnvilsCommon.init(); UseBlockCallback.EVENT.register(MendableAnvilsCommon::onBlockClick); + ServerLifecycleEvents.SERVER_STARTED.register(server -> MendableAnvilsCommon.onDatapackSync()); + ServerLifecycleEvents.END_DATA_PACK_RELOAD.register((server, resourceManager, success) -> MendableAnvilsCommon.onDatapackSync()); } } diff --git a/Forge/src/main/java/einstein/mendable_anvils/MendableAnvilsForge.java b/Forge/src/main/java/einstein/mendable_anvils/MendableAnvilsForge.java index ac1181c..45b9757 100644 --- a/Forge/src/main/java/einstein/mendable_anvils/MendableAnvilsForge.java +++ b/Forge/src/main/java/einstein/mendable_anvils/MendableAnvilsForge.java @@ -1,7 +1,9 @@ package einstein.mendable_anvils; import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.event.OnDatapackSyncEvent; import net.minecraftforge.event.entity.player.PlayerInteractEvent; +import net.minecraftforge.event.server.ServerStartedEvent; import net.minecraftforge.fml.common.Mod; @Mod(MendableAnvilsCommon.MOD_ID) @@ -10,6 +12,8 @@ public class MendableAnvilsForge { public MendableAnvilsForge() { MendableAnvilsCommon.init(); MinecraftForge.EVENT_BUS.addListener(this::onBlockClick); + MinecraftForge.EVENT_BUS.addListener((ServerStartedEvent event) -> MendableAnvilsCommon.onDatapackSync()); + MinecraftForge.EVENT_BUS.addListener((OnDatapackSyncEvent event) -> MendableAnvilsCommon.onDatapackSync()); } private void onBlockClick(PlayerInteractEvent.RightClickBlock event) {