Skip to content

Commit

Permalink
Fixed visitor location not centered to the block (#1266)
Browse files Browse the repository at this point in the history
  • Loading branch information
OmerBenGera committed Jul 21, 2022
1 parent e6fb666 commit c1882f3
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 9 deletions.
Expand Up @@ -323,10 +323,22 @@ public interface Island extends Comparable<Island>, IMissionsHolder, IPersistent

/**
* Get the visitors' teleport location of the island.
*
* @deprecated See {@link #getVisitorsLocation(World.Environment)}
*/
@Nullable
@Deprecated
Location getVisitorsLocation();

/**
* Get the visitors' teleport location of the island.
*
* @param environment The environment to get the visitors-location from.
* Currently unused, it has no effect.
*/
@Nullable
Location getVisitorsLocation(World.Environment environment);

/**
* Set the visitors' teleport location of the island.
*
Expand Down
Expand Up @@ -62,7 +62,7 @@ public void execute(SuperiorSkyblockPlugin plugin, CommandSender sender, String[

SuperiorPlayer superiorPlayer = plugin.getPlayers().getSuperiorPlayer(sender);

Location visitLocation = targetIsland.getVisitorsLocation();
Location visitLocation = targetIsland.getVisitorsLocation(null /* unused */);

if (visitLocation == null) {
Message.INVALID_VISIT_LOCATION.send(sender);
Expand All @@ -87,7 +87,7 @@ public List<String> tabComplete(SuperiorSkyblockPlugin plugin, CommandSender sen
SuperiorPlayer superiorPlayer = plugin.getPlayers().getSuperiorPlayer(sender);
return args.length == 2 ? CommandTabCompletes.getOnlinePlayersWithIslands(plugin, args[1],
plugin.getSettings().isTabCompleteHideVanished(),
(onlinePlayer, onlineIsland) -> onlineIsland != null && (onlineIsland.getVisitorsLocation() != null ||
(onlinePlayer, onlineIsland) -> onlineIsland != null && (onlineIsland.getVisitorsLocation(null /* unused */) != null ||
superiorPlayer.hasBypassModeEnabled()) && (!onlineIsland.isLocked() ||
onlineIsland.hasPermission(superiorPlayer, IslandPrivileges.CLOSE_BYPASS))) : Collections.emptyList();
}
Expand Down
Expand Up @@ -35,7 +35,7 @@ public class MenuGlobalWarps extends PagedSuperiorMenu<MenuGlobalWarps, Island>

private final Predicate<Island> ISLANDS_FILTER = island -> {
if (visitorWarps)
return island.getVisitorsLocation() != null;
return island.getVisitorsLocation(null /* unused */) != null;
else if (island.equals(inventoryViewer.getIsland()))
return !island.getIslandWarps().isEmpty();
else
Expand Down
32 changes: 29 additions & 3 deletions src/main/java/com/bgsoftware/superiorskyblock/island/SIsland.java
Expand Up @@ -793,28 +793,38 @@ public void setIslandHome(World.Environment environment, @Nullable Location home

@Override
public Location getVisitorsLocation() {
return getVisitorsLocation(null /* unused */);
}

@Nullable
@Override
public Location getVisitorsLocation(World.Environment unused) {
Location visitorsLocation = this.visitorHomes.readAndGet(visitorsLocations -> visitorsLocations[0]);

if (visitorsLocation == null)
return null;

if (adjustLocationToCenterOfBlock(visitorsLocation))
IslandsDatabaseBridge.saveVisitorLocation(this, plugin.getSettings().getWorlds().getDefaultWorld(), visitorsLocation);

World world = plugin.getGrid().getIslandsWorld(this, plugin.getSettings().getWorlds().getDefaultWorld());
visitorsLocation.setWorld(world);

return visitorsLocation;
return visitorsLocation.clone();
}

@Override
public void setVisitorsLocation(Location visitorsLocation) {
if (visitorsLocation == null) {
PluginDebugger.debug("Action: Delete Visitors Location, Island: " + owner.getName());
this.visitorHomes.write(visitorsLocations -> visitorsLocations[0] = null);
IslandsDatabaseBridge.removeVisitorLocation(this, World.Environment.NORMAL);
IslandsDatabaseBridge.removeVisitorLocation(this, plugin.getSettings().getWorlds().getDefaultWorld());
} else {
adjustLocationToCenterOfBlock(visitorsLocation);
PluginDebugger.debug("Action: Change Visitors Location, Island: " + owner.getName() + ", Location: " +
Formatters.LOCATION_FORMATTER.format(visitorsLocation));
this.visitorHomes.write(visitorsLocations -> visitorsLocations[0] = visitorsLocation.clone());
IslandsDatabaseBridge.saveVisitorLocation(this, World.Environment.NORMAL, visitorsLocation);
IslandsDatabaseBridge.saveVisitorLocation(this, plugin.getSettings().getWorlds().getDefaultWorld(), visitorsLocation);
}
}

Expand Down Expand Up @@ -3947,6 +3957,22 @@ private static int getGeneratedSchematicBitMask(World.Environment environment) {
}
}

private static boolean adjustLocationToCenterOfBlock(Location location) {
boolean changed = false;

if (location.getX() - 0.5 != location.getBlockX()) {
location.setX(location.getBlockX() + 0.5);
changed = true;
}

if (location.getZ() - 0.5 != location.getBlockZ()) {
location.setZ(location.getBlockZ() + 0.5);
changed = true;
}

return changed;
}

public static class UniqueVisitor {

private final Pair<SuperiorPlayer, Long> pair;
Expand Down
Expand Up @@ -27,12 +27,12 @@
import com.bgsoftware.superiorskyblock.core.SequentialListBuilder;
import com.bgsoftware.superiorskyblock.core.database.bridge.EmptyDatabaseBridge;
import com.bgsoftware.superiorskyblock.core.errors.ManagerLoadException;
import com.bgsoftware.superiorskyblock.core.persistence.EmptyPersistentDataContainer;
import com.bgsoftware.superiorskyblock.core.serialization.Serializers;
import com.bgsoftware.superiorskyblock.core.threads.BukkitExecutor;
import com.bgsoftware.superiorskyblock.island.algorithm.SpawnIslandBlocksTrackerAlgorithm;
import com.bgsoftware.superiorskyblock.island.algorithm.SpawnIslandCalculationAlgorithm;
import com.bgsoftware.superiorskyblock.island.algorithm.SpawnIslandEntitiesTrackerAlgorithm;
import com.bgsoftware.superiorskyblock.core.persistence.EmptyPersistentDataContainer;
import com.bgsoftware.superiorskyblock.island.privilege.IslandPrivileges;
import com.bgsoftware.superiorskyblock.island.privilege.PlayerPrivilegeNode;
import com.bgsoftware.superiorskyblock.island.privilege.PrivilegeNodeAbstract;
Expand Down Expand Up @@ -325,6 +325,12 @@ public void setIslandHome(World.Environment environment, @Nullable Location home

@Override
public Location getVisitorsLocation() {
return getVisitorsLocation(null /* unused */);
}

@Nullable
@Override
public Location getVisitorsLocation(World.Environment unused) {
return getCenter(plugin.getSettings().getWorlds().getDefaultWorld());
}

Expand Down
Expand Up @@ -4,11 +4,11 @@
import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.api.island.warps.IslandWarp;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.core.messages.Message;
import com.bgsoftware.superiorskyblock.core.Materials;
import com.bgsoftware.superiorskyblock.core.ServerVersion;
import com.bgsoftware.superiorskyblock.core.events.EventResult;
import com.bgsoftware.superiorskyblock.core.formatting.Formatters;
import com.bgsoftware.superiorskyblock.core.messages.Message;
import com.bgsoftware.superiorskyblock.island.IslandUtils;
import org.bukkit.ChatColor;
import org.bukkit.Location;
Expand Down Expand Up @@ -205,7 +205,9 @@ private boolean handleVisitorsSignPlace(SuperiorPlayer superiorPlayer, Island is
for (int i = 1; i <= 3; i++)
warpLines[i] = Formatters.COLOR_FORMATTER.format(warpLines[i]);

Block oldWelcomeSignBlock = island.getVisitorsLocation() == null ? null : island.getVisitorsLocation().getBlock();
Location islandVisitorsLocation = island.getVisitorsLocation(null /* unused */);
Block oldWelcomeSignBlock = islandVisitorsLocation == null ? null : islandVisitorsLocation.getBlock();

if (oldWelcomeSignBlock != null && Materials.isSign(oldWelcomeSignBlock.getType())) {
Sign oldWelcomeSign = (Sign) oldWelcomeSignBlock.getState();
oldWelcomeSign.setLine(0, plugin.getSettings().getVisitorsSign().getInactive());
Expand Down

0 comments on commit c1882f3

Please sign in to comment.