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: Use pattern matching instanceof everywhere Resolves #216. --- .../cottonmc/cotton/gui/SyncedGuiDescription.java | 24 +++++++++++----------- .../cotton/gui/client/BackgroundPainter.java | 3 +-- .../io/github/cottonmc/cotton/gui/widget/WBox.java | 2 +- .../cottonmc/cotton/gui/widget/WCardPanel.java | 2 +- .../cottonmc/cotton/gui/widget/WItemSlot.java | 3 +-- .../github/cottonmc/cotton/gui/widget/WPanel.java | 2 +- .../cotton/gui/widget/WPlayerInvPanel.java | 4 ++-- .../cottonmc/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 64d905e..b64ffce 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 @@ public class SyncedGuiDescription extends ScreenHandler implements GuiDescriptio 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 @@ public class SyncedGuiDescription extends ScreenHandler implements GuiDescriptio BlockState state = world.getBlockState(pos); Block b = state.getBlock(); - if (b instanceof InventoryProvider) { - Inventory inventory = ((InventoryProvider)b).getInventory(state, world, pos); + if (b instanceof InventoryProvider inventoryProvider) { + Inventory inventory = inventoryProvider.getInventory(state, world, pos); if (inventory != null) { return inventory; } @@ -402,13 +402,13 @@ public class SyncedGuiDescription extends ScreenHandler implements GuiDescriptio BlockEntity be = world.getBlockEntity(pos); if (be!=null) { - if (be instanceof InventoryProvider) { - Inventory inventory = ((InventoryProvider)be).getInventory(state, world, pos); + if (be instanceof InventoryProvider inventoryProvider) { + Inventory inventory = inventoryProvider.getInventory(state, world, pos); if (inventory != null) { return inventory; } - } else if (be instanceof Inventory) { - return (Inventory)be; + } else if (be instanceof Inventory inventory) { + return inventory; } } @@ -429,8 +429,8 @@ public class SyncedGuiDescription extends ScreenHandler implements GuiDescriptio public static PropertyDelegate getBlockPropertyDelegate(ScreenHandlerContext ctx) { 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(0); @@ -453,8 +453,8 @@ public class SyncedGuiDescription extends ScreenHandler implements GuiDescriptio 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 251fed7..13253b8 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 3d3ce9d..a625b2e 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 class WBox extends WPanelWithInsets { 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 8caad27..41e7f51 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 class WCardPanel extends WPanel { 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 76e72aa..1650e2a 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 class WItemSlot extends WWidget { @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 5503f73..63ccdea 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 abstract class WPanel extends WWidget { */ 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 f5655b4..97e7884 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 class WPlayerInvPanel extends WPlainPanel { @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 4e3669b..41185c4 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 class WScrollPanel extends WPanel { 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); -- cgit