diff --git a/common/src/main/java/github/killarexe/copper_extension/CEActions.java b/common/src/main/java/github/killarexe/copper_extension/CEActions.java index 78dc20b..5414d4e 100644 --- a/common/src/main/java/github/killarexe/copper_extension/CEActions.java +++ b/common/src/main/java/github/killarexe/copper_extension/CEActions.java @@ -3,8 +3,11 @@ import net.minecraft.core.BlockPos; import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.tags.ItemTags; import net.minecraft.util.RandomSource; +import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; +import net.minecraft.world.InteractionResultHolder; import net.minecraft.world.entity.EquipmentSlot; import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.entity.player.Player; @@ -19,13 +22,14 @@ import net.minecraft.world.phys.Vec3; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import java.util.Map; import java.util.Optional; public class CEActions { public static final float BASE_CHANCE = 0.0013666F; - public static void scrap(Item scarpItem, ItemStack currentStack, ItemStack otherStack, ServerPlayer serverPlayer, int count) { + private static void scrap(Item scarpItem, ItemStack currentStack, ItemStack otherStack, ServerPlayer serverPlayer, int count) { int damage = otherStack.getMaxDamage() - otherStack.getDamageValue(); int amount = Math.min(Math.min(count, currentStack.getCount()), damage); @@ -40,6 +44,36 @@ public static void scrap(Item scarpItem, ItemStack currentStack, ItemStack other serverPlayer.getCooldowns().addCooldown(otherStack.getItem(), amount * 8); } + private static Optional getScrapItem(Item item) { + for (Map.Entry entry: CEMaps.OXIDATION_MAP_ITEMS.entrySet()) { + if (entry.getValue() == item) { + return Optional.of(entry.getKey()); + } + } + for (Map.Entry entry: CEMaps.WAXING_MAP_ITEMS.entrySet()) { + if (entry.getValue() == item) { + return Optional.of(entry.getKey()); + } + } + return Optional.empty(); + } + + public static void scrapUse(Player player, InteractionHand hand, CallbackInfoReturnable> callbackInfo) { + ItemStack currentHandStack = player.getItemInHand(hand); + ItemStack otherHandStack = player.getOffhandItem(); + if (CEMaps.OXIDATION_MAP_ITEMS.containsValue(currentHandStack.getItem()) && otherHandStack.is(ItemTags.AXES)) { + Optional scrapItem = getScrapItem(currentHandStack.getItem()); + if (scrapItem.isEmpty()) { + callbackInfo.setReturnValue(InteractionResultHolder.fail(currentHandStack)); + } + if (player instanceof ServerPlayer serverPlayer && !player.getCooldowns().isOnCooldown(otherHandStack.getItem())) { + scrap(scrapItem.get(), currentHandStack, otherHandStack, serverPlayer, serverPlayer.isShiftKeyDown() ? currentHandStack.getCount() : 1); + callbackInfo.setReturnValue(InteractionResultHolder.success(currentHandStack)); + } + callbackInfo.setReturnValue(InteractionResultHolder.consume(currentHandStack)); + } + } + public static void rustEntityStack( T nextItem, ItemStack stack, Level level, ItemEntity entity, GameRules.Key oxidationGameRule, RandomSource random) diff --git a/common/src/main/java/github/killarexe/copper_extension/CEMaps.java b/common/src/main/java/github/killarexe/copper_extension/CEMaps.java index 7f06553..f81c723 100644 --- a/common/src/main/java/github/killarexe/copper_extension/CEMaps.java +++ b/common/src/main/java/github/killarexe/copper_extension/CEMaps.java @@ -47,6 +47,10 @@ public class CEMaps { OXIDATION_MAP_ITEMS.put(Items.WEATHERED_CHISELED_COPPER, Items.OXIDIZED_CHISELED_COPPER); + WAXING_MAP_ITEMS.put(Items.COPPER_BLOCK, Items.WAXED_COPPER_BLOCK); + WAXING_MAP_ITEMS.put(Items.EXPOSED_COPPER, Items.WAXED_EXPOSED_COPPER); + WAXING_MAP_ITEMS.put(Items.WEATHERED_COPPER, Items.WAXED_WEATHERED_COPPER); + WAXING_MAP_ITEMS.put(Items.OXIDIZED_COPPER, Items.WAXED_WEATHERED_COPPER); WAXING_MAP_ITEMS.put(Items.CUT_COPPER, Items.WAXED_CUT_COPPER); WAXING_MAP_ITEMS.put(Items.EXPOSED_CUT_COPPER, Items.WAXED_EXPOSED_CUT_COPPER); diff --git a/fabric/src/main/java/github/killarexe/copper_extension/fabric/mixin/ItemMixin.java b/fabric/src/main/java/github/killarexe/copper_extension/fabric/mixin/ItemMixin.java index 09f5f9b..0c3f669 100644 --- a/fabric/src/main/java/github/killarexe/copper_extension/fabric/mixin/ItemMixin.java +++ b/fabric/src/main/java/github/killarexe/copper_extension/fabric/mixin/ItemMixin.java @@ -26,12 +26,6 @@ import net.minecraft.world.item.context.UseOnContext; import net.minecraft.world.level.ItemLike; import net.minecraft.world.level.Level; -import net.minecraft.world.level.block.BeehiveBlock; -import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.state.BlockState; -import net.minecraft.world.phys.Vec3; - -import java.util.Map; @Mixin(Item.class) public abstract class ItemMixin implements FeatureElement, ItemLike, FabricItem { @@ -49,26 +43,7 @@ public void inventoryTick(ItemStack stack, Level level, Entity entity, int slot, @Inject(method = "use", at = @At("HEAD"), cancellable = true) public void use(Level level, Player player, InteractionHand interactionHand, CallbackInfoReturnable> callbackInfo) { - ItemStack currentHandStack = player.getItemInHand(interactionHand); - ItemStack otherHandStack = player.getOffhandItem(); - if (CEMaps.OXIDATION_MAP_ITEMS.containsValue(currentHandStack.getItem()) && otherHandStack.is(ItemTags.AXES)) { - Item scrapItem = null; - for (Map.Entry entry: CEMaps.OXIDATION_MAP_ITEMS.entrySet()) { - if (entry.getValue() == currentHandStack.getItem()) { - scrapItem = entry.getKey(); - break; - } - } - if (scrapItem == null) { - callbackInfo.setReturnValue(InteractionResultHolder.fail(currentHandStack)); - } - if (player instanceof ServerPlayer serverPlayer && !player.getCooldowns().isOnCooldown(otherHandStack.getItem())) { - CEActions.scrap(scrapItem, currentHandStack, otherHandStack, serverPlayer, serverPlayer.isShiftKeyDown() ? currentHandStack.getCount() : 1); - callbackInfo.setReturnValue(InteractionResultHolder.success(currentHandStack)); - } - - callbackInfo.setReturnValue(InteractionResultHolder.consume(currentHandStack)); - } + CEActions.scrapUse(player, interactionHand, callbackInfo); } @Inject(method = "useOn(Lnet/minecraft/world/item/context/UseOnContext;)Lnet/minecraft/world/InteractionResult;", at = @At("HEAD"), cancellable = true) diff --git a/neoforge/src/main/java/github/killarexe/copper_extension/neoforge/mixin/ItemMixin.java b/neoforge/src/main/java/github/killarexe/copper_extension/neoforge/mixin/ItemMixin.java index ee8ac84..bcd15fe 100644 --- a/neoforge/src/main/java/github/killarexe/copper_extension/neoforge/mixin/ItemMixin.java +++ b/neoforge/src/main/java/github/killarexe/copper_extension/neoforge/mixin/ItemMixin.java @@ -28,18 +28,10 @@ import net.minecraft.world.item.context.UseOnContext; import net.minecraft.world.level.ItemLike; import net.minecraft.world.level.Level; -import net.minecraft.world.level.block.BeehiveBlock; -import net.minecraft.world.level.block.Block; -import net.minecraft.world.level.block.state.BlockState; -import net.minecraft.world.phys.Vec3; - -import java.util.Map; @Mixin(Item.class) public abstract class ItemMixin implements FeatureElement, ItemLike, IItemExtension { - @Shadow @Final protected boolean canRepair; - @Inject(method = "inventoryTick(Lnet/minecraft/world/item/ItemStack;Lnet/minecraft/world/level/Level;Lnet/minecraft/world/entity/Entity;IZ)V", at = @At("HEAD")) public void inventoryTick(ItemStack stack, Level level, Entity entity, int slot, boolean selected, CallbackInfo callbackInfo) { Item item = stack.getItem(); @@ -53,26 +45,7 @@ public void inventoryTick(ItemStack stack, Level level, Entity entity, int slot, @Inject(method = "use", at = @At("HEAD"), cancellable = true) public void use(Level level, Player player, InteractionHand interactionHand, CallbackInfoReturnable> callbackInfo) { - ItemStack currentHandStack = player.getItemInHand(interactionHand); - ItemStack otherHandStack = player.getOffhandItem(); - if (CEMaps.OXIDATION_MAP_ITEMS.containsValue(currentHandStack.getItem()) && otherHandStack.is(ItemTags.AXES)) { - Item scrapItem = null; - for (Map.Entry entry: CEMaps.OXIDATION_MAP_ITEMS.entrySet()) { - if (entry.getValue() == currentHandStack.getItem()) { - scrapItem = entry.getKey(); - break; - } - } - if (scrapItem == null) { - callbackInfo.setReturnValue(InteractionResultHolder.fail(currentHandStack)); - } - if (player instanceof ServerPlayer serverPlayer && !player.getCooldowns().isOnCooldown(otherHandStack.getItem())) { - CEActions.scrap(scrapItem, currentHandStack, otherHandStack, serverPlayer, serverPlayer.isShiftKeyDown() ? currentHandStack.getCount() : 1); - callbackInfo.setReturnValue(InteractionResultHolder.success(currentHandStack)); - } - - callbackInfo.setReturnValue(InteractionResultHolder.consume(currentHandStack)); - } + CEActions.scrapUse(player, interactionHand, callbackInfo); } @Inject(method = "useOn(Lnet/minecraft/world/item/context/UseOnContext;)Lnet/minecraft/world/InteractionResult;", at = @At("HEAD"), cancellable = true)