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