|
| 1 | +package com.oroarmor.netherite_plus.mixin.better_end; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import com.oroarmor.netherite_plus.client.gui.screen.NetheriteAnvilScreen; |
| 6 | +import com.oroarmor.netherite_plus.screen.NetheriteAnvilScreenHandler; |
| 7 | +import org.spongepowered.asm.mixin.Mixin; |
| 8 | +import org.spongepowered.asm.mixin.Shadow; |
| 9 | +import org.spongepowered.asm.mixin.injection.At; |
| 10 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 11 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
| 12 | + |
| 13 | +import com.google.common.collect.Lists; |
| 14 | + |
| 15 | +import net.minecraft.client.gui.screen.ingame.ForgingScreen; |
| 16 | +import net.minecraft.client.gui.widget.AbstractButtonWidget; |
| 17 | +import net.minecraft.client.gui.widget.ButtonWidget; |
| 18 | +import net.minecraft.client.gui.widget.TextFieldWidget; |
| 19 | +import net.minecraft.client.util.math.MatrixStack; |
| 20 | +import net.minecraft.entity.player.PlayerInventory; |
| 21 | +import net.minecraft.item.ItemStack; |
| 22 | +import net.minecraft.screen.ScreenHandler; |
| 23 | +import net.minecraft.text.LiteralText; |
| 24 | +import net.minecraft.text.Text; |
| 25 | +import net.minecraft.util.Identifier; |
| 26 | +import ru.betterend.interfaces.AnvilScreenHandlerExtended; |
| 27 | + |
| 28 | +@Mixin(NetheriteAnvilScreen.class) |
| 29 | +public class NetheriteAnvilScreenMixin extends ForgingScreen<NetheriteAnvilScreenHandler> { |
| 30 | + |
| 31 | + @Shadow(remap = false) |
| 32 | + private TextFieldWidget nameField; |
| 33 | + |
| 34 | + private final List<AbstractButtonWidget> be_buttons = Lists.newArrayList(); |
| 35 | + private AnvilScreenHandlerExtended anvilHandler; |
| 36 | + |
| 37 | + public NetheriteAnvilScreenMixin(NetheriteAnvilScreenHandler handler, PlayerInventory playerInventory, Text title, |
| 38 | + Identifier texture) { |
| 39 | + super(handler, playerInventory, title, texture); |
| 40 | + } |
| 41 | + |
| 42 | + @Inject(method = "setup", at = @At("TAIL")) |
| 43 | + protected void be_setup(CallbackInfo info) { |
| 44 | + this.be_buttons.clear(); |
| 45 | + int x = (width - backgroundWidth) / 2; |
| 46 | + int y = (height - backgroundHeight) / 2; |
| 47 | + this.anvilHandler = (AnvilScreenHandlerExtended) this.handler; |
| 48 | + this.be_buttons.add(new ButtonWidget(x + 8, y + 45, 15, 20, new LiteralText("<"), (b) -> be_previousRecipe())); |
| 49 | + this.be_buttons.add(new ButtonWidget(x + 154, y + 45, 15, 20, new LiteralText(">"), (b) -> be_nextRecipe())); |
| 50 | + } |
| 51 | + |
| 52 | + @Inject(method = "renderForeground", at = @At("TAIL")) |
| 53 | + protected void be_renderForeground(MatrixStack matrices, int mouseX, int mouseY, float delta, CallbackInfo info) { |
| 54 | + this.be_buttons.forEach(button -> { |
| 55 | + button.render(matrices, mouseX, mouseY, delta); |
| 56 | + }); |
| 57 | + } |
| 58 | + |
| 59 | + @Inject(method = "onSlotUpdate", at = @At("HEAD"), cancellable = true) |
| 60 | + public void be_onSlotUpdate(ScreenHandler handler, int slotId, ItemStack stack, CallbackInfo info) { |
| 61 | + AnvilScreenHandlerExtended anvilHandler = (AnvilScreenHandlerExtended) handler; |
| 62 | + if (anvilHandler.be_getCurrentRecipe() != null) { |
| 63 | + if (anvilHandler.be_getRecipes().size() > 1) { |
| 64 | + this.be_buttons.forEach(button -> button.visible = true); |
| 65 | + } else { |
| 66 | + this.be_buttons.forEach(button -> button.visible = false); |
| 67 | + } |
| 68 | + this.nameField.setText(""); |
| 69 | + info.cancel(); |
| 70 | + } else { |
| 71 | + this.be_buttons.forEach(button -> button.visible = false); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private void be_nextRecipe() { |
| 76 | + this.anvilHandler.be_nextRecipe(); |
| 77 | + } |
| 78 | + |
| 79 | + private void be_previousRecipe() { |
| 80 | + this.anvilHandler.be_previousRecipe(); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public boolean mouseClicked(double mouseX, double mouseY, int button) { |
| 85 | + if (client != null) { |
| 86 | + for (AbstractButtonWidget elem : be_buttons) { |
| 87 | + if (elem.visible && elem.mouseClicked(mouseX, mouseY, button)) { |
| 88 | + if (client.interactionManager != null) { |
| 89 | + int i = be_buttons.indexOf(elem); |
| 90 | + this.client.interactionManager.clickButton(handler.syncId, i); |
| 91 | + return true; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + } |
| 96 | + return super.mouseClicked(mouseX, mouseY, button); |
| 97 | + } |
| 98 | +} |
0 commit comments