From ad9c5d413986d2ceabffda0603f53be9c74b8e61 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Tue, 16 Jun 2020 18:30:29 +0200 Subject: [PATCH] ThemePark 2.0 --- .idea/ThemePark.iml | 31 ++ .idea/modules.xml | 8 + dependency-reduced-pom.xml | 95 +++++ pom.xml | 45 +- .../themepark/ThemeParkPlugin.java | 118 ----- .../themepark/action/PageAction.java | 20 - .../themepark/action/TPUtils.java | 49 --- .../me/paradoxpixel/themepark/api/API.java | 160 ------- .../themepark/api/attraction/Attraction.java | 118 ----- .../themepark/api/attraction/Region.java | 96 ----- .../event/attraction/AddAttractionEvent.java | 29 -- .../attraction/ChangeAttractionEvent.java | 79 ---- .../attraction/PreStatusChangeEvent.java | 61 --- .../attraction/RemoveAttractionEvent.java | 29 -- .../event/attraction/StatusChangeEvent.java | 48 --- .../api/event/region/AddRegionEvent.java | 30 -- .../api/event/region/ChangeRegionEvent.java | 53 --- .../api/event/region/RemoveRegionEvent.java | 30 -- .../themepark/attraction/AttractionMenu.java | 359 ---------------- .../attraction/sign/SignManager.java | 152 ------- .../themepark/attraction/sign/StatusSign.java | 43 -- .../attraction/status/StatusManager.java | 76 ---- .../themepark/command/ThemeParkCommand.java | 352 --------------- .../themepark/database/Database.java | 132 ------ .../themepark/database/DatabaseHandler.java | 101 ----- .../me/paradoxpixel/themepark/gui/GUI.java | 120 ------ .../paradoxpixel/themepark/gui/GUIAction.java | 9 - .../paradoxpixel/themepark/gui/GUIHolder.java | 11 - .../paradoxpixel/themepark/gui/GUIItem.java | 45 -- .../themepark/gui/GUIListener.java | 41 -- .../themepark/listener/ChangeListener.java | 101 ----- .../themepark/listener/ClickListener.java | 45 -- .../themepark/listener/PlayerListener.java | 90 ---- .../themepark/listener/SignListener.java | 64 --- .../paradoxpixel/themepark/utils/Message.java | 25 -- .../paradoxpixel/themepark/utils/Utils.java | 15 - .../nl/iobyte/themepark/ThemeParkPlugin.java | 139 ++++++ .../java/nl/iobyte/themepark/api/API.java | 110 +++++ .../themepark/api/attraction/Attraction.java | 119 ++++++ .../themepark/api/attraction/Region.java | 152 +++++++ .../api/attraction/component/Status.java | 2 +- .../api/attraction/component/Type.java | 2 +- .../api/attraction/manager/StatusManager.java | 43 ++ .../themepark/api/events/ChangeEvent.java | 33 ++ .../attraction/ChangeLocationEvent.java | 20 + .../events/attraction/ChangeNameEvent.java | 19 + .../events/attraction/ChangeStatusEvent.java | 27 ++ .../events/attraction/ChangeTypeEvent.java | 20 + .../attraction/PreChangeStatusEvent.java | 37 ++ .../api/events/region/ChangeDataEvent.java | 19 + .../api/events/region/ChangeLoreEvent.java | 20 + .../events/region/ChangeMaterialEvent.java | 20 + .../api/events/region/ChangeNameEvent.java | 19 + .../events/ridecount/ChangeCountEvent.java | 34 ++ .../api/events/ridecount/SetCountEvent.java | 33 ++ .../api/ridecount/AttractionCount.java | 92 ++++ .../themepark/api/ridecount/CountManager.java | 73 ++++ .../themepark/api/ridecount/RideCount.java | 38 ++ .../themepark/api}/utils/ItemBuilder.java | 21 +- .../api/utils/LocationSerializer.java} | 16 +- .../iobyte/themepark/command/MainCommand.java | 403 ++++++++++++++++++ .../themepark/command/MenuCommand.java | 16 +- .../themepark/command/RidecountCommand.java | 107 +++++ .../iobyte}/themepark/config/YamlConfig.java | 2 +- .../java/nl/iobyte/themepark/database/DB.java | 77 ++++ .../iobyte/themepark/database/DBManager.java | 90 ++++ .../nl/iobyte/themepark/database/MySQL.java | 66 +++ .../nl/iobyte/themepark/database/SQLite.java | 84 ++++ .../listener/AttractionListener.java | 118 +++++ .../themepark/listener/PlayerListener.java | 190 +++++++++ .../themepark/listener/RegionListener.java | 68 +++ .../themepark/menu/AttractionLoader.java | 240 +++++++++++ .../iobyte/themepark/menu/AttractionMenu.java | 173 ++++++++ .../themepark/menu/actions/PageAction.java | 20 + .../themepark/menu/actions/TPAction.java | 39 ++ .../themepark/ridecount/RideCountAPI.java | 326 ++++++++++++++ .../java/nl/iobyte/themepark/util/Color.java | 11 + .../nl/iobyte/themepark/util/MessageUtil.java | 82 ++++ src/main/resources/attraction.yml | 40 -- src/main/resources/message.yml | 42 -- src/main/resources/messages.yml | 47 ++ src/main/resources/plugin.yml | 12 +- src/main/resources/regions.yml | 12 + src/main/resources/settings.yml | 32 +- 84 files changed, 3446 insertions(+), 2839 deletions(-) create mode 100644 .idea/ThemePark.iml create mode 100644 .idea/modules.xml create mode 100644 dependency-reduced-pom.xml delete mode 100644 src/main/java/me/paradoxpixel/themepark/ThemeParkPlugin.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/action/PageAction.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/action/TPUtils.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/api/API.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/api/attraction/Attraction.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/api/attraction/Region.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/api/event/attraction/AddAttractionEvent.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/api/event/attraction/ChangeAttractionEvent.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/api/event/attraction/PreStatusChangeEvent.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/api/event/attraction/RemoveAttractionEvent.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/api/event/attraction/StatusChangeEvent.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/api/event/region/AddRegionEvent.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/api/event/region/ChangeRegionEvent.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/api/event/region/RemoveRegionEvent.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/attraction/AttractionMenu.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/attraction/sign/SignManager.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/attraction/sign/StatusSign.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/attraction/status/StatusManager.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/command/ThemeParkCommand.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/database/Database.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/database/DatabaseHandler.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/gui/GUI.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/gui/GUIAction.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/gui/GUIHolder.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/gui/GUIItem.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/gui/GUIListener.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/listener/ChangeListener.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/listener/ClickListener.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/listener/PlayerListener.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/listener/SignListener.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/utils/Message.java delete mode 100644 src/main/java/me/paradoxpixel/themepark/utils/Utils.java create mode 100644 src/main/java/nl/iobyte/themepark/ThemeParkPlugin.java create mode 100644 src/main/java/nl/iobyte/themepark/api/API.java create mode 100644 src/main/java/nl/iobyte/themepark/api/attraction/Attraction.java create mode 100644 src/main/java/nl/iobyte/themepark/api/attraction/Region.java rename src/main/java/{me/paradoxpixel => nl/iobyte}/themepark/api/attraction/component/Status.java (93%) rename src/main/java/{me/paradoxpixel => nl/iobyte}/themepark/api/attraction/component/Type.java (95%) create mode 100644 src/main/java/nl/iobyte/themepark/api/attraction/manager/StatusManager.java create mode 100644 src/main/java/nl/iobyte/themepark/api/events/ChangeEvent.java create mode 100644 src/main/java/nl/iobyte/themepark/api/events/attraction/ChangeLocationEvent.java create mode 100644 src/main/java/nl/iobyte/themepark/api/events/attraction/ChangeNameEvent.java create mode 100644 src/main/java/nl/iobyte/themepark/api/events/attraction/ChangeStatusEvent.java create mode 100644 src/main/java/nl/iobyte/themepark/api/events/attraction/ChangeTypeEvent.java create mode 100644 src/main/java/nl/iobyte/themepark/api/events/attraction/PreChangeStatusEvent.java create mode 100644 src/main/java/nl/iobyte/themepark/api/events/region/ChangeDataEvent.java create mode 100644 src/main/java/nl/iobyte/themepark/api/events/region/ChangeLoreEvent.java create mode 100644 src/main/java/nl/iobyte/themepark/api/events/region/ChangeMaterialEvent.java create mode 100644 src/main/java/nl/iobyte/themepark/api/events/region/ChangeNameEvent.java create mode 100644 src/main/java/nl/iobyte/themepark/api/events/ridecount/ChangeCountEvent.java create mode 100644 src/main/java/nl/iobyte/themepark/api/events/ridecount/SetCountEvent.java create mode 100644 src/main/java/nl/iobyte/themepark/api/ridecount/AttractionCount.java create mode 100644 src/main/java/nl/iobyte/themepark/api/ridecount/CountManager.java create mode 100644 src/main/java/nl/iobyte/themepark/api/ridecount/RideCount.java rename src/main/java/{me/paradoxpixel/themepark => nl/iobyte/themepark/api}/utils/ItemBuilder.java (90%) rename src/main/java/{me/paradoxpixel/themepark/api/LocationUtils.java => nl/iobyte/themepark/api/utils/LocationSerializer.java} (75%) create mode 100644 src/main/java/nl/iobyte/themepark/command/MainCommand.java rename src/main/java/{me/paradoxpixel => nl/iobyte}/themepark/command/MenuCommand.java (58%) create mode 100644 src/main/java/nl/iobyte/themepark/command/RidecountCommand.java rename src/main/java/{me/paradoxpixel => nl/iobyte}/themepark/config/YamlConfig.java (98%) create mode 100644 src/main/java/nl/iobyte/themepark/database/DB.java create mode 100644 src/main/java/nl/iobyte/themepark/database/DBManager.java create mode 100644 src/main/java/nl/iobyte/themepark/database/MySQL.java create mode 100644 src/main/java/nl/iobyte/themepark/database/SQLite.java create mode 100644 src/main/java/nl/iobyte/themepark/listener/AttractionListener.java create mode 100644 src/main/java/nl/iobyte/themepark/listener/PlayerListener.java create mode 100644 src/main/java/nl/iobyte/themepark/listener/RegionListener.java create mode 100644 src/main/java/nl/iobyte/themepark/menu/AttractionLoader.java create mode 100644 src/main/java/nl/iobyte/themepark/menu/AttractionMenu.java create mode 100644 src/main/java/nl/iobyte/themepark/menu/actions/PageAction.java create mode 100644 src/main/java/nl/iobyte/themepark/menu/actions/TPAction.java create mode 100644 src/main/java/nl/iobyte/themepark/ridecount/RideCountAPI.java create mode 100644 src/main/java/nl/iobyte/themepark/util/Color.java create mode 100644 src/main/java/nl/iobyte/themepark/util/MessageUtil.java delete mode 100644 src/main/resources/attraction.yml delete mode 100644 src/main/resources/message.yml create mode 100644 src/main/resources/messages.yml create mode 100644 src/main/resources/regions.yml diff --git a/.idea/ThemePark.iml b/.idea/ThemePark.iml new file mode 100644 index 0000000..f63550d --- /dev/null +++ b/.idea/ThemePark.iml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..141afb8 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/dependency-reduced-pom.xml b/dependency-reduced-pom.xml new file mode 100644 index 0000000..ad97fa7 --- /dev/null +++ b/dependency-reduced-pom.xml @@ -0,0 +1,95 @@ + + + 4.0.0 + nl.iobyte + themepark + 2.0 + + + + maven-shade-plugin + 3.2.0 + + + package + + shade + + + + + + + de.tr7zw.changeme.nbtapi + de.tr7zw.nbtapi + + + + + + + + + placeholderapi + http://repo.extendedclip.com/content/repositories/placeholderapi/ + + + dynmap + http://repo.mikeprimm.com/ + + + + + org.spigotmc + spigot-api + 1.8.8-R0.1-SNAPSHOT + provided + + + commons-lang + commons-lang + + + json-simple + com.googlecode.json-simple + + + guava + com.google.guava + + + gson + com.google.code.gson + + + ebean + org.avaje + + + snakeyaml + org.yaml + + + bungeecord-chat + net.md-5 + + + + + org.dynmap + dynmap-api + 2.0 + provided + + + bukkit + org.bukkit + + + + + + 1.8 + 1.8 + + diff --git a/pom.xml b/pom.xml index 161a8d6..5174913 100644 --- a/pom.xml +++ b/pom.xml @@ -9,9 +9,9 @@ 1.8 - me.paradoxpixel + nl.iobyte themepark - 1.4.1 + 2.0 @@ -37,5 +37,46 @@ 2.0 provided + + nl.iobyte + menuapi + 1.0 + + + com.zaxxer + HikariCP + 3.4.3 + + + nl.iobyte + workchain + 1.0 + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.0 + + + package + + shade + + + + + + + de.tr7zw.changeme.nbtapi + de.tr7zw.nbtapi + + + + + + \ No newline at end of file diff --git a/src/main/java/me/paradoxpixel/themepark/ThemeParkPlugin.java b/src/main/java/me/paradoxpixel/themepark/ThemeParkPlugin.java deleted file mode 100644 index 142b5d0..0000000 --- a/src/main/java/me/paradoxpixel/themepark/ThemeParkPlugin.java +++ /dev/null @@ -1,118 +0,0 @@ -package me.paradoxpixel.themepark; - -import me.paradoxpixel.themepark.attraction.AttractionMenu; -import me.paradoxpixel.themepark.attraction.sign.SignManager; -import me.paradoxpixel.themepark.attraction.status.StatusManager; -import me.paradoxpixel.themepark.command.MenuCommand; -import me.paradoxpixel.themepark.command.ThemeParkCommand; -import me.paradoxpixel.themepark.config.YamlConfig; -import me.paradoxpixel.themepark.database.DatabaseHandler; -import me.paradoxpixel.themepark.gui.GUIListener; -import me.paradoxpixel.themepark.listener.ClickListener; -import me.paradoxpixel.themepark.listener.ChangeListener; -import me.paradoxpixel.themepark.listener.PlayerListener; -import me.paradoxpixel.themepark.listener.SignListener; -import org.bukkit.Bukkit; -import org.bukkit.command.CommandMap; -import org.bukkit.plugin.PluginManager; -import org.bukkit.plugin.java.JavaPlugin; -import java.lang.reflect.Field; - -public class ThemeParkPlugin extends JavaPlugin { - - private static ThemeParkPlugin instance; - private YamlConfig settings, attraction, message, signs, data; - private DatabaseHandler database; - - @Override - public void onEnable() { - instance = this; - loadConfig(); - loadListener(); - loadCommand(); - loadData(); - loadDatabase(); - } - - private void loadConfig() { - settings = new YamlConfig("settings", this); - attraction = new YamlConfig("attraction", this); - message = new YamlConfig("message", this); - signs = new YamlConfig("signs", this); - data = new YamlConfig("data", this); - } - - private void loadData() { - StatusManager.load(); - AttractionMenu.load(); - } - - private void loadDatabase() { - database = new DatabaseHandler(settings); - } - - private void loadCommand() { - getCommand("status").setExecutor(new MenuCommand()); - - String command = settings.getConfig().getString("command"); - if(command == null || command.isEmpty()) - return; - - try { - final Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap"); - - bukkitCommandMap.setAccessible(true); - CommandMap commandMap = (CommandMap) bukkitCommandMap.get(Bukkit.getServer()); - - commandMap.register(command, new ThemeParkCommand(command)); - } catch(ReflectiveOperationException e) { - e.printStackTrace(); - } - } - - private void loadListener() { - PluginManager pm = Bukkit.getPluginManager(); - pm.registerEvents(new GUIListener(), this); - pm.registerEvents(new ClickListener(), this); - pm.registerEvents(new ChangeListener(), this); - pm.registerEvents(new SignListener(), this); - pm.registerEvents(new PlayerListener(), this); - } - - @Override - public void onDisable() { - AttractionMenu.save(); - SignManager.saveSigns(); - database.disconnect(); - instance = null; - } - - public static ThemeParkPlugin getInstance() { - return instance; - } - - public YamlConfig getSettings() { - return settings; - } - - public YamlConfig getAttraction() { - return attraction; - } - - public YamlConfig getMessage() { - return message; - } - - public YamlConfig getSigns() { - return signs; - } - - public YamlConfig getData() { - return data; - } - - public DatabaseHandler getDatabaseHandler() { - return database; - } - -} diff --git a/src/main/java/me/paradoxpixel/themepark/action/PageAction.java b/src/main/java/me/paradoxpixel/themepark/action/PageAction.java deleted file mode 100644 index 9599593..0000000 --- a/src/main/java/me/paradoxpixel/themepark/action/PageAction.java +++ /dev/null @@ -1,20 +0,0 @@ -package me.paradoxpixel.themepark.action; - -import me.paradoxpixel.themepark.attraction.AttractionMenu; -import me.paradoxpixel.themepark.gui.GUIAction; -import org.bukkit.entity.Player; - -public class PageAction extends GUIAction { - - private int page; - - public PageAction(int page) { - this.page = page; - } - - @Override - public void click(Player player) { - AttractionMenu.openMenu(player, page); - } - -} diff --git a/src/main/java/me/paradoxpixel/themepark/action/TPUtils.java b/src/main/java/me/paradoxpixel/themepark/action/TPUtils.java deleted file mode 100644 index 58477e6..0000000 --- a/src/main/java/me/paradoxpixel/themepark/action/TPUtils.java +++ /dev/null @@ -1,49 +0,0 @@ -package me.paradoxpixel.themepark.action; - -import me.paradoxpixel.themepark.api.API; -import me.paradoxpixel.themepark.api.attraction.Attraction; -import me.paradoxpixel.themepark.attraction.AttractionMenu; -import me.paradoxpixel.themepark.attraction.status.StatusManager; -import me.paradoxpixel.themepark.gui.GUIAction; -import me.paradoxpixel.themepark.utils.Message; -import me.paradoxpixel.themepark.utils.Utils; -import org.bukkit.entity.Minecart; -import org.bukkit.entity.Player; - -public class TPUtils extends GUIAction { - - private String id; - - public TPUtils(String id) { - this.id = id; - } - - @Override - public void click(Player player) { - player.closeInventory(); - Attraction attraction = API.getAttraction(id); - if(attraction == null) - return; - - if(attraction.getLocation() == null) - return; - - if(!StatusManager.canTeleport(attraction.getStatus())) { - String message = Message.getMessage("attraction.teleport.status"); - message = message.replace("{name}", attraction.getName()); - message = message.replace("{status}", StatusManager.getName(attraction.getStatus())); - player.sendMessage(Utils.color(message)); - return; - } - - if(player.isInsideVehicle()) - if(player.getVehicle() instanceof Minecart) - return; - - player.teleport(attraction.getLocation()); - String message = Message.getMessage("attraction.teleport.success"); - message = message.replace("{name}", attraction.getName()); - player.sendMessage(Utils.color(message)); - } - -} diff --git a/src/main/java/me/paradoxpixel/themepark/api/API.java b/src/main/java/me/paradoxpixel/themepark/api/API.java deleted file mode 100644 index 31dfa97..0000000 --- a/src/main/java/me/paradoxpixel/themepark/api/API.java +++ /dev/null @@ -1,160 +0,0 @@ -package me.paradoxpixel.themepark.api; - -import me.paradoxpixel.themepark.api.attraction.Attraction; -import me.paradoxpixel.themepark.api.attraction.Region; -import me.paradoxpixel.themepark.api.event.attraction.AddAttractionEvent; -import me.paradoxpixel.themepark.api.event.attraction.RemoveAttractionEvent; -import me.paradoxpixel.themepark.api.event.region.AddRegionEvent; -import me.paradoxpixel.themepark.api.event.region.RemoveRegionEvent; -import org.bukkit.Bukkit; -import org.bukkit.ChatColor; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.Map; - -public class API { - - private static LinkedHashMap regions = new LinkedHashMap<>(); - private static LinkedHashMap attractions = new LinkedHashMap<>(); - - public static void addRegion(Region region) { - if(isRegion(region.getId())) - return; - - region = toLower(region); - regions.put(region.getId(), region); - - AddRegionEvent event = new AddRegionEvent(region); - Bukkit.getPluginManager().callEvent(event); - } - - public static boolean isRegion(String id) { - if(id == null || id.isEmpty()) - return false; - - return regions.containsKey(id.toLowerCase()); - } - - public static boolean isRegionFull(String id) { - if(!isRegion(id)) - return false; - - return getRegion(id).getAttractions().size() >= 8; - } - - public static Region getRegion(String id) { - if(id == null || id.isEmpty()) - return null; - - return regions.get(id.toLowerCase()); - } - - public static HashMap getRegions() { - return new LinkedHashMap<>(regions); - } - - public static void removeRegion(String id) { - if(!isRegion(id)) - return; - - HashMap map = new HashMap<>(attractions); - for(Map.Entry entry : map.entrySet()) { - Attraction attraction = entry.getValue(); - if(!attraction.getRegion_id().equalsIgnoreCase(id)) - continue; - - removeAttraction(attraction.getId()); - } - - RemoveRegionEvent event = new RemoveRegionEvent(getRegion(id)); - Bukkit.getPluginManager().callEvent(event); - regions.remove(id.toLowerCase()); - } - - public static void addAttraction(Attraction attraction) { - if(!isRegion(attraction.getRegion_id())) - return; - - if(isRegionFull(attraction.getRegion_id())) - return; - - if(isAttraction(attraction.getId())) - return; - - attraction = toLower(attraction); - getRegion(attraction.getRegion_id()).addAttraction(attraction); - attractions.put(attraction.getId(), attraction); - - AddAttractionEvent event = new AddAttractionEvent(attraction); - Bukkit.getPluginManager().callEvent(event); - } - - public static boolean isAttraction(String id) { - if(id == null || id.isEmpty()) - return false; - - return attractions.containsKey(id.toLowerCase()); - } - - public static Attraction getAttraction(String id) { - if(id == null || id.isEmpty()) - return null; - - return attractions.get(id.toLowerCase()); - } - - public static Attraction getAttractionFromName(String name) { - if(name == null || name.isEmpty()) - return null; - - for(Attraction attraction : attractions.values()) { - if(!color(attraction.getName()).equals(name)) - continue; - - return attraction; - } - - return null; - } - - public static HashMap getAttractions() { - return new LinkedHashMap<>(attractions); - } - - public static void removeAttraction(String id) { - if(!isAttraction(id)) - return; - - Attraction attraction = getAttraction(id); - if(isRegion(attraction.getRegion_id())) - getRegion(attraction.getRegion_id()).removeAttraction(attraction); - - attractions.remove(id.toLowerCase()); - - RemoveAttractionEvent event = new RemoveAttractionEvent(attraction); - Bukkit.getPluginManager().callEvent(event); - } - - private static Region toLower(Region region) { - Region re = new Region(region.getId().toLowerCase(), - region.getName(), - region.getLore()); - - re.setAttractions(region.getAttractions()); - return re; - } - - private static Attraction toLower(Attraction attraction) { - return new Attraction(attraction.getId().toLowerCase(), - attraction.getName(), - attraction.getRegion_id().toLowerCase(), - attraction.getLocation(), - attraction.getType(), - attraction.getStatus()); - } - - private static String color(String string) { - return ChatColor.translateAlternateColorCodes('&', string); - } - -} diff --git a/src/main/java/me/paradoxpixel/themepark/api/attraction/Attraction.java b/src/main/java/me/paradoxpixel/themepark/api/attraction/Attraction.java deleted file mode 100644 index 85ef631..0000000 --- a/src/main/java/me/paradoxpixel/themepark/api/attraction/Attraction.java +++ /dev/null @@ -1,118 +0,0 @@ -package me.paradoxpixel.themepark.api.attraction; - -import me.paradoxpixel.themepark.api.attraction.component.Status; -import me.paradoxpixel.themepark.api.attraction.component.Type; -import me.paradoxpixel.themepark.api.event.attraction.PreStatusChangeEvent; -import me.paradoxpixel.themepark.api.event.attraction.StatusChangeEvent; -import me.paradoxpixel.themepark.api.event.attraction.ChangeAttractionEvent; -import me.paradoxpixel.themepark.api.LocationUtils; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.entity.Player; - -public class Attraction { - - private String id, name, region_id; - private Location location; - private Type type; - private Status status; - - public Attraction(String id, String name, String region_id, Location location, Type type, Status status) { - this.id = id; - this.name = name; - this.region_id = region_id; - this.location = location; - this.type = type; - this.status = type.containsStatus(status) ? status : type.getDefault(); - } - - public String getId() { - return id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - if(this.name.equals(name)) - return; - - ChangeAttractionEvent event = new ChangeAttractionEvent(this, this.name, name, region_id, region_id, location, location, type, type); - this.name = name; - Bukkit.getPluginManager().callEvent(event); - } - - public String getRegion_id() { - return region_id; - } - - public void setRegion_id(String region_id) { - if(this.region_id.equals(region_id)) - return; - - ChangeAttractionEvent event = new ChangeAttractionEvent(this, name, name, this.region_id, region_id, location, location, type, type); - this.region_id = region_id; - Bukkit.getPluginManager().callEvent(event); - } - - public Location getLocation() { - return location; - } - - public void setLocation(Location location) { - if(location == this.location) - return; - - if(location == null) - return; - - if(LocationUtils.toString(location) == null) - return; - - if(LocationUtils.toString(this.location) != null && LocationUtils.toString(location) != null) - if(LocationUtils.toString(this.location).equals(LocationUtils.toString(location))) - return; - - ChangeAttractionEvent event = new ChangeAttractionEvent(this, name, name, region_id, region_id, this.location, location, type, type); - this.location = location; - Bukkit.getPluginManager().callEvent(event); - } - - public Type getType() { - return type; - } - - public void setType(Type type) { - ChangeAttractionEvent event = new ChangeAttractionEvent(this, name, name, region_id, region_id, location, location, this.type, type); - this.type = type; - Bukkit.getPluginManager().callEvent(event); - if(!type.containsStatus(status)) - setStatus(type.getDefault(), null); - } - - public Status getStatus() { - return status; - } - - public void setStatus(Status status, Player player) { - if(!type.hasStatus()) - return; - - if(!type.containsStatus(status)) - return; - - if(this.status == status) - return; - - PreStatusChangeEvent event = new PreStatusChangeEvent(this, player, this.status, status); - Bukkit.getPluginManager().callEvent(event); - if(event.isCancelled()) - return; - - this.status = status; - StatusChangeEvent e = new StatusChangeEvent(this, player, this.status, status); - Bukkit.getPluginManager().callEvent(e); - } - -} diff --git a/src/main/java/me/paradoxpixel/themepark/api/attraction/Region.java b/src/main/java/me/paradoxpixel/themepark/api/attraction/Region.java deleted file mode 100644 index a41e236..0000000 --- a/src/main/java/me/paradoxpixel/themepark/api/attraction/Region.java +++ /dev/null @@ -1,96 +0,0 @@ -package me.paradoxpixel.themepark.api.attraction; - -import me.paradoxpixel.themepark.api.event.region.ChangeRegionEvent; -import org.bukkit.Bukkit; -import java.util.ArrayList; -import java.util.List; - -public class Region { - - private String id, name; - private List lore; - private ArrayList attractions; - - public Region(String id, String name, List lore) { - this.id = id; - this.name = name; - this.lore = lore; - this.attractions = new ArrayList<>(); - } - - public String getId() { - return id; - } - - public String getName() { - return name; - } - - public void setName(String string) { - if(string == null || string.equals("")) - return; - - String old = name; - name = string; - - ChangeRegionEvent event = new ChangeRegionEvent(this, old, string, lore, lore); - Bukkit.getPluginManager().callEvent(event); - } - - public List getLore() { - return lore; - } - - public void setLore(List lore) { - List old = lore; - this.lore = lore; - - ChangeRegionEvent event = new ChangeRegionEvent(this, name, name, old, lore); - Bukkit.getPluginManager().callEvent(event); - } - - public void setLore(int i, String string) { - if(lore.size() >= 4 || i >= 4) - return; - - List old = lore; - lore = new ArrayList<>(); - lore.addAll(old); - - if(!string.equals("NULL")) { - if (lore.size() >= i) - for (int a = 0; a <= (i - lore.size()); a++) - lore.add(" "); - - lore.set(i, string); - } else { - lore.remove(i); - } - - ChangeRegionEvent event = new ChangeRegionEvent(this, name, name, old, lore); - Bukkit.getPluginManager().callEvent(event); - } - - public ArrayList getAttractions() { - return attractions; - } - - public void setAttractions(ArrayList attractions) { - this.attractions = attractions; - } - - public void addAttraction(Attraction attraction) { - if(!isAttraction(attraction)) - attractions.add(attraction); - } - - public boolean isAttraction(Attraction attraction) { - return attractions.contains(attraction); - } - - public void removeAttraction(Attraction attraction) { - if(isAttraction(attraction)) - attractions.remove(attraction); - } - -} diff --git a/src/main/java/me/paradoxpixel/themepark/api/event/attraction/AddAttractionEvent.java b/src/main/java/me/paradoxpixel/themepark/api/event/attraction/AddAttractionEvent.java deleted file mode 100644 index 56899fe..0000000 --- a/src/main/java/me/paradoxpixel/themepark/api/event/attraction/AddAttractionEvent.java +++ /dev/null @@ -1,29 +0,0 @@ -package me.paradoxpixel.themepark.api.event.attraction; - -import me.paradoxpixel.themepark.api.attraction.Attraction; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class AddAttractionEvent extends Event { - - private static final HandlerList handlers = new HandlerList(); - - private Attraction attraction; - - public AddAttractionEvent(Attraction attraction) { - this.attraction = attraction; - } - - public Attraction getAttraction() { - return attraction; - } - - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } - -} \ No newline at end of file diff --git a/src/main/java/me/paradoxpixel/themepark/api/event/attraction/ChangeAttractionEvent.java b/src/main/java/me/paradoxpixel/themepark/api/event/attraction/ChangeAttractionEvent.java deleted file mode 100644 index 92f4450..0000000 --- a/src/main/java/me/paradoxpixel/themepark/api/event/attraction/ChangeAttractionEvent.java +++ /dev/null @@ -1,79 +0,0 @@ -package me.paradoxpixel.themepark.api.event.attraction; - -import me.paradoxpixel.themepark.api.attraction.Attraction; -import me.paradoxpixel.themepark.api.attraction.component.Type; -import org.bukkit.Location; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class ChangeAttractionEvent extends Event { - - private static final HandlerList handlers = new HandlerList(); - - private Attraction attraction; - private String bname, aname, bregion_id, aregion_id; - private Location blocation, alocation; - private Type btype, atype; - - public ChangeAttractionEvent(Attraction attraction, - String bname, String aname, - String aregion_id, String bregion_id, - Location alocation, Location blocation, - Type btype, Type atype) { - this.attraction = attraction; - this.bname = bname; - this.aname = aname; - this.bregion_id = bregion_id; - this.aregion_id = aregion_id; - this.alocation = alocation; - this.blocation = blocation; - this.btype = btype; - this.atype = atype; - } - - public Attraction getAttraction() { - return attraction; - } - - public String getNameBefore() { - return bname; - } - - public String getNameAfter() { - return aname; - } - - public String getRegionIdBefore() { - return bregion_id; - } - - public String getRegionIdAfter() { - return aregion_id; - } - - public Location getLocationBefore() { - return blocation; - } - - public Location getLocationAfter() { - return alocation; - } - - public Type getTypeBefore() { - return btype; - } - - public Type getTypeAfter() { - return atype; - } - - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } - -} - diff --git a/src/main/java/me/paradoxpixel/themepark/api/event/attraction/PreStatusChangeEvent.java b/src/main/java/me/paradoxpixel/themepark/api/event/attraction/PreStatusChangeEvent.java deleted file mode 100644 index 529e6da..0000000 --- a/src/main/java/me/paradoxpixel/themepark/api/event/attraction/PreStatusChangeEvent.java +++ /dev/null @@ -1,61 +0,0 @@ -package me.paradoxpixel.themepark.api.event.attraction; - -import me.paradoxpixel.themepark.api.attraction.Attraction; -import me.paradoxpixel.themepark.api.attraction.component.Status; -import org.bukkit.entity.Player; -import org.bukkit.event.Cancellable; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class PreStatusChangeEvent extends Event implements Cancellable { - - private static final HandlerList handlers = new HandlerList(); - - private boolean cancel; - private Attraction attraction; - private Player player; - private Status before, after; - - public PreStatusChangeEvent(Attraction attraction, Player player, Status before, Status after) { - this.attraction = attraction; - this.player = player; - this.before = before; - this.after = after; - cancel = false; - } - - @Override - public boolean isCancelled() { - return cancel; - } - - @Override - public void setCancelled(boolean b) { - cancel = b; - } - - public Attraction getAttraction() { - return attraction; - } - - public Player getPlayer() { - return player; - } - - public Status getStatusBefore() { - return before; - } - - public Status getStatusAfter() { - return after; - } - - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } - -} diff --git a/src/main/java/me/paradoxpixel/themepark/api/event/attraction/RemoveAttractionEvent.java b/src/main/java/me/paradoxpixel/themepark/api/event/attraction/RemoveAttractionEvent.java deleted file mode 100644 index a07cb09..0000000 --- a/src/main/java/me/paradoxpixel/themepark/api/event/attraction/RemoveAttractionEvent.java +++ /dev/null @@ -1,29 +0,0 @@ -package me.paradoxpixel.themepark.api.event.attraction; - -import me.paradoxpixel.themepark.api.attraction.Attraction; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class RemoveAttractionEvent extends Event { - - private static final HandlerList handlers = new HandlerList(); - - private Attraction attraction; - - public RemoveAttractionEvent(Attraction attraction) { - this.attraction = attraction; - } - - public Attraction getAttraction() { - return attraction; - } - - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } - -} \ No newline at end of file diff --git a/src/main/java/me/paradoxpixel/themepark/api/event/attraction/StatusChangeEvent.java b/src/main/java/me/paradoxpixel/themepark/api/event/attraction/StatusChangeEvent.java deleted file mode 100644 index 2e80350..0000000 --- a/src/main/java/me/paradoxpixel/themepark/api/event/attraction/StatusChangeEvent.java +++ /dev/null @@ -1,48 +0,0 @@ -package me.paradoxpixel.themepark.api.event.attraction; - -import me.paradoxpixel.themepark.api.attraction.Attraction; -import me.paradoxpixel.themepark.api.attraction.component.Status; -import org.bukkit.entity.Player; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class StatusChangeEvent extends Event { - - private static final HandlerList handlers = new HandlerList(); - - private Attraction attraction; - private Player player; - private Status before, after; - - public StatusChangeEvent(Attraction attraction, Player player, Status before, Status after) { - this.attraction = attraction; - this.player = player; - this.before = before; - this.after = after; - } - - public Attraction getAttraction() { - return attraction; - } - - public Player getPlayer() { - return player; - } - - public Status getStatusBefore() { - return before; - } - - public Status getStatusAfter() { - return after; - } - - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } - -} diff --git a/src/main/java/me/paradoxpixel/themepark/api/event/region/AddRegionEvent.java b/src/main/java/me/paradoxpixel/themepark/api/event/region/AddRegionEvent.java deleted file mode 100644 index 21fc13e..0000000 --- a/src/main/java/me/paradoxpixel/themepark/api/event/region/AddRegionEvent.java +++ /dev/null @@ -1,30 +0,0 @@ -package me.paradoxpixel.themepark.api.event.region; - -import me.paradoxpixel.themepark.api.attraction.Region; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class AddRegionEvent extends Event { - - private static final HandlerList handlers = new HandlerList(); - - private Region region; - - public AddRegionEvent(Region region) { - this.region = region; - } - - public Region getRegion() { - return region; - } - - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } - -} - diff --git a/src/main/java/me/paradoxpixel/themepark/api/event/region/ChangeRegionEvent.java b/src/main/java/me/paradoxpixel/themepark/api/event/region/ChangeRegionEvent.java deleted file mode 100644 index 5a8ae8a..0000000 --- a/src/main/java/me/paradoxpixel/themepark/api/event/region/ChangeRegionEvent.java +++ /dev/null @@ -1,53 +0,0 @@ -package me.paradoxpixel.themepark.api.event.region; - -import me.paradoxpixel.themepark.api.attraction.Region; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; -import java.util.List; - -public class ChangeRegionEvent extends Event { - - private static final HandlerList handlers = new HandlerList(); - - private Region region; - private String bname, aname; - private List blore, alore; - - public ChangeRegionEvent(Region region, String bname, String aname, List blore, List alore) { - this.region = region; - this.bname = bname; - this.aname = aname; - this.blore = blore; - this.alore = alore; - } - - public Region getRegion() { - return region; - } - - public String getNameBefore() { - return bname; - } - - public String getNameAfter() { - return aname; - } - - public List getLoreBefore() { - return blore; - } - - public List getLoreAfter() { - return alore; - } - - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } - -} - diff --git a/src/main/java/me/paradoxpixel/themepark/api/event/region/RemoveRegionEvent.java b/src/main/java/me/paradoxpixel/themepark/api/event/region/RemoveRegionEvent.java deleted file mode 100644 index 8c7d47c..0000000 --- a/src/main/java/me/paradoxpixel/themepark/api/event/region/RemoveRegionEvent.java +++ /dev/null @@ -1,30 +0,0 @@ -package me.paradoxpixel.themepark.api.event.region; - -import me.paradoxpixel.themepark.api.attraction.Region; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -public class RemoveRegionEvent extends Event { - - private static final HandlerList handlers = new HandlerList(); - - private Region region; - - public RemoveRegionEvent(Region region) { - this.region = region; - } - - public Region getRegion() { - return region; - } - - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } - - -} diff --git a/src/main/java/me/paradoxpixel/themepark/attraction/AttractionMenu.java b/src/main/java/me/paradoxpixel/themepark/attraction/AttractionMenu.java deleted file mode 100644 index 1ecb36e..0000000 --- a/src/main/java/me/paradoxpixel/themepark/attraction/AttractionMenu.java +++ /dev/null @@ -1,359 +0,0 @@ -package me.paradoxpixel.themepark.attraction; - -import me.paradoxpixel.themepark.ThemeParkPlugin; -import me.paradoxpixel.themepark.action.PageAction; -import me.paradoxpixel.themepark.action.TPUtils; -import me.paradoxpixel.themepark.api.API; -import me.paradoxpixel.themepark.api.attraction.Attraction; -import me.paradoxpixel.themepark.api.attraction.Region; -import me.paradoxpixel.themepark.api.attraction.component.Status; -import me.paradoxpixel.themepark.api.attraction.component.Type; -import me.paradoxpixel.themepark.attraction.status.StatusManager; -import me.paradoxpixel.themepark.config.YamlConfig; -import me.paradoxpixel.themepark.gui.GUI; -import me.paradoxpixel.themepark.gui.GUIItem; -import me.paradoxpixel.themepark.utils.ItemBuilder; -import me.paradoxpixel.themepark.api.LocationUtils; -import me.paradoxpixel.themepark.utils.Utils; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.entity.Player; -import java.util.HashMap; -import java.util.List; -import java.util.Set; - -public class AttractionMenu { - - private static YamlConfig config = ThemeParkPlugin.getInstance().getAttraction(); - private static YamlConfig settings = ThemeParkPlugin.getInstance().getSettings(); - private static HashMap pages = new HashMap<>(); - private static boolean loading = false; - private static String title; - - private static HashMap regionPage; - private static HashMap attractionIndex; - - private static Material mat = Material.NAME_TAG; - private static short data = 0; - - public static void load() { - loading = true; - title = settings.getConfig().getString("menu.title"); - - mat = Material.getMaterial(settings.getConfig().getString("region.material")); - data = Short.parseShort(settings.getConfig().getString("region.data")); - - regionPage = new HashMap<>(); - attractionIndex = new HashMap<>(); - - loadData(); - loading = false; - loadItems(); - } - - public static void reload() { - loadItems(); - } - - public static void save() { - for(Region region : API.getRegions().values()) { - config.getConfig().set("region." + region.getId() + ".name", region.getName()); - config.getConfig().set("region." + region.getId() + ".lore", region.getLore()); - } - - for(Attraction attraction : API.getAttractions().values()) { - config.getConfig().set("attraction." + attraction.getId() + ".name", attraction.getName()); - config.getConfig().set("attraction." + attraction.getId() + ".region_id", attraction.getRegion_id()); - config.getConfig().set("attraction." + attraction.getId() + ".type", attraction.getType().toString()); - config.getConfig().set("attraction." + attraction.getId() + ".status", attraction.getStatus().toString()); - config.getConfig().set("attraction." + attraction.getId() + ".location", LocationUtils.toString(attraction.getLocation())); - } - - config.save(); - } - - private static void loadData() { - if(!config.getConfig().contains("region")) - return; - - if(!config.getConfig().isConfigurationSection("region")) - return; - - ConfigurationSection section = config.getConfig().getConfigurationSection("region"); - if(section.getKeys(false).isEmpty()) - return; - - HashMap regions = API.getRegions(); - HashMap size = new HashMap<>(); - for(String id : section.getKeys(false)) { - String name = config.getConfig().getString("region." + id + ".name"); - List lore = config.getConfig().getStringList("region." + id + ".lore"); - - size.put(id.toLowerCase(), 0); - regions.remove(id); - if(API.isRegion(id)) { - API.getRegion(id).setName(name); - API.getRegion(id).setLore(lore); - continue; - } - - Region region = new Region(id, name, lore); - API.addRegion(region); - if(!id.toLowerCase().equals(id)) { - config.getConfig().set("region." + id, null); - id = id.toLowerCase(); - config.getConfig().set("region." + id + ".name", name); - config.getConfig().set("region." + id + ".lore", lore); - } - } - - config.save(); - for(String string : regions.keySet()) - API.removeRegion(string); - - prepareGUI(); - if(!config.getConfig().contains("attraction")) - return; - - if(!config.getConfig().isConfigurationSection("attraction")) - return; - - section = config.getConfig().getConfigurationSection("attraction"); - if(section.getKeys(false).isEmpty()) - return; - - HashMap attractions = API.getAttractions(); - for(String id : section.getKeys(false)) { - String region_id = config.getConfig().getString("attraction." + id + ".region_id"); - if(region_id == null || region_id.isEmpty()) - continue; - - if(!region_id.toLowerCase().equals(region_id)) { - region_id = region_id.toLowerCase(); - config.getConfig().set("attraction." + id + ".region_id", region_id); - } - - if(!API.isRegion(region_id)) - continue; - - if(size.get(region_id.toLowerCase()) >= 8) - continue; - - String name = config.getConfig().getString("attraction." + id + ".name"); - Location location = LocationUtils.toLocation(config.getConfig().getString("attraction." + id + ".location")); - Type type = Type.getType(config.getConfig().getString("attraction." + id + ".type")); - Status status = Status.getStatus(config.getConfig().getString("attraction." + id + ".status")); - if(!type.containsStatus(status)) { - status = type.getDefault(); - config.getConfig().set("attraction." + id + ".status", type.getDefault().toString()); - } - - attractions.remove(id.toLowerCase()); - size.put(region_id.toLowerCase(), size.get(region_id.toLowerCase()) + 1); - if(API.isAttraction(id)) { - Attraction attraction = API.getAttraction(id); - attraction.setName(name); - attraction.setRegion_id(region_id); - attraction.setLocation(location); - attraction.setType(type); - attraction.setStatus(status, null); - MapMarker.getMarker().setAttractionMarker(attraction); - continue; - } - - Attraction attraction = new Attraction(id, name, region_id, location, type, status); - API.addAttraction(attraction); - MapMarker.getMarker().setAttractionMarker(attraction); - if(!id.toLowerCase().equals(id)) { - config.getConfig().set("attraction." + id, null); - id = id.toLowerCase(); - config.getConfig().set("attraction." + id + ".name", name); - config.getConfig().set("attraction." + id + ".location", LocationUtils.toString(location)); - config.getConfig().set("attraction." + id + ".type", type.toString()); - config.getConfig().set("attraction." + id + ".status", status.toString()); - } - } - - config.save(); - for(String string : attractions.keySet()) - API.removeAttraction(string); - } - - private static void prepareGUI() { - int keySize = API.getRegions().size(); - if(pages.isEmpty()) { - if(keySize <= 6) { - pages.put(1, new GUI(title, keySize * 9)); - } else { - int p = keySize / 5; - for(int i = 0; i < p; i++) - pages.put(i + 1, new GUI(title, 54)); - - int rest = keySize % 5; - if(rest > 0) - pages.put(p + 1, new GUI(title, rest * 9 + 9)); - } - } else { - if(keySize <= 6) { - if(pages.size() > 1) { - for(int i = 1; i < pages.size(); i++) - pages.remove(i + 1); - } - - int z = keySize*9; - GUI gui = pages.get(1); - if(gui.getSize() != z) - gui.setSize(z); - } else { - int p = keySize / 5; - int rest = keySize % 5; - if (rest > 0) - p++; - - if (pages.size() < p) { - for (int i = (pages.size() + 1); i < p; i++) - pages.put(i, new GUI(title, 54)); - - pages.put(p, new GUI(title, rest * 9 + 9)); - } else { - if (pages.size() > p) { - for (int i = pages.size(); i > p; i--) - pages.remove(i); - } else { - if(rest != 0) { - pages.get(p).setSize(rest * 9 + 9); - } else { - pages.get(p).setSize(54); - } - } - } - } - } - } - - public static void update(Attraction attraction) { - if(attraction == null) - return; - - if(!API.isAttraction(attraction.getId())) - return; - - if(!attractionIndex.containsKey(attraction.getId())) - return; - - if(!regionPage.containsKey(attraction.getRegion_id())) - return; - - ItemBuilder builder = new ItemBuilder(StatusManager.getMaterial(attraction.getStatus()), 1, StatusManager.getData(attraction.getStatus())); - builder.setName(attraction.getName()); - builder.setLore(StatusManager.getName(attraction.getStatus())); - - GUI gui = pages.get(regionPage.get(attraction.getRegion_id())); - gui.addItem(attractionIndex.get(attraction.getId()), new GUIItem(builder.getItem(), new TPUtils(attraction.getId()), true)); - } - - private static void loadItems() { - if(loading) - return; - - if(API.getRegions().values().size() == 0) - return; - - for(GUI gui : pages.values()) - gui.clear(); - - int i = 1; - int j = 0; - boolean tb = pages.size() > 1; - HashMap index = new HashMap<>(); - for(Region region : API.getRegions().values()) { - if(tb && j >= 5) { - i++; - j = 0; - } - - index.put(region.getId(), j * 9); - regionPage.put(region.getId(), i); - if(!index.containsKey(region.getId())) - index.put(region.getId(), index.size() * 9); - - ItemBuilder builder = new ItemBuilder(mat, 1, data); - builder.setName(region.getName()); - builder.setLore(region.getLore()); - - GUI gui = pages.get(i); - gui.addItem(j * 9, new GUIItem(builder.getItem(), null, true)); - j++; - } - - - for(Attraction attraction : API.getAttractions().values()) { - if(!API.isRegion(attraction.getRegion_id())) { - API.removeAttraction(attraction.getId()); - continue; - } - - if(!index.containsKey(attraction.getRegion_id())) - continue; - - int in = index.get(attraction.getRegion_id()); - GUI gui = pages.get(regionPage.get(attraction.getRegion_id())); - boolean b = false; - for(int a = 1; a < 9; a++) { - if(!gui.hasItem(in + a)) { - in += a; - b = true; - break; - } - } - - if(!b) { - API.removeAttraction(attraction.getId()); - continue; - } - - ItemBuilder builder = new ItemBuilder(StatusManager.getMaterial(attraction.getStatus()), 1, StatusManager.getData(attraction.getStatus())); - builder.setName(attraction.getName()); - builder.setLore(StatusManager.getName(attraction.getStatus())); - - gui.addItem(in, new GUIItem(builder.getItem(), new TPUtils(attraction.getId()), true)); - attractionIndex.put(attraction.getId(), in); - } - - if(pages.size() <= 1) - return; - - i = 1; - for(GUI gui : pages.values()) { - int rows = gui.getSize() / 9; - int offset = (rows - 1) * 9; - - if(i > 1) { - ItemBuilder previous = new ItemBuilder(Material.ARROW); - previous.setName(Utils.color("&6Previous")); - gui.addItem(offset + 3, new GUIItem(previous.getItem(), new PageAction(i - 1), true)); - } - - ItemBuilder middle = new ItemBuilder(Material.STAINED_GLASS_PANE, 1, (short) 1); - middle.setName(Utils.color("&6Page: "+i)); - gui.addItem(offset + 4, new GUIItem(middle.getItem(), null, true)); - - if(i < pages.size()) { - ItemBuilder next = new ItemBuilder(Material.ARROW); - next.setName(Utils.color("&6Next")); - gui.addItem(offset + 5, new GUIItem(next.getItem(), new PageAction(i + 1), true)); - } - - i++; - } - } - - public static void openMenu(Player player, int page) { - if(page < 1 || page > pages.size()) - page = 1; - - player.openInventory(pages.get(page).getInventory()); - } - -} \ No newline at end of file diff --git a/src/main/java/me/paradoxpixel/themepark/attraction/sign/SignManager.java b/src/main/java/me/paradoxpixel/themepark/attraction/sign/SignManager.java deleted file mode 100644 index 3c9c56c..0000000 --- a/src/main/java/me/paradoxpixel/themepark/attraction/sign/SignManager.java +++ /dev/null @@ -1,152 +0,0 @@ -package me.paradoxpixel.themepark.attraction.sign; - -import me.paradoxpixel.themepark.ThemeParkPlugin; -import me.paradoxpixel.themepark.api.attraction.Attraction; -import me.paradoxpixel.themepark.config.YamlConfig; -import me.paradoxpixel.themepark.api.LocationUtils; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.block.Sign; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -public class SignManager { - - private static YamlConfig config = ThemeParkPlugin.getInstance().getSigns(); - private static HashMap> signs = new HashMap<>(); - - public static void loadSigns(Attraction attraction) { - if(attraction == null) { - System.out.println("SignManager: No attraction passed"); - return; - } - - if(!config.getConfig().contains("signs." + attraction.getId())) { - System.out.println("SignManager: No signs for attraction with ID: "+attraction.getId()); - return; - } - - if(!config.getConfig().isList("signs." + attraction.getId())) { - System.out.println("SignManager: No signs for attraction with ID: "+attraction.getId()); - config.getConfig().set("signs." + attraction.getId(), null); - config.save(); - return; - } - - ArrayList array = new ArrayList<>(); - List locations = config.getConfig().getStringList("signs." + attraction.getId()); - for(String string : locations) { - Location location = LocationUtils.toLocation(string); - if(location == null) { - System.out.println("SignManager: Incorrect sign Location"); - continue; - } - - if(!(location.getBlock().getState() instanceof Sign)) { - System.out.println("SignManager: Block is not a sign at: "+string); - continue; - } - - array.add(new StatusSign(attraction, location)); - } - - signs.put(attraction, array); - } - - public static void saveSigns() { - for(Map.Entry> entry : signs.entrySet()) { - List array = new ArrayList<>(); - for(StatusSign sign : entry.getValue()) - if(LocationUtils.toString(sign.getLocation()) != null) - array.add(LocationUtils.toString(sign.getLocation())); - - config.getConfig().set("signs." + entry.getKey().getId(), array); - } - - config.save(); - } - - public static void addSign(StatusSign sign) { - if(sign == null || sign.getLocation() == null) - return; - - if(!(sign.getLocation().getBlock().getState() instanceof Sign)) - return; - - if(signs.containsKey(sign.getAttraction())) { - signs.get(sign.getAttraction()).add(sign); - return; - } - - ArrayList array = new ArrayList<>(); - array.add(sign); - signs.put(sign.getAttraction(), array); - } - - public static boolean hasSigns(Attraction attraction) { - if(attraction == null) - return false; - - return signs.containsKey(attraction) && (signs.get(attraction).size() > 0); - } - - public static void update(Attraction attraction) { - if(attraction == null) - return; - - if(!hasSigns(attraction)) - return; - - for(StatusSign sign : signs.get(attraction)) - sign.update(); - } - - public static void remove(Attraction attraction) { - if(attraction == null || !signs.containsKey(attraction)) - return; - - for(StatusSign sign : signs.get(attraction)) - sign.getLocation().getBlock().setType(Material.AIR); - - signs.remove(attraction); - config.getConfig().set("signs." + attraction.getId(), null); - config.save(); - } - - public static void removeSign(StatusSign sign) { - if(sign == null || sign.getLocation() == null || sign.getAttraction() == null) - return; - - if(!hasSigns(sign.getAttraction())) - return; - - ArrayList array = signs.get(sign.getAttraction()); - for(StatusSign s : new ArrayList<>(array)) { - Location loc1 = sign.getLocation(); - Location loc2 = s.getLocation(); - - if(loc1 == null || loc2 == null) - return; - - if(LocationUtils.toString(loc1) == null || LocationUtils.toString(loc2) == null) - return; - - if(!LocationUtils.toString(loc1).equals(LocationUtils.toString(loc2))) - return; - - array.remove(s); - } - - if(array.isEmpty()) { - signs.remove(sign.getAttraction()); - config.getConfig().set("signs." + sign.getAttraction().getId(), null); - config.save(); - return; - } - - signs.put(sign.getAttraction(), array); - } - -} diff --git a/src/main/java/me/paradoxpixel/themepark/attraction/sign/StatusSign.java b/src/main/java/me/paradoxpixel/themepark/attraction/sign/StatusSign.java deleted file mode 100644 index 90408b7..0000000 --- a/src/main/java/me/paradoxpixel/themepark/attraction/sign/StatusSign.java +++ /dev/null @@ -1,43 +0,0 @@ -package me.paradoxpixel.themepark.attraction.sign; - -import me.paradoxpixel.themepark.api.attraction.Attraction; -import me.paradoxpixel.themepark.api.attraction.component.Status; -import me.paradoxpixel.themepark.attraction.status.StatusManager; -import me.paradoxpixel.themepark.utils.Utils; -import org.bukkit.Location; -import org.bukkit.block.Sign; - -public class StatusSign { - - private Attraction attraction; - private Location location; - - public StatusSign(Attraction attraction, Location location) { - this.attraction = attraction; - this.location = location; - } - - public Attraction getAttraction() { - return attraction; - } - - public Location getLocation() { - return location; - } - - public void update() { - Status status = attraction.getStatus(); - if(!location.getChunk().isLoaded()) - location.getChunk().load(); - - if(!(location.getBlock().getState() instanceof Sign)) { - SignManager.removeSign(this); - return; - } - - Sign sign = (Sign) location.getBlock().getState(); - sign.setLine(2, Utils.color(StatusManager.getName(status))); - sign.update(); - } - -} diff --git a/src/main/java/me/paradoxpixel/themepark/attraction/status/StatusManager.java b/src/main/java/me/paradoxpixel/themepark/attraction/status/StatusManager.java deleted file mode 100644 index d632502..0000000 --- a/src/main/java/me/paradoxpixel/themepark/attraction/status/StatusManager.java +++ /dev/null @@ -1,76 +0,0 @@ -package me.paradoxpixel.themepark.attraction.status; - -import me.paradoxpixel.themepark.ThemeParkPlugin; -import me.paradoxpixel.themepark.api.attraction.component.Status; -import me.paradoxpixel.themepark.config.YamlConfig; -import org.bukkit.Material; -import java.util.HashMap; - -public class StatusManager { - - private static YamlConfig config = ThemeParkPlugin.getInstance().getSettings(); - private static HashMap names; - private static HashMap materials; - private static HashMap data; - private static HashMap teleport; - - public static void load() { - names = new HashMap<>(); - materials = new HashMap<>(); - data = new HashMap<>(); - teleport = new HashMap<>(); - - for(Status status : Status.values()) { - if(config.getConfig().contains(status.toString() + ".name")) { - names.put(status, config.getConfig().getString(status.toString() + ".name")); - } else { - names.put(status, "UNKNOWN"); - } - - if(config.getConfig().contains(status.toString() + ".material")) { - Material material = Material.getMaterial(config.getConfig().getString(status.toString() + ".material")); - if(material == null) - material = Material.STAINED_CLAY; - - materials.put(status, material); - } else { - materials.put(status, Material.STAINED_CLAY); - } - - if(config.getConfig().contains(status.toString() + ".data")) { - String string = config.getConfig().getString(status.toString() + ".data"); - if(string.isEmpty()) { - data.put(status, (short) 0); - continue; - } - - data.put(status, Short.parseShort(string)); - } else { - data.put(status, (short) 0); - } - - if(config.getConfig().contains(status.toString() + ".teleport")) { - teleport.put(status, config.getConfig().getBoolean(status.toString() + ".teleport")); - } else { - teleport.put(status, false); - } - } - } - - public static String getName(Status status) { - return names.get(status); - } - - public static Material getMaterial(Status status) { - return materials.get(status); - } - - public static short getData(Status status) { - return data.get(status); - } - - public static boolean canTeleport(Status status) { - return teleport.get(status); - } - -} diff --git a/src/main/java/me/paradoxpixel/themepark/command/ThemeParkCommand.java b/src/main/java/me/paradoxpixel/themepark/command/ThemeParkCommand.java deleted file mode 100644 index 88d7521..0000000 --- a/src/main/java/me/paradoxpixel/themepark/command/ThemeParkCommand.java +++ /dev/null @@ -1,352 +0,0 @@ -package me.paradoxpixel.themepark.command; - -import me.paradoxpixel.themepark.ThemeParkPlugin; -import me.paradoxpixel.themepark.api.API; -import me.paradoxpixel.themepark.api.LocationUtils; -import me.paradoxpixel.themepark.api.attraction.Attraction; -import me.paradoxpixel.themepark.api.attraction.Region; -import me.paradoxpixel.themepark.api.attraction.component.Status; -import me.paradoxpixel.themepark.attraction.AttractionMenu; -import me.paradoxpixel.themepark.attraction.MapMarker; -import me.paradoxpixel.themepark.attraction.status.StatusManager; -import me.paradoxpixel.themepark.config.YamlConfig; -import me.paradoxpixel.themepark.utils.ItemBuilder; -import me.paradoxpixel.themepark.utils.Message; -import me.paradoxpixel.themepark.utils.Utils; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.command.CommandSender; -import org.bukkit.command.defaults.BukkitCommand; -import org.bukkit.entity.Minecart; -import org.bukkit.entity.Player; -import java.util.Scanner; -import java.util.UUID; - -public class ThemeParkCommand extends BukkitCommand { - - private String name; - private YamlConfig config = ThemeParkPlugin.getInstance().getAttraction(); - - public ThemeParkCommand(String name) { - super(name); - - this.name = name; - } - - public boolean execute(CommandSender sender,String label, String[] args) { - if (args.length == 0 || args[0].equalsIgnoreCase("help")) { - sender.sendMessage(Utils.color("&f>==== &6ThemePark &f ====<")); - sender.sendMessage(Utils.color("&f/" + name + " help")); - sender.sendMessage(Utils.color("&f/" + name + " list")); - sender.sendMessage(Utils.color("&f/" + name + " warp [id]")); - if(sender.hasPermission("themepark.admin") || sender.hasPermission("themepark.item")) { - sender.sendMessage(Utils.color("&f/" + name + " toggleitem")); - sender.sendMessage(Utils.color("&f/" + name + " getitem")); - } - - if(sender.hasPermission("themepark.admin")) { - sender.sendMessage(Utils.color("&f/" + name + " reload")); - sender.sendMessage(Utils.color("&f/" + name + " regionname [id] [name]")); - sender.sendMessage(Utils.color("&f/" + name + " regionlore [id] [index] [lore]")); - sender.sendMessage(Utils.color("&f/" + name + " setlocation [id]")); - sender.sendMessage(Utils.color("&f/" + name + " attraction [id] [status]")); - } - - return true; - } - - if (args[0].equalsIgnoreCase("list")) { - sender.sendMessage(Utils.color("&f>==== &6ThemePark &f ====<")); - if (API.getAttractions().size() == 0) { - sender.sendMessage(Utils.color(Message.getMessage("attraction.notfound"))); - return true; - } - - for (Attraction attraction : API.getAttractions().values()) { - String message = Message.getMessage("attraction.list"); - message = message.replace("{id}", attraction.getId()); - message = message.replace("{name}", attraction.getName()); - message = message.replace("{region}", API.getRegion(attraction.getRegion_id()).getName()); - message = message.replace("{type}", attraction.getType().toString()); - message = message.replace("{status}", StatusManager.getName(attraction.getStatus())); - sender.sendMessage(Utils.color(message)); - } - - return true; - } - - if(args[0].equalsIgnoreCase("warp")) { - if(args.length < 2) { - sender.sendMessage(Utils.color( "&6Themepark&f: &4/" + name + " warp [id]")); - return true; - } - - String id = args[1]; - if(!API.isAttraction(id)) { - String message = Message.getMessage("attraction.no"); - message = message.replace("{id}", id); - sender.sendMessage(Utils.color(message)); - return true; - } - - if(sender instanceof Player) { - Attraction attraction = API.getAttraction(id); - Player player = (Player) sender; - if(!StatusManager.canTeleport(attraction.getStatus())) { - String message = Message.getMessage("attraction.teleport.status"); - message = message.replace("{name}", attraction.getName()); - message = message.replace("{status}", StatusManager.getName(attraction.getStatus())); - player.sendMessage(Utils.color(message)); - return true; - } - - player.teleport(attraction.getLocation()); - if(player.isInsideVehicle()) - if(player.getVehicle() instanceof Minecart) - return true; - - player.sendMessage(Utils.color(Message.getMessage("attraction.teleport.success").replace("{name}", attraction.getName()))); - return true; - } else { - sender.sendMessage(Utils.color(Message.getMessage("onlyplayers"))); - return true; - } - } - - if(!sender.hasPermission("themepark.admin") && !sender.hasPermission("themepark.item")) { - sender.sendMessage(Utils.color(Message.getMessage("nopermission"))); - return true; - } - - if(args[0].equalsIgnoreCase("toggleitem")) { - if(!(sender instanceof Player)) { - sender.sendMessage(Utils.color(Message.getMessage("onlyplayers"))); - return true; - } - - Player player = (Player) sender; - UUID uuid = player.getUniqueId(); - - YamlConfig config = ThemeParkPlugin.getInstance().getData(); - boolean b = config.getConfig().getBoolean(uuid.toString() + ".item"); - b = !b; - - config.getConfig().set(uuid.toString() + ".item", b); - config.save(); - - String message = Message.getMessage("menu.item.toggle"); - message = message.replace("{status}", b ? Message.getMessage("menu.item.enabled") : Message.getMessage("menu.item.disabled")); - sender.sendMessage(Utils.color(message)); - return true; - } - - if(args[0].equalsIgnoreCase("getitem")) { - YamlConfig settings = ThemeParkPlugin.getInstance().getSettings(); - Material material = Material.getMaterial(settings.getConfig().getString("item.material")); - String name = Utils.color(settings.getConfig().getString("item.display-name")); - if(material == null || name.isEmpty()) - return true; - - Player player; - if(args.length >= 2) { - String string = args[1]; - if(Bukkit.getPlayerExact(string) == null) { - String message = Message.getMessage("noplayer"); - message = message.replace("{name}", string); - sender.sendMessage(Utils.color(message)); - return true; - } - - player = Bukkit.getPlayerExact(string); - } else { - if(!(sender instanceof Player)) { - sender.sendMessage(Utils.color(Message.getMessage("onlyplayers"))); - return true; - } - - player = (Player) sender; - } - - - ItemBuilder builder = new ItemBuilder(material); - builder.setName(name); - player.getInventory().addItem(builder.getItem()); - } - - if(!sender.hasPermission("themepark.admin")) { - sender.sendMessage(Utils.color(Message.getMessage("nopermission"))); - return true; - } - - if(args[0].equalsIgnoreCase("reload")) { - ThemeParkPlugin.getInstance().getData().reload(); - ThemeParkPlugin.getInstance().getAttraction().reload(); - ThemeParkPlugin.getInstance().getSigns().reload(); - ThemeParkPlugin.getInstance().getSettings().reload(); - ThemeParkPlugin.getInstance().getMessage().reload(); - - StatusManager.load(); - AttractionMenu.load(); - Bukkit.getScheduler().runTaskAsynchronously(ThemeParkPlugin.getInstance(),() -> { - ThemeParkPlugin.getInstance().getDatabaseHandler().load(); - }); - sender.sendMessage(Utils.color(Message.getMessage("reloaded"))); - } - - if (args[0].equalsIgnoreCase("regionname")) { - if (args.length < 3) { - sender.sendMessage(Utils.color("&6Themepark&f: &4/" + name + " regionname [id] [name]")); - return true; - } - - String id = args[1]; - if(!API.isRegion(id)) { - String message = Message.getMessage("region.not"); - message = message.replace("{id}", id); - sender.sendMessage(Utils.color(message)); - return true; - } - - String name = args[2]; - name = name.replaceAll("_", " "); - - Region region = API.getRegion(id); - if(!region.getName().equals(name)) - region.setName(name); - - config.getConfig().set("region." + id + ".name", name); - config.save(); - - String message = Message.getMessage("region.changed.name"); - message = message.replace("{id}", id); - message = message.replace("{name}", name); - sender.sendMessage(Utils.color(message)); - return true; - } - - if (args[0].equalsIgnoreCase("regionlore")) { - if (args.length < 4) { - sender.sendMessage(Utils.color("&6Themepark&f: &4/" + name + " regionlore[id] [index] [lore]")); - return true; - } - - String id = args[1]; - if (!API.isRegion(id)) { - String message = Message.getMessage("region.not"); - message = message.replace("{id}", id); - sender.sendMessage(Utils.color(message)); - return true; - } - - String string = args[2]; - if(!isInteger(string)) { - sender.sendMessage(Utils.color(Message.getMessage("nonumber").replace("{number}", string))); - return true; - } - - int i = Integer.parseInt(string); - - String lore = args[3]; - lore = lore.replaceAll("_", " "); - - Region region = API.getRegion(id); - if(region.getLore().size() > i && region.getLore().get(i).equals(lore)) - return true; - - region.setLore(i, lore); - config.getConfig().set("region." + id + ".lore", region.getLore()); - config.save(); - - String message = Message.getMessage("region.changed.lore"); - message = message.replace("{id}", id); - message = message.replace("{index}", "" + i); - message = message.replace("{lore}", lore); - sender.sendMessage(Utils.color(message)); - return true; - } - - if (args[0].equalsIgnoreCase("setlocation")) { - if (args.length < 2 ) { - sender.sendMessage(Utils.color("&6Themepark&f: &4/" + name + " setlocation [id]")); - return true; - } - - String id = args[1]; - if(!API.isAttraction(id)) { - String message = Message.getMessage("attraction.not"); - message = message.replace("{id}", id); - sender.sendMessage(Utils.color(message)); - return true; - } - - if(!(sender instanceof Player)) { - sender.sendMessage(Utils.color(Message.getMessage("onlyplayers"))); - return true; - } - - Location location = ((Player) sender).getLocation(); - config.getConfig().set("attraction." + id + ".location", LocationUtils.toString(location)); - config.save(); - - Attraction attraction = API.getAttraction(id); - attraction.setLocation(location.clone()); - MapMarker.getMarker().setAttractionMarker(attraction); - sender.sendMessage(Utils.color(Message.getMessage("attraction.location").replace("{name}", attraction.getName()))); - return true; - } - - if (args[0].equalsIgnoreCase("attraction")) { - if (args.length < 3 ) { - sender.sendMessage(Utils.color("&6Themepark&f: &4/" + name + " attraction [id] [status]")); - return true; - } - - String id = args[1]; - if (!API.isAttraction(id)) { - String message = Message.getMessage("attraction.not"); - message = message.replace("{id}", id); - sender.sendMessage(Utils.color(message)); - return true; - } - - Status status = Status.getStatus(args[2]); - if (status == null) { - String message = Message.getMessage("status.not"); - message = message.replace("{status}", args[2]); - sender.sendMessage(Utils.color(message)); - return true; - } - - Attraction attraction = API.getAttraction(id); - if(!attraction.getType().containsStatus(status)) { - String message = Message.getMessage("attraction.nostatus"); - message = message.replace("{name}", attraction.getName()); - message = message.replace("{status}", status.toString()); - sender.sendMessage(Utils.color(message)); - return true; - } - - if(attraction.getStatus() == status) - return true; - - Player player = (sender instanceof Player) ? (Player) sender : null; - - attraction.setStatus(status, player); - config.getConfig().set("attraction." + id + ".status", status.toString()); - config.save(); - return true; - } - - return false; - } - - private boolean isInteger(String s) { - int radix = 10; - Scanner sc = new Scanner(s.trim()); - if(!sc.hasNextInt(radix)) return false; - sc.nextInt(radix); - return !sc.hasNext(); - } - -} diff --git a/src/main/java/me/paradoxpixel/themepark/database/Database.java b/src/main/java/me/paradoxpixel/themepark/database/Database.java deleted file mode 100644 index ac544f8..0000000 --- a/src/main/java/me/paradoxpixel/themepark/database/Database.java +++ /dev/null @@ -1,132 +0,0 @@ -package me.paradoxpixel.themepark.database; - -import me.paradoxpixel.themepark.ThemeParkPlugin; -import me.paradoxpixel.themepark.config.YamlConfig; -import org.bukkit.Bukkit; -import java.sql.Connection; -import java.sql.DriverManager; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.util.HashMap; -import java.util.Map; - -public class Database { - - public Database(YamlConfig config) { - if(config == null) { - enabled = false; - return; - } - - enabled = config.getConfig().getBoolean("mysql.enabled"); - String host = config.getConfig().getString("mysql.host"); - int port = config.getConfig().getInt("mysql.port"); - String database = config.getConfig().getString("mysql.database"); - user = config.getConfig().getString("mysql.user"); - password = config.getConfig().getString("mysql.password"); - url = config.getConfig().getString("mysql.url"); - - url = url.replace("%host%", host); - url = url.replace("%port%", Integer.toString(port)); - url = url.replace("%database%", database); - } - - private boolean enabled; - private String user, password, url; - - private Connection connection; - private int TaskID; - - public boolean isEnabled() { - return enabled; - } - - public void connect() { - try { - Class.forName("com.mysql.jdbc.Driver").newInstance(); - connection = DriverManager.getConnection(url, user, password); - - startPool(); - } catch(Exception e) { - enabled = false; - e.printStackTrace(); - } - } - - public void disconnect() { - if(connection == null) - return; - - try { - connection.close(); - connection = null; - } catch(Exception e) { - e.printStackTrace(); - } - } - - private boolean connected() { - if(connection == null) - return false; - - try { - return !connection.isClosed(); - } catch(Exception e) { - e.printStackTrace(); - } - - return false; - } - - private void startPool() { - TaskID = Bukkit.getScheduler().runTaskLater(ThemeParkPlugin.getInstance(), new Runnable() { - @Override - public void run() { - disconnect(); - TaskID = 0; - } - }, 1200L).getTaskId(); - } - - private void stopPool() { - if(TaskID == 0) - return; - - Bukkit.getScheduler().cancelTask(TaskID); - TaskID = 0; - } - - public boolean execute(String query, HashMap objects) { - if(query == null || query.isEmpty()) - return false; - - if(!enabled) - return false; - - if(!connected()) - connect(); - - if(!connected()) - return false; - - stopPool(); - try { - PreparedStatement statement = connection.prepareStatement(query); - if(objects != null) - for(Map.Entry entry : objects.entrySet()) - statement.setObject(entry.getKey(), entry.getValue()); - - boolean b = statement.execute(); - statement.close(); - startPool(); - - return b; - } catch(Exception e) { - e.printStackTrace(); - } - - startPool(); - return false; - } - -} diff --git a/src/main/java/me/paradoxpixel/themepark/database/DatabaseHandler.java b/src/main/java/me/paradoxpixel/themepark/database/DatabaseHandler.java deleted file mode 100644 index 6967e1b..0000000 --- a/src/main/java/me/paradoxpixel/themepark/database/DatabaseHandler.java +++ /dev/null @@ -1,101 +0,0 @@ -package me.paradoxpixel.themepark.database; - -import me.paradoxpixel.themepark.api.API; -import me.paradoxpixel.themepark.api.attraction.Attraction; -import me.paradoxpixel.themepark.api.attraction.Region; -import me.paradoxpixel.themepark.api.attraction.component.Status; -import me.paradoxpixel.themepark.attraction.status.StatusManager; -import me.paradoxpixel.themepark.config.YamlConfig; - -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.HashMap; -import java.util.UUID; - -public class DatabaseHandler { - - private Database database; - - public DatabaseHandler(YamlConfig config) { - database = new Database(config); - load(); - } - - public void load() { - database.execute("CREATE TABLE IF NOT EXISTS attraction (id VARCHAR(256) NOT NULL, name VARCHAR(256) NOT NULL, region_id VARCHAR(256) NOT NULL, type VARCHAR(256) NOT NULL, status VARCHAR(256) NOT NULL, PRIMARY KEY(id))", null); - database.execute("CREATE TABLE IF NOT EXISTS region (id VARCHAR(256) NOT NULL, name VARCHAR(256) NOT NULL, PRIMARY KEY(id))", null); - database.execute("CREATE TABLE IF NOT EXISTS status (statusId VARCHAR(256) NOT NULL, statusName VARCHAR(256) NOT NULL)", null); - - database.execute("TRUNCATE TABLE attraction", null); - database.execute("TRUNCATE TABLE region", null); - database.execute("TRUNCATE TABLE status", null); - - for(Attraction attraction : API.getAttractions().values()) - addAttraction(attraction); - - for(Region region : API.getRegions().values()) - addRegion(region); - - for(Status status : Status.values()) { - HashMap objects = new HashMap<>(); - objects.put(1, status.toString()); - objects.put(2, StatusManager.getName(status)); - database.execute("INSERT INTO status (statusId,statusName) VALUES(?,?)", objects); - } - } - - public void addAttraction(Attraction attraction) { - HashMap objects = new HashMap<>(); - objects.put(1, attraction.getId()); - objects.put(2, attraction.getName()); - objects.put(3, attraction.getRegion_id()); - objects.put(4, attraction.getType().toString()); - objects.put(5, attraction.getStatus().toString()); - - database.execute("INSERT IGNORE INTO attraction(id, name, region_id, type, status) VALUES (?,?,?,?,?)", objects); - } - - public void updateAttraction(Attraction attraction) { - HashMap objects = new HashMap<>(); - objects.put(1, attraction.getStatus().toString()); - objects.put(2, attraction.getId()); - - database.execute("UPDATE attraction SET status=? WHERE id=?", objects); - } - - public void removeAttraction(Attraction attraction) { - HashMap objects = new HashMap<>(); - objects.put(1, attraction.getId()); - - database.execute("DELETE FROM attraction WHERE id=?", objects); - database.execute("DELETE FROM ridecount WHERE attractionId=?", objects); - } - - public void addRegion(Region region) { - HashMap objects = new HashMap<>(); - objects.put(1, region.getId()); - objects.put(2, region.getName()); - - database.execute("INSERT IGNORE INTO region(id, name) VALUES (?,?)", objects); - } - - public void updateRegion(Region region) { - HashMap objects = new HashMap<>(); - objects.put(1, region.getName()); - objects.put(2, region.getId()); - - database.execute("UPDATE region SET name=? WHERE id=?", objects); - } - - public void removeRegion(Region region) { - HashMap objects = new HashMap<>(); - objects.put(1, region.getId()); - - database.execute("DELETE FROM region WHERE id=?", objects); - } - - public void disconnect() { - database.disconnect(); - } - -} diff --git a/src/main/java/me/paradoxpixel/themepark/gui/GUI.java b/src/main/java/me/paradoxpixel/themepark/gui/GUI.java deleted file mode 100644 index 6a1d572..0000000 --- a/src/main/java/me/paradoxpixel/themepark/gui/GUI.java +++ /dev/null @@ -1,120 +0,0 @@ -package me.paradoxpixel.themepark.gui; - -import me.paradoxpixel.themepark.utils.Utils; -import org.bukkit.Bukkit; -import org.bukkit.Material; -import org.bukkit.entity.Player; -import org.bukkit.event.inventory.ClickType; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; - -public class GUI extends GUIHolder { - - private String title; - private int size; - private Inventory inventory; - private HashMap items = new HashMap<>(); - - public GUI(String title, int size) { - if(size > 54) - size = 54; - - this.title = title; - this.size = Utils.toSize(size, 9); - inventory = Bukkit.createInventory(this, this.size, Utils.color(title)); - } - - private void update() { - ArrayList toRemove = new ArrayList<>(); - for(Map.Entry entry : items.entrySet()) { - if(entry.getKey() >= getSize()) { - toRemove.add(entry.getKey()); - continue; - } - - inventory.setItem(entry.getKey(), entry.getValue().getItem()); - } - - for(int i : toRemove) - items.remove(i); - } - - public void setSize(int size) { - if(size > 54) - size = 54; - - this.size = Utils.toSize(size, 9); - inventory = Bukkit.createInventory(this, this.size, Utils.color(title)); - update(); - } - - public int getSize() { - return size; - } - - public void clear() { - inventory = Bukkit.createInventory(this, Utils.toSize(size, 9), Utils.color(title)); - items.clear(); - } - - public void addItems(HashMap items) { - for(Map.Entry entry : items.entrySet()) - addItem(entry.getKey(), entry.getValue()); - } - - public void addItem(int slot, GUIItem item) { - if(slot >= getSize() || slot < 0) - return; - - items.put(slot, item); - inventory.setItem(slot, item.getItem()); - } - - public void removeItem(int slot) { - items.remove(slot); - } - - public void switchSlot(int from, int to) { - if(!items.containsKey(from)) - return; - - GUIItem item = items.get(from); - items.put(to, item); - items.remove(from); - } - - public boolean hasItem(int slot) { - return items.containsKey(slot); - } - - @Override - public boolean execute(Player player, int slot, ClickType type) { - if(player == null || slot >= getSize() || slot < 0 || type == null) - return false; - - if(!items.containsKey(slot)) - return false; - - GUIItem item = items.get(slot); - if(type == ClickType.RIGHT) { - if(item.getActionRight() != null) - item.getActionRight().click(player); - - return item.hasCancelRight(); - } else { - if(item.getActionLeft() != null) - item.getActionLeft().click(player); - - return item.hasCancelLeft(); - } - } - - @Override - public Inventory getInventory() { - return inventory; - } - -} diff --git a/src/main/java/me/paradoxpixel/themepark/gui/GUIAction.java b/src/main/java/me/paradoxpixel/themepark/gui/GUIAction.java deleted file mode 100644 index 539ef03..0000000 --- a/src/main/java/me/paradoxpixel/themepark/gui/GUIAction.java +++ /dev/null @@ -1,9 +0,0 @@ -package me.paradoxpixel.themepark.gui; - -import org.bukkit.entity.Player; - -public abstract class GUIAction { - - public abstract void click(Player player); - -} diff --git a/src/main/java/me/paradoxpixel/themepark/gui/GUIHolder.java b/src/main/java/me/paradoxpixel/themepark/gui/GUIHolder.java deleted file mode 100644 index 4477573..0000000 --- a/src/main/java/me/paradoxpixel/themepark/gui/GUIHolder.java +++ /dev/null @@ -1,11 +0,0 @@ -package me.paradoxpixel.themepark.gui; - -import org.bukkit.entity.Player; -import org.bukkit.event.inventory.ClickType; -import org.bukkit.inventory.InventoryHolder; - -public abstract class GUIHolder implements InventoryHolder { - - public abstract boolean execute(Player player, int slot, ClickType type); - -} diff --git a/src/main/java/me/paradoxpixel/themepark/gui/GUIItem.java b/src/main/java/me/paradoxpixel/themepark/gui/GUIItem.java deleted file mode 100644 index 62a8382..0000000 --- a/src/main/java/me/paradoxpixel/themepark/gui/GUIItem.java +++ /dev/null @@ -1,45 +0,0 @@ -package me.paradoxpixel.themepark.gui; - -import org.bukkit.inventory.ItemStack; - -public class GUIItem { - - private ItemStack item; - private GUIAction aleft, aright; - private boolean cleft, cright; - - public GUIItem(ItemStack item, GUIAction action, boolean cancel) { - this.item = item; - aleft = aright = action; - cleft = cright = cancel; - } - - public GUIItem(ItemStack item, GUIAction aleft, GUIAction aright, boolean cleft, boolean cright) { - this.item = item; - this.aleft = aleft; - this.aright = aright; - this.cleft = cleft; - this.cright = cright; - } - - public ItemStack getItem() { - return item; - } - - public boolean hasCancelLeft() { - return cleft; - } - - public boolean hasCancelRight() { - return cright; - } - - public GUIAction getActionLeft() { - return aleft; - } - - public GUIAction getActionRight() { - return aright; - } - -} diff --git a/src/main/java/me/paradoxpixel/themepark/gui/GUIListener.java b/src/main/java/me/paradoxpixel/themepark/gui/GUIListener.java deleted file mode 100644 index 2855bc5..0000000 --- a/src/main/java/me/paradoxpixel/themepark/gui/GUIListener.java +++ /dev/null @@ -1,41 +0,0 @@ -package me.paradoxpixel.themepark.gui; - -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.inventory.*; -import org.bukkit.inventory.Inventory; - -public class GUIListener implements Listener { - - @EventHandler(priority = EventPriority.HIGHEST) - public void onClick(InventoryClickEvent e) { - if(e.getClickedInventory() == null) - return; - - Inventory inventory = e.getClickedInventory(); - if(!(inventory.getHolder() instanceof GUIHolder)) - return; - - Player player = (Player) e.getWhoClicked(); - int slot = e.getRawSlot(); - ClickType type = e.getClick(); - - GUI gui = (GUI) inventory.getHolder(); - gui.execute(player, slot, type); - } - - @EventHandler(priority = EventPriority.HIGHEST) - public void onMove(InventoryClickEvent e) { - if(e.getInventory() == null) - return; - - Inventory inventory = e.getInventory(); - if(!(inventory.getHolder() instanceof GUIHolder)) - return; - - e.setCancelled(true); - } - -} diff --git a/src/main/java/me/paradoxpixel/themepark/listener/ChangeListener.java b/src/main/java/me/paradoxpixel/themepark/listener/ChangeListener.java deleted file mode 100644 index b168302..0000000 --- a/src/main/java/me/paradoxpixel/themepark/listener/ChangeListener.java +++ /dev/null @@ -1,101 +0,0 @@ -package me.paradoxpixel.themepark.listener; - -import me.paradoxpixel.themepark.ThemeParkPlugin; -import me.paradoxpixel.themepark.api.attraction.Attraction; -import me.paradoxpixel.themepark.api.attraction.component.Status; -import me.paradoxpixel.themepark.api.event.attraction.AddAttractionEvent; -import me.paradoxpixel.themepark.api.event.attraction.RemoveAttractionEvent; -import me.paradoxpixel.themepark.api.event.attraction.StatusChangeEvent; -import me.paradoxpixel.themepark.api.event.region.AddRegionEvent; -import me.paradoxpixel.themepark.api.event.region.ChangeRegionEvent; -import me.paradoxpixel.themepark.api.event.region.RemoveRegionEvent; -import me.paradoxpixel.themepark.attraction.AttractionMenu; -import me.paradoxpixel.themepark.attraction.MapMarker; -import me.paradoxpixel.themepark.attraction.sign.SignManager; -import me.paradoxpixel.themepark.attraction.status.StatusManager; -import me.paradoxpixel.themepark.config.YamlConfig; -import me.paradoxpixel.themepark.utils.Message; -import me.paradoxpixel.themepark.utils.Utils; -import org.bukkit.Bukkit; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; - -public class ChangeListener implements Listener { - - @EventHandler - public void onChange(StatusChangeEvent e) { - if(e.getAttraction() == null) - return; - - SignManager.update(e.getAttraction()); - - Attraction attraction = e.getAttraction(); - AttractionMenu.update(attraction); - - Status status = e.getStatusAfter(); - String message = Message.getMessage("attraction.changed.status." + status.toString()); - message = message.replace("{name}", attraction.getName()); - message = message.replace("{status}", StatusManager.getName(status)); - Bukkit.broadcastMessage(Utils.color(message)); - - Bukkit.getScheduler().runTaskAsynchronously(ThemeParkPlugin.getInstance(), () -> ThemeParkPlugin.getInstance().getDatabaseHandler().updateAttraction(e.getAttraction())); - } - - @EventHandler - public void onChange(AddAttractionEvent e) { - if(e.getAttraction() == null) - return; - - AttractionMenu.reload(); - SignManager.loadSigns(e.getAttraction()); - Bukkit.getScheduler().runTaskAsynchronously(ThemeParkPlugin.getInstance(), () -> ThemeParkPlugin.getInstance().getDatabaseHandler().addAttraction(e.getAttraction())); - } - - @EventHandler - public void onChange(RemoveAttractionEvent e) { - if(e.getAttraction() == null) - return; - - AttractionMenu.reload(); - SignManager.remove(e.getAttraction()); - MapMarker.getMarker().removeAttractionMarker(e.getAttraction()); - YamlConfig config = ThemeParkPlugin.getInstance().getAttraction(); - config.getConfig().set("attraction." + e.getAttraction().getId(), null); - config.save(); - - Bukkit.getScheduler().runTaskAsynchronously(ThemeParkPlugin.getInstance(), () -> ThemeParkPlugin.getInstance().getDatabaseHandler().removeAttraction(e.getAttraction())); - } - - @EventHandler - public void onChange(AddRegionEvent e) { - if(e.getRegion() == null) - return; - - AttractionMenu.reload(); - Bukkit.getScheduler().runTaskAsynchronously(ThemeParkPlugin.getInstance(), () -> ThemeParkPlugin.getInstance().getDatabaseHandler().addRegion(e.getRegion())); - } - - @EventHandler - public void onChange(ChangeRegionEvent e) { - if(e.getRegion() == null) - return; - - AttractionMenu.reload(); - Bukkit.getScheduler().runTaskAsynchronously(ThemeParkPlugin.getInstance(), () -> ThemeParkPlugin.getInstance().getDatabaseHandler().updateRegion(e.getRegion())); - } - - @EventHandler - public void onChange(RemoveRegionEvent e) { - if(e.getRegion() == null) - return; - - AttractionMenu.reload(); - YamlConfig config = ThemeParkPlugin.getInstance().getAttraction(); - config.getConfig().set("region." + e.getRegion().getId(), null); - config.save(); - - Bukkit.getScheduler().runTaskAsynchronously(ThemeParkPlugin.getInstance(), () -> ThemeParkPlugin.getInstance().getDatabaseHandler().removeRegion(e.getRegion())); - } - - -} diff --git a/src/main/java/me/paradoxpixel/themepark/listener/ClickListener.java b/src/main/java/me/paradoxpixel/themepark/listener/ClickListener.java deleted file mode 100644 index 6a95676..0000000 --- a/src/main/java/me/paradoxpixel/themepark/listener/ClickListener.java +++ /dev/null @@ -1,45 +0,0 @@ -package me.paradoxpixel.themepark.listener; - -import me.paradoxpixel.themepark.ThemeParkPlugin; -import me.paradoxpixel.themepark.attraction.AttractionMenu; -import me.paradoxpixel.themepark.config.YamlConfig; -import me.paradoxpixel.themepark.utils.Utils; -import org.bukkit.Material; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.block.Action; -import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.inventory.ItemStack; - -public class ClickListener implements Listener { - - private YamlConfig config = ThemeParkPlugin.getInstance().getSettings(); - - @EventHandler - public void onInteract(PlayerInteractEvent e) { - Material material = Material.getMaterial(config.getConfig().getString("item.material")); - String name = Utils.color(config.getConfig().getString("item.display-name")); - if(e.getItem() == null) - return; - - ItemStack item = e.getItem(); - if(item.getType() != material) - return; - - if(!item.hasItemMeta()) - return; - - if(!item.getItemMeta().getDisplayName().equals(name)) - return; - - if(e.getAction() == Action.LEFT_CLICK_AIR || - e.getAction() == Action.LEFT_CLICK_BLOCK) - return; - - e.setCancelled(true); - Player player = e.getPlayer(); - AttractionMenu.openMenu(player, 1); - } - -} diff --git a/src/main/java/me/paradoxpixel/themepark/listener/PlayerListener.java b/src/main/java/me/paradoxpixel/themepark/listener/PlayerListener.java deleted file mode 100644 index ac71e64..0000000 --- a/src/main/java/me/paradoxpixel/themepark/listener/PlayerListener.java +++ /dev/null @@ -1,90 +0,0 @@ -package me.paradoxpixel.themepark.listener; - -import me.paradoxpixel.themepark.ThemeParkPlugin; -import me.paradoxpixel.themepark.config.YamlConfig; -import me.paradoxpixel.themepark.database.DatabaseHandler; -import me.paradoxpixel.themepark.utils.ItemBuilder; -import me.paradoxpixel.themepark.utils.Utils; -import org.bukkit.Material; -import org.bukkit.entity.Minecart; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.player.PlayerChangedWorldEvent; -import org.bukkit.event.player.PlayerJoinEvent; -import org.bukkit.event.player.PlayerTeleportEvent; -import org.bukkit.inventory.ItemStack; - -import java.util.List; - -public class PlayerListener implements Listener { - - private YamlConfig config = ThemeParkPlugin.getInstance().getData(); - private YamlConfig settings = ThemeParkPlugin.getInstance().getSettings(); - - @EventHandler - public void onJoin(PlayerJoinEvent e) { - doItem(e.getPlayer()); - } - - @EventHandler - public void onSwitch(PlayerChangedWorldEvent e) { - doItem(e.getPlayer()); - } - - @EventHandler - public void onTeleport(PlayerTeleportEvent e) { - Player player = e.getPlayer(); - if(e.getCause() != PlayerTeleportEvent.TeleportCause.PLUGIN) - return; - - if(!player.isInsideVehicle()) - return; - - if(player.getVehicle() == null) - return; - - if(!(player.getVehicle() instanceof Minecart)) - return; - - e.setCancelled(true); - player.sendMessage(Utils.color("&6ThemePark&f: &4You can't teleport while in a minecart")); - } - - public void doItem(Player player) { - if(!settings.getConfig().getBoolean("item.enabled")) - return; - - Material material = Material.getMaterial(settings.getConfig().getString("item.material")); - String name = Utils.color(settings.getConfig().getString("item.display-name")); - int slot = settings.getConfig().getInt("item.slot"); - if(material == null || name.isEmpty()) - return; - - boolean b = true; - if(config.getConfig().contains(player.getUniqueId().toString() + ".item")) { - b = config.getConfig().getBoolean(player.getUniqueId().toString() + ".item"); - } else { - config.getConfig().set(player.getUniqueId().toString() + ".item", true); - config.save(); - } - - if(b) { - if(settings.getConfig().getBoolean("inventory.clear")) - player.getInventory().clear(); - - List worlds = settings.getConfig().getStringList("item.worlds"); - if(worlds != null && !worlds.isEmpty()) { - if (worlds.contains(player.getLocation().getWorld().getName())) { - player.getInventory().setItem(slot, new ItemStack(Material.AIR)); - return; - } - } - - ItemBuilder builder = new ItemBuilder(material); - builder.setName(name); - player.getInventory().setItem(slot, builder.getItem()); - } - } - -} \ No newline at end of file diff --git a/src/main/java/me/paradoxpixel/themepark/listener/SignListener.java b/src/main/java/me/paradoxpixel/themepark/listener/SignListener.java deleted file mode 100644 index c2b46dc..0000000 --- a/src/main/java/me/paradoxpixel/themepark/listener/SignListener.java +++ /dev/null @@ -1,64 +0,0 @@ -package me.paradoxpixel.themepark.listener; - -import me.paradoxpixel.themepark.ThemeParkPlugin; -import me.paradoxpixel.themepark.api.API; -import me.paradoxpixel.themepark.api.attraction.Attraction; -import me.paradoxpixel.themepark.attraction.sign.SignManager; -import me.paradoxpixel.themepark.attraction.sign.StatusSign; -import me.paradoxpixel.themepark.attraction.status.StatusManager; -import me.paradoxpixel.themepark.config.YamlConfig; -import me.paradoxpixel.themepark.utils.Utils; -import org.bukkit.Location; -import org.bukkit.block.Block; -import org.bukkit.block.Sign; -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.event.block.BlockBreakEvent; -import org.bukkit.event.block.SignChangeEvent; - -public class SignListener implements Listener { - - private YamlConfig config = ThemeParkPlugin.getInstance().getSettings(); - private String name = config.getConfig().getString("sign.name"); - private String title = config.getConfig().getString("sign.title"); - - @EventHandler - public void onPlace(SignChangeEvent e) { - if(!e.getLine(0).equalsIgnoreCase(name)) - return; - - String id = e.getLine(1); - if(id == null || id.isEmpty() || !API.isAttraction(id)) - return; - - Attraction attraction = API.getAttraction(id); - e.setLine(0, Utils.color(title)); - e.setLine(1, Utils.color(attraction.getName())); - e.setLine(2, Utils.color(StatusManager.getName(attraction.getStatus()))); - - Location location = e.getBlock().getLocation(); - StatusSign statusSign = new StatusSign(attraction, location); - SignManager.addSign(statusSign); - } - - @EventHandler - public void onBreak(BlockBreakEvent e) { - if(e.getBlock() == null) - return; - - Block block = e.getBlock(); - if(!(block.getState() instanceof Sign)) - return; - - Sign sign = (Sign) block.getState(); - if(!sign.getLine(0).equals(Utils.color(title))) - return; - - Attraction attraction = API.getAttractionFromName(sign.getLine(1)); - Location location = e.getBlock().getLocation(); - - StatusSign s = new StatusSign(attraction, location); - SignManager.removeSign(s); - } - -} diff --git a/src/main/java/me/paradoxpixel/themepark/utils/Message.java b/src/main/java/me/paradoxpixel/themepark/utils/Message.java deleted file mode 100644 index 204ada2..0000000 --- a/src/main/java/me/paradoxpixel/themepark/utils/Message.java +++ /dev/null @@ -1,25 +0,0 @@ -package me.paradoxpixel.themepark.utils; - -import me.paradoxpixel.themepark.ThemeParkPlugin; -import me.paradoxpixel.themepark.config.YamlConfig; - -public class Message { - - private static YamlConfig config = ThemeParkPlugin.getInstance().getMessage(); - - public static boolean isMessage(String string) { - return config.getConfig().contains(string); - } - - public static String getMessage(String string) { - if(!isMessage(string)) - return ""; - - String message = config.getConfig().getString(string); - if(isMessage("prefix")) - message = message.replace("{prefix}", config.getConfig().getString("prefix")); - - return message; - } - -} diff --git a/src/main/java/me/paradoxpixel/themepark/utils/Utils.java b/src/main/java/me/paradoxpixel/themepark/utils/Utils.java deleted file mode 100644 index 1116260..0000000 --- a/src/main/java/me/paradoxpixel/themepark/utils/Utils.java +++ /dev/null @@ -1,15 +0,0 @@ -package me.paradoxpixel.themepark.utils; - -import org.bukkit.ChatColor; - -public class Utils { - - public static String color(String string) { - return ChatColor.translateAlternateColorCodes('&', string); - } - - public static int toSize(int i, int size) { - return i % size == 0 ? i : i + (size - (i % size)); - } - -} diff --git a/src/main/java/nl/iobyte/themepark/ThemeParkPlugin.java b/src/main/java/nl/iobyte/themepark/ThemeParkPlugin.java new file mode 100644 index 0000000..9064b15 --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/ThemeParkPlugin.java @@ -0,0 +1,139 @@ +package nl.iobyte.themepark; + +import nl.iobyte.menuapi.MenuAPI; +import nl.iobyte.themepark.command.MainCommand; +import nl.iobyte.themepark.command.MenuCommand; +import nl.iobyte.themepark.command.RidecountCommand; +import nl.iobyte.themepark.config.YamlConfig; +import nl.iobyte.themepark.database.DB; +import nl.iobyte.themepark.database.DBManager; +import nl.iobyte.themepark.database.MySQL; +import nl.iobyte.themepark.database.SQLite; +import nl.iobyte.themepark.listener.AttractionListener; +import nl.iobyte.themepark.listener.PlayerListener; +import nl.iobyte.themepark.listener.RegionListener; +import nl.iobyte.themepark.menu.AttractionLoader; +import nl.iobyte.workchain.thread.WorkThreader; +import org.bukkit.Bukkit; +import org.bukkit.command.CommandMap; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.plugin.PluginManager; +import org.bukkit.plugin.java.JavaPlugin; +import java.lang.reflect.Field; + +public class ThemeParkPlugin extends JavaPlugin { + + private static ThemeParkPlugin instance; + private YamlConfig settings, messages, data, regions; + private WorkThreader threader; + + public void onEnable() { + instance = this; + loadConfigs(); + loadDatabases(); + loadWorkers(); + loadListeners(); + loadCommands(); + AttractionLoader.load(); + } + + private void loadConfigs() { + settings = new YamlConfig("settings", this); + messages = new YamlConfig("messages", this); + data = new YamlConfig("data", this); + regions = new YamlConfig("regions", this); + } + + private void loadDatabases() { + DBManager.addDatabase("data", new SQLite("data")); + + FileConfiguration fc = settings.getConfig(); + if(fc.getBoolean("mysql.enabled")) { + String host = fc.getString("mysql.host"); + int port = fc.getInt("mysql.port"); + String database = fc.getString("mysql.database"); + String username = fc.getString("mysql.user"); + String password = fc.getString("mysql.pass"); + DBManager.addDatabase("public", new MySQL(host, port, database, username, password)); + } + + loadTables(); + } + + private void loadTables() { + DB db = DBManager.getDatabase("data"); + DBManager.createTable(db, "CREATE TABLE IF NOT EXISTS counts(uuid VARCHAR(255) NOT NULL, attraction_id VARCHAR(255) NOT NULL, points BIGINT(20), created_at TEXT DEFAULT CURRENT_DATE, UNIQUE(uuid, attraction_id))"); + + db = DBManager.getDatabase("public"); + if(db == null) + return; + + DBManager.createTable(db, "CREATE TABLE IF NOT EXISTS attraction (id VARCHAR(256) NOT NULL, name VARCHAR(256) NOT NULL, region_id VARCHAR(256) NOT NULL, type VARCHAR(256) NOT NULL, status VARCHAR(256) NOT NULL, PRIMARY KEY(id))"); + DBManager.createTable(db, "CREATE TABLE IF NOT EXISTS region (id VARCHAR(256) NOT NULL, name VARCHAR(256) NOT NULL, PRIMARY KEY(id))"); + DBManager.createTable(db, "CREATE TABLE IF NOT EXISTS status (statusId VARCHAR(256) NOT NULL, statusName VARCHAR(256) NOT NULL)"); + } + + private void loadWorkers() { + int workers = settings.getConfig().getInt("workers"); + if(workers < 1) + workers = 1; + + threader = new WorkThreader(workers); + threader.createWorkers(); + threader.start(); + } + + private void loadListeners() { + PluginManager pm = Bukkit.getPluginManager(); + pm.registerEvents(new PlayerListener(), this); + pm.registerEvents(new AttractionListener(), this); + pm.registerEvents(new RegionListener(), this); + MenuAPI.register(this); + } + + private void loadCommands() { + getCommand("status").setExecutor(new MenuCommand()); + getCommand("ridecount").setExecutor(new RidecountCommand()); + String command = settings.getConfig().getString("command"); + if(command == null || command.isEmpty()) + return; + + try { + final Field bukkitCommandMap = Bukkit.getServer().getClass().getDeclaredField("commandMap"); + + bukkitCommandMap.setAccessible(true); + CommandMap commandMap = (CommandMap) bukkitCommandMap.get(Bukkit.getServer()); + + commandMap.register(command, new MainCommand(command)); + } catch(ReflectiveOperationException e) { + e.printStackTrace(); + } + } + + public void onDisable() { + threader.stop(); + DBManager.shutdown(); + instance = null; + } + + public static ThemeParkPlugin getInstance() { + return instance; + } + + public YamlConfig getSettings() { + return settings; + } + + public YamlConfig getMessages() { + return messages; + } + + public YamlConfig getData() { + return data; + } + + public YamlConfig getRegions() { + return regions; + } + +} diff --git a/src/main/java/nl/iobyte/themepark/api/API.java b/src/main/java/nl/iobyte/themepark/api/API.java new file mode 100644 index 0000000..10e9514 --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/api/API.java @@ -0,0 +1,110 @@ +package nl.iobyte.themepark.api; + +import nl.iobyte.themepark.api.attraction.Attraction; +import nl.iobyte.themepark.api.attraction.Region; +import nl.iobyte.themepark.api.ridecount.CountManager; +import org.bukkit.ChatColor; +import java.util.LinkedHashMap; + +public class API { + + private static LinkedHashMap regions = new LinkedHashMap<>(); + private static LinkedHashMap attractions = new LinkedHashMap<>(); + + public static void addRegion(Region region) { + if(isRegion(region.getId())) + return; + + regions.put(region.getId(), region); + } + + public static boolean isRegion(String id) { + if(id == null || id.isEmpty()) + return false; + + return regions.containsKey(id); + } + + public static Region getRegion(String id) { + if(id == null || id.isEmpty()) + return null; + + return regions.get(id); + } + + public static LinkedHashMap getRegions() { + return regions; + } + + public static void removeRegion(String id) { + Region region = getRegion(id); + if(region == null) + return; + + for(Attraction attraction : region.getAttractions().values()) + attractions.remove(attraction.getId()); + + regions.remove(id); + } + + public static void addAttraction(Attraction attraction) { + if(!isRegion(attraction.getRegionId())) + return; + + if(isAttraction(attraction.getId())) + return; + + getRegion(attraction.getRegionId()).addAttraction(attraction); + attractions.put(attraction.getId(), attraction); + CountManager.addCounter(attraction); + } + + public static boolean isAttraction(String id) { + if(id == null || id.isEmpty()) + return false; + + return attractions.containsKey(id); + } + + public static Attraction getAttraction(String id) { + if(id == null || id.isEmpty()) + return null; + + return attractions.get(id); + } + + public static Attraction getAttractionFromName(String name) { + if(name == null || name.isEmpty()) + return null; + + for(Attraction attraction : attractions.values()) { + if(!color(attraction.getName()).equals(name)) + continue; + + return attraction; + } + + return null; + } + + public static LinkedHashMap getAttractions() { + return attractions; + } + + public static void removeAttraction(String id) { + if(!isAttraction(id)) + return; + + Attraction attraction = getAttraction(id); + if(isRegion(attraction.getRegionId())) + getRegion(attraction.getRegionId()).removeAttraction(attraction); + + attractions.remove(id); + CountManager.removeCounter(id); + } + + private static String color(String string) { + return ChatColor.translateAlternateColorCodes('&', string); + } + +} diff --git a/src/main/java/nl/iobyte/themepark/api/attraction/Attraction.java b/src/main/java/nl/iobyte/themepark/api/attraction/Attraction.java new file mode 100644 index 0000000..9e70854 --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/api/attraction/Attraction.java @@ -0,0 +1,119 @@ +package nl.iobyte.themepark.api.attraction; + +import nl.iobyte.themepark.api.API; +import nl.iobyte.themepark.api.attraction.component.Status; +import nl.iobyte.themepark.api.attraction.component.Type; +import nl.iobyte.themepark.api.events.attraction.*; +import nl.iobyte.themepark.api.utils.LocationSerializer; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.entity.Player; + +public class Attraction { + + private String id, name, region_id; + private Location location; + private Type type; + private Status status; + + public Attraction(String id, String name, String region_id, Location location, Type type, Status status) { + this.id = id; + this.name = name; + this.region_id = region_id; + this.location = location; + this.type = type; + this.status = type.containsStatus(status) ? status : type.getDefault(); + } + + public String getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + if(this.name.equals(name)) + return; + + String old = this.name; + this.name = name; + ChangeNameEvent e = new ChangeNameEvent(this, old, name); + Bukkit.getPluginManager().callEvent(e); + } + + public String getRegionId() { + return region_id; + } + + public Region getRegion() { + return API.getRegion(region_id); + } + + public Location getLocation() { + return location; + } + + public void setLocation(Location location) { + if(location == this.location) + return; + + if(location == null) + return; + + String loc = LocationSerializer.toString(location); + if(loc == null) + return; + + if(this.location != null) + if(loc.equals(LocationSerializer.toString(location))) + return; + + Location old = this.location; + this.location = location; + ChangeLocationEvent e = new ChangeLocationEvent(this, old, location); + Bukkit.getPluginManager().callEvent(e); + } + + public Type getType() { + return type; + } + + public void setType(Type type) { + Type old = this.type; + this.type = type; + if(!type.containsStatus(status)) + setStatus(type.getDefault(), null); + + ChangeTypeEvent e = new ChangeTypeEvent(this, old, type); + Bukkit.getPluginManager().callEvent(e); + } + + public Status getStatus() { + return status; + } + + public void setStatus(Status status, Player player) { + if(!type.hasStatus()) + return; + + if(!type.containsStatus(status)) + return; + + if(this.status == status) + return; + + Status old = this.status; + + PreChangeStatusEvent e = new PreChangeStatusEvent(this, player, old, status); + Bukkit.getPluginManager().callEvent(e); + if(e.isCancelled()) + return; + + this.status = status; + ChangeStatusEvent ev = new ChangeStatusEvent(this, player, old, status); + Bukkit.getPluginManager().callEvent(ev); + } + +} diff --git a/src/main/java/nl/iobyte/themepark/api/attraction/Region.java b/src/main/java/nl/iobyte/themepark/api/attraction/Region.java new file mode 100644 index 0000000..8a6618e --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/api/attraction/Region.java @@ -0,0 +1,152 @@ +package nl.iobyte.themepark.api.attraction; + +import nl.iobyte.themepark.api.events.region.ChangeDataEvent; +import nl.iobyte.themepark.api.events.region.ChangeLoreEvent; +import nl.iobyte.themepark.api.events.region.ChangeMaterialEvent; +import nl.iobyte.themepark.api.events.region.ChangeNameEvent; +import org.bukkit.Bukkit; +import org.bukkit.Material; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; + +public class Region { + + private String id, name; + private Material material; + private short data; + private List lore; + private LinkedHashMap attractions = new LinkedHashMap<>(); + + public Region(String id, String name, List lore) { + this.id = id; + this.name = name; + this.material = Material.NAME_TAG; + this.data = 0; + this.lore = lore; + } + + public Region(String id, String name, List lore, Material material, short data) { + this.id = id; + this.name = name; + this.material = material; + this.data = data; + this.lore = lore; + } + + public String getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String string) { + if(string == null || string.equals("")) + return; + + if(string.equals(name)) + return; + + String old = name; + name = string; + ChangeNameEvent e = new ChangeNameEvent(this, old, string); + Bukkit.getPluginManager().callEvent(e); + } + + public Material getMaterial() { + return material; + } + + public void setMaterial(Material material) { + if(material == null || material == this.material) + return; + + Material old = this.material; + this.material = material; + ChangeMaterialEvent e = new ChangeMaterialEvent(this, old, material); + Bukkit.getPluginManager().callEvent(e); + } + + public short getData() { + return data; + } + + public void setData(short data) { + if(this.data == data) + return; + + short old = this.data; + this.data = data; + ChangeDataEvent e = new ChangeDataEvent(this, old, data); + Bukkit.getPluginManager().callEvent(e); + } + + public List getLore() { + return lore; + } + + public void setLore(List lore) { + if(this.lore.equals(lore)) + return; + + List old = this.lore; + this.lore = lore; + + ChangeLoreEvent e = new ChangeLoreEvent(this, old, lore); + Bukkit.getPluginManager().callEvent(e); + } + + public void setLore(int i, String string) { + if(lore.size() >= 4 || i >= 4) + return; + + List old = lore; + lore = new ArrayList<>(); + lore.addAll(old); + + if(!string.equals("NULL")) { + if (lore.size() >= i) + for (int a = 0; a <= (i - lore.size()); a++) + lore.add(" "); + + lore.set(i, string); + } else { + lore.remove(i); + } + + ChangeLoreEvent e = new ChangeLoreEvent(this, old, lore); + Bukkit.getPluginManager().callEvent(e); + } + + public LinkedHashMap getAttractions() { + return attractions; + } + + public void setAttractions(LinkedHashMap attractions) { + this.attractions = attractions; + } + + public void addAttraction(Attraction attraction) { + if(attraction == null) + return; + + if(!isAttraction(attraction)) + attractions.put(attraction.getId(), attraction); + } + + public boolean isAttraction(Attraction attraction) { + if(attraction == null) + return false; + + return attractions.containsKey(attraction.getId()); + } + + public void removeAttraction(Attraction attraction) { + if(isAttraction(attraction)) + attractions.remove(attraction.getId()); + } + +} diff --git a/src/main/java/me/paradoxpixel/themepark/api/attraction/component/Status.java b/src/main/java/nl/iobyte/themepark/api/attraction/component/Status.java similarity index 93% rename from src/main/java/me/paradoxpixel/themepark/api/attraction/component/Status.java rename to src/main/java/nl/iobyte/themepark/api/attraction/component/Status.java index ea82f85..357ba57 100644 --- a/src/main/java/me/paradoxpixel/themepark/api/attraction/component/Status.java +++ b/src/main/java/nl/iobyte/themepark/api/attraction/component/Status.java @@ -1,4 +1,4 @@ -package me.paradoxpixel.themepark.api.attraction.component; +package nl.iobyte.themepark.api.attraction.component; public enum Status { diff --git a/src/main/java/me/paradoxpixel/themepark/api/attraction/component/Type.java b/src/main/java/nl/iobyte/themepark/api/attraction/component/Type.java similarity index 95% rename from src/main/java/me/paradoxpixel/themepark/api/attraction/component/Type.java rename to src/main/java/nl/iobyte/themepark/api/attraction/component/Type.java index e0a0465..ef5dbbd 100644 --- a/src/main/java/me/paradoxpixel/themepark/api/attraction/component/Type.java +++ b/src/main/java/nl/iobyte/themepark/api/attraction/component/Type.java @@ -1,4 +1,4 @@ -package me.paradoxpixel.themepark.api.attraction.component; +package nl.iobyte.themepark.api.attraction.component; public enum Type { diff --git a/src/main/java/nl/iobyte/themepark/api/attraction/manager/StatusManager.java b/src/main/java/nl/iobyte/themepark/api/attraction/manager/StatusManager.java new file mode 100644 index 0000000..af438fa --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/api/attraction/manager/StatusManager.java @@ -0,0 +1,43 @@ +package nl.iobyte.themepark.api.attraction.manager; + +import nl.iobyte.themepark.api.attraction.component.Status; +import org.bukkit.Material; +import java.util.HashMap; + +public class StatusManager { + + private static HashMap names = new HashMap<>(); + private static HashMap materials = new HashMap<>(); + private static HashMap datas = new HashMap<>(); + private static HashMap teleports = new HashMap<>(); + + public static void addStatus(Status status, String name, Material material, short data, boolean teleport) { + if(status == null || name == null || material == null || data < 0) + return; + + if(name.isEmpty()) + return; + + names.put(status, name); + materials.put(status, material); + datas.put(status, data); + teleports.put(status, teleport); + } + + public static String getName(Status status) { + return names.get(status); + } + + public static Material getMaterial(Status status) { + return materials.get(status); + } + + public static short getData(Status status) { + return datas.get(status); + } + + public static boolean canTeleport(Status status) { + return teleports.get(status); + } + +} diff --git a/src/main/java/nl/iobyte/themepark/api/events/ChangeEvent.java b/src/main/java/nl/iobyte/themepark/api/events/ChangeEvent.java new file mode 100644 index 0000000..a6fee04 --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/api/events/ChangeEvent.java @@ -0,0 +1,33 @@ +package nl.iobyte.themepark.api.events; + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class ChangeEvent extends Event { + + private static final HandlerList handlers = new HandlerList(); + + private T before, after; + + public ChangeEvent(T before, T after) { + this.before = before; + this.after = after; + } + + public T getBefore() { + return before; + } + + public T getAfter() { + return after; + } + + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + +} diff --git a/src/main/java/nl/iobyte/themepark/api/events/attraction/ChangeLocationEvent.java b/src/main/java/nl/iobyte/themepark/api/events/attraction/ChangeLocationEvent.java new file mode 100644 index 0000000..561373d --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/api/events/attraction/ChangeLocationEvent.java @@ -0,0 +1,20 @@ +package nl.iobyte.themepark.api.events.attraction; + +import nl.iobyte.themepark.api.attraction.Attraction; +import nl.iobyte.themepark.api.events.ChangeEvent; +import org.bukkit.Location; + +public class ChangeLocationEvent extends ChangeEvent { + + private Attraction attraction; + + public ChangeLocationEvent(Attraction attraction, Location before, Location after) { + super(before, after); + this.attraction = attraction; + } + + public Attraction getAttraction() { + return attraction; + } + +} diff --git a/src/main/java/nl/iobyte/themepark/api/events/attraction/ChangeNameEvent.java b/src/main/java/nl/iobyte/themepark/api/events/attraction/ChangeNameEvent.java new file mode 100644 index 0000000..9e7d365 --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/api/events/attraction/ChangeNameEvent.java @@ -0,0 +1,19 @@ +package nl.iobyte.themepark.api.events.attraction; + +import nl.iobyte.themepark.api.attraction.Attraction; +import nl.iobyte.themepark.api.events.ChangeEvent; + +public class ChangeNameEvent extends ChangeEvent { + + private Attraction attraction; + + public ChangeNameEvent(Attraction attraction, String before, String after) { + super(before, after); + this.attraction = attraction; + } + + public Attraction getAttraction() { + return attraction; + } + +} diff --git a/src/main/java/nl/iobyte/themepark/api/events/attraction/ChangeStatusEvent.java b/src/main/java/nl/iobyte/themepark/api/events/attraction/ChangeStatusEvent.java new file mode 100644 index 0000000..51fc33a --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/api/events/attraction/ChangeStatusEvent.java @@ -0,0 +1,27 @@ +package nl.iobyte.themepark.api.events.attraction; + +import nl.iobyte.themepark.api.attraction.Attraction; +import nl.iobyte.themepark.api.attraction.component.Status; +import nl.iobyte.themepark.api.events.ChangeEvent; +import org.bukkit.entity.Player; + +public class ChangeStatusEvent extends ChangeEvent { + + private Attraction attraction; + private Player player; + + public ChangeStatusEvent(Attraction attraction, Player player, Status before, Status after) { + super(before, after); + this.attraction = attraction; + this.player = player; + } + + public Attraction getAttraction() { + return attraction; + } + + public Player getPlayer() { + return player; + } + +} diff --git a/src/main/java/nl/iobyte/themepark/api/events/attraction/ChangeTypeEvent.java b/src/main/java/nl/iobyte/themepark/api/events/attraction/ChangeTypeEvent.java new file mode 100644 index 0000000..7d96891 --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/api/events/attraction/ChangeTypeEvent.java @@ -0,0 +1,20 @@ +package nl.iobyte.themepark.api.events.attraction; + +import nl.iobyte.themepark.api.attraction.Attraction; +import nl.iobyte.themepark.api.attraction.component.Type; +import nl.iobyte.themepark.api.events.ChangeEvent; + +public class ChangeTypeEvent extends ChangeEvent { + + private Attraction attraction; + + public ChangeTypeEvent(Attraction attraction, Type before, Type after) { + super(before, after); + this.attraction = attraction; + } + + public Attraction getAttraction() { + return attraction; + } + +} diff --git a/src/main/java/nl/iobyte/themepark/api/events/attraction/PreChangeStatusEvent.java b/src/main/java/nl/iobyte/themepark/api/events/attraction/PreChangeStatusEvent.java new file mode 100644 index 0000000..5bc302d --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/api/events/attraction/PreChangeStatusEvent.java @@ -0,0 +1,37 @@ +package nl.iobyte.themepark.api.events.attraction; + +import nl.iobyte.themepark.api.attraction.Attraction; +import nl.iobyte.themepark.api.attraction.component.Status; +import nl.iobyte.themepark.api.events.ChangeEvent; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; + +public class PreChangeStatusEvent extends ChangeEvent implements Cancellable { + + private Attraction attraction; + private Player player; + private boolean cancel = false; + + public PreChangeStatusEvent(Attraction attraction, Player player, Status before, Status after) { + super(before, after); + this.attraction = attraction; + this.player = player; + } + + public Attraction getAttraction() { + return attraction; + } + + public Player getPlayer() { + return player; + } + + public boolean isCancelled() { + return cancel; + } + + public void setCancelled(boolean b) { + cancel = b; + } + +} diff --git a/src/main/java/nl/iobyte/themepark/api/events/region/ChangeDataEvent.java b/src/main/java/nl/iobyte/themepark/api/events/region/ChangeDataEvent.java new file mode 100644 index 0000000..7ef4cc3 --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/api/events/region/ChangeDataEvent.java @@ -0,0 +1,19 @@ +package nl.iobyte.themepark.api.events.region; + +import nl.iobyte.themepark.api.attraction.Region; +import nl.iobyte.themepark.api.events.ChangeEvent; + +public class ChangeDataEvent extends ChangeEvent { + + private Region region; + + public ChangeDataEvent(Region region, short before, short after) { + super(before, after); + this.region = region; + } + + public Region getRegion() { + return region; + } + +} diff --git a/src/main/java/nl/iobyte/themepark/api/events/region/ChangeLoreEvent.java b/src/main/java/nl/iobyte/themepark/api/events/region/ChangeLoreEvent.java new file mode 100644 index 0000000..8cb17fa --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/api/events/region/ChangeLoreEvent.java @@ -0,0 +1,20 @@ +package nl.iobyte.themepark.api.events.region; + +import nl.iobyte.themepark.api.attraction.Region; +import nl.iobyte.themepark.api.events.ChangeEvent; +import java.util.List; + +public class ChangeLoreEvent extends ChangeEvent> { + + private Region region; + + public ChangeLoreEvent(Region region, List before, List after) { + super(before, after); + this.region = region; + } + + public Region getRegion() { + return region; + } + +} diff --git a/src/main/java/nl/iobyte/themepark/api/events/region/ChangeMaterialEvent.java b/src/main/java/nl/iobyte/themepark/api/events/region/ChangeMaterialEvent.java new file mode 100644 index 0000000..e355c26 --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/api/events/region/ChangeMaterialEvent.java @@ -0,0 +1,20 @@ +package nl.iobyte.themepark.api.events.region; + +import nl.iobyte.themepark.api.attraction.Region; +import nl.iobyte.themepark.api.events.ChangeEvent; +import org.bukkit.Material; + +public class ChangeMaterialEvent extends ChangeEvent { + + private Region region; + + public ChangeMaterialEvent(Region region, Material before, Material after) { + super(before, after); + this.region = region; + } + + public Region getRegion() { + return region; + } + +} diff --git a/src/main/java/nl/iobyte/themepark/api/events/region/ChangeNameEvent.java b/src/main/java/nl/iobyte/themepark/api/events/region/ChangeNameEvent.java new file mode 100644 index 0000000..c7513be --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/api/events/region/ChangeNameEvent.java @@ -0,0 +1,19 @@ +package nl.iobyte.themepark.api.events.region; + +import nl.iobyte.themepark.api.attraction.Region; +import nl.iobyte.themepark.api.events.ChangeEvent; + +public class ChangeNameEvent extends ChangeEvent { + + private Region region; + + public ChangeNameEvent(Region region, String before, String after) { + super(before, after); + this.region = region; + } + + public Region getRegion() { + return region; + } + +} diff --git a/src/main/java/nl/iobyte/themepark/api/events/ridecount/ChangeCountEvent.java b/src/main/java/nl/iobyte/themepark/api/events/ridecount/ChangeCountEvent.java new file mode 100644 index 0000000..254ea10 --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/api/events/ridecount/ChangeCountEvent.java @@ -0,0 +1,34 @@ +package nl.iobyte.themepark.api.events.ridecount; + +import nl.iobyte.themepark.api.attraction.Attraction; +import nl.iobyte.themepark.api.events.ChangeEvent; +import nl.iobyte.themepark.api.ridecount.AttractionCount; + +import java.util.UUID; + +public class ChangeCountEvent extends ChangeEvent { + + private Attraction attraction; + private AttractionCount counter; + private UUID uuid; + + public ChangeCountEvent(Attraction attraction, AttractionCount counter, UUID uuid, int before, int after) { + super(before, after); + this.attraction = attraction; + this.counter = counter; + this.uuid = uuid; + } + + public Attraction getAttraction() { + return attraction; + } + + public AttractionCount getCounter() { + return counter; + } + + public UUID getUUID() { + return uuid; + } + +} diff --git a/src/main/java/nl/iobyte/themepark/api/events/ridecount/SetCountEvent.java b/src/main/java/nl/iobyte/themepark/api/events/ridecount/SetCountEvent.java new file mode 100644 index 0000000..b694a9f --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/api/events/ridecount/SetCountEvent.java @@ -0,0 +1,33 @@ +package nl.iobyte.themepark.api.events.ridecount; + +import nl.iobyte.themepark.api.attraction.Attraction; +import nl.iobyte.themepark.api.events.ChangeEvent; +import nl.iobyte.themepark.api.ridecount.AttractionCount; +import java.util.UUID; + +public class SetCountEvent extends ChangeEvent { + + private Attraction attraction; + private AttractionCount counter; + private UUID uuid; + + public SetCountEvent(Attraction attraction, AttractionCount counter, UUID uuid, int before, int after) { + super(before, after); + this.attraction = attraction; + this.counter = counter; + this.uuid = uuid; + } + + public Attraction getAttraction() { + return attraction; + } + + public AttractionCount getCounter() { + return counter; + } + + public UUID getUUID() { + return uuid; + } + +} diff --git a/src/main/java/nl/iobyte/themepark/api/ridecount/AttractionCount.java b/src/main/java/nl/iobyte/themepark/api/ridecount/AttractionCount.java new file mode 100644 index 0000000..5a4c408 --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/api/ridecount/AttractionCount.java @@ -0,0 +1,92 @@ +package nl.iobyte.themepark.api.ridecount; + +import nl.iobyte.themepark.api.attraction.Attraction; +import nl.iobyte.themepark.api.events.ridecount.ChangeCountEvent; +import nl.iobyte.themepark.api.events.ridecount.SetCountEvent; +import org.bukkit.Bukkit; + +import java.util.HashMap; +import java.util.UUID; + +public class AttractionCount { + + private Attraction attraction; + private HashMap counts = new HashMap<>(); + + public AttractionCount(Attraction attraction) { + this.attraction = attraction; + } + + public Attraction getAttraction() { + return attraction; + } + + public void addCount(RideCount rideCount) { + if(rideCount == null) + return; + + if(!attraction.getId().equals(rideCount.getAttraction().getId())) + return; + + counts.put(rideCount.getUUID(), rideCount); + } + + public boolean hasCount(UUID uuid) { + if(uuid == null) + return false; + + return counts.containsKey(uuid); + } + + public RideCount getCount(UUID uuid) { + if(!hasCount(uuid)) + return null; + + return counts.get(uuid); + } + + public void removeCount(UUID uuid) { + if(!hasCount(uuid)) + return; + + counts.remove(uuid); + } + + public void addCount(UUID uuid, int amount) { + if(amount < 1) + return; + + int before = 0; + int after = amount; + RideCount count = getCount(uuid); + if(count == null) { + count = new RideCount(uuid, attraction, amount); + counts.put(uuid, count); + } else { + before = count.getCount(); + after += before; + count.addCount(amount); + } + + ChangeCountEvent e = new ChangeCountEvent(attraction, this, uuid, before, after); + Bukkit.getPluginManager().callEvent(e); + } + + public void setCount(UUID uuid, int amount) { + RideCount count = getCount(uuid); + + if(count == null) { + count = new RideCount(uuid, attraction, amount); + counts.put(uuid, count); + } else { + if(count.getCount() == amount) + return; + + int before = count.getCount(); + count.setCount(amount); + SetCountEvent e = new SetCountEvent(attraction, this, uuid, before, amount); + Bukkit.getPluginManager().callEvent(e); + } + } + +} diff --git a/src/main/java/nl/iobyte/themepark/api/ridecount/CountManager.java b/src/main/java/nl/iobyte/themepark/api/ridecount/CountManager.java new file mode 100644 index 0000000..452b8c1 --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/api/ridecount/CountManager.java @@ -0,0 +1,73 @@ +package nl.iobyte.themepark.api.ridecount; + +import nl.iobyte.themepark.api.API; +import nl.iobyte.themepark.api.attraction.Attraction; +import java.util.HashMap; +import java.util.UUID; + +public class CountManager { + + private static HashMap counts = new HashMap<>(); + + public static void addCounter(Attraction attraction) { + if(attraction == null) + return; + + if(counts.containsKey(attraction.getId())) + return; + + if(!API.isAttraction(attraction.getId())) + return; + + counts.put(attraction.getId(), new AttractionCount(attraction)); + } + + public static boolean isCounter(String id) { + if(id == null || id.isEmpty()) + return false; + + return counts.containsKey(id); + } + + public static AttractionCount getCounter(String id) { + if(!isCounter(id)) + return null; + + return counts.get(id); + } + + public static void removeCounter(String id) { + if(!isCounter(id)) + return; + + counts.remove(id); + } + + public static void addCount(Attraction attraction, UUID uuid, int amount) { + if(attraction == null) + return; + + AttractionCount count = getCounter(attraction.getId()); + if(count == null) + return; + + count.addCount(uuid, amount); + } + + public static void setCount(Attraction attraction, UUID uuid, int amount) { + if(attraction == null) + return; + + AttractionCount count = getCounter(attraction.getId()); + if(count == null) + return; + + count.setCount(uuid, amount); + } + + public static void removeCount(UUID uuid) { + for(AttractionCount attractionCount : counts.values()) + attractionCount.removeCount(uuid); + } + +} diff --git a/src/main/java/nl/iobyte/themepark/api/ridecount/RideCount.java b/src/main/java/nl/iobyte/themepark/api/ridecount/RideCount.java new file mode 100644 index 0000000..feb923a --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/api/ridecount/RideCount.java @@ -0,0 +1,38 @@ +package nl.iobyte.themepark.api.ridecount; + +import nl.iobyte.themepark.api.attraction.Attraction; +import java.util.UUID; + +public class RideCount { + + private UUID uuid; + private Attraction attraction; + private int count; + + public RideCount(UUID uuid, Attraction attraction, int count) { + this.uuid = uuid; + this.attraction = attraction; + this.count = count; + } + + public UUID getUUID() { + return uuid; + } + + public Attraction getAttraction() { + return attraction; + } + + public int getCount() { + return count; + } + + public void setCount(int count) { + this.count = count; + } + + public void addCount(int amount) { + count += amount; + } + +} diff --git a/src/main/java/me/paradoxpixel/themepark/utils/ItemBuilder.java b/src/main/java/nl/iobyte/themepark/api/utils/ItemBuilder.java similarity index 90% rename from src/main/java/me/paradoxpixel/themepark/utils/ItemBuilder.java rename to src/main/java/nl/iobyte/themepark/api/utils/ItemBuilder.java index d9d8009..019bfd2 100644 --- a/src/main/java/me/paradoxpixel/themepark/utils/ItemBuilder.java +++ b/src/main/java/nl/iobyte/themepark/api/utils/ItemBuilder.java @@ -1,13 +1,14 @@ -package me.paradoxpixel.themepark.utils; +package nl.iobyte.themepark.api.utils; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; public class ItemBuilder { @@ -43,7 +44,7 @@ public class ItemBuilder { return this; ItemMeta meta = item.getItemMeta(); - meta.setDisplayName(Utils.color(name)); + meta.setDisplayName(color(name)); this.item.setItemMeta(meta); return this; } @@ -54,7 +55,7 @@ public class ItemBuilder { ItemMeta meta = item.getItemMeta(); for(String string : lore) - lore.set(lore.indexOf(string), Utils.color(string)); + lore.set(lore.indexOf(string), color(string)); meta.setLore(lore); item.setItemMeta(meta); @@ -108,4 +109,8 @@ public class ItemBuilder { return new ItemBuilder(item); } -} \ No newline at end of file + private String color(String string) { + return ChatColor.translateAlternateColorCodes('&', string); + } + +} diff --git a/src/main/java/me/paradoxpixel/themepark/api/LocationUtils.java b/src/main/java/nl/iobyte/themepark/api/utils/LocationSerializer.java similarity index 75% rename from src/main/java/me/paradoxpixel/themepark/api/LocationUtils.java rename to src/main/java/nl/iobyte/themepark/api/utils/LocationSerializer.java index ab0a920..b6dfac0 100644 --- a/src/main/java/me/paradoxpixel/themepark/api/LocationUtils.java +++ b/src/main/java/nl/iobyte/themepark/api/utils/LocationSerializer.java @@ -1,10 +1,10 @@ -package me.paradoxpixel.themepark.api; +package nl.iobyte.themepark.api.utils; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; -public class LocationUtils { +public class LocationSerializer { public static String toString(Location location) { if(location == null) @@ -26,22 +26,16 @@ public class LocationUtils { } public static Location toLocation(String string) { - if(string == null || string.isEmpty()) { - System.out.println("LocationUtils: Incorrect String passed"); + if(string == null || string.isEmpty()) return null; - } String[] args = string.split(":"); - if(args.length < 4) { - System.out.println("LocationUtils: Not enough arguments"); + if(args.length < 4) return null; - } World world = Bukkit.getWorld(args[0]); - if(world == null) { - System.out.println("LocationUtils: No world with name: "+args[0]); + if(world == null) return null; - } double x = Double.parseDouble(args[1]); double y = Double.parseDouble(args[2]); diff --git a/src/main/java/nl/iobyte/themepark/command/MainCommand.java b/src/main/java/nl/iobyte/themepark/command/MainCommand.java new file mode 100644 index 0000000..a30dd6f --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/command/MainCommand.java @@ -0,0 +1,403 @@ +package nl.iobyte.themepark.command; + +import nl.iobyte.themepark.ThemeParkPlugin; +import nl.iobyte.themepark.api.API; +import nl.iobyte.themepark.api.attraction.Attraction; +import nl.iobyte.themepark.api.attraction.Region; +import nl.iobyte.themepark.api.attraction.component.Status; +import nl.iobyte.themepark.api.attraction.component.Type; +import nl.iobyte.themepark.api.attraction.manager.StatusManager; +import nl.iobyte.themepark.api.utils.ItemBuilder; +import nl.iobyte.themepark.api.utils.LocationSerializer; +import nl.iobyte.themepark.config.YamlConfig; +import nl.iobyte.themepark.menu.AttractionLoader; +import nl.iobyte.themepark.menu.AttractionMenu; +import nl.iobyte.themepark.util.Color; +import nl.iobyte.themepark.util.MessageUtil; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.command.CommandSender; +import org.bukkit.command.defaults.BukkitCommand; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Minecart; +import org.bukkit.entity.Player; +import java.util.ArrayList; +import java.util.UUID; + +public class MainCommand extends BukkitCommand { + + private String name; + + public MainCommand(String name) { + super(name); + + this.name = name; + } + + public boolean execute(CommandSender sender,String label, String[] args) { + if (args.length == 0 || args[0].equalsIgnoreCase("help")) { + sender.sendMessage(Color.color("&f&L>==== &r&6ThemePark &L&f ====<")); + sender.sendMessage(Color.color("&f/" + name + " help")); + sender.sendMessage(Color.color("&f/" + name + " list")); + sender.sendMessage(Color.color("&f/" + name + " warp ")); + if(sender.hasPermission("themepark.admin") || sender.hasPermission("themepark.item")) { + sender.sendMessage(Color.color("&f/" + name + " toggleitem")); + sender.sendMessage(Color.color("&f/" + name + " getitem")); + } + + if(sender.hasPermission("themepark.admin")) { + sender.sendMessage(Color.color("&f/" + name + " region create ")); + sender.sendMessage(Color.color("&f/" + name + " region name ")); + sender.sendMessage(Color.color("&f/" + name + " attraction create ")); + sender.sendMessage(Color.color("&f/" + name + " attraction name ")); + sender.sendMessage(Color.color("&f/" + name + " attraction status ")); + sender.sendMessage(Color.color("&f/" + name + " attraction location ")); + } + + return true; + } + + if (args[0].equalsIgnoreCase("list")) { + sender.sendMessage(Color.color("&f&L>==== &r&6ThemePark &f&L ====<")); + if (API.getAttractions().size() == 0) { + sender.sendMessage(Color.color(MessageUtil.getMessage("attraction.notfound"))); + return true; + } + + for (Attraction attraction : API.getAttractions().values()) { + String message = MessageUtil.getMessage("attraction.list"); + message = message.replace("{id}", attraction.getId()); + message = message.replace("{name}", attraction.getName()); + message = message.replace("{region}", attraction.getRegion().getName()); + message = message.replace("{type}", attraction.getType().toString()); + message = message.replace("{status}", StatusManager.getName(attraction.getStatus())); + sender.sendMessage(Color.color(message)); + } + + return true; + } + + if(args[0].equalsIgnoreCase("warp")) { + if(args.length < 2) { + sender.sendMessage(Color.color( MessageUtil.getMessage("prefix")+" &4/" + name + " warp ")); + return true; + } + + String id = args[1]; + if(!API.isAttraction(id)) { + String message = MessageUtil.getMessage("attraction.not"); + message = message.replace("{id}", id); + sender.sendMessage(Color.color(message)); + return true; + } + + if(sender instanceof Player) { + Attraction attraction = API.getAttraction(id); + Player player = (Player) sender; + if(!StatusManager.canTeleport(attraction.getStatus())) { + String message = MessageUtil.getMessage("attraction.teleport.status"); + message = message.replace("{name}", attraction.getName()); + message = message.replace("{status}", StatusManager.getName(attraction.getStatus())); + player.sendMessage(Color.color(message)); + return true; + } + + player.teleport(attraction.getLocation()); + if(player.isInsideVehicle()) + if(player.getVehicle() instanceof Minecart) + return true; + + player.sendMessage(Color.color(MessageUtil.getMessage("attraction.teleport.success").replace("{name}", attraction.getName()))); + return true; + } else { + sender.sendMessage(Color.color(MessageUtil.getMessage("onlyplayers"))); + return true; + } + } + + if(!sender.hasPermission("themepark.admin") && !sender.hasPermission("themepark.item")) { + sender.sendMessage(Color.color(MessageUtil.getMessage("nopermission"))); + return true; + } + + if(args[0].equalsIgnoreCase("toggleitem")) { + if(!(sender instanceof Player)) { + sender.sendMessage(Color.color(MessageUtil.getMessage("onlyplayers"))); + return true; + } + + Player player = (Player) sender; + UUID uuid = player.getUniqueId(); + + YamlConfig config = ThemeParkPlugin.getInstance().getData(); + boolean b = config.getConfig().getBoolean(uuid.toString() + ".item"); + b = !b; + + config.getConfig().set(uuid.toString() + ".item", b); + config.save(); + + String message = MessageUtil.getMessage("menu-item.toggle"); + message = message.replace("{status}", b ? MessageUtil.getMessage("menu-item.enabled") : MessageUtil.getMessage("menu-item.disabled")); + sender.sendMessage(Color.color(message)); + return true; + } + + if(args[0].equalsIgnoreCase("getitem")) { + YamlConfig settings = ThemeParkPlugin.getInstance().getSettings(); + Material material = Material.getMaterial(settings.getConfig().getString("item.material")); + short data = Short.parseShort(settings.getConfig().getString("item.data")); + String name = Color.color(settings.getConfig().getString("item.display-name")); + if(material == null || name.isEmpty()) + return true; + + Player player; + if(args.length >= 2) { + String string = args[1]; + if(Bukkit.getPlayerExact(string) == null) { + String message = MessageUtil.getMessage("noplayer"); + message = message.replace("{name}", string); + sender.sendMessage(Color.color(message)); + return true; + } + + player = Bukkit.getPlayerExact(string); + } else { + if(!(sender instanceof Player)) { + sender.sendMessage(Color.color(MessageUtil.getMessage("onlyplayers"))); + return true; + } + + player = (Player) sender; + } + + + ItemBuilder builder = new ItemBuilder(material, 1, data); + builder.setName(name); + player.getInventory().addItem(builder.getItem()); + } + + if(!sender.hasPermission("themepark.admin")) { + sender.sendMessage(Color.color(MessageUtil.getMessage("nopermission"))); + return true; + } + + if (args[0].equalsIgnoreCase("region")) { + if(args.length < 2) + return false; + + if (args[1].equalsIgnoreCase("create")) { + if (args.length < 4 ) { + sender.sendMessage(Color.color(MessageUtil.getMessage("prefix")+" &4/" + name + " region create ")); + return true; + } + + String id = args[2]; + if (API.isRegion(id)) { + String message = MessageUtil.getMessage("region.exists"); + message = message.replace("{id}", id); + sender.sendMessage(Color.color(message)); + return true; + } + + StringBuilder builder = new StringBuilder(); + builder.append(args[3]); + for(int i = 4; i < args.length; i++) + builder.append(" ").append(args[i]); + + String name = builder.toString(); + Region region = new Region(id, name, new ArrayList<>()); + API.addRegion(region); + AttractionMenu.load(); + AttractionLoader.addRegion(id); + + YamlConfig config = ThemeParkPlugin.getInstance().getRegions(); + FileConfiguration fc = config.getConfig(); + String path = "regions."+id+"."; + fc.set(path+"name", name); + fc.set(path+"lore", region.getLore()); + config.save(); + + sender.sendMessage(Color.color(MessageUtil.getMessage("region.create").replace("{name}", name))); + return true; + } + + if(args[1].equalsIgnoreCase("name")) { + if (args.length < 4 ) { + sender.sendMessage(Color.color("&f/" + name + " region name ")); + return true; + } + + String id = args[2]; + if(!API.isRegion(id)) { + String message = MessageUtil.getMessage("region.not"); + message = message.replace("{id}", id); + sender.sendMessage(Color.color(message)); + return true; + } + + StringBuilder builder = new StringBuilder(); + builder.append(args[3]); + for(int i = 4; i < args.length; i++) + builder.append(" ").append(args[i]); + + String name = builder.toString(); + Region region = API.getRegion(id); + region.setName(name); + sender.sendMessage(Color.color(MessageUtil.getMessage("region.name").replace("{name}", region.getName()))); + return true; + } + } + + if (args[0].equalsIgnoreCase("attraction")) { + if(args.length < 2) + return false; + + if (args[1].equalsIgnoreCase("create")) { + if (args.length < 6 ) { + sender.sendMessage(Color.color(MessageUtil.getMessage("prefix")+" &4/" + name + " attraction create ")); + return true; + } + + String id = args[2]; + if(!API.isRegion(id)) { + String message = MessageUtil.getMessage("region.not"); + message = message.replace("{id}", id); + sender.sendMessage(Color.color(message)); + return true; + } + + id = args[3]; + if (API.isAttraction(id)) { + String message = MessageUtil.getMessage("attraction.exists"); + message = message.replace("{id}", id); + sender.sendMessage(Color.color(message)); + return true; + } + + Type type = Type.getType(args[4]); + if(type == null) { + String message = MessageUtil.getMessage("no-type"); + message = message.replace("{type}", args[4]); + sender.sendMessage(Color.color(message)); + return true; + } + + StringBuilder builder = new StringBuilder(); + builder.append(args[5]); + for(int i = 6; i < args.length; i++) + builder.append(" ").append(args[i]); + + String name = builder.toString(); + Attraction attraction = new Attraction(args[3], name, args[2], null, type, type.getDefault()); + API.addAttraction(attraction); + AttractionMenu.load(); + + YamlConfig config = AttractionLoader.getConfig(attraction.getRegionId()); + FileConfiguration fc = config.getConfig(); + String path = "attractions."+attraction.getId()+"."; + fc.set(path+"name", attraction.getName()); + fc.set(path+"type", type.toString()); + fc.set(path+"status", attraction.getStatus().toString()); + config.save(); + + sender.sendMessage(Color.color(MessageUtil.getMessage("attraction.create").replace("{name}", name))); + return true; + } + + if(args[1].equalsIgnoreCase("name")) { + if (args.length < 4 ) { + sender.sendMessage(Color.color("&f/" + name + " attraction name ")); + return true; + } + + String id = args[2]; + if(!API.isAttraction(id)) { + String message = MessageUtil.getMessage("attraction.not"); + message = message.replace("{id}", id); + sender.sendMessage(Color.color(message)); + return true; + } + + StringBuilder builder = new StringBuilder(); + builder.append(args[3]); + for(int i = 4; i < args.length; i++) + builder.append(" ").append(args[i]); + + String name = builder.toString(); + Attraction attraction = API.getAttraction(id); + attraction.setName(name); + sender.sendMessage(Color.color(MessageUtil.getMessage("attraction.name").replace("{name}", attraction.getName()))); + return true; + } + + if (args[1].equalsIgnoreCase("status")) { + if (args.length < 4 ) { + sender.sendMessage(Color.color(MessageUtil.getMessage("prefix")+" &4/" + name + " attraction status ")); + return true; + } + + String id = args[2]; + if (!API.isAttraction(id)) { + String message = MessageUtil.getMessage("attraction.not"); + message = message.replace("{id}", id); + sender.sendMessage(Color.color(message)); + return true; + } + + Status status = Status.getStatus(args[3].toUpperCase()); + if (status == null) { + String message = MessageUtil.getMessage("no-status"); + message = message.replace("{status}", args[3]); + sender.sendMessage(Color.color(message)); + return true; + } + + Attraction attraction = API.getAttraction(id); + if(!attraction.getType().containsStatus(status)) { + String message = MessageUtil.getMessage("attraction.nostatus"); + message = message.replace("{name}", attraction.getName()); + message = message.replace("{status}", status.toString()); + sender.sendMessage(Color.color(message)); + return true; + } + + if(attraction.getStatus() == status) + return true; + + Player player = (sender instanceof Player) ? (Player) sender : null; + attraction.setStatus(status, player); + return true; + } + + if(args[1].equalsIgnoreCase("location")) { + if (args.length < 3 ) { + sender.sendMessage(Color.color("&f/" + name + " attraction location ")); + return true; + } + + String id = args[2]; + if(!API.isAttraction(id)) { + String message = MessageUtil.getMessage("attraction.not"); + message = message.replace("{id}", id); + sender.sendMessage(Color.color(message)); + return true; + } + + if(!(sender instanceof Player)) { + sender.sendMessage(Color.color(MessageUtil.getMessage("onlyplayers"))); + return true; + } + + Attraction attraction = API.getAttraction(id); + Location location = ((Player) sender).getLocation(); + attraction.setLocation(location.clone()); + sender.sendMessage(Color.color(MessageUtil.getMessage("attraction.location").replace("{name}", attraction.getName()))); + return true; + } + } + + return false; + } + +} \ No newline at end of file diff --git a/src/main/java/me/paradoxpixel/themepark/command/MenuCommand.java b/src/main/java/nl/iobyte/themepark/command/MenuCommand.java similarity index 58% rename from src/main/java/me/paradoxpixel/themepark/command/MenuCommand.java rename to src/main/java/nl/iobyte/themepark/command/MenuCommand.java index 6017db3..b181a90 100644 --- a/src/main/java/me/paradoxpixel/themepark/command/MenuCommand.java +++ b/src/main/java/nl/iobyte/themepark/command/MenuCommand.java @@ -1,7 +1,8 @@ -package me.paradoxpixel.themepark.command; +package nl.iobyte.themepark.command; -import me.paradoxpixel.themepark.attraction.AttractionMenu; -import me.paradoxpixel.themepark.utils.Utils; +import nl.iobyte.themepark.menu.AttractionMenu; +import nl.iobyte.themepark.util.Color; +import nl.iobyte.themepark.util.MessageUtil; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -13,13 +14,16 @@ public class MenuCommand implements CommandExecutor { public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { if(cmd.getName().equalsIgnoreCase("status")) { if(sender instanceof Player) { - AttractionMenu.openMenu((Player) sender, 1); - return true; + AttractionMenu.open((Player) sender); } else { - sender.sendMessage(Utils.color("&6Themepark&f: &4Only players can use this command")); + sender.sendMessage(Color.color(MessageUtil.getMessage("onlyplayers"))); } + + return true; } + return false; } } + diff --git a/src/main/java/nl/iobyte/themepark/command/RidecountCommand.java b/src/main/java/nl/iobyte/themepark/command/RidecountCommand.java new file mode 100644 index 0000000..4bb8aac --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/command/RidecountCommand.java @@ -0,0 +1,107 @@ +package nl.iobyte.themepark.command; + +import nl.iobyte.menuapi.map.WeakConcurrentHashMap; +import nl.iobyte.themepark.api.API; +import nl.iobyte.themepark.api.attraction.Attraction; +import nl.iobyte.themepark.ridecount.RideCountAPI; +import nl.iobyte.themepark.util.Color; +import nl.iobyte.themepark.util.MessageUtil; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import java.util.Scanner; + +public class RidecountCommand implements CommandExecutor { + + private static WeakConcurrentHashMap data = new WeakConcurrentHashMap<>(5 * 60 * 1000); + + @Override + public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { + if(!cmd.getName().equalsIgnoreCase("ridecount")) + return false; + + if(args.length == 0 || args[0].equalsIgnoreCase("help")) { + sender.sendMessage(Color.color("&f&L>==== &r&6Ridecount &L&f ====<")); + sender.sendMessage(Color.color("&f/ridecount help")); + sender.sendMessage(Color.color("&f/ridecount get ")); + sender.sendMessage(Color.color("&f/ridecount add ")); + return true; + } + + if(args[0].equalsIgnoreCase("get")) { + if(args.length < 3) { + sender.sendMessage(Color.color(MessageUtil.getMessage("prefix")+" &4/ridecount get ")); + return true; + } + + if(!API.isAttraction(args[2])) { + String message = MessageUtil.getMessage("attraction.not"); + message = message.replace("{id}", args[2]); + sender.sendMessage(Color.color(message)); + return true; + } + + Attraction attraction = API.getAttraction(args[2]); + RideCountAPI.getCount(args[1], attraction).thenAccept(points -> { + if(points < 0) { + sender.sendMessage(Color.color(MessageUtil.getMessage("noplayer").replace("{name}", args[1]))); + return; + } + + String mesage = MessageUtil.getMessage("ridecount.count"); + mesage = mesage.replace("{name}", attraction.getName()); + mesage = mesage.replace("{count}", points.toString()); + sender.sendMessage(Color.color(mesage)); + }); + return true; + } + + if(args[0].equalsIgnoreCase("add")) { + if(args.length < 4) { + sender.sendMessage(Color.color(MessageUtil.getMessage("prefix")+" &4/ridecount add ")); + return true; + } + + if(!API.isAttraction(args[2])) { + String message = MessageUtil.getMessage("attraction.not"); + message = message.replace("{id}", args[2]); + sender.sendMessage(Color.color(message)); + return true; + } + + if(!isInteger(args[3])) { + sender.sendMessage(Color.color(MessageUtil.getMessage("nonumber").replace("{number}", args[3]))); + return true; + } + + int amount = Integer.parseInt(args[3]); + Attraction attraction = API.getAttraction(args[2]); + RideCountAPI.addCount(args[1], attraction, amount).thenAccept(result -> { + if(!result) { + sender.sendMessage(Color.color(MessageUtil.getMessage("noplayer").replace("{name}", args[1]))); + return; + } + + String mesage = MessageUtil.getMessage("ridecount.add"); + mesage = mesage.replace("{name}", API.getAttraction(args[2]).getName()); + mesage = mesage.replace("{count}", Integer.toString(amount)); + sender.sendMessage(Color.color(mesage)); + }); + return true; + } + + return false; + } + + + + private boolean isInteger(String s) { + int radix = 10; + Scanner sc = new Scanner(s.trim()); + if(!sc.hasNextInt(radix)) return false; + sc.nextInt(radix); + return !sc.hasNext(); + } + +} + diff --git a/src/main/java/me/paradoxpixel/themepark/config/YamlConfig.java b/src/main/java/nl/iobyte/themepark/config/YamlConfig.java similarity index 98% rename from src/main/java/me/paradoxpixel/themepark/config/YamlConfig.java rename to src/main/java/nl/iobyte/themepark/config/YamlConfig.java index 5c4f9f3..0b92d9e 100644 --- a/src/main/java/me/paradoxpixel/themepark/config/YamlConfig.java +++ b/src/main/java/nl/iobyte/themepark/config/YamlConfig.java @@ -1,4 +1,4 @@ -package me.paradoxpixel.themepark.config; +package nl.iobyte.themepark.config; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; diff --git a/src/main/java/nl/iobyte/themepark/database/DB.java b/src/main/java/nl/iobyte/themepark/database/DB.java new file mode 100644 index 0000000..0d8c5d9 --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/database/DB.java @@ -0,0 +1,77 @@ +package nl.iobyte.themepark.database; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.util.HashMap; +import java.util.Map; + +public abstract class DB { + + public abstract Connection getConnection(); + + public abstract void closeConnection(); + + public boolean execute(String query, HashMap objects) { + if(query == null || query.isEmpty()) + return false; + + if(getConnection() == null) + return false; + + try { + PreparedStatement statement = getConnection().prepareStatement(query); + statement.closeOnCompletion(); + if(objects != null) + for(Map.Entry entry : objects.entrySet()) + statement.setObject(entry.getKey(), entry.getValue()); + + return statement.execute(); + } catch(Exception e) { + e.printStackTrace(); + return false; + } + } + + public int executeUpdate(String query, HashMap objects) { + if (query == null || query.isEmpty()) + return 0; + + if(getConnection() == null) + return 0; + + try { + PreparedStatement statement = getConnection().prepareStatement(query); + statement.closeOnCompletion(); + for (Map.Entry entry : objects.entrySet()) + statement.setObject(entry.getKey(), entry.getValue()); + + return statement.executeUpdate(); + } catch (Exception e) { + e.printStackTrace(); + return 0; + } + } + + public ResultSet executeQuery(String query, HashMap objects) { + if (query == null || query.isEmpty()) + return null; + + if (getConnection() == null) + return null; + + try { + PreparedStatement statement = getConnection().prepareStatement(query); + statement.closeOnCompletion(); + if (objects != null) + for (Map.Entry entry : objects.entrySet()) + statement.setObject(entry.getKey(), entry.getValue()); + + return statement.executeQuery(); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + } + +} diff --git a/src/main/java/nl/iobyte/themepark/database/DBManager.java b/src/main/java/nl/iobyte/themepark/database/DBManager.java new file mode 100644 index 0000000..3da41b0 --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/database/DBManager.java @@ -0,0 +1,90 @@ +package nl.iobyte.themepark.database; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.util.HashMap; + +public class DBManager { + + private static HashMap databases = new HashMap<>(); + + /** + * Add DB to manager + * @param id identifier + * @param db DB Instance + */ + public static void addDatabase(String id, DB db) { + if(id == null || id.isEmpty() || db == null) + return; + + if(databases.containsKey(id)) + return; + + databases.put(id, db); + } + + /** + * Get Database + * @param id identifier + * @return DB Instance + */ + public static DB getDatabase(String id) { + if(id == null || id.isEmpty()) + return null; + + return databases.get(id); + } + + /** + * Database exists + * @param id identifier + * @return boolean + */ + public static boolean hasDatabase(String id) { + if(id == null || id.isEmpty()) + return false; + + return databases.containsKey(id); + } + + /** + * Remove Database + * @param id identifier + */ + public static void removeDatabase(String id) { + DB db = getDatabase(id); + if(db == null) + return; + + db.closeConnection(); + databases.remove(id); + } + + /** + * Close all connections + */ + public static void shutdown() { + for(DB db : databases.values()) + db.closeConnection(); + } + + /** + * Create tabke + * @param db DB Instance + * @param query Query + */ + public static void createTable(DB db, String query) { + if(db == null || query == null || query.isEmpty()) + return; + + try { + Connection connection = db.getConnection(); + PreparedStatement statement = connection.prepareStatement(query); + statement.execute(); + statement.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + +} \ No newline at end of file diff --git a/src/main/java/nl/iobyte/themepark/database/MySQL.java b/src/main/java/nl/iobyte/themepark/database/MySQL.java new file mode 100644 index 0000000..a78ba00 --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/database/MySQL.java @@ -0,0 +1,66 @@ +package nl.iobyte.themepark.database; + +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; +import nl.iobyte.themepark.ThemeParkPlugin; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.Properties; + +public class MySQL extends DB { + + private String dbName; + private HikariDataSource source; + private Connection con; + + /** + * Initialize a new connection + * + * @param dbName The database name + */ + public MySQL(String host, int port, String dbName, String username, String password) { + this.dbName = dbName; + HikariConfig config = new HikariConfig(); + config.setPoolName(ThemeParkPlugin.getInstance().getName()); + config.setUsername(username); + config.setPassword(password); + config.setDriverClassName("com.mysql.jdbc.Driver"); + config.setConnectionTestQuery("SELECT 1"); + config.setMaximumPoolSize(1); + + Properties prop = new Properties(); + prop.setProperty("date_string_format", "yyyy-MM-dd HH:mm:ss"); + config.setJdbcUrl("jdbc:mysql://"+host+":"+port+"/"+dbName); + config.setDataSourceProperties(prop); + source = new HikariDataSource(config); + + try { + con = source.getConnection(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + /** + * Get connection + * @return Connection Instance + */ + public Connection getConnection() { + return con; + } + + /** + * Close connection + */ + public void closeConnection() { + System.out.println("Closing the database connection for " + dbName + ".db!"); + try { + con.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + + source.close(); + } + +} diff --git a/src/main/java/nl/iobyte/themepark/database/SQLite.java b/src/main/java/nl/iobyte/themepark/database/SQLite.java new file mode 100644 index 0000000..de18c01 --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/database/SQLite.java @@ -0,0 +1,84 @@ +package nl.iobyte.themepark.database; + +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; +import nl.iobyte.themepark.ThemeParkPlugin; +import java.io.File; +import java.io.IOException; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.Properties; + +public class SQLite extends DB { + + private String dbName; + private HikariDataSource source; + private Connection con; + + /** + * Initialize a new connection + * + * @param dbName The database name + */ + public SQLite(String dbName) { + this.dbName = dbName; + + File dbFile = new File(ThemeParkPlugin.getInstance().getDataFolder(), dbName + ".db"); + if (!dbFile.exists()) { + try { + System.out.println("Generating the " + dbName + ".db!"); + if (!dbFile.createNewFile()) { + System.out.println("Couldn't generate the " + dbName + ".db!"); + return; + } + } catch (IOException e) { + System.out.println("Couldn't generate the " + dbName + ".db!"); + return; + } + } + + HikariConfig config = new HikariConfig(); + config.setPoolName(ThemeParkPlugin.getInstance().getName()); + config.setUsername(null); + config.setPassword(null); + config.setDriverClassName("org.sqlite.JDBC"); + config.setConnectionTestQuery("SELECT 1"); + config.setMaximumPoolSize(1); + + Properties prop = new Properties(); + prop.setProperty("date_string_format", "yyyy-MM-dd HH:mm:ss"); + + config.setJdbcUrl("jdbc:sqlite:" + dbFile.getAbsolutePath()); + config.setDataSourceProperties(prop); + source = new HikariDataSource(config); + + try { + con = source.getConnection(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + + /** + * Get connection + * @return Connection Instance + */ + public Connection getConnection() { + return con; + } + + /** + * Close connection + */ + public void closeConnection() { + System.out.println("Closing the database connection for " + dbName + ".db!"); + try { + con.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + + source.close(); + } + +} \ No newline at end of file diff --git a/src/main/java/nl/iobyte/themepark/listener/AttractionListener.java b/src/main/java/nl/iobyte/themepark/listener/AttractionListener.java new file mode 100644 index 0000000..8a0e2ec --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/listener/AttractionListener.java @@ -0,0 +1,118 @@ +package nl.iobyte.themepark.listener; + +import net.md_5.bungee.api.chat.BaseComponent; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.HoverEvent; +import net.md_5.bungee.api.chat.TextComponent; +import nl.iobyte.themepark.ThemeParkPlugin; +import nl.iobyte.themepark.api.attraction.Attraction; +import nl.iobyte.themepark.api.events.attraction.ChangeLocationEvent; +import nl.iobyte.themepark.api.events.attraction.ChangeNameEvent; +import nl.iobyte.themepark.api.events.attraction.ChangeStatusEvent; +import nl.iobyte.themepark.api.events.attraction.ChangeTypeEvent; +import nl.iobyte.themepark.api.utils.LocationSerializer; +import nl.iobyte.themepark.config.YamlConfig; +import nl.iobyte.themepark.database.DB; +import nl.iobyte.themepark.database.DBManager; +import nl.iobyte.themepark.menu.AttractionLoader; +import nl.iobyte.themepark.menu.AttractionMenu; +import nl.iobyte.themepark.util.Color; +import nl.iobyte.themepark.util.MessageUtil; +import nl.iobyte.workchain.components.Work; +import org.bukkit.Bukkit; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; + +import java.util.HashMap; + +public class AttractionListener implements Listener { + + @EventHandler + public void onChange(ChangeStatusEvent e) { + AttractionMenu.updateAttraction(e.getAttraction()); + + Attraction attraction = e.getAttraction(); + YamlConfig config = AttractionLoader.getConfig(attraction.getRegionId()); + config.getConfig().set("attractions." + attraction.getId() + ".status", e.getAfter().toString()); + config.save(); + + Work.firstTask(() -> DBManager.getDatabase("public")).abortIfNull().lastTask(database -> { + HashMap obj = new HashMap<>(); + obj.put(1, e.getAfter().toString()); + obj.put(2, attraction.getId()); + + String query ="UPDATE attraction SET status=? WHERE id=?"; + database.execute(query, obj); + }).execute(); + + String command = ThemeParkPlugin.getInstance().getSettings().getConfig().getString("command"); + if(command == null || command.isEmpty()) + return; + + HashMap change = new HashMap<>(); + change.put("{name}", attraction.getName()); + ClickEvent click = new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/"+command+" warp "+attraction.getId()); + HoverEvent hover = new HoverEvent(HoverEvent.Action.SHOW_TEXT, TextComponent.fromLegacyText(Color.color(ThemeParkPlugin.getInstance().getMessages().getConfig().getString("attraction.changed.hover")))); + if(hover.getValue()[0].toPlainText().isEmpty()) + hover = null; + + BaseComponent message = MessageUtil.parseTP("attraction.changed."+e.getAfter().toString(), change, click, hover); + if(message == null) + return; + + for(Player player : Bukkit.getOnlinePlayers()) + player.spigot().sendMessage(message); + } + + @EventHandler + public void onChange(ChangeTypeEvent e) { + AttractionMenu.updateAttraction(e.getAttraction()); + + Attraction attraction = e.getAttraction(); + YamlConfig config = AttractionLoader.getConfig(attraction.getRegionId()); + config.getConfig().set("attractions." + attraction.getId() + ".type", e.getAfter().toString()); + config.save(); + + Work.firstTask(() -> DBManager.getDatabase("public")).abortIfNull().lastTask(database -> { + HashMap obj = new HashMap<>(); + obj.put(1, e.getAfter().toString()); + obj.put(2, attraction.getStatus().toString()); + obj.put(3, attraction.getId()); + + String query ="UPDATE attraction SET type=?, status=? WHERE id=?"; + database.execute(query, obj); + }).execute(); + } + + @EventHandler + public void onChange(ChangeNameEvent e) { + AttractionMenu.updateAttraction(e.getAttraction()); + + Attraction attraction = e.getAttraction(); + YamlConfig config = AttractionLoader.getConfig(attraction.getRegionId()); + config.getConfig().set("attractions." + attraction.getId() + ".name", e.getAfter()); + config.save(); + + Work.firstTask(() -> DBManager.getDatabase("public")).abortIfNull().lastTask(database -> { + HashMap obj = new HashMap<>(); + obj.put(1, e.getAfter()); + obj.put(2, attraction.getId()); + + String query ="UPDATE attraction SET name=? WHERE id=?"; + database.execute(query, obj); + }).execute(); + } + + @EventHandler + public void onChange(ChangeLocationEvent e) { + Attraction attraction = e.getAttraction(); + YamlConfig config = AttractionLoader.getConfig(attraction.getRegionId()); + config.getConfig().set("attractions." + attraction.getId() + ".location", LocationSerializer.toString(e.getAfter())); + config.save(); + + //TODO MapMarker.getMarker().setAttractionMarker(attraction); + } + +} diff --git a/src/main/java/nl/iobyte/themepark/listener/PlayerListener.java b/src/main/java/nl/iobyte/themepark/listener/PlayerListener.java new file mode 100644 index 0000000..0d807a8 --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/listener/PlayerListener.java @@ -0,0 +1,190 @@ +package nl.iobyte.themepark.listener; + +import nl.iobyte.themepark.ThemeParkPlugin; +import nl.iobyte.themepark.api.API; +import nl.iobyte.themepark.api.attraction.Attraction; +import nl.iobyte.themepark.api.events.ridecount.ChangeCountEvent; +import nl.iobyte.themepark.api.ridecount.CountManager; +import nl.iobyte.themepark.api.utils.ItemBuilder; +import nl.iobyte.themepark.config.YamlConfig; +import nl.iobyte.themepark.database.DB; +import nl.iobyte.themepark.database.DBManager; +import nl.iobyte.themepark.menu.AttractionMenu; +import nl.iobyte.themepark.util.Color; +import nl.iobyte.themepark.util.MessageUtil; +import nl.iobyte.workchain.components.Work; +import org.bukkit.Bukkit; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.player.PlayerChangedWorldEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.inventory.ItemStack; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; + +public class PlayerListener implements Listener { + + @EventHandler + public void onJoin(PlayerJoinEvent e) { + Player player = e.getPlayer(); + UUID uuid = player.getUniqueId(); + + doItem(e.getPlayer()); + loadRideCount(uuid); + } + + @EventHandler + public void onSwitch(PlayerChangedWorldEvent e) { + doItem(e.getPlayer()); + } + + public void doItem(Player player) { + YamlConfig settings = ThemeParkPlugin.getInstance().getSettings(); + YamlConfig config = ThemeParkPlugin.getInstance().getData(); + + CompletableFuture fc = new CompletableFuture<>(); + Work.firstTask(() -> { + if(!settings.getConfig().getBoolean("item.enabled")) + return null; + + boolean b = true; + if (config.getConfig().contains(player.getUniqueId().toString() + ".item")) { + b = config.getConfig().getBoolean(player.getUniqueId().toString() + ".item"); + } else { + config.getConfig().set(player.getUniqueId().toString() + ".item", true); + config.save(); + } + + return b; + }).abortIfNull(() -> { + fc.complete(null); + }).nextTask(b -> { + if(!b) + return null; + + Material material = Material.getMaterial(settings.getConfig().getString("item.material")); + short data = Short.parseShort(settings.getConfig().getString("item.data")); + String name = Color.color(settings.getConfig().getString("item.display-name")); + if(material == null || name.isEmpty()) + return null; + + ItemBuilder builder = new ItemBuilder(material, 1, data); + builder.setName(name); + return builder.getItem(); + }).abortIfNull(() -> { + fc.complete(null); + }).lastTask(item -> { + if(settings.getConfig().getBoolean("item.clear")) + player.getInventory().clear(); + + List worlds = settings.getConfig().getStringList("item.worlds"); + if(worlds != null && !worlds.isEmpty()) { + if (worlds.contains(player.getLocation().getWorld().getName())) { + fc.complete(new ItemStack(Material.AIR)); + return; + } + } + + fc.complete(item); + }).setFullExecute(true).execute(); + + fc.thenAccept(item -> { + if(item == null) + return; + + int slot = settings.getConfig().getInt("item.slot"); + player.getInventory().setItem(slot, item); + player.updateInventory(); + }); + } + + @EventHandler + public void onInteract(PlayerInteractEvent e) { + YamlConfig config = ThemeParkPlugin.getInstance().getSettings(); + Material material = Material.getMaterial(config.getConfig().getString("item.material")); + String name = Color.color(config.getConfig().getString("item.display-name")); + if(e.getItem() == null) + return; + + ItemStack item = e.getItem(); + if(item.getType() != material) + return; + + if(!item.hasItemMeta()) + return; + + if(!item.getItemMeta().getDisplayName().equals(name)) + return; + + if(e.getAction() == Action.LEFT_CLICK_AIR || + e.getAction() == Action.LEFT_CLICK_BLOCK) + return; + + e.setCancelled(true); + Player player = e.getPlayer(); + AttractionMenu.open(player); + } + + @EventHandler + public void onQuit(PlayerQuitEvent e) { + Player player = e.getPlayer(); + UUID uuid = player.getUniqueId(); + + CountManager.removeCount(uuid); + } + + private void loadRideCount(UUID uuid) { + Work.firstTask(() -> { + DB db = DBManager.getDatabase("data"); + if(db == null) + return null; + + Connection connection = db.getConnection(); + if(connection == null || connection.isClosed()) + return null; + + PreparedStatement statement = connection.prepareStatement("SELECT points, attraction_id FROM counts WHERE uuid=?"); + statement.setString(1, uuid.toString()); + statement.closeOnCompletion(); + return statement.executeQuery(); + }).abortIfNull().lastTask(result -> { + while(result.next()) { + Attraction attraction = API.getAttraction(result.getString("attraction_id")); + if(attraction == null) + continue; + + int points = result.getInt("points"); + if(points < 0) + continue; + + CountManager.setCount(attraction, uuid, points); + } + + result.close(); + }).setFullExecute(true).execute(); + } + + @EventHandler + public void onChange(ChangeCountEvent e) { + Player player = Bukkit.getPlayer(e.getUUID()); + if(player == null || !player.isOnline()) + return; + + Attraction attraction = e.getAttraction(); + int amount = e.getAfter() - e.getBefore(); + String message = MessageUtil.getMessage("ridecount.message"); + message = message.replace("{name}", attraction.getName()); + message = message.replace("{amount}", Integer.toString(amount)); + message = message.replace("{total}", Integer.toString(e.getCounter().getCount(e.getUUID()).getCount())); + player.sendMessage(Color.color(message)); + } + +} diff --git a/src/main/java/nl/iobyte/themepark/listener/RegionListener.java b/src/main/java/nl/iobyte/themepark/listener/RegionListener.java new file mode 100644 index 0000000..e3c221f --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/listener/RegionListener.java @@ -0,0 +1,68 @@ +package nl.iobyte.themepark.listener; + +import nl.iobyte.themepark.ThemeParkPlugin; +import nl.iobyte.themepark.api.attraction.Region; +import nl.iobyte.themepark.api.events.region.ChangeDataEvent; +import nl.iobyte.themepark.api.events.region.ChangeLoreEvent; +import nl.iobyte.themepark.api.events.region.ChangeMaterialEvent; +import nl.iobyte.themepark.api.events.region.ChangeNameEvent; +import nl.iobyte.themepark.config.YamlConfig; +import nl.iobyte.themepark.database.DBManager; +import nl.iobyte.themepark.menu.AttractionMenu; +import nl.iobyte.workchain.components.Work; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import java.util.HashMap; + +public class RegionListener implements Listener { + + @EventHandler + public void onChange(ChangeNameEvent e) { + AttractionMenu.updateRegion(e.getRegion()); + + Region region = e.getRegion(); + YamlConfig config = ThemeParkPlugin.getInstance().getRegions(); + config.getConfig().set("regions." + region.getId() + ".name", e.getAfter()); + config.save(); + + Work.firstTask(() -> DBManager.getDatabase("public")).abortIfNull().lastTask(database -> { + HashMap obj = new HashMap<>(); + obj.put(1, e.getAfter()); + obj.put(2, region.getId()); + + String query ="UPDATE region SET name=? WHERE id=?"; + database.execute(query, obj); + }).execute(); + } + + @EventHandler + public void onChange(ChangeLoreEvent e) { + AttractionMenu.updateRegion(e.getRegion()); + + Region region = e.getRegion(); + YamlConfig config = ThemeParkPlugin.getInstance().getRegions(); + config.getConfig().set("regions." + region.getId() + ".lore", e.getAfter()); + config.save(); + } + + @EventHandler + public void onChange(ChangeMaterialEvent e) { + AttractionMenu.updateRegion(e.getRegion()); + + Region region = e.getRegion(); + YamlConfig config = ThemeParkPlugin.getInstance().getRegions(); + config.getConfig().set("regions." + region.getId() + ".material", e.getAfter().toString()); + config.save(); + } + + @EventHandler + public void onChange(ChangeDataEvent e) { + AttractionMenu.updateRegion(e.getRegion()); + + Region region = e.getRegion(); + YamlConfig config = ThemeParkPlugin.getInstance().getRegions(); + config.getConfig().set("regions." + region.getId() + ".data", e.getAfter()); + config.save(); + } + +} diff --git a/src/main/java/nl/iobyte/themepark/menu/AttractionLoader.java b/src/main/java/nl/iobyte/themepark/menu/AttractionLoader.java new file mode 100644 index 0000000..a882309 --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/menu/AttractionLoader.java @@ -0,0 +1,240 @@ +package nl.iobyte.themepark.menu; + +import nl.iobyte.themepark.ThemeParkPlugin; +import nl.iobyte.themepark.api.API; +import nl.iobyte.themepark.api.attraction.Attraction; +import nl.iobyte.themepark.api.attraction.Region; +import nl.iobyte.themepark.api.attraction.component.Status; +import nl.iobyte.themepark.api.attraction.component.Type; +import nl.iobyte.themepark.api.attraction.manager.StatusManager; +import nl.iobyte.themepark.api.utils.LocationSerializer; +import nl.iobyte.themepark.config.YamlConfig; +import nl.iobyte.themepark.database.DB; +import nl.iobyte.themepark.database.DBManager; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +public class AttractionLoader { + + private static HashMap configs = new HashMap<>(); + + public static YamlConfig getConfig(String id) { + if(id == null || id.isEmpty()) + return null; + + return configs.get(id); + } + + public static void load() { + loadRegions(); + loadAttractions(); + loadStatusData(); + AttractionMenu.load(); + loadDatabase(); + } + + private static void loadRegions() { + YamlConfig config = ThemeParkPlugin.getInstance().getRegions(); + FileConfiguration fc = config.getConfig(); + if(!fc.contains("regions")) + return; + + ConfigurationSection section = fc.getConfigurationSection("regions"); + if(section == null) + return; + + for(String id : section.getKeys(false)) { + if(API.isRegion(id)) + continue; + + String path = "regions."+id+"."; + String name = fc.getString(path+"name"); + List lore = fc.getStringList(path+"lore"); + if(fc.contains(path+"material")) { + Material material = Material.getMaterial(fc.getString(path + "material")); + short data = Short.parseShort(fc.getString(path + "data")); + Region region = new Region(id, name, lore, material, data); + API.addRegion(region); + continue; + } + + Region region = new Region(id, name, lore); + API.addRegion(region); + } + } + + public static YamlConfig addRegion(String id) { + if(!API.isRegion(id)) + return null; + + File root = new File(ThemeParkPlugin.getInstance().getDataFolder(), "attractions"); + if(!root.exists()) + if(!root.mkdirs()) + return null; + + YamlConfig config = new YamlConfig("attractions/"+id, ThemeParkPlugin.getInstance()); + configs.put(id, config); + return config; + } + + private static void loadAttractions() { + File root = new File(ThemeParkPlugin.getInstance().getDataFolder(), "attractions"); + if(!root.exists()) + if(!root.mkdirs()) + return; + + try { + for(Region region : API.getRegions().values()) { + YamlConfig config = new YamlConfig("attractions/"+region.getId(), ThemeParkPlugin.getInstance()); + configs.put(region.getId(), config); + FileConfiguration fc = config.getConfig(); + if(fc == null) + continue; + + if(!fc.contains("attractions")) { + fc.set("attractions", new ArrayList<>()); + Bukkit.getScheduler().runTaskAsynchronously(ThemeParkPlugin.getInstance(), config::save); + continue; + } + + ConfigurationSection section = fc.getConfigurationSection("attractions"); + if(section == null) + return; + + for(String id : section.getKeys(false)) { + if(API.isAttraction(id)) + continue; + + String path = "attractions."+id+"."; + String name = fc.getString(path+"name"); + Type type = Type.getType(fc.getString(path+"type")); + if(type == null) + continue; + + Status status = Status.getStatus(fc.getString(path+"status")); + if(!type.containsStatus(status)) { + status = type.getDefault(); + fc.set(path+"status", status.toString()); + } + + Location location = LocationSerializer.toLocation(fc.getString(path+".location")); + Attraction attraction = new Attraction(id, name, region.getId(), location, type, status); + API.addAttraction(attraction); + } + + Bukkit.getScheduler().runTaskAsynchronously(ThemeParkPlugin.getInstance(), config::save); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + private static void loadStatusData() { + YamlConfig config = ThemeParkPlugin.getInstance().getSettings(); + for(Status status : Status.values()) { + String name = "UNKNOWN"; + if(config.getConfig().contains(status.toString() + ".name")) + name = config.getConfig().getString(status.toString() + ".name"); + + Material material = Material.STAINED_CLAY; + if(config.getConfig().contains(status.toString() + ".material")) { + material = Material.getMaterial(config.getConfig().getString(status.toString() + ".material")); + if(material == null) + material = Material.STAINED_CLAY; + } + + short data = 0; + if(config.getConfig().contains(status.toString() + ".data")) { + String string = config.getConfig().getString(status.toString() + ".data"); + if (!string.isEmpty()) + data = Short.parseShort(string); + } + + boolean teleport = false; + if(config.getConfig().contains(status.toString() + ".teleport")) + teleport = config.getConfig().getBoolean(status.toString() + ".teleport"); + + StatusManager.addStatus(status, name, material, data, teleport); + } + } + + private static void loadDatabase() { + DB db = DBManager.getDatabase("public"); + if(db == null) + return; + + boolean b = false; + StringBuilder builder = new StringBuilder(); + for(Region region : API.getRegions().values()) { + if(b) { + builder.append(","); + } else { + b = true; + } + + builder.append("(?,?)"); + } + + + int i = 1; + HashMap obj = new HashMap<>(); + for(Region region : API.getRegions().values()) { + obj.put(i++, region.getId()); + obj.put(i++, region.getName()); + } + + db.execute("INSERT INTO region(id, name) VALUES "+builder.toString()+" ON DUPLICATE KEY UPDATE name=VALUES(name)", obj); + b = false; + builder = new StringBuilder(); + for(Attraction attraction : API.getAttractions().values()) { + if(b) { + builder.append(","); + } else { + b = true; + } + + builder.append("(?, ?, ?, ?, ?)"); + } + + i = 1; + obj = new HashMap<>(); + for(Attraction attraction : API.getAttractions().values()) { + obj.put(i++, attraction.getId()); + obj.put(i++, attraction.getName()); + obj.put(i++, attraction.getRegionId()); + obj.put(i++, attraction.getType().toString()); + obj.put(i++, attraction.getStatus().toString()); + } + + + db.execute("INSERT INTO attraction(id, name, region_id, type, status) VALUES "+builder.toString()+" ON DUPLICATE KEY UPDATE name=VALUES(name), region_id=VALUES(region_id), type=VALUES(type), status=VALUES(status)", obj); + b = false; + builder = new StringBuilder(); + for(Status status : Status.values()) { + if(b) { + builder.append(","); + } else { + b = true; + } + + builder.append("(?, ?)"); + } + + i = 1; + obj = new HashMap<>(); + for(Status status : Status.values()) { + obj.put(i++, status.toString()); + obj.put(i++, StatusManager.getName(status)); + } + + db.execute("INSERT INTO status(statusId, statusName) VALUES "+builder.toString()+" ON DUPLICATE KEY UPDATE statusName=VALUES(statusName)", obj); + } + +} diff --git a/src/main/java/nl/iobyte/themepark/menu/AttractionMenu.java b/src/main/java/nl/iobyte/themepark/menu/AttractionMenu.java new file mode 100644 index 0000000..49bb8d6 --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/menu/AttractionMenu.java @@ -0,0 +1,173 @@ +package nl.iobyte.themepark.menu; + +import com.google.common.collect.Iterables; +import nl.iobyte.menuapi.components.Types; +import nl.iobyte.menuapi.item.MenuItem; +import nl.iobyte.menuapi.multi.MenuPage; +import nl.iobyte.menuapi.multi.MultiMenu; +import nl.iobyte.themepark.ThemeParkPlugin; +import nl.iobyte.themepark.api.API; +import nl.iobyte.themepark.api.attraction.Attraction; +import nl.iobyte.themepark.api.attraction.Region; +import nl.iobyte.themepark.api.attraction.manager.StatusManager; +import nl.iobyte.themepark.api.utils.ItemBuilder; +import nl.iobyte.themepark.menu.actions.PageAction; +import nl.iobyte.themepark.menu.actions.TPAction; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; + +public class AttractionMenu { + + private static MultiMenu menu; + private static HashMap reg_slots = new HashMap<>(); + private static HashMap att_slots = new HashMap<>(); + + public static void load() { + LinkedHashMap regions = new LinkedHashMap<>(); + LinkedHashMap> attractions = new LinkedHashMap<>(); + for(Region region : API.getRegions().values()) { + if(region.getAttractions().isEmpty()) { + regions.put(regions.size(), region); + continue; + } + + Iterable> iterable = Iterables.partition(region.getAttractions().values(), 8); + for(List list : iterable) { + regions.put(regions.size(), region); + attractions.put(attractions.size(), list); + } + } + + int region_size = regions.size(); + loadMenu(region_size); + for(int i = 0; i < region_size; i++) { + int start_index = i * 9; + if(region_size > 6) + start_index += (i / 5) * 9; + + int j = 1; + List list = attractions.get(i); + Region region = regions.get(i); + if(region == null) + continue; + + reg_slots.put(region.getId(), start_index); + menu.setItem(start_index, new MenuItem(getRegion(region), true)); + if(list == null) + continue; + + for(Attraction attraction : list) { + MenuItem item = new MenuItem(getAttraction(attraction), true); + item.addActions(Types.NORMAL.getTypesList(), new TPAction(attraction)); + + int slot = start_index + j; + att_slots.put(attraction.getId(), slot); + menu.setItem(slot, item); + j++; + } + } + + if(region_size > 6) { + int page_size = menu.getPageSizes().size(); + for (int i = 0; i < page_size; i++) { + MenuPage page = menu.getPage(i + 1); + int size = page.getSize(); + if (i > 0) + page.setItem(size - 6, getPrevious(i)); + + if (i < (page_size - 1)) + page.setItem(size - 4, getNext(i)); + + ItemBuilder builder = new ItemBuilder(Material.MINECART); + builder.setName("&6Page: &f"+(i + 1)); + page.setItem(size - 5, new MenuItem(builder.getItem(), true)); + } + } + + menu.build(); + } + + public static void updateRegion(Region region) { + if(region == null) + return; + + if(!reg_slots.containsKey(region.getId())) + return; + + int slot = reg_slots.get(region.getId()); + menu.setItem(slot, getRegion(region)); + menu.updateItem(slot); + } + + public static void updateAttraction(Attraction attraction) { + if(attraction == null) + return; + + if(!att_slots.containsKey(attraction.getId())) + return; + + int slot = att_slots.get(attraction.getId()); + menu.setItem(slot, getAttraction(attraction)); + menu.updateItem(slot); + } + + public static void open(Player player) { + open(player, 1); + } + + public static void open(Player player, int page) { + menu.open(player, page); + } + + private static void loadMenu(int regions) { + ArrayList page_sizes = new ArrayList<>(); + if(regions <= 6) { + page_sizes.add(regions * 9); + } else { + int pages = regions / 5; + for (int i = 0; i < pages; i++) + page_sizes.add(54); + + pages = regions % 5; + if (pages > 0) + page_sizes.add(pages * 9 + 9); + } + + menu = new MultiMenu(ThemeParkPlugin.getInstance().getSettings().getConfig().getString("menu-title"), page_sizes); + } + + private static ItemStack getRegion(Region region) { + ItemBuilder builder = new ItemBuilder(region.getMaterial(), 1, region.getData()); + builder.setName(region.getName()).setLore(region.getLore()); + return builder.getItem(); + } + + private static ItemStack getAttraction(Attraction attraction) { + ItemBuilder builder = new ItemBuilder(StatusManager.getMaterial(attraction.getStatus()), 1, StatusManager.getData(attraction.getStatus())); + builder.setName(attraction.getName()).setLore(StatusManager.getName(attraction.getStatus())); + return builder.getItem(); + } + + private static MenuItem getNext(int page) { + return getPage("&6&LNext \u23F5", page + 1); + } + + private static MenuItem getPrevious(int page) { + return getPage("&6&L\u23F4 Previous", page - 1); + } + + private static MenuItem getPage(String text, int page) { + ItemBuilder builder = new ItemBuilder(Material.ARROW); + builder.setName(text); + + MenuItem item = new MenuItem(builder.getItem(), true); + item.addActions(new PageAction(page + 1)); + return item; + } + +} diff --git a/src/main/java/nl/iobyte/themepark/menu/actions/PageAction.java b/src/main/java/nl/iobyte/themepark/menu/actions/PageAction.java new file mode 100644 index 0000000..96ef097 --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/menu/actions/PageAction.java @@ -0,0 +1,20 @@ +package nl.iobyte.themepark.menu.actions; + +import nl.iobyte.menuapi.action.MenuAction; +import nl.iobyte.themepark.menu.AttractionMenu; +import org.bukkit.entity.Player; + +public class PageAction extends MenuAction { + + private int page; + + public PageAction(int page) { + this.page = page; + } + + @Override + public void execute(Player player) { + AttractionMenu.open(player, page); + } + +} diff --git a/src/main/java/nl/iobyte/themepark/menu/actions/TPAction.java b/src/main/java/nl/iobyte/themepark/menu/actions/TPAction.java new file mode 100644 index 0000000..11c7123 --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/menu/actions/TPAction.java @@ -0,0 +1,39 @@ +package nl.iobyte.themepark.menu.actions; + +import nl.iobyte.menuapi.action.MenuAction; +import nl.iobyte.themepark.api.attraction.Attraction; +import nl.iobyte.themepark.api.attraction.manager.StatusManager; +import nl.iobyte.themepark.util.Color; +import nl.iobyte.themepark.util.MessageUtil; +import org.bukkit.Location; +import org.bukkit.entity.Player; + +public class TPAction extends MenuAction { + + private Attraction attraction; + + public TPAction(Attraction attraction) { + this.attraction = attraction; + } + + @Override + public void execute(Player player) { + Location location = attraction.getLocation(); + if(location == null) + return; + + if(!StatusManager.canTeleport(attraction.getStatus())) { + String message = MessageUtil.getMessage("attraction.teleport.status"); + message = message.replace("{name}", attraction.getName()); + message = message.replace("{status}", StatusManager.getName(attraction.getStatus())); + player.sendMessage(Color.color(message)); + return; + } + + player.teleport(location); + String message = MessageUtil.getMessage("attraction.teleport.success"); + message = message.replace("{name}", attraction.getName()); + player.sendMessage(Color.color(message)); + } + +} diff --git a/src/main/java/nl/iobyte/themepark/ridecount/RideCountAPI.java b/src/main/java/nl/iobyte/themepark/ridecount/RideCountAPI.java new file mode 100644 index 0000000..a45e8f6 --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/ridecount/RideCountAPI.java @@ -0,0 +1,326 @@ +package nl.iobyte.themepark.ridecount; + +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import nl.iobyte.menuapi.map.WeakConcurrentHashMap; +import nl.iobyte.themepark.ThemeParkPlugin; +import nl.iobyte.themepark.api.API; +import nl.iobyte.themepark.api.attraction.Attraction; +import nl.iobyte.themepark.api.ridecount.AttractionCount; +import nl.iobyte.themepark.api.ridecount.CountManager; +import nl.iobyte.themepark.database.DB; +import nl.iobyte.themepark.database.DBManager; +import nl.iobyte.workchain.components.Work; +import org.bukkit.Bukkit; +import org.bukkit.entity.Player; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.URL; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.util.HashMap; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; + +public class RideCountAPI { + + private static WeakConcurrentHashMap data = new WeakConcurrentHashMap<>(5 * 60 * 1000); + + public static CompletableFuture getCount(String name, Attraction attraction) { + CompletableFuture fc = new CompletableFuture<>(); + if(name == null || name.isEmpty() || attraction == null) { + fc.complete(-1); + return fc; + } + + if(!API.isAttraction(attraction.getId())) { + fc.complete(-1); + return fc; + } + + getData(name).thenAccept(uuid -> { + if(uuid == null) { + fc.complete(-1); + return; + } + + getCount(uuid, attraction).thenAccept(fc::complete); + }); + return fc; + } + + public static CompletableFuture getCount(UUID uuid, Attraction attraction) { + CompletableFuture fc = new CompletableFuture<>(); + if(uuid == null || attraction == null) { + fc.complete(-1); + return fc; + } + + if(!API.isAttraction(attraction.getId())) { + fc.complete(-1); + return fc; + } + + AttractionCount count = CountManager.getCounter(attraction.getId()); + if(count != null && count.hasCount(uuid)) { + fc.complete(count.getCount(uuid).getCount()); + return fc; + } + + Work.firstTask(() -> { + DB db = DBManager.getDatabase("data"); + if(db == null) + return null; + + Connection connection = db.getConnection(); + if(connection == null || connection.isClosed()) + return null; + + PreparedStatement statement = connection.prepareStatement("SELECT points FROM counts WHERE uuid=? AND attraction_id=?"); + statement.setString(1, uuid.toString()); + statement.setString(2, attraction.getId()); + statement.closeOnCompletion(); + return statement.executeQuery(); + }).abortIfNull(() -> fc.complete(-1)).nextTask(result -> { + int i = -1; + while(result.next()) { + int points = result.getInt("points"); + if(points < 0) + continue; + + i = points; + } + + result.close(); + return i; + }).lastTask(fc::complete).setFullExecute(true).execute(); + return fc; + } + + public static CompletableFuture addCount(String name, Attraction attraction, int amount) { + CompletableFuture fc = new CompletableFuture<>(); + if(name == null || name.isEmpty() || attraction == null || amount < 0) { + fc.complete(false); + return fc; + } + + if(!API.isAttraction(attraction.getId())) { + fc.complete(false); + return fc; + } + + getData(name).thenAccept(uuid -> { + if(uuid == null) { + fc.complete(false); + return; + } + + addCount(uuid, attraction, amount).thenAccept(fc::complete); + }); + return fc; + } + + public static CompletableFuture addCount(UUID uuid, Attraction attraction, int amount) { + CompletableFuture fc = new CompletableFuture<>(); + if(uuid == null || attraction == null || amount < 0) { + fc.complete(false); + return fc; + } + + if(!API.isAttraction(attraction.getId())) { + fc.complete(false); + return fc; + } + + AttractionCount count = CountManager.getCounter(attraction.getId()); + if(count != null && count.hasCount(uuid)) + Bukkit.getScheduler().runTask(ThemeParkPlugin.getInstance(), () -> count.addCount(uuid, amount)); + + Work.firstTask(() -> { + DB db = DBManager.getDatabase("data"); + if(db == null) + return null; + + Connection connection = db.getConnection(); + if(connection == null || connection.isClosed()) + return null; + + PreparedStatement statement = connection.prepareStatement("INSERT INTO counts(uuid, attraction_id, points) VALUES (?, ?, ?) ON CONFLICT(uuid, attraction_id) DO UPDATE SET points=points+?"); + statement.setString(1, uuid.toString()); + statement.setString(2, attraction.getId()); + statement.setInt(3, amount); + statement.setInt(4, amount); + statement.closeOnCompletion(); + return statement.executeUpdate() > 0; + }).abortIfNull(() -> fc.complete(false)).lastTask(fc::complete).setFullExecute(true).execute(); + return fc; + } + + private static CompletableFuture getData(String name) { + CompletableFuture fc = new CompletableFuture<>(); + Bukkit.getScheduler().runTaskAsynchronously(ThemeParkPlugin.getInstance(), () -> { + if(data.containsKey(name)) { + fc.complete(UUID.fromString(data.get(name))); + return; + } + + Player player = Bukkit.getPlayerExact(name); + if(player != null) { + UUID uuid = player.getUniqueId(); + data.put(name, uuid.toString()); + fc.complete(uuid); + return; + } + + try { + JsonObject object = sendGETRequestJSON("https://api.mojang.com/users/profiles/minecraft/" + name); + if(object == null) { + data.put(name, null); + fc.complete(null); + return; + } + + String id = object.get("id").getAsString(); + if(id == null || id.isEmpty()) { + data.put(name, null); + fc.complete(null); + return; + } + + id = fixUUID(id); + UUID uuid = UUID.fromString(id); + data.put(name, id); + fc.complete(uuid); + } catch (Exception e) { + data.put(name, null); + fc.complete(null); + } + }); + + return fc; + } + + public static CompletableFuture> getTop(Attraction attraction, boolean week) { + CompletableFuture> fc = new CompletableFuture<>(); + if(attraction == null) { + fc.complete(null); + return fc; + } + + if(!API.isAttraction(attraction.getId())) { + fc.complete(null); + return fc; + } + + Work.firstTask(() -> { + DB db = DBManager.getDatabase("data"); + if(db == null) + return null; + + Connection connection = db.getConnection(); + if(connection == null || connection.isClosed()) + return null; + + String timeSort = week ? "YEARWEEK(created_at, 1)=YEARWEEK(CURDATE(), 1)" : "DAYOFYEAR(created_at)=DAYOFYEAR(CURDATE())"; + PreparedStatement statement = connection.prepareStatement("SELECT uuid, SUM(points) AS count FROM ridecount WHERE attraction_id=? AND "+timeSort+" AND YEAR(created_at)=YEAR(CURDATE()) GROUP BY uuid ORDER BY points DESC LIMIT 3"); + statement.setString(1, attraction.getId()); + statement.closeOnCompletion(); + return statement.executeQuery(); + }).abortIfNull(() -> fc.complete(null)).nextTask(result -> { + HashMap top = new HashMap<>(); + while(result.next()) { + UUID uuid = UUID.fromString(result.getString("uuid")); + int points = result.getInt("points"); + if(points < 0) + continue; + + top.put(uuid, points); + } + + result.close(); + return top; + }).lastTask(fc::complete).setFullExecute(true).execute(); + return fc; + } + + public static CompletableFuture> getTopTotal(Attraction attraction) { + CompletableFuture> fc = new CompletableFuture<>(); + if(attraction == null) { + fc.complete(null); + return fc; + } + + if(!API.isAttraction(attraction.getId())) { + fc.complete(null); + return fc; + } + + Work.firstTask(() -> { + DB db = DBManager.getDatabase("data"); + if(db == null) + return null; + + Connection connection = db.getConnection(); + if(connection == null || connection.isClosed()) + return null; + + PreparedStatement statement = connection.prepareStatement("SELECT uuid, SUM(points) AS count FROM ridecount WHERE attraction_id=? GROUP BY uuid ORDER BY points DESC LIMIT 3"); + statement.setString(1, attraction.getId()); + statement.closeOnCompletion(); + return statement.executeQuery(); + }).abortIfNull(() -> fc.complete(null)).nextTask(result -> { + HashMap top = new HashMap<>(); + while(result.next()) { + UUID uuid = UUID.fromString(result.getString("uuid")); + int points = result.getInt("points"); + if(points < 0) + continue; + + top.put(uuid, points); + } + + result.close(); + return top; + }).lastTask(fc::complete).setFullExecute(true).execute(); + return fc; + } + + private static String fixUUID(String uuid) { + if(uuid == null || uuid.isEmpty()) + return null; + + StringBuilder sb = new StringBuilder(uuid); + sb.insert(8, "-"); + sb = new StringBuilder(sb.toString()); + sb.insert(13, "-"); + sb = new StringBuilder(sb.toString()); + sb.insert(18, "-"); + sb = new StringBuilder(sb.toString()); + sb.insert(23, "-"); + + return sb.toString(); + } + + private static JsonObject sendGETRequestJSON(String uri) throws IOException { + URL url = new URL(uri); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.setRequestMethod("GET"); + con.setRequestProperty("User-Agent", "Mozilla/5.0"); + int code = con.getResponseCode(); + if (code == 404) + return null; + + BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); + String inputLine; + StringBuilder response = new StringBuilder(); + while ((inputLine = in.readLine()) != null) + response.append(inputLine); + + in.close(); + JsonParser parser = new JsonParser(); + return parser.parse(response.toString()).getAsJsonObject(); + } + +} diff --git a/src/main/java/nl/iobyte/themepark/util/Color.java b/src/main/java/nl/iobyte/themepark/util/Color.java new file mode 100644 index 0000000..90583d3 --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/util/Color.java @@ -0,0 +1,11 @@ +package nl.iobyte.themepark.util; + +import org.bukkit.ChatColor; + +public class Color { + + public static String color(String str) { + return ChatColor.translateAlternateColorCodes('&', str); + } + +} diff --git a/src/main/java/nl/iobyte/themepark/util/MessageUtil.java b/src/main/java/nl/iobyte/themepark/util/MessageUtil.java new file mode 100644 index 0000000..4158283 --- /dev/null +++ b/src/main/java/nl/iobyte/themepark/util/MessageUtil.java @@ -0,0 +1,82 @@ +package nl.iobyte.themepark.util; + +import net.md_5.bungee.api.ChatColor; +import net.md_5.bungee.api.chat.BaseComponent; +import net.md_5.bungee.api.chat.ClickEvent; +import net.md_5.bungee.api.chat.HoverEvent; +import net.md_5.bungee.api.chat.TextComponent; +import nl.iobyte.themepark.ThemeParkPlugin; +import nl.iobyte.themepark.config.YamlConfig; + +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class MessageUtil { + + private static YamlConfig config = ThemeParkPlugin.getInstance().getMessages(); + + public static boolean isMessage(String string) { + return config.getConfig().contains(string); + } + + public static String getMessage(String string) { + if(!isMessage(string)) + return ""; + + String message = config.getConfig().getString(string); + if(isMessage("prefix")) + message = message.replace("{prefix}", config.getConfig().getString("prefix")); + + return message; + } + + public static BaseComponent parseTP(String string, HashMap change, ClickEvent click, HoverEvent hover) { + String message = getMessage(string); + for(Map.Entry entrySet : change.entrySet()) + message = message.replace(entrySet.getKey(), entrySet.getValue()); + + message = Color.color(message); + if(click != null) { + String regex = "(.*)\\[TP\\](.*?)\\[/TP\\](.*)"; + Pattern p = Pattern.compile(regex); + Matcher m = p.matcher(message); + while (m.find()) { + String first = m.group(1); + String middle = m.group(2); + String last = m.group(3); + + BaseComponent[] components = TextComponent.fromLegacyText(first); + BaseComponent component = components[0]; + for(int i = 1; i < components.length; i++) + component.addExtra(components[i]); + + components = TextComponent.fromLegacyText(middle); + BaseComponent compClick = components[0]; + for(int i = 1; i < components.length; i++) + compClick.addExtra(components[i]); + + compClick.setClickEvent(click); + if(hover != null) + compClick.setHoverEvent(hover); + + component.addExtra(compClick); + components = TextComponent.fromLegacyText(last); + BaseComponent compLast = components[0]; + for (BaseComponent baseComponent : components) component.addExtra(baseComponent); + + component.addExtra(compLast); + return component; + } + } + + BaseComponent[] components = TextComponent.fromLegacyText(message); + BaseComponent component = components[0]; + for(int i = 1; i < components.length; i++) + component.addExtra(components[i]); + + return component; + } + +} diff --git a/src/main/resources/attraction.yml b/src/main/resources/attraction.yml deleted file mode 100644 index 1eb28e1..0000000 --- a/src/main/resources/attraction.yml +++ /dev/null @@ -1,40 +0,0 @@ -region: - global: - name: "&5Global" - lore: - - "&5Global Locations" - fantasy: - name: "&aFantasy" - lore: - - "&aFantasy Themed rides" - multiple: - name: "&fMultiple" - lore: - - "&fYou can have" - - "&fMultiple lines" - - "&fWith Lore" - -attraction: - ride1: - name: "&aRide #1" - region_id: "fantasy" - type: RIDE - status: CLOSED - location: "world:0:0:0" - show1: - name: "&aFirst Show" - region_id: "fantasy" - type: SHOW - status: INACTIVE - location: "world:0:0:0" - global1: - name: "&aFirst Global" - region_id: "global" - type: GLOBAL - location: "world:0:0:0" - random: - name: "&fRandom" - type: RIDE - status: CLOSED - region_id: multiple - location: "world:0:0:0" \ No newline at end of file diff --git a/src/main/resources/message.yml b/src/main/resources/message.yml deleted file mode 100644 index 9bc27ca..0000000 --- a/src/main/resources/message.yml +++ /dev/null @@ -1,42 +0,0 @@ -prefix: "&6Themepark&f:" - -onlyplayers: "{prefix} &4Only players can use this command" -nopermission: "{prefix} &4You don't have permission to use this command" -noplayer: "{prefix} &4No player found with name: {name}" -nonumber: "&6Themepark&f: &4{number} &4is not a valid number" - -reloaded: "{prefix} &aThe plugin has been reloaded" - -menu: - item: - enabled: "&aENABLED" - disabled: "&4DISABLED" - toggle: "{prefix} &aMenu item has been {status}" - -region: - not: "{prefix} &4There is no region with ID: {id}" - changed: - name: "{prefix} &aChanged the name tho: {name} &aFor region: {id}" - lore: "{prefix} &aChanged the lore with index &f{index} &ato: {lore} &afor region: &f{id}" - -status: - not: "{prefix} &4There is no status named: {status}" - -attraction: - notfound: "&4No attractions found" - list: "&6ID: &f{id} &6Name: &f{name} &6Region: &f{region} &6Status: &f{status}" - not: "{prefix} &4There is no attraction with ID: {id}" - nostatus: "{prefix} &4Attraction: {name} &4has no status named: {status}" - changed: - status: - CONSTRUCTION: "{prefix} &fAttraction: {name} &fis now &7Under Construction" - OPEN: "{prefix} &fAttraction: {name} &fhas been &aOpened" - CLOSED: "{prefix} &fAttraction: {name} &fhas been &4Closed" - MAINTENANCE: "{prefix} &fAttraction: {name} &fis now in &6Maintenance" - MALFUNCTION: "{prefix} &fAttraction: {name} &fhas been closed due to a &5Malfunction" - ACTIVE: "{prefix} &fShow: {name} &fis now &aActive" - INACTIVE: "{prefix} &fShow: {name} &fis now &4In Active" - location: "&6ThemePark&f: &aChanged locatation of attraction: {name} &ato your current location" - teleport: - status: "{prefix} &4It's not possible to teleport to {name} &4because it's {status}" - success: "{prefix} &ayou have been teleported to the attraction: {name}" \ No newline at end of file diff --git a/src/main/resources/messages.yml b/src/main/resources/messages.yml new file mode 100644 index 0000000..6890151 --- /dev/null +++ b/src/main/resources/messages.yml @@ -0,0 +1,47 @@ +prefix: "&6<hemePark&f:" +onlyplayers: "{prefix} &4Only players can use this command" +nopermission: "{prefix} &4You don't have permission to use this command" +noplayer: "{prefix} &4No player found with name: {name}" +nonumber: "&6Themepark&f: &4{number} &4is not a valid number" + +menu-item: + enabled: "&aENABLED" + disabled: "&4DISABLED" + toggle: "{prefix} &aMenu item has been {status}" + +no-type: "{prefix} &4There is no type named: {type}" +no-status: "{prefix} &4There is no status named: {status}" + +region: + not: "{prefix} &4There is no region with ID: {id}" + create: "{prefix} &aSuccessfully created region with name: &r{name}" + exists: "{prefix} &4There already is a region with ID: {id}" + name: "{prefix} &aChanged name of region to: &r{name}" + +ridecount: + count: "{prefix} &fplayer has a ridecount of: &6{count} &ffor attraction {name}" + set: "{prefix} &fChanged ridecount of player to: &6{count} &ffor attraction {name}" + add: "{prefix} &fAdded &6{count} &fto ridecount of player for attraction {name}" + message: "{prefix} &fYou have ridden the attraction {name} &6{amount}&Lx &r&ffor a total of &6{total}&Lx" + +attraction: + exists: "{prefix} &4There already is a region with ID: {id}" + create: "{prefix} &aSuccessfully created attraction with name: &r{name}" + notfound: "&4No attractions found" + list: "&6ID: &f{id} &6Name: &f{name} &6Region: &f{region} &6Status: &f{status}" + not: "{prefix} &4There is no attraction with ID: {id}" + nostatus: "{prefix} &4Attraction: {name} &4has no status named: {status}" + changed: + hover: "&6Click to Warp" + CONSTRUCTION: "{prefix} &fAttraction: {name} &fis now &7Under Construction" + OPEN: "{prefix} &fAttraction: [TP]{name}[/TP] &fhas been &aOpened" + CLOSED: "{prefix} &fAttraction: {name} &fhas been &4Closed" + MAINTENANCE: "{prefix} &fAttraction: {name} &fis now in &6Maintenance" + MALFUNCTION: "{prefix} &fAttraction: {name} &fhas been closed due to a &5Malfunction" + ACTIVE: "{prefix} &fShow: {name} &fis now &aActive" + INACTIVE: "{prefix} &fShow: {name} &fis now &4In Active" + location: "{prefix} &aChanged locatation of attraction: {name} &ato your current location" + name: "{prefix} &aChanged name of attraction to: &r{name}" + teleport: + status: "{prefix} &4It's not possible to teleport to {name} &4because it's {status}" + success: "{prefix} &ayou have been teleported to the attraction: {name}" \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 6fd307e..2497e1c 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,7 +1,9 @@ name: ThemePark -version: 1.4.1 -main: me.paradoxpixel.themepark.ThemeParkPlugin -author: ParadoxPixel -softdepend: [PlaceholderAPI,dynmap] +version: 2.0 +main: nl.iobyte.themepark.ThemeParkPlugin +author: IOByte +website: "https://www.iobyte.nl/" +softdepend: [dynmap,Multiverse-Core,MultiWorld] commands: - status: \ No newline at end of file + status: + ridecount: \ No newline at end of file diff --git a/src/main/resources/regions.yml b/src/main/resources/regions.yml new file mode 100644 index 0000000..e6ce961 --- /dev/null +++ b/src/main/resources/regions.yml @@ -0,0 +1,12 @@ +regions: + region_1: + name: '&4First Region' + lore: + - '&fFirst line of Lore' + material: IRON_BLOCK + data: 0 + region_2: + name: '&4Second Region' + lore: + - '&fFirst line of Lore' + - '&fSecond line of Lore' \ No newline at end of file diff --git a/src/main/resources/settings.yml b/src/main/resources/settings.yml index f188b3d..4d51e3b 100644 --- a/src/main/resources/settings.yml +++ b/src/main/resources/settings.yml @@ -1,37 +1,27 @@ -command: 'themepark' - -sign: - name: "[ThemePark]" - title: "&f[&6ThemePark&f]" - -menu: - title: "&6StatusMenu" - -inventory: - clear: true +command: 'tp' +workers: 2 +menu-title: "&6StatusMenu" item: enabled: true - material: NETHER_STAR - display-name: 'Themepark' + material: 'NETHER_STAR' + data: 0 + display-name: '&6Themepark' slot: 4 + clear: true worlds: - world_nether - world_the_end mysql: enabled: false - host: 'localhost' + host: "localhost" port: 3306 - database: 'database' - user: 'username' - password: 'password' + database: "themepark" + user: "root" + pass: "" url: 'jdbc:mysql://%host%:%port%/%database%?useSSL=false' -region: - material: NAME_TAG - data: 0 - CONSTRUCTION: name: "&7Under Construction" material: STAINED_CLAY