A Minecraft server-side mod that adds various teleportation related commands
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Turn DeathLocation into a Hashmap

MrSnowy 7d883bf0 c55096d6

+35 -52
+25 -30
common/src/main/java/dev/mrsnowy/teleport_commands/commands/back.java
··· 63 63 64 64 // ----- 65 65 66 - 67 66 // Gets the DeathLocation of the player and teleports the player to it 68 67 private static void ToDeathLocation(ServerPlayer player, boolean safetyDisabled) throws Exception { 68 + DeathLocation deathLocation = DeathLocationStorage 69 + .getDeathLocation(player.getStringUUID()) 70 + .orElse(null); 69 71 70 - Optional<DeathLocation> optionalDeathLocation = DeathLocationStorage.getDeathLocation(player.getStringUUID()); 71 - 72 - if (optionalDeathLocation.isEmpty()) { 73 - player.displayClientMessage(getTranslatedText("commands.teleport_commands.common.noLocation", player) 74 - .withStyle(ChatFormatting.RED), true); 72 + if (deathLocation == null) { 73 + player.displayClientMessage(getTranslatedText("commands.teleport_commands.common.noLocation", player).withStyle(ChatFormatting.RED), true); 75 74 return; 76 75 } 77 76 78 - DeathLocation deathLocation = optionalDeathLocation.get(); 79 - 80 77 // Get the world, otherwise give a warning and error message 81 - Optional<ServerLevel> optionalWorld = deathLocation.getWorld(); 78 + ServerLevel deathLocationWorld = deathLocation.getWorld().orElse(null); 82 79 83 - if (optionalWorld.isEmpty()) { 80 + if (deathLocationWorld == null) { 84 81 Constants.LOGGER.warn("({}) Error while going back! \nCouldn't find a world with the id: \"{}\" \nAvailable worlds: {}", 85 82 player.getName().getString(), 86 83 deathLocation.getWorldString(), ··· 92 89 return; 93 90 } 94 91 95 - ServerLevel deathLocationWorld = optionalWorld.get(); 96 92 BlockPos teleportBlockPos; 97 93 98 94 // Sets the teleportBlockPos based on if it should do safety checking ··· 100 96 Optional<BlockPos> safeBlockPos = getSafeBlockPos(deathLocation.getBlockPos(), deathLocationWorld); 101 97 102 98 // Check if there is a safe BlockPos 103 - if (safeBlockPos.isPresent()) { 104 - teleportBlockPos = safeBlockPos.get(); 105 - 106 - } else { 99 + if (safeBlockPos.isEmpty()) { 107 100 // asks the player if they want to teleport anyway 108 101 player.displayClientMessage( 109 102 Component.empty() 110 - .append(getTranslatedText("commands.teleport_commands.common.noSafeLocation", player) 111 - .withStyle(ChatFormatting.RED, ChatFormatting.BOLD) 112 - ) 113 - .append("\n") 114 - .append(getTranslatedText("commands.teleport_commands.common.safetyIsForLosers", player) 115 - .withStyle(ChatFormatting.WHITE) 116 - ) 117 - .append("\n") 118 - .append(getTranslatedText("commands.teleport_commands.common.forceTeleport", player) 119 - .withStyle(ChatFormatting.DARK_AQUA, ChatFormatting.BOLD) 120 - .withStyle(style -> style.withClickEvent(new ClickEvent.RunCommand("/back true"))) 121 - ) 122 - .append("\n"), false); 103 + .append(getTranslatedText("commands.teleport_commands.common.noSafeLocation", player) 104 + .withStyle(ChatFormatting.RED, ChatFormatting.BOLD) 105 + ) 106 + .append("\n") 107 + .append(getTranslatedText("commands.teleport_commands.common.safetyIsForLosers", player) 108 + .withStyle(ChatFormatting.WHITE) 109 + ) 110 + .append("\n") 111 + .append(getTranslatedText("commands.teleport_commands.common.forceTeleport", player) 112 + .withStyle(ChatFormatting.DARK_AQUA, ChatFormatting.BOLD) 113 + .withStyle(style -> style.withClickEvent(new ClickEvent.RunCommand("/back true"))) 114 + ) 115 + .append("\n"), false); 123 116 return; 117 + 118 + } else { 119 + teleportBlockPos = safeBlockPos.get(); 124 120 } 125 - 126 121 } else { 127 122 // no checking needed, just set it. 128 123 teleportBlockPos = deathLocation.getBlockPos(); ··· 137 132 Vec3 teleportPos = new Vec3(teleportBlockPos.getX() + 0.5, teleportBlockPos.getY(), teleportBlockPos.getZ() + 0.5); 138 133 139 134 player.displayClientMessage(getTranslatedText("commands.teleport_commands.back.go", player), true); 140 - Teleporter(player, deathLocationWorld, teleportPos); 135 + tools.Teleporter(player, deathLocationWorld, teleportPos); 141 136 } 142 137 } 143 138 }
+1 -7
common/src/main/java/dev/mrsnowy/teleport_commands/common/DeathLocation.java
··· 9 9 import java.util.stream.StreamSupport; 10 10 11 11 public class DeathLocation { 12 - private final String UUID; 13 12 private BlockPos pos; 14 13 private String world; 15 14 16 - public DeathLocation(String uuid, BlockPos pos, String world) { 17 - this.UUID = uuid; 15 + public DeathLocation(BlockPos pos, String world) { 18 16 this.pos = pos; 19 17 this.world = world; 20 18 } 21 19 22 20 // ----- 23 - 24 - public String getUUID() { 25 - return UUID; 26 - } 27 21 28 22 public BlockPos getBlockPos() { 29 23 return pos;
-1
common/src/main/java/dev/mrsnowy/teleport_commands/common/NamedLocation.java
··· 56 56 return StreamSupport.stream( TeleportCommands.SERVER.getAllLevels().spliterator(), false ) // woa, this looks silly 57 57 .filter(level -> Objects.equals( level.dimension().location().toString(), this.world )) 58 58 .findFirst(); 59 - 60 59 } 61 60 62 61 // -----
+9 -14
common/src/main/java/dev/mrsnowy/teleport_commands/storage/DeathLocationStorage.java
··· 3 3 import dev.mrsnowy.teleport_commands.common.DeathLocation; 4 4 import net.minecraft.core.BlockPos; 5 5 6 - import java.util.ArrayList; 7 - import java.util.Objects; 6 + import java.util.HashMap; 8 7 import java.util.Optional; 9 8 10 9 public class DeathLocationStorage { 11 - private static final ArrayList<DeathLocation> deathLocations = new ArrayList<>(); 10 + private static final HashMap<String, DeathLocation> deathLocations = new HashMap<>(); 12 11 13 12 // filters the deathLocationList and finds the one with the matching player uuid (if there is one) 14 13 public static Optional<DeathLocation> getDeathLocation(String uuid) { 15 - return deathLocations.stream() 16 - .filter( deathLocation -> Objects.equals( deathLocation.getUUID(), uuid )) 17 - .findFirst(); 14 + return Optional.ofNullable(deathLocations.get(uuid)); 18 15 } 19 16 20 17 // updates the deathLocation of a player, if there is no existing entry it will create a new deathLocation. 21 18 public static void setDeathLocation(String uuid, BlockPos pos, String world) { 22 - Optional<DeathLocation> OptionalDeathLocation = getDeathLocation(uuid); 23 19 24 - if (OptionalDeathLocation.isEmpty()) { 25 - // create a new deathLocation 26 - DeathLocation deathLocation = new DeathLocation(uuid, pos, world); 27 - deathLocations.add(deathLocation); 28 - } else { 20 + if (deathLocations.containsKey(uuid)) { 29 21 // modify existing deathLocation 30 - DeathLocation deathLocation = OptionalDeathLocation.get(); 31 - 22 + DeathLocation deathLocation = deathLocations.get(uuid); 32 23 deathLocation.setBlockPos(pos); 33 24 deathLocation.setWorld(world); 25 + } else { 26 + // create a new deathLocation 27 + DeathLocation deathLocation = new DeathLocation(pos, world); 28 + deathLocations.put(uuid, deathLocation); 34 29 } 35 30 } 36 31