From 0a45702f19dd836a5b00f43e784cf7ce197efc5b Mon Sep 17 00:00:00 2001 From: Juuz <6596629+Juuxel@users.noreply.github.com> Date: Sat, 18 Nov 2023 22:01:13 +0200 Subject: [PATCH] Use pattern matching instanceof everywhere Resolves #216. --- .../cotton/gui/SyncedGuiDescription.java | 24 +++++++++---------- .../cotton/gui/client/BackgroundPainter.java | 3 +-- .../cottonmc/cotton/gui/widget/WBox.java | 2 +- .../cotton/gui/widget/WCardPanel.java | 2 +- .../cottonmc/cotton/gui/widget/WItemSlot.java | 3 +-- .../cottonmc/cotton/gui/widget/WPanel.java | 2 +- .../cotton/gui/widget/WPlayerInvPanel.java | 4 ++-- .../cotton/gui/widget/WScrollPanel.java | 2 +- 8 files changed, 20 insertions(+), 22 deletions(-) diff --git a/src/main/java/io/github/cottonmc/cotton/gui/SyncedGuiDescription.java b/src/main/java/io/github/cottonmc/cotton/gui/SyncedGuiDescription.java index 64d905e4..b64ffce9 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/SyncedGuiDescription.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/SyncedGuiDescription.java @@ -259,8 +259,8 @@ private boolean swapHotbar(ItemStack toInsert, int slotNumber, Inventory invento boolean inserted = false; for(Slot slot : slots) { - if (slot.inventory==inventory && slot instanceof ValidatedSlot) { - int index = ((ValidatedSlot)slot).getInventoryIndex(); + if (slot.inventory == inventory && slot instanceof ValidatedSlot validated) { + int index = validated.getInventoryIndex(); if (PlayerInventory.isValidHotbarIndex(index)) { hotbarSlots.add(slot); } else { @@ -393,8 +393,8 @@ private static Inventory getBlockInventory(ScreenHandlerContext ctx, Supplier { BlockEntity be = world.getBlockEntity(pos); - if (be!=null && be instanceof PropertyDelegateHolder) { - return ((PropertyDelegateHolder)be).getPropertyDelegate(); + if (be instanceof PropertyDelegateHolder holder) { + return holder.getPropertyDelegate(); } return new ArrayPropertyDelegate(0); @@ -453,8 +453,8 @@ public static PropertyDelegate getBlockPropertyDelegate(ScreenHandlerContext ctx public static PropertyDelegate getBlockPropertyDelegate(ScreenHandlerContext ctx, int size) { return ctx.get((world, pos) -> { BlockEntity be = world.getBlockEntity(pos); - if (be!=null && be instanceof PropertyDelegateHolder) { - return ((PropertyDelegateHolder)be).getPropertyDelegate(); + if (be instanceof PropertyDelegateHolder holder) { + return holder.getPropertyDelegate(); } return new ArrayPropertyDelegate(size); diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java b/src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java index 251fed77..13253b8d 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/client/BackgroundPainter.java @@ -51,10 +51,9 @@ public interface BackgroundPainter { *

For {@linkplain WItemSlot item slots}, this painter uses {@link WItemSlot#SLOT_TEXTURE libgui:textures/widget/item_slot.png}. */ public static BackgroundPainter SLOT = (context, left, top, panel) -> { - if (!(panel instanceof WItemSlot)) { + if (!(panel instanceof WItemSlot slot)) { ScreenDrawing.drawBeveledPanel(context, left-1, top-1, panel.getWidth()+2, panel.getHeight()+2, 0xB8000000, 0x4C000000, 0xB8FFFFFF); } else { - WItemSlot slot = (WItemSlot)panel; for(int x = 0; x < slot.getWidth()/18; ++x) { for(int y = 0; y < slot.getHeight()/18; ++y) { int index = x + y * (slot.getWidth() / 18); diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WBox.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WBox.java index 3d3ce9d5..a625b2e0 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WBox.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WBox.java @@ -125,7 +125,7 @@ public void layout() { child.setLocation(x, dimension); } - if (child instanceof WPanel) ((WPanel) child).layout(); + if (child instanceof WPanel panel) panel.layout(); expandToFit(child, insets); if (i != children.size() - 1) { diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WCardPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WCardPanel.java index 8caad27c..41e7f518 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WCardPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WCardPanel.java @@ -144,7 +144,7 @@ public void layout() { children.clear(); for (WWidget child : cards) { - if (child instanceof WPanel) ((WPanel) child).layout(); + if (child instanceof WPanel panel) panel.layout(); expandToFit(child); if (child == getSelectedCard()) { diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java index 76e72aa7..1650e2a0 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WItemSlot.java @@ -384,8 +384,7 @@ public void validate(GuiDescription host) { @Environment(EnvType.CLIENT) @Override public InputResult onKeyPressed(int ch, int key, int modifiers) { - if (isActivationKey(ch) && host instanceof ScreenHandler && focusedSlot >= 0) { - ScreenHandler handler = (ScreenHandler) host; + if (isActivationKey(ch) && host instanceof ScreenHandler handler && focusedSlot >= 0) { MinecraftClient client = MinecraftClient.getInstance(); ValidatedSlot peer = peers.get(focusedSlot); diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java index 5503f738..63ccdea0 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPanel.java @@ -68,7 +68,7 @@ public BackgroundPainter getBackgroundPainter() { */ public void layout() { for(WWidget child : children) { - if (child instanceof WPanel) ((WPanel) child).layout(); + if (child instanceof WPanel panel) panel.layout(); expandToFit(child); } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java index f5655b40..97e78844 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WPlayerInvPanel.java @@ -124,8 +124,8 @@ public WPanel setBackgroundPainter(BackgroundPainter painter) { @Override public void validate(GuiDescription c) { super.validate(c); - if (c != null && label instanceof WLabel) { - ((WLabel) label).setColor(c.getTitleColor()); + if (c != null && label instanceof WLabel l) { + l.setColor(c.getTitleColor()); } } } diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollPanel.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollPanel.java index 4e3669b5..41185c46 100644 --- a/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollPanel.java +++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WScrollPanel.java @@ -182,7 +182,7 @@ public void layout() { horizontalScrollBar.setSize(this.width - offset, SCROLL_BAR_SIZE); horizontalScrollBar.setLocation(0, this.height - horizontalScrollBar.getHeight()); - if (widget instanceof WPanel) ((WPanel) widget).layout(); + if (widget instanceof WPanel panel) panel.layout(); children.add(widget); Insets insets = getInsets(); int x = insets.left() + (horizontal ? -horizontalScrollBar.getValue() : 0);