From dec22b9960167b648a11424d6839483d0b730825 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Sat, 16 May 2020 17:51:42 +0200 Subject: [PATCH] Added WaypointCreateEvent --- CHANGELOG.md | 1 + .../api/events/WaypointCreateEvent.java | 113 ++++++++++++++++++ .../slimefun4/api/gps/GPSNetwork.java | 22 ++-- .../items/gps/GPSMarkerTool.java | 2 +- 4 files changed, 129 insertions(+), 9 deletions(-) create mode 100644 src/main/java/io/github/thebusybiscuit/slimefun4/api/events/WaypointCreateEvent.java diff --git a/CHANGELOG.md b/CHANGELOG.md index b633b8097..fea9151a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ * Added Ukrainian translations * Added /sf backpack to restore lost backpacks * Added automated Unit Tests +* Added WaypointCreateEvent #### Changes * Little performance improvements diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/WaypointCreateEvent.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/WaypointCreateEvent.java new file mode 100644 index 000000000..a5a36060f --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/events/WaypointCreateEvent.java @@ -0,0 +1,113 @@ +package io.github.thebusybiscuit.slimefun4.api.events; + +import org.apache.commons.lang.Validate; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.HandlerList; +import org.bukkit.event.player.PlayerEvent; + +import io.github.thebusybiscuit.slimefun4.api.gps.GPSNetwork; +import io.github.thebusybiscuit.slimefun4.api.gps.TeleportationManager; + +/** + * A {@link WaypointCreateEvent} is called when a {@link Player} creates a new waypoint. + * Either manually or through dying with an emergency transmitter. + * + * @author TheBusyBiscuit + * + * @see GPSNetwork + * @see TeleportationManager + * + */ +public class WaypointCreateEvent extends PlayerEvent implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + + private Location location; + private String name; + + private final boolean deathpoint; + private boolean cancelled; + + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + public WaypointCreateEvent(Player player, String name, Location location, boolean deathpoint) { + super(player); + + Validate.notNull(location, "Location must not be null!"); + Validate.notNull(name, "Name must not be null!"); + + this.location = location; + this.name = name; + this.deathpoint = deathpoint; + } + + /** + * This returns the {@link Location} of the waypoint that should be created. + * + * @return The {@link Location} of this waypoint + */ + public Location getLocation() { + return location; + } + + /** + * This sets the {@link Location} of the waypoint. + * The {@link Location} may never be null! + * + * @param loc + * The {@link Location} to set + */ + public void setLocation(Location loc) { + Validate.notNull(loc, "Cannot set the Location to null!"); + this.location = loc; + } + + /** + * This returns the name of the waypoint. + * + * @return The name of this waypoint + */ + public String getName() { + return name; + } + + /** + * This sets the name of the waypoint to the given argument. + * + * @param name + * The name for this waypoint + */ + public void setName(String name) { + Validate.notEmpty(name, "The name of a waypoint must not be empty!"); + this.name = name; + } + + /** + * This method returns whether this waypoint was created by an Emergency Transmitter. + * This should mean that our {@link Player} has died. + * + * @return Whether this is a deathpoint + */ + public boolean isDeathpoint() { + return deathpoint; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + +} diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/GPSNetwork.java b/src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/GPSNetwork.java index cdf059d92..c29e599ce 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/GPSNetwork.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/api/gps/GPSNetwork.java @@ -17,11 +17,13 @@ import org.bukkit.World.Environment; import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; +import io.github.thebusybiscuit.cscorelib2.chat.ChatColors; import io.github.thebusybiscuit.cscorelib2.chat.ChatInput; import io.github.thebusybiscuit.cscorelib2.config.Config; import io.github.thebusybiscuit.cscorelib2.item.CustomItem; import io.github.thebusybiscuit.cscorelib2.math.DoubleHandler; import io.github.thebusybiscuit.cscorelib2.skull.SkullItem; +import io.github.thebusybiscuit.slimefun4.api.events.WaypointCreateEvent; import io.github.thebusybiscuit.slimefun4.api.geo.ResourceManager; import io.github.thebusybiscuit.slimefun4.implementation.items.gps.GPSTransmitter; import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; @@ -273,7 +275,7 @@ public class GPSNetwork { * @param l * The {@link Location} of the new waypoint */ - public void addWaypoint(Player p, Location l) { + public void createWaypoint(Player p, Location l) { if ((getWaypoints(p.getUniqueId()).size() + 2) > inventory.length) { SlimefunPlugin.getLocal().sendMessage(p, "gps.waypoint.max", true); return; @@ -301,15 +303,19 @@ public class GPSNetwork { return; } - Config cfg = new Config(WAYPOINTS_DIRECTORY + p.getUniqueId().toString() + ".yml"); - String id = ChatColor.stripColor(ChatColor.translateAlternateColorCodes('&', name)).toUpperCase(Locale.ROOT).replace(' ', '_'); + WaypointCreateEvent event = new WaypointCreateEvent(p, name, l, name.startsWith("player:death ")); + Bukkit.getPluginManager().callEvent(event); + if (!event.isCancelled()) { + Config cfg = new Config(WAYPOINTS_DIRECTORY + p.getUniqueId().toString() + ".yml"); + String id = ChatColor.stripColor(ChatColors.color(name)).toUpperCase(Locale.ROOT).replace(' ', '_'); - cfg.setValue(id, l); - cfg.setValue(id + ".name", name); - cfg.save(); + cfg.setValue(id, l); + cfg.setValue(id + ".name", name); + cfg.save(); - p.playSound(p.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 1F, 1F); - SlimefunPlugin.getLocal().sendMessage(p, "gps.waypoint.added", true); + p.playSound(p.getLocation(), Sound.BLOCK_NOTE_BLOCK_PLING, 1F, 1F); + SlimefunPlugin.getLocal().sendMessage(p, "gps.waypoint.added", true); + } } /** diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/gps/GPSMarkerTool.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/gps/GPSMarkerTool.java index 863217d23..8ccef0015 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/gps/GPSMarkerTool.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/gps/GPSMarkerTool.java @@ -24,7 +24,7 @@ public class GPSMarkerTool extends SimpleSlimefunItem implements if (e.getClickedBlock().isPresent()) { Block b = e.getClickedBlock().get().getRelative(e.getClickedFace()); - SlimefunPlugin.getGPSNetwork().addWaypoint(e.getPlayer(), b.getLocation()); + SlimefunPlugin.getGPSNetwork().createWaypoint(e.getPlayer(), b.getLocation()); } }; }