April 19, 20232 yr comment_91858 Assuming your source is compatable with Java 13 you could use enhanced Switch as shown below and if your using java 17 LTS you could move to using Switch Expressions they are very beautiful even more so if you are using Arrays with named types. Fun fact for anyone that is Beginner to Intermediate with the Java Programming Language did you know that Switch Cases are typically faster once compiled into byte code assuming you are using java conventions and using a modern JVM similar to Java 17. Just using one or two if statements will be faster than using Switch or Switch Expressions but if you look at most Server Applications they are riddled with if statements paired with ugly outdated and depressingly complex for a simple task. Meet Easier public boolean isDemon() { switch (Defender.npcType) { case 1531, 3134, 2006, 2026, 7244, 1432, 415, 7410, 135, 3133, 484, 1619, 7276, 3138, 7397, 7398, 11, 22978 -> { //Dragon Hunter Lance if (attacker.lastWeaponUsed == 22978 && defender.isDragon()) { damage *= 1.2; accuracy *= 1.2; } } } The First Part public boolean isDemon() { return switch (npcType) { case 1531, 3134, 2006, 2026, 7244, 1432, 415, 7410, 135, 3133, 484, 1619, 7276, 3138, 7397, 7398, 11 -> true; default -> false; }; } This assumes you have added anything useful yet here is the first switch statement switch (Defender.npcType) { case 1531: case 3134: case 2006: case 2026: case 7244: case 1432: case 415: case 7410: case 135: case 3133: case 484: case 1619: case 7276: case 3138: case 7397: case 7398: case 11: case 2978: { //Dragon Hunter Lance if (attacker.lastWeaponUsed == 22978 && defender.isDragon()) { damage *= 1.2; accuracy *= 1.2; } } return true; } return false; } yes this works just fine but its freaking ugly also not using break; or return; on each case also slows down the byte code optimizations as it skips and moves threw each case.
Create an account or sign in to comment