diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java index dd312b5..76bd7e6 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java @@ -4,10 +4,7 @@ import club.minnced.discord.webhook.WebhookClient; import club.minnced.discord.webhook.WebhookClientBuilder; import net.milkbowl.vault.economy.Economy; import nl.sbdeveloper.themeparkplus.commands.TPPCMD; -import nl.sbdeveloper.themeparkplus.listeners.AntiFreerunListener; -import nl.sbdeveloper.themeparkplus.listeners.DirectionalGateListener; -import nl.sbdeveloper.themeparkplus.listeners.FastpassListeners; -import nl.sbdeveloper.themeparkplus.listeners.StatusChangeListener; +import nl.sbdeveloper.themeparkplus.listeners.*; import nl.sbdeveloper.themeparkplus.managers.DBManager; import nl.sbdeveloper.themeparkplus.sbutils.UpdateManager; import nl.sbdeveloper.themeparkplus.util.LGUtil; @@ -140,6 +137,7 @@ public final class ThemeParkPlus extends JavaPlugin { Bukkit.getLogger().info("[ThemeParkPlus] Loading listeners..."); Bukkit.getPluginManager().registerEvents(new DirectionalGateListener(), this); Bukkit.getPluginManager().registerEvents(new FastpassListeners(), this); + Bukkit.getPluginManager().registerEvents(new WaitingTimeListener(), this); if (getSConfig().getFile().getBoolean("AntiFreerun.Enabled")) { Bukkit.getPluginManager().registerEvents(new AntiFreerunListener(), this); } diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/api/PlusAPI.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/api/PlusAPI.java index b791758..ea152ae 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/api/PlusAPI.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/api/PlusAPI.java @@ -1,5 +1,6 @@ package nl.sbdeveloper.themeparkplus.api; +import com.sk89q.worldedit.bukkit.BukkitAdapter; import de.tr7zw.changeme.nbtapi.NBTItem; import me.paradoxpixel.themepark.api.attraction.Attraction; import nl.sbdeveloper.themeparkplus.ThemeParkPlus; @@ -16,6 +17,7 @@ import org.jetbrains.annotations.Nullable; import java.util.Collections; import java.util.HashMap; import java.util.List; +import java.util.Optional; public class PlusAPI { private static HashMap gates = new HashMap<>(); @@ -97,7 +99,7 @@ public class PlusAPI { } /** - * Get a row by the location + * Get a row by the rideid * * @param rideID The rideID * @return The row @@ -106,6 +108,28 @@ public class PlusAPI { return rows.get(rideID); } + /** + * Get a row by the location + * + * @param loc The location + * @return The row + */ + @NotNull + public static Optional getRow(Location loc) { + return rows.values().stream().filter(row -> row.getRegion().contains(BukkitAdapter.asBlockVector(loc))).findFirst(); + } + + /** + * Get a row by the sign location + * + * @param signLoc The sign location + * @return The row + */ + @NotNull + public static Optional getRowBySign(Location signLoc) { + return rows.values().stream().filter(row -> row.getSignLocations().contains(signLoc)).findFirst(); + } + /** * Get all the rows * diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/api/objects/WaitingRow.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/api/objects/WaitingRow.java index 13105a4..3507232 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/api/objects/WaitingRow.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/api/objects/WaitingRow.java @@ -14,7 +14,7 @@ public class WaitingRow { @Getter @Setter private String rideID; @Getter @Setter private Region region; @Getter @Setter private int waitingPlayers = 0; - @Getter @Setter private int waitingTimeMinutes = 0; + @Getter @Setter private double waitingTimeMinutes = 0; private ArrayList signLocations = new ArrayList<>(); public WaitingRow(String rideID, Region region) { diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/SignListeners.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/SignListeners.java index 008b9b1..330c0a5 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/SignListeners.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/SignListeners.java @@ -6,13 +6,17 @@ import nl.sbdeveloper.themeparkplus.ThemeParkPlus; import nl.sbdeveloper.themeparkplus.api.PlusAPI; import nl.sbdeveloper.themeparkplus.api.objects.WaitingRow; import nl.sbdeveloper.themeparkplus.util.ConfigUtil; +import org.bukkit.block.BlockState; +import org.bukkit.block.Sign; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.SignChangeEvent; import org.jetbrains.annotations.NotNull; import java.util.Collections; +import java.util.Optional; /** * Sign setup listener @@ -72,4 +76,21 @@ public class SignListeners implements Listener { p.sendMessage(ConfigUtil.getMessage("General.IncorrectSign")); } } + + @EventHandler + public void onDestroySign(BlockBreakEvent e) { + BlockState signblocks = e.getBlock().getState(); + if ((signblocks instanceof Sign)) { + Optional rowOptional = PlusAPI.getRowBySign(e.getBlock().getLocation()); + if (!rowOptional.isPresent()) return; + + WaitingRow row = rowOptional.get(); + if (row.getSignLocations().size() == 1) { + PlusAPI.removeRow(row); + } else { + row.getSignLocations().remove(e.getBlock().getLocation()); + } + ThemeParkPlus.getData().save(); + } + } } diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/WaitingTimeListener.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/WaitingTimeListener.java new file mode 100644 index 0000000..6e3b51d --- /dev/null +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/WaitingTimeListener.java @@ -0,0 +1,68 @@ +package nl.sbdeveloper.themeparkplus.listeners; + +import nl.sbdeveloper.themeparkplus.ThemeParkPlus; +import nl.sbdeveloper.themeparkplus.api.PlusAPI; +import nl.sbdeveloper.themeparkplus.api.objects.WaitingRow; +import nl.sbdeveloper.themeparkplus.util.ConfigUtil; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.block.Block; +import org.bukkit.block.BlockState; +import org.bukkit.block.Sign; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerMoveEvent; +import org.jetbrains.annotations.NotNull; + +import java.util.Collections; +import java.util.Optional; + +public class WaitingTimeListener implements Listener { + + private static final double PER_PLAYER = ThemeParkPlus.getSConfig().getFile().getDouble("WaitingRow.MinutesPerPlayer"); + + @EventHandler + public void onPlayerMove(@NotNull PlayerMoveEvent e) { + if (e.getTo() == null) return; + if ((e.getFrom().getBlockX() != e.getTo().getBlockX()) + || (e.getFrom().getBlockY() != e.getTo().getBlockY()) + || (e.getFrom().getBlockZ() != e.getTo().getBlockZ())) { + Optional rowOptional = PlusAPI.getRow(e.getFrom()); + Optional rowOptional2 = PlusAPI.getRow(e.getTo()); + + WaitingRow row = null; + int waitingNew = 0; + double waitingTimeNew = 0; + if (!rowOptional.isPresent() && rowOptional2.isPresent()) { + //ENTER + row = rowOptional2.get(); + waitingNew = row.getWaitingPlayers() + 1; + waitingTimeNew = row.getWaitingTimeMinutes() + PER_PLAYER; + } else if (rowOptional.isPresent() && !rowOptional2.isPresent()) { + //LEAVE + row = rowOptional.get(); + waitingNew = row.getWaitingPlayers() - 1; + waitingTimeNew = row.getWaitingTimeMinutes() - PER_PLAYER; + } + + if (row == null) return; + + row.setWaitingPlayers(waitingNew); + row.setWaitingTimeMinutes(waitingTimeNew); + + for (Location location : row.getSignLocations()) { + Block signblock = location.getBlock(); + BlockState signblocks = signblock.getState(); + if ((signblocks instanceof Sign)) { + Sign signs = (Sign) signblocks; + signs.setLine(3, waitingNew + " min."); + signs.update(); + } else { + Bukkit.getLogger().warning(ConfigUtil.getMessage("WaitingRow.WrongLocation", Collections.singletonMap("%ridename%", row.getRideID()))); + PlusAPI.removeRow(row); + ThemeParkPlus.getData().save(); + } + } + } + } +} diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml index b7d3c7f..2fc85bc 100644 --- a/src/main/resources/messages.yml +++ b/src/main/resources/messages.yml @@ -34,4 +34,5 @@ Lamps: TurnedOnSec: '&aLamps in region turned on for &f%sec% &asecond(s)!' TurnedOff: '&aLamps in region turned off!' WaitingRow: - SignCreated: '&aYou''ve successfully created a waitingrow sign!' \ No newline at end of file + SignCreated: '&aYou''ve successfully created a waitingrow sign!' + WrongLocation: '&cA waitingrow sign (from the attraction %ridename%) couldn''t be found! It will be deleted.' \ No newline at end of file