diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Info.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Info.java index a08e41cd0..abdc2b2e8 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Info.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Info.java @@ -174,7 +174,7 @@ import java.util.UUID; final String trusted = getPlayerList(plot.trusted); final String denied = getPlayerList(plot.denied); final String rating = String.format("%.1f", DBFunc.getRatings(plot)); - final String flags = "&6" + (StringUtils.join(plot.settings.getFlags(), "").length() > 0 ? StringUtils.join(plot.settings.getFlags(), "&7, &6") : "none"); + final String flags = "&6" + (StringUtils.join(plot.settings.flags, "").length() > 0 ? StringUtils.join(plot.settings.flags, "&7, &6") : "none"); final boolean build = (player == null) || plot.hasRights(player); String owner = "none"; diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Set.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Set.java index 9c8837ac8..8fb61493f 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Set.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Set.java @@ -124,7 +124,7 @@ public class Set extends SubCommand { return false; } if (args.length == 2) { - if (plot.settings.getFlag(args[1].toLowerCase()) == null) { + if (FlagManager.getPlotFlagAbs(plot, args[1].toLowerCase()) == null) { if (PlotMain.worldGuardListener != null) { if (PlotMain.worldGuardListener.str_flags.contains(args[1].toLowerCase())) { PlotMain.worldGuardListener.removeFlag(plr, plr.getWorld(), plot, args[1]); @@ -134,17 +134,13 @@ public class Set extends SubCommand { PlayerFunctions.sendMessage(plr, C.FLAG_NOT_IN_PLOT); return false; } - final Flag flag = plot.settings.getFlag(args[1].toLowerCase()); - final PlotFlagRemoveEvent event = new PlotFlagRemoveEvent(flag, plot); - Bukkit.getServer().getPluginManager().callEvent(event); - if (event.isCancelled()) { + + boolean result = FlagManager.removePlotFlag(plot, args[1].toLowerCase()); + + if (!result) { PlayerFunctions.sendMessage(plr, C.FLAG_NOT_REMOVED); - event.setCancelled(true); return false; } - final java.util.Set newflags = FlagManager.removeFlag(plot.settings.getFlags(), args[1].toLowerCase()); - plot.settings.setFlags(newflags); - DBFunc.setFlags(plr.getWorld().getName(), plot, newflags); PlayerFunctions.sendMessage(plr, C.FLAG_REMOVED); PlotListener.plotEntry(plr, plot); return true; @@ -163,15 +159,11 @@ public class Set extends SubCommand { } final Flag flag = new Flag(FlagManager.getFlag(args[1].toLowerCase(), true), value); - final PlotFlagAddEvent event = new PlotFlagAddEvent(flag, plot); - Bukkit.getServer().getPluginManager().callEvent(event); - if (event.isCancelled()) { + boolean result = FlagManager.addPlotFlag(plot, flag); + if (!result) { PlayerFunctions.sendMessage(plr, C.FLAG_NOT_ADDED); - event.setCancelled(true); return false; } - plot.settings.addFlag(flag); - DBFunc.setFlags(plr.getWorld().getName(), plot, plot.settings.getFlags()); PlayerFunctions.sendMessage(plr, C.FLAG_ADDED); PlotListener.plotEntry(plr, plot); return true; diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java index e08704ea6..4168d64b5 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/database/SQLManager.java @@ -678,7 +678,7 @@ public class SQLManager implements AbstractDB { PlotMain.sendConsoleSenderMessage("&cPlot " + id + " had an invalid flag. A fix has been attempted."); setFlags(id, flags.toArray(new Flag[0])); } - plot.settings.setFlags(flags); + FlagManager.setPlotFlags(plot, flags); } else { PlotMain.sendConsoleSenderMessage("&cPLOT " + id + " in plot_settings does not exist. Please create the plot or remove this entry."); } @@ -732,7 +732,6 @@ public class SQLManager implements AbstractDB { @Override public void setFlags(final String world, final Plot plot, final Set flags) { - plot.settings.setFlags(flags); final StringBuilder flag_string = new StringBuilder(); int i = 0; for (final Flag flag : flags) { diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/FlagManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/FlagManager.java index cdc59283e..c0e863458 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/FlagManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/FlagManager.java @@ -21,11 +21,19 @@ package com.intellectualcrafters.plot.flag; +import com.intellectualcrafters.plot.PlotMain; +import com.intellectualcrafters.plot.database.DBFunc; +import com.intellectualcrafters.plot.events.PlotFlagAddEvent; +import com.intellectualcrafters.plot.events.PlotFlagRemoveEvent; import com.intellectualcrafters.plot.object.Plot; +import com.intellectualcrafters.plot.object.PlotWorld; +import com.sun.istack.internal.NotNull; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; @@ -56,6 +64,100 @@ import java.util.Set; return (getFlag(flag.getKey()) == null) && flags.add(flag); } + /** + * Get the value of a flag for a plot (respects flag defaults) + * @param plot + * @param flag + * @return + */ + public static Flag getPlotFlag(Plot plot, String flag) { + ArrayList flags = new ArrayList<>(); + flags.addAll(plot.settings.flags); + PlotWorld plotworld = PlotMain.getWorldSettings(plot.world); + flags.addAll(Arrays.asList(plotworld.DEFAULT_FLAGS)); + for (final Flag myflag : flags) { + if (myflag.getKey().equals(flag)) { + return myflag; + } + } + return null; + } + + /** + * Get the value of a flag for a plot (ignores flag defaults) + * @param plot + * @param flag + * @return + */ + public static Flag getPlotFlagAbs(Plot plot, String flag) { + for (final Flag myflag : plot.settings.flags) { + if (myflag.getKey().equals(flag)) { + return myflag; + } + } + return null; + } + + /** + * Add a flag to a plot + * @param plot + * @param flag + */ + public static boolean addPlotFlag(Plot plot, final Flag flag) { + final PlotFlagAddEvent event = new PlotFlagAddEvent(flag, plot); + Bukkit.getServer().getPluginManager().callEvent(event); + if (event.isCancelled()) { + return false; + } + final Flag hasFlag = getPlotFlag(plot, flag.getKey()); + if (hasFlag != null) { + plot.settings.flags.remove(hasFlag); + } + plot.settings.flags.add(flag); + DBFunc.setFlags(plot.world, plot, plot.settings.flags); + return true; + } + + /** + * + * @param plot + * @return + */ + public static Set getPlotFlags(Plot plot) { + Set plotflags = plot.settings.flags; + PlotWorld plotworld = PlotMain.getWorldSettings(plot.world); + plotflags.addAll(Arrays.asList(plotworld.DEFAULT_FLAGS)); + return plotflags; + } + + public static boolean removePlotFlag(Plot plot, String flag) { + final Flag hasFlag = getPlotFlag(plot, flag); + if (hasFlag != null) { + Flag flagObj = FlagManager.getPlotFlagAbs(plot, flag); + if (flagObj != null) { + final PlotFlagRemoveEvent event = new PlotFlagRemoveEvent(flagObj, plot); + Bukkit.getServer().getPluginManager().callEvent(event); + if (event.isCancelled()) { + return false; + } + plot.settings.flags.remove(hasFlag); + DBFunc.setFlags(plot.world, plot, plot.settings.flags); + return true; + } + } + return false; + } + + public static void setPlotFlags(Plot plot, Set flags) { + if (flags == null) { + plot.settings.flags = new HashSet<>(); + DBFunc.setFlags(plot.world, plot, plot.settings.flags); + return; + } + plot.settings.flags = flags; + DBFunc.setFlags(plot.world, plot, plot.settings.flags); + } + public static Flag[] removeFlag(final Flag[] flags, final String r) { final Flag[] f = new Flag[flags.length - 1]; int index = 0; @@ -174,8 +276,8 @@ import java.util.Set; * * @return List (AbstractFlag) */ - public static List getPlotFlags(final Plot plot) { - final Set plotFlags = plot.settings.getFlags(); + public static List getPlotAbstractFlags(final Plot plot) { + final Set plotFlags = getPlotFlags(plot); final List flags = new ArrayList<>(); for (final Flag flag : plotFlags) { flags.add(flag.getAbstractFlag()); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/ForceFieldListener.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/ForceFieldListener.java index 6760ee76e..5d6748ef5 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/ForceFieldListener.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/ForceFieldListener.java @@ -21,8 +21,10 @@ package com.intellectualcrafters.plot.listeners; +import com.intellectualcrafters.plot.flag.FlagManager; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.util.PlayerFunctions; + import org.bukkit.Location; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; @@ -102,7 +104,7 @@ public class ForceFieldListener implements Listener { return; } final Plot plot = PlayerFunctions.getCurrentPlot(player); - if ((plot.settings.getFlag("forcefield") != null) && plot.settings.getFlag("forcefield").getValue().equals("true")) { + if ((FlagManager.getPlotFlag(plot, "forcefield") != null) && FlagManager.getPlotFlag(plot, "forcefield").getValue().equals("true")) { if (!PlotListener.booleanFlag(plot, "forcefield")) { if (plot.hasRights(player)) { final Set players = getNearbyPlayers(player, plot); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java index 62070fe67..7778735d9 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java @@ -26,6 +26,7 @@ import com.intellectualcrafters.plot.commands.Setup; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.Settings; import com.intellectualcrafters.plot.database.DBFunc; +import com.intellectualcrafters.plot.flag.FlagManager; import com.intellectualcrafters.plot.object.*; import com.intellectualcrafters.plot.util.PlayerFunctions; import com.intellectualcrafters.plot.util.PlotHelper; @@ -403,7 +404,7 @@ import java.util.UUID; if (PlotMain.booleanFlags.containsKey(event.getClickedBlock().getType())) { final String flag = PlotMain.booleanFlags.get(event.getClickedBlock().getType()); - if ((plot.settings.getFlag(flag) != null) && getFlagValue(plot.settings.getFlag(flag).getValue())) { + if ((FlagManager.getPlotFlag(plot, flag) != null) && getFlagValue(FlagManager.getPlotFlag(plot, flag).getValue())) { return; } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlotListener.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlotListener.java index 65059814c..24acc02db 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlotListener.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlotListener.java @@ -24,7 +24,6 @@ package com.intellectualcrafters.plot.listeners; import com.intellectualcrafters.plot.PlotMain; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.config.Settings; -import com.intellectualcrafters.plot.database.DBFunc; import com.intellectualcrafters.plot.events.PlayerEnterPlotEvent; import com.intellectualcrafters.plot.events.PlayerLeavePlotEvent; import com.intellectualcrafters.plot.flag.FlagManager; @@ -142,7 +141,7 @@ import java.util.UUID; } public static boolean booleanFlag(final Plot plot, final String flag) { - return (plot.settings.getFlag(flag) != null) && getBooleanFlag(plot.settings.getFlag(flag).getValue()).equals("true"); + return (FlagManager.getPlotFlag(plot, flag) != null) && getBooleanFlag(FlagManager.getPlotFlag(plot, flag).getValue()).equals("true"); } private static String getBooleanFlag(final String value) { @@ -177,23 +176,22 @@ import java.util.UUID; public static void plotEntry(final Player player, final Plot plot) { if (plot.hasOwner()) { - if (plot.settings.getFlag("gamemode") != null) { - player.setGameMode(getGameMode(plot.settings.getFlag("gamemode").getValue())); + if (FlagManager.getPlotFlag(plot, "gamemode") != null) { + player.setGameMode(getGameMode(FlagManager.getPlotFlag(plot, "gamemode").getValue())); } - if (plot.settings.getFlag("fly") != null) { - player.setAllowFlight(getFlagValue(plot.settings.getFlag("fly").getValue())); + if (FlagManager.getPlotFlag(plot, "fly") != null) { + player.setAllowFlight(getFlagValue(FlagManager.getPlotFlag(plot, "fly").getValue())); } - if (plot.settings.getFlag("time") != null) { + if (FlagManager.getPlotFlag(plot, "time") != null) { try { - final Long time = Long.parseLong(plot.settings.getFlag("time").getValue()); + final Long time = Long.parseLong(FlagManager.getPlotFlag(plot, "time").getValue()); player.setPlayerTime(time, true); } catch (final Exception e) { - plot.settings.setFlags(FlagManager.removeFlag(plot.settings.getFlags(), "time")); - DBFunc.setFlags(plot.world, plot, plot.settings.getFlags()); + FlagManager.removePlotFlag(plot, "time"); } } - if (plot.settings.getFlag("weather") != null) { - player.setPlayerWeather(getWeatherType(plot.settings.getFlag("weather").getValue())); + if (FlagManager.getPlotFlag(plot, "weather") != null) { + player.setPlayerWeather(getWeatherType(FlagManager.getPlotFlag(plot, "weather").getValue())); } if ((booleanFlag(plot, "titles") || Settings.TITLES) && (C.TITLE_ENTERED_PLOT.s().length() > 2)) { final String sTitleMain = C.TITLE_ENTERED_PLOT.s().replaceAll("%x%", plot.id.x + "").replaceAll("%y%", plot.id.y + "").replaceAll("%world%", plot.world + ""); @@ -218,16 +216,16 @@ import java.util.UUID; final PlayerLeavePlotEvent callEvent = new PlayerLeavePlotEvent(player, plot); Bukkit.getPluginManager().callEvent(callEvent); } - if (plot.settings.getFlag("fly") != null) { + if (FlagManager.getPlotFlag(plot, "fly") != null) { player.setAllowFlight(Bukkit.getAllowFlight()); } - if (plot.settings.getFlag("gamemode") != null) { + if (FlagManager.getPlotFlag(plot, "gamemode") != null) { player.setGameMode(Bukkit.getDefaultGameMode()); } - if (plot.settings.getFlag("time") != null) { + if (FlagManager.getPlotFlag(plot, "time") != null) { player.resetPlayerTime(); } - if (plot.settings.getFlag("weather") != null) { + if (FlagManager.getPlotFlag(plot, "weather") != null) { player.resetPlayerWeather(); } } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlotPlusListener.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlotPlusListener.java index e7440d852..1446d549c 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlotPlusListener.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlotPlusListener.java @@ -24,9 +24,11 @@ package com.intellectualcrafters.plot.listeners; import com.intellectualcrafters.plot.config.C; import com.intellectualcrafters.plot.events.PlayerEnterPlotEvent; import com.intellectualcrafters.plot.events.PlayerLeavePlotEvent; +import com.intellectualcrafters.plot.flag.FlagManager; import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.util.PlayerFunctions; import com.intellectualcrafters.plot.util.UUIDHandler; + import org.bukkit.*; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; @@ -170,8 +172,8 @@ import java.util.*; @EventHandler public void onPlotEnter(final PlayerEnterPlotEvent event) { final Plot plot = event.getPlot(); - if (plot.settings.getFlag("greeting") != null) { - event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', plot.settings.getFlag("greeting").getValue())); + if (FlagManager.getPlotFlag(plot, "greeting") != null) { + event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', FlagManager.getPlotFlag(plot, "greeting").getValue())); } if (booleanFlag(plot, "notify-enter")) { if (plot.hasOwner()) { @@ -204,8 +206,8 @@ import java.util.*; public void onPlotLeave(final PlayerLeavePlotEvent event) { event.getPlayer().playEffect(event.getPlayer().getLocation(), Effect.RECORD_PLAY, 0); final Plot plot = event.getPlot(); - if (plot.settings.getFlag("farewell") != null) { - event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', plot.settings.getFlag("farewell").getValue())); + if (FlagManager.getPlotFlag(plot, "farewell") != null) { + event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', FlagManager.getPlotFlag(plot, "farewell").getValue())); } if (feedRunnable.containsKey(event.getPlayer().getName())) { feedRunnable.remove(event.getPlayer().getName()); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/InfoInventory.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/InfoInventory.java index 3a589eeeb..648691e69 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/InfoInventory.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/InfoInventory.java @@ -55,7 +55,7 @@ public class InfoInventory implements InventoryHolder { final ItemStack helpers = getItem(Material.EMERALD, "&cHelpers", "&cAmount: &6" + this.plot.helpers.size(), "&8Click to view a list of the plot helpers"); final ItemStack trusted = getItem(Material.EMERALD, "&cTrusted", "&cAmount: &6" + this.plot.trusted.size(), "&8Click to view a list of trusted players"); final ItemStack denied = getItem(Material.EMERALD, "&cDenied", "&cAmount: &6" + this.plot.denied.size(), "&8Click to view a list of denied players"); - final ItemStack flags = getItem(Material.EMERALD, "&cFlags", "&cAmount: &6" + this.plot.settings.getFlags().size(), "&8Click to view a list of plot flags"); + final ItemStack flags = getItem(Material.EMERALD, "&cFlags", "&cAmount: &6" + this.plot.settings.flags.size(), "&8Click to view a list of plot flags"); this.inventory.setItem(2, generalInfo); this.inventory.setItem(3, helpers); this.inventory.setItem(4, trusted); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/Plot.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/Plot.java index 1e9208e50..1a65ee042 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/Plot.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/Plot.java @@ -111,7 +111,7 @@ import java.util.UUID; this.settings.setAlias(""); this.settings.setPosition(PlotHomePosition.DEFAULT); this.delete = false; - this.settings.setFlags(new HashSet()); + this.settings.flags = new HashSet(); this.world = world; } @@ -134,7 +134,7 @@ import java.util.UUID; this.settings.setAlias(""); this.settings.setPosition(PlotHomePosition.DEFAULT); this.delete = false; - this.settings.setFlags(new HashSet()); + this.settings.flags = new HashSet(); this.world = world; } @@ -165,9 +165,9 @@ import java.util.UUID; this.settings.setMerged(merged); this.delete = false; if (flags != null) { - this.settings.setFlags(flags); + this.settings.flags = flags; } else { - this.settings.setFlags(new HashSet()); + this.settings.flags = new HashSet(); } this.world = world; } @@ -194,9 +194,9 @@ import java.util.UUID; this.settings.setMerged(merged); this.delete = false; if (flags != null) { - this.settings.setFlags(flags); + this.settings.flags = flags; } else { - this.settings.setFlags(new HashSet()); + this.settings.flags = new HashSet(); } this.world = world; } @@ -271,7 +271,7 @@ import java.util.UUID; public Object clone() throws CloneNotSupportedException { final Plot p = (Plot) super.clone(); if (!p.equals(this) || (p != this)) { - return new Plot(this.id, this.owner, this.helpers, this.trusted, this.denied, this.settings.getAlias(), this.settings.getPosition(), this.settings.getFlags(), getWorld().getName(), this.settings.getMerged()); + return new Plot(this.id, this.owner, this.helpers, this.trusted, this.denied, this.settings.getAlias(), this.settings.getPosition(), this.settings.flags, getWorld().getName(), this.settings.getMerged()); } return p; }