improve some instanceof expressions

This commit is contained in:
Sen 2025-06-12 10:01:16 +02:00
parent 9eb5f2cfd3
commit 678f37e184
Signed by: sen
GPG key ID: 3AC50A6F47D1B722
7 changed files with 48 additions and 58 deletions

View file

@ -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.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.playingSoundsStopTime.put(s, this.playTime + 20);
this.playingSounds.put(s, sound); this.playingSounds.put(s, sound);
if (sound instanceof MovingSound) if (sound instanceof MovingSound moving)
this.tickableSounds.add((MovingSound)sound); this.tickableSounds.add(moving);
} }
} }

View file

@ -176,8 +176,8 @@ public abstract class Gui {
this.min_y = Math.min(this.min_y, elem.getY()); this.min_y = Math.min(this.min_y, elem.getY());
this.max_x = Math.max(this.max_x, elem.getX() + elem.getWidth()); this.max_x = Math.max(this.max_x, elem.getX() + elem.getWidth());
this.max_y = Math.max(this.max_y, elem.getY() + elem.getHeight()); this.max_y = Math.max(this.max_y, elem.getY() + elem.getHeight());
if(elem instanceof Dropdown) if(elem instanceof Dropdown drop)
this.add(((Dropdown)elem).getHandle()); this.add(drop.getHandle());
return elem; return elem;
} }

View file

@ -17,7 +17,7 @@ public class Dropdown<T> extends Element {
for(T value : Dropdown.this.values) { for(T value : Dropdown.this.values) {
if(sb.length() > 0) if(sb.length() > 0)
sb.append('\n'); 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.setText(sb.toString());
this.visible = /* this.r_dirty = */ false; this.visible = /* this.r_dirty = */ false;
@ -85,12 +85,12 @@ public class Dropdown<T> extends Element {
this(x, y, w, h, up, values, def, init, callback, text == null ? new Formatter<Dropdown<T>>() { this(x, y, w, h, up, values, def, init, callback, text == null ? new Formatter<Dropdown<T>>() {
public String use(Dropdown<T> elem) { public String use(Dropdown<T> elem) {
T value = elem.getValue(); T value = elem.getValue();
return value instanceof Displayable ? ((Displayable)value).getDisplay() : value.toString(); return value instanceof Displayable disp ? disp.getDisplay() : value.toString();
} }
} : new Formatter<Dropdown<T>>() { } : new Formatter<Dropdown<T>>() {
public String use(Dropdown<T> elem) { public String use(Dropdown<T> elem) {
T value = elem.getValue(); 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());
} }
}); });
} }

View file

@ -25,7 +25,7 @@ public class Switch<T> extends Element {
this(x, y, w, h, values, def, init, callback, new Formatter<Switch<T>>() { this(x, y, w, h, values, def, init, callback, new Formatter<Switch<T>>() {
public String use(Switch<T> elem) { public String use(Switch<T> elem) {
T value = elem.getValue(); 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());
} }
}); });
} }

View file

