From 3b3a86a4020b9b271da2dfb9cf41e01a1c9d3bd1 Mon Sep 17 00:00:00 2001 From: LeoDog896 Date: Fri, 15 Jan 2021 13:39:12 -0500 Subject: [PATCH] Prefer nullable over optional --- .../net/minestom/codegen/blocks/BlockEnumGenerator.java | 6 +----- .../net/minestom/codegen/items/ItemEnumGenerator.java | 2 -- .../java/net/minestom/server/command/CommandSender.java | 2 +- src/main/java/net/minestom/server/entity/Player.java | 2 +- .../selfmodification/MinestomExtensionClassLoader.java | 3 +-- .../network/packet/server/play/CombatEventPacket.java | 8 ++++---- .../net/minestom/server/utils/entity/EntityFinder.java | 6 +++--- src/test/java/demo/commands/DimensionCommand.java | 3 +-- src/test/java/demo/commands/ReloadExtensionCommand.java | 5 ++--- 9 files changed, 14 insertions(+), 23 deletions(-) diff --git a/src/generators/java/net/minestom/codegen/blocks/BlockEnumGenerator.java b/src/generators/java/net/minestom/codegen/blocks/BlockEnumGenerator.java index 9b9eb8fb8..7b519b96e 100644 --- a/src/generators/java/net/minestom/codegen/blocks/BlockEnumGenerator.java +++ b/src/generators/java/net/minestom/codegen/blocks/BlockEnumGenerator.java @@ -35,7 +35,7 @@ public class BlockEnumGenerator extends MinestomEnumGenerator { private final String targetVersion; private final File targetFolder; - private CodeBlock.Builder staticBlock = CodeBlock.builder(); + private final CodeBlock.Builder staticBlock = CodeBlock.builder(); public static void main(String[] args) throws IOException { @@ -161,8 +161,6 @@ public class BlockEnumGenerator extends MinestomEnumGenerator { } return blocks; - } catch (IOException e) { - throw e; } } @@ -178,8 +176,6 @@ public class BlockEnumGenerator extends MinestomEnumGenerator { try (BufferedReader bufferedReader = new BufferedReader(new FileReader(blockFile))) { PrismarineJSBlock[] blocks = gson.fromJson(bufferedReader, PrismarineJSBlock[].class); return Arrays.asList(blocks); - } catch (IOException e) { - throw e; } } diff --git a/src/generators/java/net/minestom/codegen/items/ItemEnumGenerator.java b/src/generators/java/net/minestom/codegen/items/ItemEnumGenerator.java index ab3900ece..4b4f44053 100644 --- a/src/generators/java/net/minestom/codegen/items/ItemEnumGenerator.java +++ b/src/generators/java/net/minestom/codegen/items/ItemEnumGenerator.java @@ -85,8 +85,6 @@ public class ItemEnumGenerator extends MinestomEnumGenerator { items.add(item); } return items; - } catch (IOException e) { - throw e; } } diff --git a/src/main/java/net/minestom/server/command/CommandSender.java b/src/main/java/net/minestom/server/command/CommandSender.java index d2cfea6db..a5f928af2 100644 --- a/src/main/java/net/minestom/server/command/CommandSender.java +++ b/src/main/java/net/minestom/server/command/CommandSender.java @@ -39,7 +39,7 @@ public interface CommandSender extends PermissionHandler { * */ default void sendMessage(@NotNull JsonMessage text) { if (this instanceof Player) { - ((Player) this).sendMessage((JsonMessage) text); + this.sendMessage(text); } else { sendMessage(text.getRawMessage()); } diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 43ba66023..ba2ce704e 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -536,7 +536,7 @@ public class Player extends LivingEntity implements CommandSender { // #buildDeathScreenText can return null, check here if (deathText != null) { - CombatEventPacket deathPacket = CombatEventPacket.death(this, Optional.empty(), deathText); + CombatEventPacket deathPacket = CombatEventPacket.death(this, null, deathText); playerConnection.sendPacket(deathPacket); } diff --git a/src/main/java/net/minestom/server/extras/selfmodification/MinestomExtensionClassLoader.java b/src/main/java/net/minestom/server/extras/selfmodification/MinestomExtensionClassLoader.java index 7df07b288..6afbee103 100644 --- a/src/main/java/net/minestom/server/extras/selfmodification/MinestomExtensionClassLoader.java +++ b/src/main/java/net/minestom/server/extras/selfmodification/MinestomExtensionClassLoader.java @@ -58,8 +58,7 @@ public class MinestomExtensionClassLoader extends HierarchyClassLoader { } catch (ClassNotFoundException e) { for(MinestomExtensionClassLoader child : children) { try { - Class loaded = child.loadClassAsChild(name, resolve); - return loaded; + return child.loadClassAsChild(name, resolve); } catch (ClassNotFoundException e1) { // move on to next child } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/CombatEventPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/CombatEventPacket.java index 685a2ad1c..86ddb4df5 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/CombatEventPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/CombatEventPacket.java @@ -31,19 +31,19 @@ public class CombatEventPacket implements ServerPacket { return packet; } - public static CombatEventPacket end(int durationInTicks, Optional opponent) { + public static CombatEventPacket end(int durationInTicks, Entity opponent) { CombatEventPacket packet = new CombatEventPacket(); packet.type = EventType.END_COMBAT; packet.duration = durationInTicks; - packet.opponent = opponent.map(Entity::getEntityId).orElse(-1); + packet.opponent = opponent != null ? opponent.getEntityId() : -1; return packet; } - public static CombatEventPacket death(Player player, Optional killer, JsonMessage message) { + public static CombatEventPacket death(Player player, Entity killer, JsonMessage message) { CombatEventPacket packet = new CombatEventPacket(); packet.type = EventType.DEATH; packet.playerID = player.getEntityId(); - packet.opponent = killer.map(Entity::getEntityId).orElse(-1); + packet.opponent = killer != null ? killer.getEntityId() : -1; packet.deathMessage = message; return packet; } diff --git a/src/main/java/net/minestom/server/utils/entity/EntityFinder.java b/src/main/java/net/minestom/server/utils/entity/EntityFinder.java index e048ad979..2d8e34b44 100644 --- a/src/main/java/net/minestom/server/utils/entity/EntityFinder.java +++ b/src/main/java/net/minestom/server/utils/entity/EntityFinder.java @@ -281,20 +281,20 @@ public class EntityFinder { closestDistance = distance; } } - return Arrays.asList(entity); + return Collections.singletonList(entity); } else if (targetSelector == TargetSelector.RANDOM_PLAYER) { Collection instancePlayers = instance != null ? instance.getPlayers() : MinecraftServer.getConnectionManager().getOnlinePlayers(); final int index = ThreadLocalRandom.current().nextInt(instancePlayers.size()); final Player player = instancePlayers.stream().skip(index).findFirst().orElseThrow(); - return Arrays.asList(player); + return Collections.singletonList(player); } else if (targetSelector == TargetSelector.ALL_PLAYERS) { return new ArrayList<>(instance != null ? instance.getPlayers() : MinecraftServer.getConnectionManager().getOnlinePlayers()); } else if (targetSelector == TargetSelector.ALL_ENTITIES) { return new ArrayList<>(instance.getEntities()); } else if (targetSelector == TargetSelector.SELF) { - return Arrays.asList(self); + return Collections.singletonList(self); } throw new IllegalStateException("Weird thing happened"); } diff --git a/src/test/java/demo/commands/DimensionCommand.java b/src/test/java/demo/commands/DimensionCommand.java index bfbf0667b..285fba90f 100644 --- a/src/test/java/demo/commands/DimensionCommand.java +++ b/src/test/java/demo/commands/DimensionCommand.java @@ -36,8 +36,7 @@ public class DimensionCommand implements CommandProcessor { // targetDimensionType = DimensionType.OVERWORLD; //} - DimensionType finalTargetDimensionType = targetDimensionType; - Optional targetInstance = MinecraftServer.getInstanceManager().getInstances().stream().filter(in -> in.getDimensionType() == finalTargetDimensionType).findFirst(); + Optional targetInstance = MinecraftServer.getInstanceManager().getInstances().stream().filter(in -> in.getDimensionType() == targetDimensionType).findFirst(); if (targetInstance.isPresent()) { player.sendMessage("You were in " + instance.getDimensionType()); player.setInstance(targetInstance.get()); diff --git a/src/test/java/demo/commands/ReloadExtensionCommand.java b/src/test/java/demo/commands/ReloadExtensionCommand.java index a7f21874b..305cba1bd 100644 --- a/src/test/java/demo/commands/ReloadExtensionCommand.java +++ b/src/test/java/demo/commands/ReloadExtensionCommand.java @@ -25,11 +25,10 @@ public class ReloadExtensionCommand extends Command { private static String[] extensionsName; static { - List extensionsName = MinecraftServer.getExtensionManager().getExtensions() + ReloadExtensionCommand.extensionsName = MinecraftServer.getExtensionManager().getExtensions() .stream() .map(extension -> extension.getDescription().getName()) - .collect(Collectors.toList()); - ReloadExtensionCommand.extensionsName = extensionsName.toArray(new String[0]); + .toArray(String[]::new); } public ReloadExtensionCommand() {