From 84f4e0f732de0f08bebe04da4611b9dafbc013e1 Mon Sep 17 00:00:00 2001 From: Driftay Date: Sat, 24 Aug 2019 12:23:04 -0400 Subject: [PATCH] Essentials Home Removal on Player Leave --- .../java/com/massivecraft/factions/Conf.java | 1 + .../java/com/massivecraft/factions/P.java | 8 ++++ .../factions/integration/Essentials.java | 5 ++- .../listeners/EssentialsHomeHandler.java | 44 +++++++++++++++++++ .../factions/struct/Permission.java | 1 + 5 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/massivecraft/factions/listeners/EssentialsHomeHandler.java diff --git a/src/main/java/com/massivecraft/factions/Conf.java b/src/main/java/com/massivecraft/factions/Conf.java index 0f7c687d..32f00e78 100644 --- a/src/main/java/com/massivecraft/factions/Conf.java +++ b/src/main/java/com/massivecraft/factions/Conf.java @@ -84,6 +84,7 @@ public class Conf { public static String modChatFormat = ChatColor.RED + "%s:" + ChatColor.WHITE + " %s"; public static int stealthFlyCheckRadius = 32; public static int factionBufferSize = 20; + public static boolean removeHomesOnLeave = true; public static boolean gracePeriod = false; public static boolean noEnderpearlsInFly = false; public static boolean broadcastDescriptionChanges = false; diff --git a/src/main/java/com/massivecraft/factions/P.java b/src/main/java/com/massivecraft/factions/P.java index 2bff7467..a3a34285 100644 --- a/src/main/java/com/massivecraft/factions/P.java +++ b/src/main/java/com/massivecraft/factions/P.java @@ -2,12 +2,14 @@ package com.massivecraft.factions; import ch.njol.skript.Skript; import ch.njol.skript.SkriptAddon; +import com.earth2me.essentials.IEssentials; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; import com.massivecraft.factions.cmd.CmdAutoHelp; import com.massivecraft.factions.cmd.FCmdRoot; import com.massivecraft.factions.cmd.chest.ChestLogsHandler; import com.massivecraft.factions.integration.Econ; +import com.massivecraft.factions.integration.Essentials; import com.massivecraft.factions.integration.Worldguard; import com.massivecraft.factions.integration.dynmap.EngineDynmap; import com.massivecraft.factions.listeners.*; @@ -264,6 +266,12 @@ public class P extends MPlugin { for (Listener eventListener : eventsListener) getServer().getPluginManager().registerEvents(eventListener, this); + IEssentials ess = Essentials.setup(); + + if(ess != null && Conf.removeHomesOnLeave){ + getServer().getPluginManager().registerEvents(new EssentialsHomeHandler(ess), this); + } + // since some other plugins execute commands directly through this command interface, provide it getCommand(this.refCommand).setExecutor(this); getCommand(this.refCommand).setTabCompleter(this); diff --git a/src/main/java/com/massivecraft/factions/integration/Essentials.java b/src/main/java/com/massivecraft/factions/integration/Essentials.java index 835d8562..2c43feb4 100644 --- a/src/main/java/com/massivecraft/factions/integration/Essentials.java +++ b/src/main/java/com/massivecraft/factions/integration/Essentials.java @@ -19,11 +19,12 @@ 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.essentials = (IEssentials)ess; } + return null; } public static boolean isOverBalCap(EconomyParticipator participator, double amount) { diff --git a/src/main/java/com/massivecraft/factions/listeners/EssentialsHomeHandler.java b/src/main/java/com/massivecraft/factions/listeners/EssentialsHomeHandler.java new file mode 100644 index 00000000..fa173c14 --- /dev/null +++ b/src/main/java/com/massivecraft/factions/listeners/EssentialsHomeHandler.java @@ -0,0 +1,44 @@ +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.List; +import java.util.UUID; +import java.util.logging.Level; + +public class EssentialsHomeHandler implements Listener { + + private IEssentials ess; + + public EssentialsHomeHandler(IEssentials essentials) { + this.ess = essentials; + } + + @EventHandler + public void onLeave(FPlayerLeaveEvent event) throws Exception { + Faction faction = event.getFaction(); + User user = ess.getUser(UUID.fromString(event.getfPlayer().getId())); + List homes = user.getHomes(); + if (homes == null || homes.isEmpty()) { + return; + } + for (String homeName : user.getHomes()) { + Location loc = user.getHome(homeName); + FLocation floc = new FLocation(loc); + Faction factionAt = Board.getInstance().getFactionAt(floc); + if (factionAt.equals(faction) && factionAt.isNormal()) { + 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/java/com/massivecraft/factions/struct/Permission.java b/src/main/java/com/massivecraft/factions/struct/Permission.java index c07af987..a89fa670 100644 --- a/src/main/java/com/massivecraft/factions/struct/Permission.java +++ b/src/main/java/com/massivecraft/factions/struct/Permission.java @@ -28,6 +28,7 @@ public enum Permission { CORNER("corner"), DEFAULTRANK("defaultrank"), DEINVITE("deinvite"), + DELHOME("delhome"), DESCRIPTION("description"), DISBAND("disband"), DISBAND_ANY("disband.any"),