From c91f88858a3a13c20e22d960fd70746f9781e84e Mon Sep 17 00:00:00 2001 From: montlikadani Date: Mon, 21 Sep 2020 17:42:20 +0200 Subject: [PATCH] Added wildStacker and stackMob support to pay for stacked entities Closes #923 --- pom.xml | 12 +++- .../jobs/commands/JobsCommands.java | 2 +- .../jobs/config/GeneralConfigManager.java | 13 +++-- .../gamingmesh/jobs/hooks/HookManager.java | 58 +++++++++++++++---- .../com/gamingmesh/jobs/hooks/HookPlugin.java | 8 +++ .../jobs/hooks/MythicMobs/MythicMobs2.java | 5 +- .../jobs/hooks/MythicMobs/MythicMobs4.java | 5 +- .../jobs/hooks/stackMob/StackMobHandler.java | 29 ++++++++++ .../hooks/wildStacker/WildStackerHandler.java | 19 ++++++ .../jobs/listeners/JobsPaymentListener.java | 50 +++++++++++++--- 10 files changed, 168 insertions(+), 33 deletions(-) create mode 100644 src/main/java/com/gamingmesh/jobs/hooks/HookPlugin.java create mode 100644 src/main/java/com/gamingmesh/jobs/hooks/stackMob/StackMobHandler.java create mode 100644 src/main/java/com/gamingmesh/jobs/hooks/wildStacker/WildStackerHandler.java diff --git a/pom.xml b/pom.xml index a96cd0f4..0c38c80b 100644 --- a/pom.xml +++ b/pom.xml @@ -18,7 +18,7 @@ org.spigotmc spigot-api - 1.16.2-R0.1-SNAPSHOT + 1.16.3-R0.1-SNAPSHOT provided @@ -164,6 +164,16 @@ + + com.github.OmerBenGera + WildStackerAPI + master-SNAPSHOT + + + com.github.Nathat23 + StackMob-5 + master-SNAPSHOT + diff --git a/src/main/java/com/gamingmesh/jobs/commands/JobsCommands.java b/src/main/java/com/gamingmesh/jobs/commands/JobsCommands.java index 0b171dfc..08130a83 100644 --- a/src/main/java/com/gamingmesh/jobs/commands/JobsCommands.java +++ b/src/main/java/com/gamingmesh/jobs/commands/JobsCommands.java @@ -49,7 +49,7 @@ public class JobsCommands implements CommandExecutor { @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - if (sender instanceof Player && !Jobs.getGCManager().canPerformActionInWorld(((Player) sender).getWorld()) && !sender.hasPermission("jobs.disabledworld.commands")) { + if (sender instanceof Player && !Jobs.getGCManager().canPerformActionInWorld(((Player) sender).getWorld())) { sender.sendMessage(Jobs.getLanguage().getMessage("general.error.worldisdisabled")); return true; } diff --git a/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java b/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java index ca67fb32..1999758b 100644 --- a/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java +++ b/src/main/java/com/gamingmesh/jobs/config/GeneralConfigManager.java @@ -52,10 +52,10 @@ public class GeneralConfigManager { */ @Deprecated public List BoostSchedule = new ArrayList<>(); - public HashMap> whiteListedItems = new HashMap<>(); - private HashMap currencyLimitUse = new HashMap<>(); - private HashMap generalMulti = new HashMap<>(); - private HashMap> commandArgs = new HashMap<>(); + public final HashMap> whiteListedItems = new HashMap<>(); + private final HashMap currencyLimitUse = new HashMap<>(); + private final HashMap generalMulti = new HashMap<>(); + private final HashMap> commandArgs = new HashMap<>(); protected Locale locale; private ConfigReader c = null; @@ -101,7 +101,7 @@ public class GeneralConfigManager { hideJobsInfoWithoutPermission, UseTaxes, TransferToServerAccount, TakeFromPlayersPayment, AutoJobJoinUse, AllowDelevel, BossBarEnabled, BossBarShowOnEachAction, BossBarsMessageByDefault, ExploreCompact, DBCleaningJobsUse, DBCleaningUsersUse, DisabledWorldsUse, UseAsWhiteListWorldList, PaymentMethodsMoney, PaymentMethodsPoints, PaymentMethodsExp, MythicMobsEnabled, - LoggingUse, payForCombiningItems, BlastFurnacesReassign, SmokerReassign; + LoggingUse, payForCombiningItems, BlastFurnacesReassign, SmokerReassign, payForStackedEntities; public ItemStack guiBackButton, guiNextButton, guiFiller; @@ -497,6 +497,9 @@ public class GeneralConfigManager { "This option is only related to: sugar_cane, cactus, kelp, bamboo"); preventCropResizePayment = c.get("prevent-crop-resize-payment", false); + c.addComment("pay-for-stacked-entities", "Allows to pay for stacked entities for each one. Requires StackMob or WildStacker."); + payForStackedEntities = c.get("pay-for-stacked-entities", false); + c.addComment("allow-pay-for-durability-loss", "Allows, when losing maximum durability of item then it does not pay the player until it is repaired.", "E.g. the player wants to enchant a item with enchanting table and the item has durability loss then not paying."); c.addComment("allow-pay-for-durability-loss.Use", "Do not disable this if you don't know what mean this option."); diff --git a/src/main/java/com/gamingmesh/jobs/hooks/HookManager.java b/src/main/java/com/gamingmesh/jobs/hooks/HookManager.java index b29fb08d..0b1f0e70 100644 --- a/src/main/java/com/gamingmesh/jobs/hooks/HookManager.java +++ b/src/main/java/com/gamingmesh/jobs/hooks/HookManager.java @@ -12,16 +12,20 @@ import com.gamingmesh.jobs.hooks.MythicMobs.MythicMobInterface; import com.gamingmesh.jobs.hooks.MythicMobs.MythicMobs2; import com.gamingmesh.jobs.hooks.MythicMobs.MythicMobs4; import com.gamingmesh.jobs.hooks.WorldGuard.WorldGuardManager; +import com.gamingmesh.jobs.hooks.stackMob.StackMobHandler; +import com.gamingmesh.jobs.hooks.wildStacker.WildStackerHandler; public class HookManager { - private static McMMOManager McMMOManager = null; - private static MythicMobInterface MythicManager = null; - private static MyPetManager myPetManager = null; - private static WorldGuardManager worldGuardManager = null; - private static BossManager bossManager = null; + private static McMMOManager McMMOManager; + private static MythicMobInterface MythicManager; + private static MyPetManager myPetManager; + private static WorldGuardManager worldGuardManager; + private static BossManager bossManager; + private static StackMobHandler stackMobHandler; + private static WildStackerHandler wildStackerHandler; - private static PluginManager pm = null; + private static PluginManager pm; public static void loadHooks() { pm = Jobs.getInstance().getServer().getPluginManager(); @@ -33,11 +37,29 @@ public class HookManager { setWorldGuard(); setMythicManager(); setBossManager(); + setStackMobHandler(); + setWildStackerHandler(); if (checkMythicMobs()) MythicManager.registerListener(); } + public static StackMobHandler getStackMobHandler() { + if (stackMobHandler == null) { + setStackMobHandler(); + } + + return stackMobHandler; + } + + public static WildStackerHandler getWildStackerHandler() { + if (wildStackerHandler == null) { + setWildStackerHandler(); + } + + return wildStackerHandler; + } + public static MyPetManager getMyPetManager() { if (myPetManager == null) { setMyPetManager(); @@ -77,7 +99,7 @@ public class HookManager { } private static boolean setWorldGuard() { - if (pm.getPlugin("WorldGuard") != null && pm.isPluginEnabled("WorldGuard")) { + if (isPluginEnabled("WorldGuard")) { worldGuardManager = new WorldGuardManager(); Jobs.consoleMsg("&e[Jobs] WorldGuard detected."); return true; @@ -87,7 +109,7 @@ public class HookManager { } private static void setMythicManager() { - if (pm.getPlugin("MythicMobs") == null) + if (!isPluginEnabled("MythicMobs")) return; try { @@ -122,16 +144,32 @@ public class HookManager { } private static void setMyPetManager() { - if (pm.getPlugin("MyPet") != null && pm.isPluginEnabled("MyPet")) { + if (isPluginEnabled("MyPet")) { myPetManager = new MyPetManager(); Jobs.consoleMsg("&e[Jobs] MyPet detected."); } } private static void setBossManager() { - if (pm.getPlugin("Boss") != null && pm.isPluginEnabled("Boss")) { + if (isPluginEnabled("Boss")) { bossManager = new BossManager(); Jobs.consoleMsg("&e[Jobs] Boss detected."); } } + + private static void setStackMobHandler() { + if (isPluginEnabled("StackMob")) { + stackMobHandler = new StackMobHandler(); + } + } + + private static void setWildStackerHandler() { + if (isPluginEnabled("WildStacker")) { + wildStackerHandler = new WildStackerHandler(); + } + } + + public static boolean isPluginEnabled(String name) { + return pm.getPlugin(name) != null && pm.isPluginEnabled(name); + } } diff --git a/src/main/java/com/gamingmesh/jobs/hooks/HookPlugin.java b/src/main/java/com/gamingmesh/jobs/hooks/HookPlugin.java new file mode 100644 index 00000000..18c2fa4e --- /dev/null +++ b/src/main/java/com/gamingmesh/jobs/hooks/HookPlugin.java @@ -0,0 +1,8 @@ +package com.gamingmesh.jobs.hooks; + +import org.bukkit.plugin.java.JavaPlugin; + +public abstract class HookPlugin { + + public abstract JavaPlugin getPlugin(); +} diff --git a/src/main/java/com/gamingmesh/jobs/hooks/MythicMobs/MythicMobs2.java b/src/main/java/com/gamingmesh/jobs/hooks/MythicMobs/MythicMobs2.java index 7203ddb7..806b0eab 100644 --- a/src/main/java/com/gamingmesh/jobs/hooks/MythicMobs/MythicMobs2.java +++ b/src/main/java/com/gamingmesh/jobs/hooks/MythicMobs/MythicMobs2.java @@ -27,10 +27,7 @@ public class MythicMobs2 implements MythicMobInterface { @Override public boolean isMythicMob(LivingEntity lVictim) { - if (MMAPI == null || lVictim == null) - return false; - - return MMAPI.getMobAPI().isMythicMob(lVictim); + return MMAPI != null && lVictim != null && MMAPI.getMobAPI().isMythicMob(lVictim); } @Override diff --git a/src/main/java/com/gamingmesh/jobs/hooks/MythicMobs/MythicMobs4.java b/src/main/java/com/gamingmesh/jobs/hooks/MythicMobs/MythicMobs4.java index 5beb914f..9b292995 100644 --- a/src/main/java/com/gamingmesh/jobs/hooks/MythicMobs/MythicMobs4.java +++ b/src/main/java/com/gamingmesh/jobs/hooks/MythicMobs/MythicMobs4.java @@ -26,10 +26,7 @@ public class MythicMobs4 implements MythicMobInterface { @Override public boolean isMythicMob(LivingEntity lVictim) { - if (MMAPI == null || lVictim == null) - return false; - - return MMAPI.isMythicMob(lVictim); + return MMAPI != null && lVictim != null && MMAPI.isMythicMob(lVictim); } @Override diff --git a/src/main/java/com/gamingmesh/jobs/hooks/stackMob/StackMobHandler.java b/src/main/java/com/gamingmesh/jobs/hooks/stackMob/StackMobHandler.java new file mode 100644 index 00000000..77beefa8 --- /dev/null +++ b/src/main/java/com/gamingmesh/jobs/hooks/stackMob/StackMobHandler.java @@ -0,0 +1,29 @@ +package com.gamingmesh.jobs.hooks.stackMob; + +import java.util.HashSet; + +import org.bukkit.entity.LivingEntity; +import org.bukkit.plugin.java.JavaPlugin; + +import com.gamingmesh.jobs.hooks.HookPlugin; + +import uk.antiperson.stackmob.StackMob; +import uk.antiperson.stackmob.entity.StackEntity; + +public class StackMobHandler extends HookPlugin { + + public boolean isStacked(LivingEntity entity) { + return getPlugin().getEntityManager().isStackedEntity(entity); + } + + public HashSet getStackEntities() { + return new HashSet<>(); + // waiting for the author to fix build issue + //return getPlugin().getEntityManager().getStackEntities(); + } + + @Override + public StackMob getPlugin() { + return JavaPlugin.getPlugin(StackMob.class); + } +} diff --git a/src/main/java/com/gamingmesh/jobs/hooks/wildStacker/WildStackerHandler.java b/src/main/java/com/gamingmesh/jobs/hooks/wildStacker/WildStackerHandler.java new file mode 100644 index 00000000..f1dd85db --- /dev/null +++ b/src/main/java/com/gamingmesh/jobs/hooks/wildStacker/WildStackerHandler.java @@ -0,0 +1,19 @@ +package com.gamingmesh.jobs.hooks.wildStacker; + +import java.util.List; + +import org.bukkit.entity.LivingEntity; + +import com.bgsoftware.wildstacker.api.WildStackerAPI; +import com.bgsoftware.wildstacker.api.objects.StackedEntity; + +public class WildStackerHandler { + + public boolean isStackedEntity(LivingEntity entity) { + return WildStackerAPI.getStackedEntity(entity) != null; + } + + public List getStackedEntities() { + return WildStackerAPI.getWildStacker().getSystemManager().getStackedEntities(); + } +} diff --git a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java index bc63eca1..f58a93aa 100644 --- a/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java +++ b/src/main/java/com/gamingmesh/jobs/listeners/JobsPaymentListener.java @@ -87,8 +87,12 @@ import org.bukkit.metadata.FixedMetadataValue; import org.bukkit.metadata.MetadataValue; import org.bukkit.projectiles.ProjectileSource; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import java.util.Map.Entry; +import java.util.UUID; public class JobsPaymentListener implements Listener { @@ -278,6 +282,17 @@ public class JobsPaymentListener implements Listener { if (jDamager == null || sheep.getColor() == null) return; + if (Jobs.getGCManager().payForStackedEntities && HookManager.isPluginEnabled("WildStacker") + && HookManager.getWildStackerHandler().isStackedEntity(sheep)) { + for (com.bgsoftware.wildstacker.api.objects.StackedEntity stacked : HookManager.getWildStackerHandler().getStackedEntities()) { + if (stacked.getType() == sheep.getType()) { + Jobs.action(jDamager, new CustomKillInfo(((Sheep) stacked.getLivingEntity()).getColor().name(), ActionType.SHEAR)); + } + } + + return; + } + Jobs.action(jDamager, new CustomKillInfo(sheep.getColor().name(), ActionType.SHEAR)); } @@ -462,7 +477,7 @@ public class JobsPaymentListener implements Listener { if (!payForItemDurabilityLoss(player)) return; - if (event.getState().equals(PlayerFishEvent.State.CAUGHT_FISH) && event.getCaught() instanceof Item) { + if (event.getState() == PlayerFishEvent.State.CAUGHT_FISH && event.getCaught() instanceof Item) { JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(player); if (jPlayer == null) return; @@ -511,6 +526,17 @@ public class JobsPaymentListener implements Listener { if (jDamager == null) return; + if (Jobs.getGCManager().payForStackedEntities && HookManager.isPluginEnabled("WildStacker") + && HookManager.getWildStackerHandler().isStackedEntity(animal)) { + for (com.bgsoftware.wildstacker.api.objects.StackedEntity stacked : HookManager.getWildStackerHandler().getStackedEntities()) { + if (stacked.getType() == animal.getType()) { + Jobs.action(jDamager, new EntityActionInfo(stacked.getLivingEntity(), ActionType.TAME)); + } + } + + return; + } + Jobs.action(jDamager, new EntityActionInfo(animal, ActionType.TAME)); } @@ -851,7 +877,7 @@ public class JobsPaymentListener implements Listener { return; } - if (Jobs.getGCManager().PayForEnchantingOnAnvil && inv.getItem(1) != null && inv.getItem(1).getType().equals(Material.ENCHANTED_BOOK)) { + if (Jobs.getGCManager().PayForEnchantingOnAnvil && inv.getItem(1) != null && inv.getItem(1).getType() == Material.ENCHANTED_BOOK) { Map enchants = resultStack.getEnchantments(); for (Entry oneEnchant : enchants.entrySet()) { Enchantment enchant = oneEnchant.getKey(); @@ -1090,10 +1116,7 @@ public class JobsPaymentListener implements Listener { return; } - if (!(damager instanceof Projectile)) - return; - - if (!(ent instanceof Damageable)) + if (!(damager instanceof Projectile) || !(ent instanceof Damageable)) return; Projectile projectile = (Projectile) damager; @@ -1205,6 +1228,17 @@ public class JobsPaymentListener implements Listener { return; } + if (Jobs.getGCManager().payForStackedEntities && HookManager.isPluginEnabled("WildStacker") + && HookManager.getWildStackerHandler().isStackedEntity(lVictim)) { + for (com.bgsoftware.wildstacker.api.objects.StackedEntity stacked : HookManager.getWildStackerHandler().getStackedEntities()) { + if (stacked.getType() == lVictim.getType()) { + Jobs.action(jDamager, new EntityActionInfo(stacked.getLivingEntity(), ActionType.KILL), pDamager, stacked.getLivingEntity()); + } + } + + return; + } + Jobs.action(jDamager, new EntityActionInfo(lVictim, ActionType.KILL), pDamager, lVictim); // Payment for killing player with particular job, except NPC's @@ -1298,7 +1332,7 @@ public class JobsPaymentListener implements Listener { return; Location loc = event.getLocation(); - Collection ents = Version.isCurrentEqualOrLower(Version.v1_8_R1) || loc.getWorld() == null + java.util.Collection ents = Version.isCurrentEqualOrLower(Version.v1_8_R1) || loc.getWorld() == null ? null : loc.getWorld().getNearbyEntities(loc, 4, 4, 4); if (ents == null) { return;