Skip to content

Commit

Permalink
Fixed locations of invalid worlds stay with null world if put in maps
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Jul 24, 2022
1 parent e756657 commit da7d0da
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 16 deletions.
@@ -0,0 +1,42 @@
package com.bgsoftware.superiorskyblock.core;

import org.bukkit.Location;

import java.util.Objects;

public class LocationKey {

private final String worldName;
private final double x;
private final double y;
private final double z;

public LocationKey(Location location) {
this(LazyWorldLocation.getWorldName(location), location.getX(), location.getY(), location.getZ());
}

public LocationKey(LazyWorldLocation location) {
this(location.getWorldName(), location.getX(), location.getY(), location.getZ());
}

private LocationKey(String worldName, double x, double y, double z) {
this.worldName = worldName;
this.x = x;
this.y = y;
this.z = z;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
LocationKey that = (LocationKey) o;
return Double.compare(that.x, x) == 0 && Double.compare(that.y, y) == 0 && Double.compare(that.z, z) == 0 && worldName.equals(that.worldName);
}

@Override
public int hashCode() {
return Objects.hash(worldName, x, y, z);
}

}
Expand Up @@ -30,6 +30,7 @@
import com.bgsoftware.superiorskyblock.core.ChunkPosition;
import com.bgsoftware.superiorskyblock.core.IslandArea;
import com.bgsoftware.superiorskyblock.core.LazyWorldLocation;
import com.bgsoftware.superiorskyblock.core.LocationKey;
import com.bgsoftware.superiorskyblock.core.SBlockPosition;
import com.bgsoftware.superiorskyblock.core.SequentialListBuilder;
import com.bgsoftware.superiorskyblock.core.ServerVersion;
Expand Down Expand Up @@ -194,7 +195,7 @@ public class SIsland implements Island {
* Island Warps
*/
private final Map<String, IslandWarp> warpsByName = new ConcurrentHashMap<>();
private final Map<Location, IslandWarp> warpsByLocation = new ConcurrentHashMap<>();
private final Map<LocationKey, IslandWarp> warpsByLocation = new ConcurrentHashMap<>();
private final Map<String, WarpCategory> warpCategories = new ConcurrentHashMap<>();

/*
Expand Down Expand Up @@ -3940,8 +3941,7 @@ private void loadIslandWarp(IslandWarp islandWarp) {

Location location = islandWarp.getLocation();

warpsByLocation.put(new Location(location.getWorld(), location.getBlockX(),
location.getBlockY(), location.getBlockZ()), islandWarp);
warpsByLocation.put(new LocationKey(location), islandWarp);
}

private static int getGeneratedSchematicBitMask(World.Environment environment) {
Expand Down
Expand Up @@ -4,14 +4,14 @@
import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.commands.ISuperiorCommand;
import com.bgsoftware.superiorskyblock.core.threads.BukkitExecutor;
import com.bgsoftware.superiorskyblock.core.key.KeyImpl;
import com.bgsoftware.superiorskyblock.core.collections.AutoRemovalMap;
import com.bgsoftware.superiorskyblock.world.WorldBlocks;
import com.bgsoftware.superiorskyblock.core.LocationKey;
import com.bgsoftware.superiorskyblock.core.Materials;
import com.bgsoftware.superiorskyblock.core.ServerVersion;
import com.bgsoftware.superiorskyblock.core.collections.AutoRemovalMap;
import com.bgsoftware.superiorskyblock.core.key.KeyImpl;
import com.bgsoftware.superiorskyblock.core.threads.BukkitExecutor;
import com.bgsoftware.superiorskyblock.world.BukkitEntities;
import com.bgsoftware.superiorskyblock.world.BukkitItems;
import com.bgsoftware.superiorskyblock.core.Materials;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Location;
Expand Down Expand Up @@ -60,7 +60,7 @@ public List<ISuperiorCommand> getCommands() {

private class EntityLimitsListener implements Listener {

private final Map<Location, UUID> vehiclesOwners = AutoRemovalMap.newHashMap(2, TimeUnit.SECONDS);
private final Map<LocationKey, UUID> vehiclesOwners = AutoRemovalMap.newHashMap(2, TimeUnit.SECONDS);

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onEntitySpawn(CreatureSpawnEvent e) {
Expand Down Expand Up @@ -120,7 +120,7 @@ public void onVehicleSpawn(PlayerInteractEvent e) {

Location blockLocation = e.getClickedBlock().getLocation();

vehiclesOwners.put(blockLocation, e.getPlayer().getUniqueId());
vehiclesOwners.put(new LocationKey(blockLocation), e.getPlayer().getUniqueId());
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
Expand All @@ -133,7 +133,7 @@ public void onVehicleSpawn(VehicleCreateEvent e) {
if (island == null)
return;

UUID placedVehicle = vehiclesOwners.remove(WorldBlocks.getBlockLocation(e.getVehicle().getLocation()));
UUID placedVehicle = vehiclesOwners.remove(new LocationKey(e.getVehicle().getLocation()));

if (!BukkitEntities.canHaveLimit(e.getVehicle().getType()))
return;
Expand Down
Expand Up @@ -6,7 +6,6 @@
import com.bgsoftware.superiorskyblock.core.key.KeyImpl;
import com.bgsoftware.superiorskyblock.world.chunk.ChunksTracker;
import org.bukkit.ChunkSnapshot;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
Expand Down Expand Up @@ -61,8 +60,4 @@ public static boolean isChunkEmpty(Island island, ChunkSnapshot chunkSnapshot) {
return true;
}

public static Location getBlockLocation(Location location) {
return new Location(location.getWorld(), location.getBlockX(), location.getBlockY(), location.getBlockZ());
}

}

0 comments on commit da7d0da

Please sign in to comment.