diff --git a/src/main/java/com/massivecraft/factions/P.java b/src/main/java/com/massivecraft/factions/P.java index d4dd7446..8e9c1c92 100644 --- a/src/main/java/com/massivecraft/factions/P.java +++ b/src/main/java/com/massivecraft/factions/P.java @@ -1,5 +1,6 @@ package com.massivecraft.factions; +import com.earth2me.essentials.IEssentials; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; import com.massivecraft.factions.cmd.CmdAutoHelp; @@ -34,7 +35,6 @@ import java.util.Map; import java.util.Set; import java.util.logging.Level; - public class P extends MPlugin { // Our single plugin instance. @@ -78,8 +78,19 @@ public class P extends MPlugin { // Load Conf from disk Conf.load(); - Essentials.setup(); + + // Check for Essentials + IEssentials ess = Essentials.setup(); + + // We set the option to TRUE by default in the config.yml for new users, + // BUT we leave it set to false for users updating that haven't added it to their config. + if (ess != null && getConfig().getBoolean("delete-ess-homes", false)) { + P.p.log(Level.INFO, "Found Essentials. We'll delete player homes in their old Faction's when kicked."); + getServer().getPluginManager().registerEvents(new EssentialsListener(ess), this); + } + hookedPlayervaults = setupPlayervaults(); + FPlayers.getInstance().load(); Factions.getInstance().load(); for (FPlayer fPlayer : FPlayers.getInstance().getAllFPlayers()) { diff --git a/src/main/java/com/massivecraft/factions/integration/Essentials.java b/src/main/java/com/massivecraft/factions/integration/Essentials.java index 0c9310ed..4871b435 100644 --- a/src/main/java/com/massivecraft/factions/integration/Essentials.java +++ b/src/main/java/com/massivecraft/factions/integration/Essentials.java @@ -14,11 +14,14 @@ public class Essentials { private static IEssentials essentials; - public static void setup() { + public static IEssentials setup() { Plugin ess = Bukkit.getPluginManager().getPlugin("Essentials"); if (ess != null) { essentials = (IEssentials) ess; + return essentials; } + + return null; } // return false if feature is disabled or Essentials isn't available diff --git a/src/main/java/com/massivecraft/factions/listeners/EssentialsListener.java b/src/main/java/com/massivecraft/factions/listeners/EssentialsListener.java new file mode 100644 index 00000000..04f7cd19 --- /dev/null +++ b/src/main/java/com/massivecraft/factions/listeners/EssentialsListener.java @@ -0,0 +1,46 @@ +package com.massivecraft.factions.listeners; + +import com.earth2me.essentials.IEssentials; +import com.earth2me.essentials.User; +import com.massivecraft.factions.Board; +import com.massivecraft.factions.FLocation; +import com.massivecraft.factions.Faction; +import com.massivecraft.factions.P; +import com.massivecraft.factions.event.FPlayerLeaveEvent; +import org.bukkit.Location; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; + +import java.util.UUID; +import java.util.logging.Level; + +public class EssentialsListener implements Listener { + + private final IEssentials ess; + + public EssentialsListener(IEssentials essentials) { + this.ess = essentials; + } + + @EventHandler + public void onLeave(FPlayerLeaveEvent event) throws Exception { + // Get the USER from their UUID. + Faction faction = event.getFaction(); + User user = ess.getUser(UUID.fromString(event.getfPlayer().getId())); + + // Not a great way to do this on essential's side. + for (String homeName : user.getHomes()) { + + // This can throw an exception for some reason. + Location loc = user.getHome(homeName); + FLocation floc = new FLocation(loc); + + // We're only going to remove homes in territory that belongs to THEIR faction. + if (Board.getInstance().getFactionAt(floc).equals(faction)) { + user.delHome(homeName); + P.p.log(Level.INFO, "FactionLeaveEvent: Removing home %s, player %s, in territory of %s", + homeName, event.getfPlayer().getName(), faction.getTag()); + } + } + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 4291cf59..f692c110 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -20,6 +20,12 @@ findfactionsexploit: cooldown: 2000 # in miliseconds. 2000 = 2 seconds. log: false + +# Essentials Hook +# Should we delete player homes that they set via Essentials when they leave a Faction if they have homes set in that +# Faction's territory? +delete-ess-homes: true + ### Hard Core Settings ### # Many of the features that are / are to come in this section have been requested by # people in relation to HCF servers. All settings are set to the normal Factions