diff --git a/src/main/java/ru/betterend/mixin/client/AnvilScreenMixin.java b/src/main/java/ru/betterend/mixin/client/AnvilScreenMixin.java index 4577283f..a47b4c53 100644 --- a/src/main/java/ru/betterend/mixin/client/AnvilScreenMixin.java +++ b/src/main/java/ru/betterend/mixin/client/AnvilScreenMixin.java @@ -31,7 +31,7 @@ public class AnvilScreenMixin extends ForgingScreen { @Shadow private TextFieldWidget nameField; - private List be_buttons = Lists.newArrayList(); + private final List be_buttons = Lists.newArrayList(); private AnvilScreenHandlerExtended anvilHandler; public AnvilScreenMixin(AnvilScreenHandler handler, PlayerInventory playerInventory, Text title, @@ -44,24 +44,31 @@ public class AnvilScreenMixin extends ForgingScreen { this.be_buttons.clear(); int x = (width - backgroundWidth) / 2; int y = (height - backgroundHeight) / 2; - this.anvilHandler = AnvilScreenHandlerExtended.class.cast(this.handler); + this.anvilHandler = (AnvilScreenHandlerExtended) this.handler; this.be_buttons.add(new ButtonWidget(x + 8, y + 45, 15, 20, new LiteralText("<"), (b) -> be_previousRecipe())); this.be_buttons.add(new ButtonWidget(x + 154, y + 45, 15, 20, new LiteralText(">"), (b) -> be_nextRecipe())); } @Inject(method = "renderForeground", at = @At("TAIL")) protected void be_renderForeground(MatrixStack matrices, int mouseX, int mouseY, float delta, CallbackInfo info) { - if (anvilHandler.be_getRecipes().size() > 1) { - this.be_buttons.forEach(button -> button.render(matrices, mouseX, mouseY, delta)); - } + this.be_buttons.forEach(button -> { + button.render(matrices, mouseX, mouseY, delta); + }); } @Inject(method = "onSlotUpdate", at = @At("HEAD"), cancellable = true) public void be_onSlotUpdate(ScreenHandler handler, int slotId, ItemStack stack, CallbackInfo info) { - AnvilScreenHandlerExtended anvilHandler = AnvilScreenHandlerExtended.class.cast(handler); + AnvilScreenHandlerExtended anvilHandler = (AnvilScreenHandlerExtended) handler; if (anvilHandler.be_getCurrentRecipe() != null) { + if (anvilHandler.be_getRecipes().size() > 1) { + this.be_buttons.forEach(button -> button.visible = true); + } else { + this.be_buttons.forEach(button -> button.visible = false); + } this.nameField.setText(""); info.cancel(); + } else { + this.be_buttons.forEach(button -> button.visible = false); } } @@ -75,11 +82,15 @@ public class AnvilScreenMixin extends ForgingScreen { @Override public boolean mouseClicked(double mouseX, double mouseY, int button) { - for (AbstractButtonWidget elem : be_buttons) { - if (elem.mouseClicked(mouseX, mouseY, button)) { - int i = be_buttons.indexOf(elem); - this.client.interactionManager.clickButton(handler.syncId, i); - return true; + if (client != null) { + for (AbstractButtonWidget elem : be_buttons) { + if (elem.visible && elem.mouseClicked(mouseX, mouseY, button)) { + if (client.interactionManager != null) { + int i = be_buttons.indexOf(elem); + this.client.interactionManager.clickButton(handler.syncId, i); + return true; + } + } } } return super.mouseClicked(mouseX, mouseY, button);