1
0
mirror of https://github.com/BentoBoxWorld/Warps.git synced 2024-11-24 19:46:28 +01:00

feat: changed location to playerwarp in preparation of toggle state

This commit is contained in:
TreemanK 2024-06-20 21:57:50 +10:00
parent 0a12a26b06
commit 0a343219bc
7 changed files with 79 additions and 39 deletions

View File

@ -28,6 +28,7 @@ import world.bentobox.bentobox.api.events.team.TeamLeaveEvent;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.util.Util;
import world.bentobox.warps.objects.PlayerWarp;
import world.bentobox.warps.Warp;
import world.bentobox.warps.event.WarpRemoveEvent;
@ -60,12 +61,12 @@ public class WarpSignsListener implements Listener {
@Override
public void run() {
boolean changed = false;
Iterator<Map.Entry<UUID, Location>> iterator =
Iterator<Map.Entry<UUID, PlayerWarp>> iterator =
addon.getWarpSignsManager().getWarpMap(event.getWorld()).entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<UUID, Location> entry = iterator.next();
Map.Entry<UUID, PlayerWarp> entry = iterator.next();
UUID uuid = entry.getKey();
Location location = entry.getValue();
Location location = entry.getValue().getLocation();
if (event.getChunk().getX() == location.getBlockX() >> 4
&& event.getChunk().getZ() == location.getBlockZ() >> 4
&& !Tag.SIGNS.isTagged(location.getBlock().getType())) {
@ -126,16 +127,16 @@ public class WarpSignsListener implements Listener {
private boolean isPlayersSign(Player player, Block b, boolean inWorld) {
// Welcome sign detected - check to see if it is this player's sign
Map<UUID, Location> list = addon.getWarpSignsManager().getWarpMap(b.getWorld());
Map<UUID, PlayerWarp> list = addon.getWarpSignsManager().getWarpMap(b.getWorld());
String reqPerm = inWorld ? addon.getPermPrefix(b.getWorld()) + "mod.removesign" : Warp.WELCOME_WARP_SIGNS + ".mod.removesign";
return ((list.containsKey(player.getUniqueId()) && list.get(player.getUniqueId()).equals(b.getLocation()))
return ((list.containsKey(player.getUniqueId()) && list.get(player.getUniqueId()).getLocation().equals(b.getLocation()))
|| player.isOp() || player.hasPermission(reqPerm));
}
private boolean isWarpSign(Block b) {
Sign s = (Sign) b.getState();
return s.getLine(0).equalsIgnoreCase(ChatColor.GREEN + addon.getSettings().getWelcomeLine())
&& addon.getWarpSignsManager().getWarpMap(b.getWorld()).containsValue(s.getLocation());
&& addon.getWarpSignsManager().getWarpMap(b.getWorld()).values().stream().anyMatch(playerWarp -> playerWarp.getLocation().equals(s.getLocation()));
}
/**
@ -216,15 +217,15 @@ public class WarpSignsListener implements Listener {
final Island island = e.getIsland();
final Map<UUID, Location> islandWarps = addon
final Map<UUID, PlayerWarp> islandWarps = addon
.getWarpSignsManager()
.getWarpMap(island.getWorld())
.entrySet()
.stream()
.filter(x -> island.inIslandSpace(x.getValue()))
.filter(x -> island.inIslandSpace(x.getValue().getLocation()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
for(Map.Entry<UUID, Location> entry : islandWarps.entrySet()) {
for(Map.Entry<UUID, PlayerWarp> entry : islandWarps.entrySet()) {
if(island.getRank(entry.getKey()) >= e.getSetTo()) continue;
//The user has a lower rank than the new set value.

View File

@ -37,6 +37,7 @@ import world.bentobox.bentobox.database.Database;
import world.bentobox.bentobox.database.objects.Island;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util;
import world.bentobox.warps.objects.PlayerWarp;
import world.bentobox.warps.Warp;
import world.bentobox.warps.event.WarpCreateEvent;
import world.bentobox.warps.event.WarpInitiateEvent;
@ -55,7 +56,7 @@ public class WarpSignsManager {
private static final String WARPS = "warps";
private final BentoBox plugin;
// Map of all warps stored as player, warp sign Location
private Map<World, Map<UUID, Location>> worldsWarpList;
private Map<World, Map<UUID, PlayerWarp>> worldsWarpList;
// Database handler for level data
private final Database<WarpsData> handler;
@ -68,7 +69,7 @@ public class WarpSignsManager {
* @return map of warps
*/
@NonNull
public Map<UUID, Location> getWarpMap(@Nullable World world) {
public Map<UUID, PlayerWarp> getWarpMap(@Nullable World world) {
return worldsWarpList.computeIfAbsent(Util.getWorld(world), k -> new HashMap<>());
}
@ -100,11 +101,13 @@ public class WarpSignsManager {
return false;
}
// Check for warps placed in a location where there was a warp before
if (getWarpMap(loc.getWorld()).containsValue(loc)) {
// remove the warp at this location, then place it
for (PlayerWarp playerWarp : getWarpMap(loc.getWorld()).values()) {
if (playerWarp.getLocation().equals(loc)) {
this.removeWarp(loc);
break;
}
getWarpMap(loc.getWorld()).put(playerUUID, loc);
}
getWarpMap(loc.getWorld()).put(playerUUID, new PlayerWarp(loc, true));
saveWarpList();
Bukkit.getPluginManager().callEvent(new WarpCreateEvent(addon, loc, playerUUID));
return true;
@ -120,7 +123,8 @@ public class WarpSignsManager {
*/
@Nullable
public Location getWarp(World world, UUID playerUUID) {
return getWarpMap(world).get(playerUUID);
PlayerWarp playerWarp = getWarpMap(world).get(playerUUID);
return playerWarp != null ? playerWarp.getLocation() : null;
}
/**
@ -130,7 +134,7 @@ public class WarpSignsManager {
*/
@NonNull
public String getWarpOwner(Location location) {
return getWarpMap(location.getWorld()).entrySet().stream().filter(en -> en.getValue().equals(location))
return getWarpMap(location.getWorld()).entrySet().stream().filter(en -> en.getValue().getLocation().equals(location))
.findFirst().map(en -> plugin.getPlayers().getName(en.getKey())).orElse("");
}
@ -140,7 +144,7 @@ public class WarpSignsManager {
* @return Optional UUID of warp owner or empty if there is none
*/
public Optional<UUID> getWarpOwnerUUID(Location location) {
return getWarpMap(location.getWorld()).entrySet().stream().filter(en -> en.getValue().equals(location))
return getWarpMap(location.getWorld()).entrySet().stream().filter(en -> en.getValue().getLocation().equals(location))
.findFirst().map(Map.Entry::getKey);
}
@ -188,7 +192,7 @@ public class WarpSignsManager {
public Set<UUID> listWarps(@NonNull World world) {
// Remove any null locations
getWarpMap(world).values().removeIf(Objects::isNull);
return getWarpMap(world).entrySet().stream().filter(e -> Util.sameWorld(world, Objects.requireNonNull(e.getValue().getWorld()))).map(Map.Entry::getKey).collect(Collectors.toSet());
return getWarpMap(world).entrySet().stream().filter(e -> Util.sameWorld(world, Objects.requireNonNull(e.getValue().getLocation().getWorld()))).map(Map.Entry::getKey).collect(Collectors.toSet());
}
/**
@ -201,7 +205,8 @@ public class WarpSignsManager {
warpsData = handler.loadObject(WARPS);
// Load into map
if (warpsData != null) {
warpsData.getWarpSigns().forEach((location,uuid) -> {
warpsData.getWarpSigns().forEach((pw, uuid) -> {
Location location = pw.getLocation();
if (location != null && location.getWorld() != null) {
if (location.getWorld().isChunkLoaded(location.getBlockX() >> 4, location.getBlockZ() >> 4)
&& !location.getBlock().getType().name().contains("SIGN")) {
@ -209,7 +214,7 @@ public class WarpSignsManager {
}
// Add to map
getWarpMap(location.getWorld()).put(uuid, location);
getWarpMap(location.getWorld()).put(uuid, new PlayerWarp(location, true));
}
});
} else {
@ -240,10 +245,10 @@ public class WarpSignsManager {
*/
public void removeWarp(Location loc) {
popSign(loc);
Iterator<Entry<UUID, Location>> it = getWarpMap(loc.getWorld()).entrySet().iterator();
Iterator<Entry<UUID, PlayerWarp>> it = getWarpMap(loc.getWorld()).entrySet().iterator();
while (it.hasNext()) {
Entry<UUID, Location> en = it.next();
if (en.getValue().equals(loc)) {
Entry<UUID, PlayerWarp> en = it.next();
if (en.getValue().getLocation().equals(loc)) {
// Inform player
Optional.ofNullable(addon.getServer().getPlayer(en.getKey()))
.map(User::getInstance)
@ -263,7 +268,7 @@ public class WarpSignsManager {
*/
public void removeWarp(World world, UUID uuid) {
if (getWarpMap(world).containsKey(uuid)) {
popSign(getWarpMap(world).get(uuid));
popSign(getWarpMap(world).get(uuid).getLocation());
getWarpMap(world).remove(uuid);
}

View File

@ -0,0 +1,32 @@
package world.bentobox.warps.objects;
import com.google.gson.annotations.Expose;
import org.bukkit.Location;
import java.io.Serializable;
public class PlayerWarp implements Serializable {
@Expose
private final Location location;
@Expose
private boolean isEnabled;
public PlayerWarp(Location location, boolean isEnabled) {
this.location = location;
this.isEnabled = isEnabled;
}
public Location getLocation() {
return location;
}
public boolean isEnabled() {
return isEnabled;
}
public void toggle() {
isEnabled = !isEnabled;
}
}

View File

@ -4,7 +4,6 @@ import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Location;
import org.bukkit.World;
import com.google.gson.annotations.Expose;
@ -18,7 +17,7 @@ public class WarpsData implements DataObject {
@Expose
private String uniqueId = "warps";
@Expose
private Map<Location, UUID> warpSigns = new HashMap<>();
private Map<PlayerWarp, UUID> warpSigns = new HashMap<>();
public WarpsData() {
// Required by YAML database
@ -34,24 +33,24 @@ public class WarpsData implements DataObject {
this.uniqueId = uniqueId;
}
public Map<Location, UUID> getWarpSigns() {
public Map<PlayerWarp, UUID> getWarpSigns() {
if (warpSigns == null)
return new HashMap<>();
return warpSigns;
}
public void setWarpSigns(Map<Location, UUID> warpSigns) {
public void setWarpSigns(Map<PlayerWarp, UUID> warpSigns) {
this.warpSigns = warpSigns;
}
/**
* Puts all the data from the map into this objects ready for saving
* Puts all the data from the map into these objects ready for saving
* @param worldsWarpList 2D map of warp locations by world vs UUID
* @return this class filled with data
*/
public WarpsData save(Map<World, Map<UUID, Location>> worldsWarpList) {
public WarpsData save(Map<World, Map<UUID, PlayerWarp>> worldsWarpList) {
getWarpSigns().clear();
worldsWarpList.values().forEach(world -> world.forEach((uuid,location) -> warpSigns.put(location, uuid)));
worldsWarpList.values().forEach(world -> world.forEach((uuid,playerWarp) -> warpSigns.put(playerWarp, uuid)));
return this;
}

View File

@ -52,7 +52,7 @@ public class Utils
List<String> permissions = user.getEffectivePermissions().stream().
map(PermissionAttachmentInfo::getPermission).
filter(permission -> permission.startsWith(permPrefix)).
collect(Collectors.toList());
toList();
for (String permission : permissions)
{

View File

@ -67,6 +67,7 @@ import world.bentobox.warps.event.WarpCreateEvent;
import world.bentobox.warps.event.WarpInitiateEvent;
import world.bentobox.warps.managers.SignCacheManager;
import world.bentobox.warps.managers.WarpSignsManager;
import world.bentobox.warps.objects.PlayerWarp;
import world.bentobox.warps.objects.WarpsData;
@ -195,7 +196,7 @@ public class WarpSignsManagerTest {
// Handler
when(handler.objectExists("warps")).thenReturn(true);
Map<Location, UUID> warpMap = Collections.singletonMap(location, uuid);
Map<PlayerWarp, UUID> warpMap = Collections.singletonMap(new PlayerWarp(location, true), uuid);
when(load.getWarpSigns()).thenReturn(warpMap);
when(handler.loadObject(anyString())).thenReturn(load);
@ -275,7 +276,8 @@ public class WarpSignsManagerTest {
*/
@Test
public void testGetWarpMapNullLocation() {
Map<Location, UUID> warpMap = Collections.singletonMap(null, uuid);
PlayerWarp playerWarp = new PlayerWarp(null, true);
Map<PlayerWarp, UUID> warpMap = Collections.singletonMap(playerWarp, uuid);
when(load.getWarpSigns()).thenReturn(warpMap);
wsm = new WarpSignsManager(addon, plugin);
assertTrue("Map is not empty", wsm.getWarpMap(world).isEmpty());

View File

@ -50,6 +50,7 @@ import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.PlaceholdersManager;
import world.bentobox.bentobox.util.Util;
import world.bentobox.warps.objects.PlayerWarp;
import world.bentobox.warps.Warp;
import world.bentobox.warps.managers.WarpSignsManager;
import world.bentobox.warps.config.Settings;
@ -123,12 +124,12 @@ public class WarpSignsListenerTest {
when(block.getState()).thenReturn(s);
// warp signs manager
when(addon.getWarpSignsManager()).thenReturn(wsm);
Map<UUID, Location> list = new HashMap<>();
Map<UUID, PlayerWarp> list = new HashMap<>();
Location location = mock(Location.class);
when(location.getBlock()).thenReturn(block);
when(s.getLocation()).thenReturn(location);
when(block.getLocation()).thenReturn(location);
list.put(uuid, location);
list.put(uuid, new PlayerWarp(location, true));
// Player is in world
when(wsm.getWarpMap(world)).thenReturn(list);
//Player has a warp sign already here
@ -339,8 +340,8 @@ public class WarpSignsListenerTest {
when(settings.getRemoveExistingWarpsWhenFlagChanges()).thenReturn(true);
WarpSignsListener wsl = new WarpSignsListener(addon);
Map<UUID, Location> warps = Map.of(
player.getUniqueId(), block.getLocation()
Map<UUID, PlayerWarp> warps = Map.of(
player.getUniqueId(), new PlayerWarp(block.getLocation(), true)
);
when(wsm.getWarpMap(any())).thenReturn(warps);