From a283ca19516980191f1accdfc0474b2551b13dca Mon Sep 17 00:00:00 2001 From: Aleksey Date: Sat, 17 Apr 2021 21:21:17 +0300 Subject: [PATCH] More refactor --- .../mixin/client/AnvilScreenMixin.java | 22 +++++++------- .../mixin/common/AnvilBlockMixin.java | 2 -- .../mixin/common/AnvilMenuMixin.java | 29 ++++++++++--------- .../resources/betterend.mixins.common.json | 4 +-- 4 files changed, 27 insertions(+), 30 deletions(-) diff --git a/src/main/java/ru/betterend/mixin/client/AnvilScreenMixin.java b/src/main/java/ru/betterend/mixin/client/AnvilScreenMixin.java index 162c05c8..41cc84be 100644 --- a/src/main/java/ru/betterend/mixin/client/AnvilScreenMixin.java +++ b/src/main/java/ru/betterend/mixin/client/AnvilScreenMixin.java @@ -30,7 +30,6 @@ public class AnvilScreenMixin extends ItemCombinerScreen { private EditBox name; private final List be_buttons = Lists.newArrayList(); - private AnvilScreenHandlerExtended anvilHandler; public AnvilScreenMixin(AnvilMenu handler, Inventory playerInventory, Component title, ResourceLocation texture) { @@ -38,18 +37,17 @@ public class AnvilScreenMixin extends ItemCombinerScreen { } @Inject(method = "subInit", at = @At("TAIL")) - protected void be_setup(CallbackInfo info) { + protected void be_subInit(CallbackInfo info) { int x = (width - imageWidth) / 2; int y = (height - imageHeight) / 2; - anvilHandler = (AnvilScreenHandlerExtended) menu; be_buttons.clear(); - be_buttons.add(new Button(x + 8, y + 45, 15, 20, new TextComponent("<"), (b) -> be_previousRecipe())); - be_buttons.add(new Button(x + 154, y + 45, 15, 20, new TextComponent(">"), (b) -> be_nextRecipe())); + be_buttons.add(new Button(x + 8, y + 45, 15, 20, new TextComponent("<"), b -> be_previousRecipe())); + be_buttons.add(new Button(x + 154, y + 45, 15, 20, new TextComponent(">"), b -> be_nextRecipe())); } @Inject(method = "renderFg", at = @At("TAIL")) protected void be_renderForeground(PoseStack matrices, int mouseX, int mouseY, float delta, CallbackInfo info) { - this.be_buttons.forEach(button -> { + be_buttons.forEach(button -> { button.render(matrices, mouseX, mouseY, delta); }); } @@ -59,23 +57,23 @@ public class AnvilScreenMixin extends ItemCombinerScreen { AnvilScreenHandlerExtended anvilHandler = (AnvilScreenHandlerExtended) handler; if (anvilHandler.be_getCurrentRecipe() != null) { if (anvilHandler.be_getRecipes().size() > 1) { - this.be_buttons.forEach(button -> button.visible = true); + be_buttons.forEach(button -> button.visible = true); } else { - this.be_buttons.forEach(button -> button.visible = false); + be_buttons.forEach(button -> button.visible = false); } - this.name.setValue(""); + name.setValue(""); info.cancel(); } else { - this.be_buttons.forEach(button -> button.visible = false); + be_buttons.forEach(button -> button.visible = false); } } private void be_nextRecipe() { - anvilHandler.be_nextRecipe(); + ((AnvilScreenHandlerExtended) menu).be_nextRecipe(); } private void be_previousRecipe() { - anvilHandler.be_previousRecipe(); + ((AnvilScreenHandlerExtended) menu).be_previousRecipe(); } @Override diff --git a/src/main/java/ru/betterend/mixin/common/AnvilBlockMixin.java b/src/main/java/ru/betterend/mixin/common/AnvilBlockMixin.java index 46d69ce5..b58e098b 100644 --- a/src/main/java/ru/betterend/mixin/common/AnvilBlockMixin.java +++ b/src/main/java/ru/betterend/mixin/common/AnvilBlockMixin.java @@ -19,10 +19,8 @@ public class AnvilBlockMixin { try { BlockState state = fallingState.setValue(destructionProperty, destruction + 1); info.setReturnValue(state); - info.cancel(); } catch (Exception ex) { info.setReturnValue(null); - info.cancel(); } } } diff --git a/src/main/java/ru/betterend/mixin/common/AnvilMenuMixin.java b/src/main/java/ru/betterend/mixin/common/AnvilMenuMixin.java index 48cf2e5b..f23b4e0f 100644 --- a/src/main/java/ru/betterend/mixin/common/AnvilMenuMixin.java +++ b/src/main/java/ru/betterend/mixin/common/AnvilMenuMixin.java @@ -38,17 +38,18 @@ public abstract class AnvilMenuMixin extends ItemCombinerMenu implements AnvilSc @Inject(method = "(ILnet/minecraft/world/entity/player/Inventory;Lnet/minecraft/world/inventory/ContainerLevelAccess;)V", at = @At("TAIL")) public void be_initAnvilLevel(int syncId, Inventory inventory, ContainerLevelAccess context, CallbackInfo info) { + this.anvilLevel = addDataSlot(DataSlot.standalone()); if (context != ContainerLevelAccess.NULL) { - int anvLevel = context.evaluate((world, blockPos) -> { + int level = context.evaluate((world, blockPos) -> { Block anvilBlock = world.getBlockState(blockPos).getBlock(); if (anvilBlock instanceof EndAnvilBlock) { return ((EndAnvilBlock) anvilBlock).getCraftingLevel(); } return 1; }, 1); - DataSlot anvilLevel = DataSlot.standalone(); - anvilLevel.set(anvLevel); - this.anvilLevel = addDataSlot(anvilLevel); + anvilLevel.set(level); + } else { + anvilLevel.set(1); } } @@ -65,10 +66,10 @@ public abstract class AnvilMenuMixin extends ItemCombinerMenu implements AnvilSc @Inject(method = "onTake", at = @At("HEAD"), cancellable = true) protected void be_onTakeOutput(Player player, ItemStack stack, CallbackInfoReturnable info) { if (be_currentRecipe != null) { - this.inputSlots.getItem(0).shrink(be_currentRecipe.getInputCount()); + inputSlots.getItem(0).shrink(be_currentRecipe.getInputCount()); stack = be_currentRecipe.craft(inputSlots, player); - this.slotsChanged(inputSlots); - this.access.execute((world, blockPos) -> { + slotsChanged(inputSlots); + access.execute((world, blockPos) -> { BlockState anvilState = world.getBlockState(blockPos); if (!player.abilities.instabuild && anvilState.is(BlockTags.ANVIL) && player.getRandom().nextFloat() < 0.12F) { BlockState landingState = AnvilBlock.damage(anvilState); @@ -117,10 +118,10 @@ public abstract class AnvilMenuMixin extends ItemCombinerMenu implements AnvilSc @Override public boolean clickMenuButton(Player player, int id) { if (id == 0) { - this.be_previousRecipe(); + be_previousRecipe(); return true; } else if (id == 1) { - this.be_nextRecipe(); + be_nextRecipe(); return true; } return super.clickMenuButton(player, id); @@ -128,23 +129,23 @@ public abstract class AnvilMenuMixin extends ItemCombinerMenu implements AnvilSc private void be_updateResult() { if (be_currentRecipe == null) return; - this.resultSlots.setItem(0, be_currentRecipe.assemble(inputSlots)); - this.broadcastChanges(); + resultSlots.setItem(0, be_currentRecipe.assemble(inputSlots)); + broadcastChanges(); } @Override public void be_updateCurrentRecipe(AnvilRecipe recipe) { this.be_currentRecipe = recipe; - this.be_updateResult(); + be_updateResult(); } @Override public AnvilRecipe be_getCurrentRecipe() { - return this.be_currentRecipe; + return be_currentRecipe; } @Override public List be_getRecipes() { - return this.be_recipes; + return be_recipes; } } diff --git a/src/main/resources/betterend.mixins.common.json b/src/main/resources/betterend.mixins.common.json index 88651502..1225e6e1 100644 --- a/src/main/resources/betterend.mixins.common.json +++ b/src/main/resources/betterend.mixins.common.json @@ -26,10 +26,10 @@ "CraftingMenuMixin", "LivingEntityMixin", "ServerPlayerMixin", - "SpikeFeatureMixin", + "SpikeFeatureMixin", + "ServerLevelMixin", "AnvilBlockMixin", "PlayerListMixin", - "ServerLevelMixin", "AnvilMenuMixin", "TagLoaderMixin", "EnderManMixin",