From 678f37e18419695352d0dad3a436e2137e8950a9 Mon Sep 17 00:00:00 2001 From: Sen Date: Thu, 12 Jun 2025 10:01:16 +0200 Subject: [PATCH] improve some instanceof expressions --- .../main/java/client/audio/SoundManager.java | 4 +- client/src/main/java/client/gui/Gui.java | 4 +- .../java/client/gui/element/Dropdown.java | 6 +- .../main/java/client/gui/element/Switch.java | 2 +- .../main/java/client/gui/ingame/GuiForm.java | 7 +- .../java/client/gui/options/GuiStyle.java | 3 +- .../java/client/network/ClientPlayer.java | 80 +++++++++---------- 7 files changed, 48 insertions(+), 58 deletions(-) diff --git a/client/src/main/java/client/audio/SoundManager.java b/client/src/main/java/client/audio/SoundManager.java index 8886b95..b37eb87 100755 --- a/client/src/main/java/client/audio/SoundManager.java +++ b/client/src/main/java/client/audio/SoundManager.java @@ -238,8 +238,8 @@ public class SoundManager { this.sources.put(s, new Source(getBuffer(sound.getSoundLocation()), Volume.getByType(sound.getChannelType()), sound.canRepeat(), s, sound.getXPosF(), sound.getYPosF(), sound.getZPosF(), sound.getAttenuationType(), attn, volume)); this.playingSoundsStopTime.put(s, this.playTime + 20); this.playingSounds.put(s, sound); - if (sound instanceof MovingSound) - this.tickableSounds.add((MovingSound)sound); + if (sound instanceof MovingSound moving) + this.tickableSounds.add(moving); } } diff --git a/client/src/main/java/client/gui/Gui.java b/client/src/main/java/client/gui/Gui.java index 73628a8..871b64f 100644 --- a/client/src/main/java/client/gui/Gui.java +++ b/client/src/main/java/client/gui/Gui.java @@ -176,8 +176,8 @@ public abstract class Gui { this.min_y = Math.min(this.min_y, elem.getY()); this.max_x = Math.max(this.max_x, elem.getX() + elem.getWidth()); this.max_y = Math.max(this.max_y, elem.getY() + elem.getHeight()); - if(elem instanceof Dropdown) - this.add(((Dropdown)elem).getHandle()); + if(elem instanceof Dropdown drop) + this.add(drop.getHandle()); return elem; } diff --git a/client/src/main/java/client/gui/element/Dropdown.java b/client/src/main/java/client/gui/element/Dropdown.java index 2850a3b..773cda0 100644 --- a/client/src/main/java/client/gui/element/Dropdown.java +++ b/client/src/main/java/client/gui/element/Dropdown.java @@ -17,7 +17,7 @@ public class Dropdown extends Element { for(T value : Dropdown.this.values) { if(sb.length() > 0) sb.append('\n'); - sb.append(value instanceof Displayable ? ((Displayable)value).getDisplay() : value.toString()); + sb.append(value instanceof Displayable disp ? disp.getDisplay() : value.toString()); } this.setText(sb.toString()); this.visible = /* this.r_dirty = */ false; @@ -85,12 +85,12 @@ public class Dropdown extends Element { this(x, y, w, h, up, values, def, init, callback, text == null ? new Formatter>() { public String use(Dropdown elem) { T value = elem.getValue(); - return value instanceof Displayable ? ((Displayable)value).getDisplay() : value.toString(); + return value instanceof Displayable disp ? disp.getDisplay() : value.toString(); } } : new Formatter>() { public String use(Dropdown elem) { T value = elem.getValue(); - return String.format("%s: %s", text, value instanceof Displayable ? ((Displayable)value).getDisplay() : value.toString()); + return String.format("%s: %s", text, value instanceof Displayable disp ? disp.getDisplay() : value.toString()); } }); } diff --git a/client/src/main/java/client/gui/element/Switch.java b/client/src/main/java/client/gui/element/Switch.java index 878ebcc..02449a8 100644 --- a/client/src/main/java/client/gui/element/Switch.java +++ b/client/src/main/java/client/gui/element/Switch.java @@ -25,7 +25,7 @@ public class Switch extends Element { this(x, y, w, h, values, def, init, callback, new Formatter>() { public String use(Switch elem) { T value = elem.getValue(); - return String.format("%s: %s", text, value instanceof Displayable ? ((Displayable)value).getDisplay() : value.toString()); + return String.format("%s: %s", text, value instanceof Displayable disp ? disp.getDisplay() : value.toString()); } }); } diff --git a/client/src/main/java/client/gui/ingame/GuiForm.java b/client/src/main/java/client/gui/ingame/GuiForm.java index e64bd18..510de33 100644 --- a/client/src/main/java/client/gui/ingame/GuiForm.java +++ b/client/src/main/java/client/gui/ingame/GuiForm.java @@ -38,15 +38,14 @@ public class GuiForm extends Gui implements ButtonCallback { final String name = this.inputData[z].first(); Object obj = this.inputData[z].second(); int param = this.inputData[z].third(); - if(obj instanceof Boolean) { - this.inputs[z] = this.add(new Toggle(0, 50 * z, 300, 0, (Boolean)obj, (Boolean)obj, new ToggleCallback() { + if(obj instanceof Boolean bool) { + this.inputs[z] = this.add(new Toggle(0, 50 * z, 300, 0, bool, bool, new ToggleCallback() { public void use(Toggle elem, boolean value) { GuiForm.this.outputData[index] = value; } }, name)); } - else if(obj instanceof String[]) { - final String[] strs = (String[])obj; + else if(obj instanceof String[] strs) { param = ExtMath.clampi(param, 0, strs.length - 1); this.inputs[z] = this.add(new Switch(0, 50 * z, 300, 0, strs, strs[param], strs[param], new SwitchCallback() { public void use(Switch elem, String value) { diff --git a/client/src/main/java/client/gui/options/GuiStyle.java b/client/src/main/java/client/gui/options/GuiStyle.java index df62b65..c322e25 100644 --- a/client/src/main/java/client/gui/options/GuiStyle.java +++ b/client/src/main/java/client/gui/options/GuiStyle.java @@ -40,8 +40,7 @@ public class GuiStyle extends GuiOptions implements DropdownCallback, Bu protected Element addSelector(String cvar, int x, int y, int w, int h) { CVar cv = this.gm.getVar(cvar); - if(cv instanceof ColorVar) { - ColorVar color = (ColorVar)cv; + if(cv instanceof ColorVar color) { if(!color.getDisplay().isEmpty()) this.add(color.label(x, y, w)); if(this.gm.style != Style.CUSTOM) diff --git a/client/src/main/java/client/network/ClientPlayer.java b/client/src/main/java/client/network/ClientPlayer.java index e58f94f..ff04180 100755 --- a/client/src/main/java/client/network/ClientPlayer.java +++ b/client/src/main/java/client/network/ClientPlayer.java @@ -391,8 +391,8 @@ public class ClientPlayer implements IClientPlayer entity.setId(packetIn.getEntityID()); this.world.addEntityToWorld(packetIn.getEntityID(), entity); - if(entity instanceof EntityProjectile) { - ((EntityProjectile)entity).setAcceleration((double)packetIn.getSpeedX() / 8000.0D, (double)packetIn.getSpeedY() / 8000.0D, (double)packetIn.getSpeedZ() / 8000.0D); + if(entity instanceof EntityProjectile projectile) { + projectile.setAcceleration((double)packetIn.getSpeedX() / 8000.0D, (double)packetIn.getSpeedY() / 8000.0D, (double)packetIn.getSpeedZ() / 8000.0D); } else if (entity.hasSpawnVelocity()) // packetIn.getData() > 0) { @@ -492,8 +492,8 @@ public class ClientPlayer implements IClientPlayer { entity.getDataWatcher().updateWatchedObjectsFromList(packetIn.func_149376_c()); - if(entity == this.gm.player && this.gm.open instanceof GuiChar) - ((GuiChar)this.gm.open).checkReopen(); + if(entity == this.gm.player && this.gm.open instanceof GuiChar gui) + gui.checkReopen(); } } @@ -990,16 +990,16 @@ public class ClientPlayer implements IClientPlayer { entity = this.gm.player; - if (entity1 instanceof EntityBoat) + if (entity1 instanceof EntityBoat boat) { - ((EntityBoat)entity1).setIsBoatEmpty(false); + boat.setIsBoatEmpty(false); } // flag = entity.vehicle == null && entity1 != null; } - else if (entity1 instanceof EntityBoat) + else if (entity1 instanceof EntityBoat boat) { - ((EntityBoat)entity1).setIsBoatEmpty(true); + boat.setIsBoatEmpty(true); } if (entity == null) @@ -1014,15 +1014,15 @@ public class ClientPlayer implements IClientPlayer // this.gameController.ingameGui.displayHotbar(TextColor.LIGHT_GRAY + String.format("Drücke %s zum Verlassen", this.gameController.keyBindSneak.getKeyDisplay())); // } } - else if (packetIn.getLeash() == 1 && entity instanceof EntityLiving) + else if (packetIn.getLeash() == 1 && entity instanceof EntityLiving living) { if (entity1 != null) { - ((EntityLiving)entity).setLeashedTo(entity1, false); + living.setLeashedTo(entity1, false); } else { - ((EntityLiving)entity).clearLeashed(false, false); + living.clearLeashed(false, false); } } } @@ -1164,15 +1164,15 @@ public class ClientPlayer implements IClientPlayer else if ("tile".equals(id)) { TileEntity tile = this.world.getTileEntity(packet.getTilePos()); - if(!(tile instanceof TileEntityDevice)) + if(!(tile instanceof TileEntityDevice dev)) return; - this.gm.displayGuiScreen(new GuiTile(this.gm.player.inventory, local, (TileEntityDevice)tile)); + this.gm.displayGuiScreen(new GuiTile(this.gm.player.inventory, local, dev)); } else { Block block = BlockRegistry.getRegisteredBlock(id); - if(block instanceof BlockWorkbench) { - this.gm.displayGuiScreen(new GuiCrafting(player.inventory, player.worldObj, (BlockWorkbench)block)); + if(block instanceof BlockWorkbench bench) { + this.gm.displayGuiScreen(new GuiCrafting(player.inventory, player.worldObj, bench)); } else { this.gm.displayGuiScreen(new GuiChest(player.inventory, local)); @@ -1299,10 +1299,8 @@ public class ClientPlayer implements IClientPlayer { TileEntity tileentity = this.gm.world.getTileEntity(packetIn.getPos()); - if (tileentity instanceof TileEntitySign) + if (tileentity instanceof TileEntitySign tileentitysign) { - TileEntitySign tileentitysign = (TileEntitySign)tileentity; - // if (tileentitysign.getIsEditable()) // { System.arraycopy(packetIn.getLines(), 0, tileentitysign.signText, 0, 4); @@ -1512,11 +1510,11 @@ public class ClientPlayer implements IClientPlayer NetHandler.checkThread(packetIn, this, this.gm, this.world); Entity entity = this.world.getEntityByID(packetIn.getEntityId()); - if (entity instanceof EntityLiving) + if (entity instanceof EntityLiving living) { PotionEffect potioneffect = new PotionEffect(packetIn.getEffectId(), packetIn.getDuration(), packetIn.getAmplifier(), false, packetIn.hasParticles()) .setRemaining(packetIn.getRemaining()); - ((EntityLiving)entity).addEffect(potioneffect); + living.addEffect(potioneffect); } } @@ -1589,9 +1587,9 @@ public class ClientPlayer implements IClientPlayer NetHandler.checkThread(packetIn, this, this.gm, this.world); Entity entity = this.world.getEntityByID(packetIn.getEntityId()); - if (entity instanceof EntityLiving) + if (entity instanceof EntityLiving living) { - ((EntityLiving)entity).removeEffectClient(packetIn.getEffectId()); + living.removeEffectClient(packetIn.getEffectId()); } } @@ -1661,9 +1659,8 @@ public class ClientPlayer implements IClientPlayer NetHandler.checkThread(packetIn, this, this.gm, this.world); String[] astring = packetIn.func_149630_c(); // this.gameController.complete(astring); - if (this.gm.open instanceof GuiConsole) + if (this.gm.open instanceof GuiConsole guichat) { - GuiConsole guichat = (GuiConsole)this.gm.open; guichat.onAutocompleteResponse(astring); } } @@ -1886,30 +1883,25 @@ public class ClientPlayer implements IClientPlayer if (entity != null) { - if (!(entity instanceof EntityLiving)) - { + if (!(entity instanceof EntityLiving living)) throw new IllegalStateException("Server tried to update attributes of a non-living entity (actually: " + entity + ")"); - } - else + AttributeMap baseattributemap = living.getAttributeMap(); + + for (SPacketEntityProperties.Snapshot s20packetentityproperties$snapshot : packetIn.func_149441_d()) { - AttributeMap baseattributemap = ((EntityLiving)entity).getAttributeMap(); + AttributeInstance iattributeinstance = baseattributemap.getAttributeInstanceByName(s20packetentityproperties$snapshot.func_151409_a()); - for (SPacketEntityProperties.Snapshot s20packetentityproperties$snapshot : packetIn.func_149441_d()) + if (iattributeinstance == null) { - AttributeInstance iattributeinstance = baseattributemap.getAttributeInstanceByName(s20packetentityproperties$snapshot.func_151409_a()); + iattributeinstance = baseattributemap.registerAttribute(Attribute.getAttribute(s20packetentityproperties$snapshot.func_151409_a())); + } - if (iattributeinstance == null) - { - iattributeinstance = baseattributemap.registerAttribute(Attribute.getAttribute(s20packetentityproperties$snapshot.func_151409_a())); - } + iattributeinstance.setBaseValue(s20packetentityproperties$snapshot.func_151410_b()); + iattributeinstance.removeAllModifiers(); - iattributeinstance.setBaseValue(s20packetentityproperties$snapshot.func_151410_b()); - iattributeinstance.removeAllModifiers(); - - for (AttributeModifier attributemodifier : s20packetentityproperties$snapshot.func_151408_c()) - { - iattributeinstance.applyModifier(attributemodifier); - } + for (AttributeModifier attributemodifier : s20packetentityproperties$snapshot.func_151408_c()) + { + iattributeinstance.applyModifier(attributemodifier); } } } @@ -1950,11 +1942,11 @@ public class ClientPlayer implements IClientPlayer int i = packetIn.getWindowId(); Gui gui = this.gm.open; - if (gui != null && gui instanceof GuiMerchant && i == this.gm.player.openContainer.windowId) + if (gui != null && gui instanceof GuiMerchant merchant && i == this.gm.player.openContainer.windowId) { // NpcMerchant imerchant = ((GuiMerchant)guiscreen).getMerchant(); MerchantRecipeList merchantrecipelist = packetIn.getTrades(); - ((GuiMerchant)gui).setRecipes(merchantrecipelist); + merchant.setRecipes(merchantrecipelist); } } catch (Exception exception)