Skip to content

Commit

Permalink
Anvil repair items can now be changed with a tag
Browse files Browse the repository at this point in the history
  • Loading branch information
MincraftEinstein committed May 29, 2024
1 parent d930fbd commit e46ab9c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,16 +35,33 @@ public class MendableAnvilsCommon {
Blocks.CHIPPED_ANVIL, Blocks.ANVIL,
Blocks.DAMAGED_ANVIL, Blocks.CHIPPED_ANVIL
);
private static final TagKey<Item> 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<Item, DispenseItemBehavior> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"replace": false,
"values": [
"minecraft:iron_ingot"
]
}
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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) {
Expand Down

0 comments on commit e46ab9c

Please sign in to comment.