Anvil Screen crash fix

This commit is contained in:
Aleksey 2021-01-23 13:08:46 +03:00
parent 482dcc1638
commit f309af077b

View file

@ -31,7 +31,7 @@ public class AnvilScreenMixin extends ForgingScreen<AnvilScreenHandler> {
@Shadow
private TextFieldWidget nameField;
private List<AbstractButtonWidget> be_buttons = Lists.newArrayList();
private final List<AbstractButtonWidget> be_buttons = Lists.newArrayList();
private AnvilScreenHandlerExtended anvilHandler;
public AnvilScreenMixin(AnvilScreenHandler handler, PlayerInventory playerInventory, Text title,
@ -44,24 +44,31 @@ public class AnvilScreenMixin extends ForgingScreen<AnvilScreenHandler> {
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<AnvilScreenHandler> {
@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);