Skip to content

Commit

Permalink
Use pattern matching instanceof everywhere
Browse files Browse the repository at this point in the history
Resolves #216.
  • Loading branch information
Juuxel committed Nov 18, 2023
1 parent e79301a commit 0a45702
Show file tree
Hide file tree
Showing 8 changed files with 20 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -393,22 +393,22 @@ private static Inventory getBlockInventory(ScreenHandlerContext ctx, Supplier<In
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;
}
}

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;
}
}

Expand All @@ -429,8 +429,8 @@ private static Inventory getBlockInventory(ScreenHandlerContext ctx, Supplier<In
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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ public interface BackgroundPainter {
* <p>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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 0a45702

Please sign in to comment.