@ -38,15 +38,14 @@ public class GuiForm extends Gui implements ButtonCallback {
final String name = this.inputData[z].first(); final String name = this.inputData[z].first();
Object obj = this.inputData[z].second(); Object obj = this.inputData[z].second();
int param = this.inputData[z].third(); int param = this.inputData[z].third();
if(obj instanceof Boolean) { if(obj instanceof Boolean bool) {
this.inputs[z] = this.add(new Toggle(0, 50 * z, 300, 0, (Boolean)obj, (Boolean)obj, new ToggleCallback() { this.inputs[z] = this.add(new Toggle(0, 50 * z, 300, 0, bool, bool, new ToggleCallback() {
public void use(Toggle elem, boolean value) { public void use(Toggle elem, boolean value) {
GuiForm.this.outputData[index] = value; GuiForm.this.outputData[index] = value;
} }
}, name)); }, name));
} }
else if(obj instanceof String[]) { else if(obj instanceof String[] strs) {
final String[] strs = (String[])obj;
param = ExtMath.clampi(param, 0, strs.length - 1); param = ExtMath.clampi(param, 0, strs.length - 1);
this.inputs[z] = this.add(new Switch<String>(0, 50 * z, 300, 0, strs, strs[param], strs[param], new SwitchCallback<String>() { this.inputs[z] = this.add(new Switch<String>(0, 50 * z, 300, 0, strs, strs[param], strs[param], new SwitchCallback<String>() {
public void use(Switch<String> elem, String value) { public void use(Switch<String> elem, String value) {

View file

@ -40,8 +40,7 @@ public class GuiStyle extends GuiOptions implements DropdownCallback<String>, Bu
protected Element addSelector(String cvar, int x, int y, int w, int h) { protected Element addSelector(String cvar, int x, int y, int w, int h) {
CVar cv = this.gm.getVar(cvar); CVar cv = this.gm.getVar(cvar);
if(cv instanceof ColorVar) { if(cv instanceof ColorVar color) {
ColorVar color = (ColorVar)cv;
if(!color.getDisplay().isEmpty()) if(!color.getDisplay().isEmpty())
this.add(color.label(x, y, w)); this.add(color.label(x, y, w));
if(this.gm.style != Style.CUSTOM) if(this.gm.style != Style.CUSTOM)

View file

@ -391,8 +391,8 @@ public class ClientPlayer implements IClientPlayer
entity.setId(packetIn.getEntityID()); entity.setId(packetIn.getEntityID());
this.world.addEntityToWorld(packetIn.getEntityID(), entity); this.world.addEntityToWorld(packetIn.getEntityID(), entity);
if(entity instanceof EntityProjectile) { if(entity instanceof EntityProjectile projectile) {
((EntityProjectile)entity).setAcceleration((double)packetIn.getSpeedX() / 8000.0D, (double)packetIn.getSpeedY() / 8000.0D, (double)packetIn.getSpeedZ() / 8000.0D); projectile.setAcceleration((double)packetIn.getSpeedX() / 8000.0D, (double)packetIn.getSpeedY() / 8000.0D, (double)packetIn.getSpeedZ() / 8000.0D);
} }
else if (entity.hasSpawnVelocity()) // packetIn.getData() > 0) else if (entity.hasSpawnVelocity()) // packetIn.getData() > 0)
{ {
@ -492,8 +492,8 @@ public class ClientPlayer implements IClientPlayer
{ {
entity.getDataWatcher().updateWatchedObjectsFromList(packetIn.func_149376_c()); entity.getDataWatcher().updateWatchedObjectsFromList(packetIn.func_149376_c());
if(entity == this.gm.player && this.gm.open instanceof GuiChar) if(entity == this.gm.player && this.gm.open instanceof GuiChar gui)
((GuiChar)this.gm.open).checkReopen(); gui.checkReopen();
} }
} }
@ -990,16 +990,16 @@ public class ClientPlayer implements IClientPlayer
{ {
entity = this.gm.player; 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; // 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) 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())); // 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) if (entity1 != null)
{ {
((EntityLiving)entity).setLeashedTo(entity1, false); living.setLeashedTo(entity1, false);
} }
else else
{ {
((EntityLiving)entity).clearLeashed(false, false); living.clearLeashed(false, false);
} }
} }
} }
@ -1164,15 +1164,15 @@ public class ClientPlayer implements IClientPlayer
else if ("tile".equals(id)) else if ("tile".equals(id))
{ {
TileEntity tile = this.world.getTileEntity(packet.getTilePos()); TileEntity tile = this.world.getTileEntity(packet.getTilePos());
if(!(tile instanceof TileEntityDevice)) if(!(tile instanceof TileEntityDevice dev))
return; 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 else
{ {
Block block = BlockRegistry.getRegisteredBlock(id); Block block = BlockRegistry.getRegisteredBlock(id);
if(block instanceof BlockWorkbench) { if(block instanceof BlockWorkbench bench) {
this.gm.displayGuiScreen(new GuiCrafting(player.inventory, player.worldObj, (BlockWorkbench)block)); this.gm.displayGuiScreen(new GuiCrafting(player.inventory, player.worldObj, bench));
} }
else { else {
this.gm.displayGuiScreen(new GuiChest(player.inventory, local)); 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()); TileEntity tileentity = this.gm.world.getTileEntity(packetIn.getPos());
if (tileentity instanceof TileEntitySign) if (tileentity instanceof TileEntitySign tileentitysign)
{ {
TileEntitySign tileentitysign = (TileEntitySign)tileentity;
// if (tileentitysign.getIsEditable()) // if (tileentitysign.getIsEditable())
// { // {
System.arraycopy(packetIn.getLines(), 0, tileentitysign.signText, 0, 4); 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); NetHandler.checkThread(packetIn, this, this.gm, this.world);
Entity entity = this.world.getEntityByID(packetIn.getEntityId()); 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()) PotionEffect potioneffect = new PotionEffect(packetIn.getEffectId(), packetIn.getDuration(), packetIn.getAmplifier(), false, packetIn.hasParticles())
.setRemaining(packetIn.getRemaining()); .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); NetHandler.checkThread(packetIn, this, this.gm, this.world);
Entity entity = this.world.getEntityByID(packetIn.getEntityId()); 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); NetHandler.checkThread(packetIn, this, this.gm, this.world);
String[] astring = packetIn.func_149630_c(); String[] astring = packetIn.func_149630_c();
// this.gameController.complete(astring); // 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); guichat.onAutocompleteResponse(astring);
} }
} }
@ -1886,13 +1883,9 @@ public class ClientPlayer implements IClientPlayer
if (entity != null) 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 + ")"); throw new IllegalStateException("Server tried to update attributes of a non-living entity (actually: " + entity + ")");
} AttributeMap baseattributemap = living.getAttributeMap();
else
{
AttributeMap baseattributemap = ((EntityLiving)entity).getAttributeMap();
for (SPacketEntityProperties.Snapshot s20packetentityproperties$snapshot : packetIn.func_149441_d()) for (SPacketEntityProperties.Snapshot s20packetentityproperties$snapshot : packetIn.func_149441_d())
{ {
@ -1913,7 +1906,6 @@ public class ClientPlayer implements IClientPlayer
} }
} }
} }
}
public void handleSkin(SPacketSkin packetIn) { public void handleSkin(SPacketSkin packetIn) {
NetHandler.checkThread(packetIn, this, this.gm, this.world); NetHandler.checkThread(packetIn, this, this.gm, this.world);
@ -1950,11 +1942,11 @@ public class ClientPlayer implements IClientPlayer
int i = packetIn.getWindowId(); int i = packetIn.getWindowId();
Gui gui = this.gm.open; 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(); // NpcMerchant imerchant = ((GuiMerchant)guiscreen).getMerchant();
MerchantRecipeList merchantrecipelist = packetIn.getTrades(); MerchantRecipeList merchantrecipelist = packetIn.getTrades();
((GuiMerchant)gui).setRecipes(merchantrecipelist); merchant.setRecipes(merchantrecipelist);
} }
} }
catch (Exception exception) catch (Exception exception)