From 9eff4c093528c47d928b7c0d15ce34188c3b09d8 Mon Sep 17 00:00:00 2001 From: Frank Date: Sun, 9 Jul 2023 02:30:55 +0200 Subject: [PATCH] [Change] Rebalanced Anvil Durability (#259) --- .../betterx/bclib/blocks/BaseAnvilBlock.java | 18 +++- .../AnvilScreenHandlerExtended.java | 22 ++--- .../bclib/mixin/client/AnvilScreenMixin.java | 8 +- .../bclib/mixin/common/AnvilMenuMixin.java | 89 +++++++++---------- 4 files changed, 73 insertions(+), 64 deletions(-) diff --git a/src/main/java/org/betterx/bclib/blocks/BaseAnvilBlock.java b/src/main/java/org/betterx/bclib/blocks/BaseAnvilBlock.java index 0d714686..e5503480 100644 --- a/src/main/java/org/betterx/bclib/blocks/BaseAnvilBlock.java +++ b/src/main/java/org/betterx/bclib/blocks/BaseAnvilBlock.java @@ -6,18 +6,22 @@ import org.betterx.bclib.client.models.PatternsHelper; import org.betterx.bclib.interfaces.BlockModelProvider; import org.betterx.bclib.interfaces.CustomItemProvider; import org.betterx.bclib.items.BaseAnvilItem; +import org.betterx.bclib.util.BlocksHelper; import net.minecraft.client.renderer.block.model.BlockModel; import net.minecraft.client.resources.model.UnbakedModel; +import net.minecraft.core.BlockPos; import net.minecraft.resources.ResourceLocation; import net.minecraft.util.RandomSource; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.PickaxeItem; +import net.minecraft.world.level.Level; import net.minecraft.world.level.block.AnvilBlock; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.LevelEvent; import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.StateDefinition; @@ -36,6 +40,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Optional; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; public abstract class BaseAnvilBlock extends AnvilBlock implements BlockModelProvider, CustomItemProvider { @@ -126,7 +131,7 @@ public abstract class BaseAnvilBlock extends AnvilBlock implements BlockModelPro public BlockState damageAnvilUse(BlockState state, RandomSource random) { IntegerProperty durability = getDurabilityProp(); int value = state.getValue(durability); - if (value < getMaxDurability() && random.nextInt(10) == 0) { + if (value < getMaxDurability()) { return state.setValue(durability, value + 1); } value = state.getValue(DESTRUCTION); @@ -137,4 +142,15 @@ public abstract class BaseAnvilBlock extends AnvilBlock implements BlockModelPro int destruction = state.getValue(DESTRUCTION); return destruction < 2 ? state.setValue(DESTRUCTION, destruction + 1) : null; } + + @ApiStatus.Internal + public static void destroyWhenNull(Level level, BlockPos blockPos, BlockState damaged) { + if (damaged == null) { + level.removeBlock(blockPos, false); + level.levelEvent(LevelEvent.SOUND_ANVIL_BROKEN, blockPos, 0); + } else { + level.setBlock(blockPos, damaged, BlocksHelper.FLAG_SEND_CLIENT_CHANGES); + level.levelEvent(LevelEvent.SOUND_ANVIL_USED, blockPos, 0); + } + } } diff --git a/src/main/java/org/betterx/bclib/interfaces/AnvilScreenHandlerExtended.java b/src/main/java/org/betterx/bclib/interfaces/AnvilScreenHandlerExtended.java index b17116f2..4f616abc 100644 --- a/src/main/java/org/betterx/bclib/interfaces/AnvilScreenHandlerExtended.java +++ b/src/main/java/org/betterx/bclib/interfaces/AnvilScreenHandlerExtended.java @@ -6,31 +6,31 @@ import org.betterx.bclib.recipes.AnvilRecipe; import java.util.List; public interface AnvilScreenHandlerExtended { - void be_updateCurrentRecipe(AnvilRecipe recipe); + void bcl_updateCurrentRecipe(AnvilRecipe recipe); - AnvilRecipe be_getCurrentRecipe(); + AnvilRecipe bcl_getCurrentRecipe(); - List be_getRecipes(); + List bcl_getRecipes(); - default void be_nextRecipe() { - List recipes = be_getRecipes(); + default void bcl_nextRecipe() { + List recipes = bcl_getRecipes(); if (recipes.size() < 2) return; - AnvilRecipe current = be_getCurrentRecipe(); + AnvilRecipe current = bcl_getCurrentRecipe(); int i = recipes.indexOf(current) + 1; if (i >= recipes.size()) { i = 0; } - be_updateCurrentRecipe(recipes.get(i)); + bcl_updateCurrentRecipe(recipes.get(i)); } - default void be_previousRecipe() { - List recipes = be_getRecipes(); + default void bcl_previousRecipe() { + List recipes = bcl_getRecipes(); if (recipes.size() < 2) return; - AnvilRecipe current = be_getCurrentRecipe(); + AnvilRecipe current = bcl_getCurrentRecipe(); int i = recipes.indexOf(current) - 1; if (i <= 0) { i = recipes.size() - 1; } - be_updateCurrentRecipe(recipes.get(i)); + bcl_updateCurrentRecipe(recipes.get(i)); } } diff --git a/src/main/java/org/betterx/bclib/mixin/client/AnvilScreenMixin.java b/src/main/java/org/betterx/bclib/mixin/client/AnvilScreenMixin.java index dd7faa9c..31b8f20f 100644 --- a/src/main/java/org/betterx/bclib/mixin/client/AnvilScreenMixin.java +++ b/src/main/java/org/betterx/bclib/mixin/client/AnvilScreenMixin.java @@ -58,8 +58,8 @@ public class AnvilScreenMixin extends ItemCombinerScreen { @Inject(method = "slotChanged", at = @At("HEAD"), cancellable = true) public void be_onSlotUpdate(AbstractContainerMenu handler, int slotId, ItemStack stack, CallbackInfo info) { AnvilScreenHandlerExtended anvilHandler = (AnvilScreenHandlerExtended) handler; - if (anvilHandler.be_getCurrentRecipe() != null) { - if (anvilHandler.be_getRecipes().size() > 1) { + if (anvilHandler.bcl_getCurrentRecipe() != null) { + if (anvilHandler.bcl_getRecipes().size() > 1) { be_buttons.forEach(button -> button.visible = true); } else { be_buttons.forEach(button -> button.visible = false); @@ -72,11 +72,11 @@ public class AnvilScreenMixin extends ItemCombinerScreen { } private void be_nextRecipe() { - ((AnvilScreenHandlerExtended) menu).be_nextRecipe(); + ((AnvilScreenHandlerExtended) menu).bcl_nextRecipe(); } private void be_previousRecipe() { - ((AnvilScreenHandlerExtended) menu).be_previousRecipe(); + ((AnvilScreenHandlerExtended) menu).bcl_previousRecipe(); } diff --git a/src/main/java/org/betterx/bclib/mixin/common/AnvilMenuMixin.java b/src/main/java/org/betterx/bclib/mixin/common/AnvilMenuMixin.java index 8004c532..ea595f4b 100644 --- a/src/main/java/org/betterx/bclib/mixin/common/AnvilMenuMixin.java +++ b/src/main/java/org/betterx/bclib/mixin/common/AnvilMenuMixin.java @@ -32,8 +32,8 @@ import org.jetbrains.annotations.Nullable; @Mixin(AnvilMenu.class) public abstract class AnvilMenuMixin extends ItemCombinerMenu implements AnvilScreenHandlerExtended { - private List be_recipes = Collections.emptyList(); - private AnvilRecipe be_currentRecipe; + private List bcl_recipes = Collections.emptyList(); + private AnvilRecipe bcl_currentRecipe; private DataSlot anvilLevel; @Shadow @@ -70,49 +70,42 @@ public abstract class AnvilMenuMixin extends ItemCombinerMenu implements AnvilSc public abstract void createResult(); @Inject(method = "mayPickup", at = @At("HEAD"), cancellable = true) - protected void be_canTakeOutput(Player player, boolean present, CallbackInfoReturnable info) { - if (be_currentRecipe != null) { - info.setReturnValue(be_currentRecipe.checkHammerDurability(inputSlots, player)); + protected void bcl_canTakeOutput(Player player, boolean present, CallbackInfoReturnable info) { + if (bcl_currentRecipe != null) { + info.setReturnValue(bcl_currentRecipe.checkHammerDurability(inputSlots, player)); } } @Inject(method = "method_24922", at = @At(value = "HEAD"), cancellable = true) - private static void bclib_onDamageAnvil(Player player, Level level, BlockPos blockPos, CallbackInfo ci) { + private static void bcl_onDamageAnvil(Player player, Level level, BlockPos blockPos, CallbackInfo ci) { BlockState blockState = level.getBlockState(blockPos); - if (blockState.getBlock() instanceof BaseAnvilBlock anvil) { + if (!player.getAbilities().instabuild + && blockState.getBlock() instanceof BaseAnvilBlock anvil + && player.getRandom().nextDouble() < 0.12) { BlockState damaged = anvil.damageAnvilUse(blockState, player.getRandom()); - bcl_destroyWhenNull(level, blockPos, damaged); + BaseAnvilBlock.destroyWhenNull(level, blockPos, damaged); ci.cancel(); } } - private static void bcl_destroyWhenNull(Level level, BlockPos blockPos, BlockState damaged) { - if (damaged == null) { - level.removeBlock(blockPos, false); - level.levelEvent(LevelEvent.SOUND_ANVIL_BROKEN, blockPos, 0); - } else { - level.setBlock(blockPos, damaged, 2); - level.levelEvent(LevelEvent.SOUND_ANVIL_USED, blockPos, 0); - } - } @Inject(method = "onTake", at = @At("HEAD"), cancellable = true) protected void bclib_onTakeAnvilOutput(Player player, ItemStack stack, CallbackInfo info) { - if (be_currentRecipe != null) { + if (bcl_currentRecipe != null) { final int ingredientSlot = AnvilRecipe.getIngredientSlot(inputSlots); - inputSlots.getItem(ingredientSlot).shrink(be_currentRecipe.getInputCount()); - stack = be_currentRecipe.craft(inputSlots, player); + inputSlots.getItem(ingredientSlot).shrink(bcl_currentRecipe.getInputCount()); + stack = bcl_currentRecipe.craft(inputSlots, player); slotsChanged(inputSlots); access.execute((level, blockPos) -> { final BlockState anvilState = level.getBlockState(blockPos); final Block anvilBlock = anvilState.getBlock(); - if (anvilBlock instanceof BaseAnvilBlock) { - final BaseAnvilBlock anvil = (BaseAnvilBlock) anvilBlock; - if (!player.getAbilities().instabuild && anvilState.is(BlockTags.ANVIL) && player.getRandom() - .nextDouble() < 0.1) { + if (anvilBlock instanceof BaseAnvilBlock anvil) { + if (!player.getAbilities().instabuild + && anvilState.is(BlockTags.ANVIL) + && player.getRandom().nextDouble() < 0.1) { BlockState damagedState = anvil.damageAnvilUse(anvilState, player.getRandom()); - bcl_destroyWhenNull(level, blockPos, damagedState); + BaseAnvilBlock.destroyWhenNull(level, blockPos, damagedState); } else { level.levelEvent(LevelEvent.SOUND_ANVIL_USED, blockPos, 0); } @@ -125,27 +118,27 @@ public abstract class AnvilMenuMixin extends ItemCombinerMenu implements AnvilSc @Inject(method = "createResult", at = @At("HEAD"), cancellable = true) public void be_updateOutput(CallbackInfo info) { RecipeManager recipeManager = this.player.level.getRecipeManager(); - be_recipes = recipeManager.getRecipesFor(AnvilRecipe.TYPE, inputSlots, player.level); - if (be_recipes.size() > 0) { + bcl_recipes = recipeManager.getRecipesFor(AnvilRecipe.TYPE, inputSlots, player.level); + if (bcl_recipes.size() > 0) { int anvilLevel = this.anvilLevel.get(); - be_recipes = be_recipes.stream() - .filter(recipe -> anvilLevel >= recipe.getAnvilLevel()) - .collect(Collectors.toList()); - if (be_recipes.size() > 0) { - if (be_currentRecipe == null || !be_recipes.contains(be_currentRecipe)) { - be_currentRecipe = be_recipes.get(0); + bcl_recipes = bcl_recipes.stream() + .filter(recipe -> anvilLevel >= recipe.getAnvilLevel()) + .collect(Collectors.toList()); + if (bcl_recipes.size() > 0) { + if (bcl_currentRecipe == null || !bcl_recipes.contains(bcl_currentRecipe)) { + bcl_currentRecipe = bcl_recipes.get(0); } - be_updateResult(); + bcl_updateResult(); info.cancel(); } else { - be_currentRecipe = null; + bcl_currentRecipe = null; } } } @Inject(method = "setItemName", at = @At("HEAD"), cancellable = true) public void be_setNewItemName(String string, CallbackInfo info) { - if (be_currentRecipe != null) { + if (bcl_currentRecipe != null) { info.cancel(); } } @@ -153,34 +146,34 @@ public abstract class AnvilMenuMixin extends ItemCombinerMenu implements AnvilSc @Override public boolean clickMenuButton(Player player, int id) { if (id == 0) { - be_previousRecipe(); + bcl_previousRecipe(); return true; } else if (id == 1) { - be_nextRecipe(); + bcl_nextRecipe(); return true; } return super.clickMenuButton(player, id); } - private void be_updateResult() { - if (be_currentRecipe == null) return; - resultSlots.setItem(0, be_currentRecipe.assemble(inputSlots)); + private void bcl_updateResult() { + if (bcl_currentRecipe == null) return; + resultSlots.setItem(0, bcl_currentRecipe.assemble(inputSlots)); broadcastChanges(); } @Override - public void be_updateCurrentRecipe(AnvilRecipe recipe) { - this.be_currentRecipe = recipe; - be_updateResult(); + public void bcl_updateCurrentRecipe(AnvilRecipe recipe) { + this.bcl_currentRecipe = recipe; + bcl_updateResult(); } @Override - public AnvilRecipe be_getCurrentRecipe() { - return be_currentRecipe; + public AnvilRecipe bcl_getCurrentRecipe() { + return bcl_currentRecipe; } @Override - public List be_getRecipes() { - return be_recipes; + public List bcl_getRecipes() { + return bcl_recipes; } }