From 7a136e28d1669ba3e9123e0092b21f727afeb21f Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sat, 19 Jan 2019 23:22:15 +0100 Subject: [PATCH] Small Change optimizing future updates --- .../me/paradoxpixel/themepark/api/API.java | 5 -- .../themepark/api/attraction/Attraction.java | 21 +++++ .../event/region/ChangeAttractionEvent.java | 79 +++++++++++++++++++ .../themepark/attraction/AttractionMenu.java | 33 +++++++- .../themepark/command/ThemeParkCommand.java | 1 - 5 files changed, 132 insertions(+), 7 deletions(-) create mode 100644 src/main/java/me/paradoxpixel/themepark/api/event/region/ChangeAttractionEvent.java diff --git a/src/main/java/me/paradoxpixel/themepark/api/API.java b/src/main/java/me/paradoxpixel/themepark/api/API.java index 300367d..900fda3 100644 --- a/src/main/java/me/paradoxpixel/themepark/api/API.java +++ b/src/main/java/me/paradoxpixel/themepark/api/API.java @@ -68,11 +68,6 @@ public class API { regions.remove(id.toLowerCase()); } - public static void clearRegions() { - regions.clear(); - attractions.clear(); - } - public static void addAttraction(Attraction attraction) { if(!isRegion(attraction.getRegion_id())) return; diff --git a/src/main/java/me/paradoxpixel/themepark/api/attraction/Attraction.java b/src/main/java/me/paradoxpixel/themepark/api/attraction/Attraction.java index d8d8ec6..b97f697 100644 --- a/src/main/java/me/paradoxpixel/themepark/api/attraction/Attraction.java +++ b/src/main/java/me/paradoxpixel/themepark/api/attraction/Attraction.java @@ -4,6 +4,7 @@ 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.region.ChangeAttractionEvent; import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.entity.Player; @@ -32,22 +33,42 @@ public class Attraction { return name; } + public void setName(String name) { + 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) { + 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) { + 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); + } + public Status getStatus() { return status; } diff --git a/src/main/java/me/paradoxpixel/themepark/api/event/region/ChangeAttractionEvent.java b/src/main/java/me/paradoxpixel/themepark/api/event/region/ChangeAttractionEvent.java new file mode 100644 index 0000000..a4d767e --- /dev/null +++ b/src/main/java/me/paradoxpixel/themepark/api/event/region/ChangeAttractionEvent.java @@ -0,0 +1,79 @@ +package me.paradoxpixel.themepark.api.event.region; + +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/attraction/AttractionMenu.java b/src/main/java/me/paradoxpixel/themepark/attraction/AttractionMenu.java index 1f4180d..a0cc2cf 100644 --- a/src/main/java/me/paradoxpixel/themepark/attraction/AttractionMenu.java +++ b/src/main/java/me/paradoxpixel/themepark/attraction/AttractionMenu.java @@ -25,14 +25,17 @@ public class AttractionMenu { private static YamlConfig config = ThemeParkPlugin.getInstance().getAttraction(); private static YamlConfig settings = ThemeParkPlugin.getInstance().getSettings(); private static GUI gui; + private static boolean loading = false; private static HashMap index; public static void load() { + loading = true; gui = new GUI(settings.getConfig().getString("menu.title"), 9); index = new HashMap<>(); loadData(); loadItems(); + loading = false; } public static void reload() { @@ -67,6 +70,7 @@ public class AttractionMenu { if(section.getKeys(false).isEmpty()) return; + HashMap regions = API.getRegions(); for(String id : section.getKeys(false)) { if(API.getRegions().size() >= 6) { config.getConfig().set("regions." + id, null); @@ -77,10 +81,20 @@ public class AttractionMenu { String name = config.getConfig().getString("region." + id + ".name"); List lore = config.getConfig().getStringList("region." + id + ".lore"); + 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); } + for(String string : regions.keySet()) + API.removeRegion(string); + if(!config.getConfig().contains("attraction")) return; @@ -91,6 +105,7 @@ public class AttractionMenu { 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(!API.isRegion(region_id)) @@ -104,12 +119,29 @@ public class AttractionMenu { Type type = Type.getType(config.getConfig().getString("attraction." + id + ".type")); Status status = Status.getStatus(config.getConfig().getString("attraction." + id + ".status")); + attractions.remove(id); + 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); + continue; + } + Attraction attraction = new Attraction(id, name, region_id, location, type, status); API.addAttraction(attraction); } + + for(String string : attractions.keySet()) + API.removeAttraction(string); } private static void loadItems() { + if(loading) + return; + if(API.getRegions().values().size() == 0) return; @@ -147,7 +179,6 @@ public class AttractionMenu { } if(!b) { - API.getRegion(attraction.getRegion_id()).removeAttraction(attraction); API.removeAttraction(attraction.getId()); continue; } diff --git a/src/main/java/me/paradoxpixel/themepark/command/ThemeParkCommand.java b/src/main/java/me/paradoxpixel/themepark/command/ThemeParkCommand.java index 9ad8b4a..bcfa800 100644 --- a/src/main/java/me/paradoxpixel/themepark/command/ThemeParkCommand.java +++ b/src/main/java/me/paradoxpixel/themepark/command/ThemeParkCommand.java @@ -180,7 +180,6 @@ public class ThemeParkCommand extends BukkitCommand { ThemeParkPlugin.getInstance().getSigns().reload(); ThemeParkPlugin.getInstance().getSettings().reload(); ThemeParkPlugin.getInstance().getMessage().reload(); - API.clearRegions(); StatusManager.load(); AttractionMenu.load();