Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/BentoBoxWorld/Warps.git
Browse files Browse the repository at this point in the history
…into develop
  • Loading branch information
tastybento committed Jul 2, 2024
2 parents 87f3ea7 + 18b63f4 commit fe83d09
Show file tree
Hide file tree
Showing 18 changed files with 445 additions and 99 deletions.
5 changes: 4 additions & 1 deletion src/main/java/world/bentobox/warps/Warp.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.bentobox.util.Util;
import world.bentobox.level.Level;
import world.bentobox.warps.commands.ToggleWarpCommand;
import world.bentobox.warps.commands.WarpCommand;
import world.bentobox.warps.commands.WarpsCommand;
import world.bentobox.warps.config.Settings;
Expand Down Expand Up @@ -100,6 +101,7 @@ public void onLoad()
// Load the master warp and warps command
new WarpCommand(this);
new WarpsCommand(this);
new ToggleWarpCommand(this);
}
}

Expand Down Expand Up @@ -140,6 +142,7 @@ public void onEnable() {

new WarpCommand(this, gameModeAddon.getPlayerCommand().get());
new WarpsCommand(this, gameModeAddon.getPlayerCommand().get());
new ToggleWarpCommand(this, gameModeAddon.getPlayerCommand().get());
this.hooked = true;
}
});
Expand Down Expand Up @@ -288,7 +291,7 @@ public Object request(String requestLabel, Map<String, Object> metaData) {
}
return switch (requestLabel) {
case "getSortedWarps" -> getWarpSignsManager().getSortedWarps(world);
case "getWarp" -> uuid == null ? null : getWarpSignsManager().getWarp(world, uuid);
case "getWarp" -> uuid == null ? null : getWarpSignsManager().getWarpLocation(world, uuid);
case "getWarpMap" -> getWarpSignsManager().getWarpMap(world);
case "hasWarp" -> uuid == null ? null : getWarpSignsManager().hasWarp(world, uuid);
case "listWarps" -> getWarpSignsManager().listWarps(world);
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/world/bentobox/warps/commands/ToggleWarpCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package world.bentobox.warps.commands;

import org.bukkit.Bukkit;
import org.bukkit.World;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.warps.Warp;
import world.bentobox.warps.event.WarpToggleEvent;
import world.bentobox.warps.objects.PlayerWarp;

import java.util.List;
import java.util.UUID;

public class ToggleWarpCommand extends CompositeCommand {

private final Warp addon;

public ToggleWarpCommand(Warp addon, CompositeCommand bsbIslandCmd) {
super(bsbIslandCmd, addon.getSettings().getToggleWarpCommand());
this.addon = addon;
}

public ToggleWarpCommand(Warp addon) {
super(addon.getSettings().getToggleWarpCommand());
this.addon = addon;
}


@Override
public void setup() {
this.setPermission(this.getParent() == null ? Warp.WELCOME_WARP_SIGNS + ".togglewarp" : "island.warp.toggle");
this.setOnlyPlayer(true);
this.setDescription("togglewarp.help.description");
}

@Override
public boolean execute(User user, String s, List<String> list) {
UUID userUUID = user.getUniqueId();
World userWorld = user.getWorld();

// Check if the user has a warp
boolean hasWarp = addon.getWarpSignsManager().hasWarp(userWorld, userUUID);

if (hasWarp) {
// If the user has a warp, toggle its visibility
PlayerWarp warp = addon.getWarpSignsManager().getPlayerWarp(userWorld, userUUID);
// Check extreme case if PlayerWarp is null
if (warp == null) {
user.sendMessage("togglewarp.error.generic");
return false;
}
warp.toggle();
Bukkit.getPluginManager().callEvent(new WarpToggleEvent(userUUID, warp));
String message = warp.isEnabled() ? "togglewarp.enabled" : "togglewarp.disabled";
user.sendMessage(message);
} else {
user.sendMessage("togglewarp.error.no-warp");
}
return false;
}
}
4 changes: 2 additions & 2 deletions src/main/java/world/bentobox/warps/commands/WarpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public boolean execute(User user, String label, List<String> args) {
user.sendMessage("warps.warpTip", "[text]", addon.getSettings().getWelcomeLine());
return false;
} else {
// Attemp to find warp with exact player's name
// Attempt to find warp with exact player's name
UUID foundWarp = warpList.stream().filter(u -> getPlayers().getName(u).equalsIgnoreCase(args.get(0))).findFirst().orElse(null);

if (foundWarp == null) {

// Atempt to find warp which starts with the given name
// Attempt to find warp which starts with the given name
UUID foundAlernativeWarp = warpList.stream().filter(u -> getPlayers().getName(u).toLowerCase().startsWith(args.get(0).toLowerCase())).findFirst().orElse(null);

if (foundAlernativeWarp == null) {
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/world/bentobox/warps/config/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public class Settings implements ConfigObject
String warpCommand = "warp";
@ConfigEntry(path = "warps-command")
String warpsCommand = "warps";
@ConfigEntry(path = "togglewarp-command")
String toggleWarpCommand = "togglewarp";

// ---------------------------------------------------------------------
// Section: Constructor
Expand Down Expand Up @@ -205,6 +207,21 @@ public void setWarpsCommand(String warpsCommand) {
this.warpsCommand = warpsCommand;
}


/**
* @return the toggleWarpCommand
*/
public String getToggleWarpCommand() {
return toggleWarpCommand;
}

/**
* @param toggleWarpCommand the toggleWarpCommand to set
*/
public void setToggleWarpCommand(String toggleWarpCommand) {
this.toggleWarpCommand = toggleWarpCommand;
}

/**
* @return the removeExistingWarpsWhenFlagChanges
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @author Poslovitch
*
*/
public class WarpCreateEvent extends Event{
public class WarpCreateEvent extends Event {
private static final HandlerList handlers = new HandlerList();

private final Location warpLoc;
Expand Down
72 changes: 72 additions & 0 deletions src/main/java/world/bentobox/warps/event/WarpToggleEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package world.bentobox.warps.event;

import org.bukkit.Location;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import world.bentobox.warps.objects.PlayerWarp;

import java.util.UUID;

/**
* This event is fired when a warp is toggled
* A Listener to this event can use it only to get information. e.g: broadcast something
*
* @since 1.16.0
* @author TreemanKing
*/
public class WarpToggleEvent extends Event {
private static final HandlerList handlers = new HandlerList();

private final UUID user;
private final PlayerWarp playerWarp;

public WarpToggleEvent(UUID user, PlayerWarp playerWarp) {
this.playerWarp = playerWarp;
this.user = user;
}

/**
* Gets the user who has toggled the warp
*
* @return the UUID of the player who toggled the warp
*/
public UUID getUser() {
return user;
}

/**
* Gets the state of the warp
*
* @return true if the warp is enabled, false otherwise
*/
public boolean isEnabled() {
return playerWarp.isEnabled();
}

/**
* Gets the PlayerWarp object
*
* @return the PlayerWarp object
*/
public PlayerWarp getPlayerWarp() {
return playerWarp;
}

/**
* Gets the location of the toggled warp
*
* @return the location of the warp
*/
public Location getLocation() {
return playerWarp.getLocation();
}

@Override
public HandlerList getHandlers() {
return handlers;
}

public static HandlerList getHandlerList() {
return handlers;
}
}
28 changes: 17 additions & 11 deletions src/main/java/world/bentobox/warps/listeners/WarpSignsListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;

Expand Down Expand Up @@ -60,12 +61,12 @@ public void onChunkLoad(ChunkLoadEvent event) {
@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())) {
Expand Down Expand Up @@ -126,16 +127,16 @@ public void onSignBreak(BlockBreakEvent e) {

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()));
}

/**
Expand All @@ -158,19 +159,24 @@ public void onSignWarpCreate(SignChangeEvent e) {
if (noPerms(user, b.getWorld(), inWorld)) {
return;
}
// TODO: These checks are useless if the sign is placed outside a BSB world.
// This will mean level and rank requirements are nil in the case of allow-in-other-worlds: true.
// I'm not sure if there is a better way around this without adding new API checking for primary
// or last island accessed with relevant permissions.
// ignored.
if (inWorld && noLevelOrIsland(user, b.getWorld())) {
e.setLine(0, ChatColor.RED + addon.getSettings().getWelcomeLine());
return;
}

if(!hasCorrectIslandRank(b, user)) {
if (inWorld && !hasCorrectIslandRank(b, user)) {
e.setLine(0, ChatColor.RED + addon.getSettings().getWelcomeLine());
user.sendMessage("warps.error.not-correct-rank");
return;
}

// Check if the player already has a sign
final Location oldSignLoc = addon.getWarpSignsManager().getWarp(b.getWorld(), user.getUniqueId());
final Location oldSignLoc = addon.getWarpSignsManager().getWarpLocation(b.getWorld(), user.getUniqueId());
if (oldSignLoc != null) {
// A sign already exists. Check if it still there and if
// so,
Expand Down Expand Up @@ -216,15 +222,15 @@ public void onFlagChange(FlagProtectionChangeEvent e) {

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.
Expand Down
Loading

0 comments on commit fe83d09

Please sign in to comment.