From 8f70b21cde326e9f673910533fcc259b9f9b7b4d Mon Sep 17 00:00:00 2001 From: BuildTools Date: Sat, 3 Oct 2020 12:23:02 +0100 Subject: [PATCH] Optimised Magnetic --- .../ecoenchants/normal/Magnetic.java | 44 +++- .../com/willfp/ecoenchants/loader/Loader.java | 193 +++++++++--------- 2 files changed, 129 insertions(+), 108 deletions(-) diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/ecoenchants/normal/Magnetic.java b/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/ecoenchants/normal/Magnetic.java index 39ae726f..3ed4fc5c 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/ecoenchants/normal/Magnetic.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/enchantments/ecoenchants/normal/Magnetic.java @@ -5,12 +5,20 @@ import com.willfp.ecoenchants.enchantments.EcoEnchant; import com.willfp.ecoenchants.enchantments.EcoEnchantBuilder; import com.willfp.ecoenchants.enchantments.EcoEnchants; import com.willfp.ecoenchants.enchantments.util.EnchantChecks; +import com.willfp.ecoenchants.events.armorequip.ArmorEquipEvent; import com.willfp.ecoenchants.util.EcoRunnable; import com.willfp.ecoenchants.util.VectorUtils; import org.bukkit.entity.Entity; import org.bukkit.entity.ExperienceOrb; import org.bukkit.entity.Item; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.util.Vector; + +import java.util.HashMap; + public class Magnetic extends EcoEnchant implements EcoRunnable { public Magnetic() { super( @@ -18,13 +26,37 @@ public class Magnetic extends EcoEnchant implements EcoRunnable { ); } + private HashMap players = new HashMap<>(); + private double initialDistance = 1; + private double bonus = 1; + + @EventHandler + public void onArmorEquip(ArmorEquipEvent event) { + refresh(); + } + + @EventHandler + public void onPlayerJoin(PlayerJoinEvent event) { + refresh(); + } + + @EventHandler + public void onPlayerLeave(PlayerQuitEvent event) { + refresh(); + } + + private void refresh() { + players.clear(); + EcoEnchantsPlugin.getInstance().getServer().getOnlinePlayers().forEach(player -> { + players.put(player, EnchantChecks.getArmorPoints(player, this, 0)); + }); + initialDistance = EcoEnchants.MAGNETIC.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "initial-distance"); + bonus = EcoEnchants.MAGNETIC.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "bonus-per-level"); + } + @Override public void run() { - EcoEnchantsPlugin.getInstance().getServer().getOnlinePlayers().stream().filter(player -> EnchantChecks.getArmorPoints(player, EcoEnchants.MAGNETIC, 0) > 0).forEach((player -> { - int level = EnchantChecks.getArmorPoints(player, EcoEnchants.MAGNETIC, 0); - - double initialDistance = EcoEnchants.MAGNETIC.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "initial-distance"); - double bonus = EcoEnchants.MAGNETIC.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "bonus-per-level"); + players.forEach((player, level) -> { double distance = initialDistance + (level * bonus); for (Entity e : player.getWorld().getNearbyEntities(player.getLocation(), distance, 2.0d, distance)) { @@ -42,7 +74,7 @@ public class Magnetic extends EcoEnchant implements EcoRunnable { e.setVelocity(vector); } } - })); + }); } @Override diff --git a/Plugin/src/main/java/com/willfp/ecoenchants/loader/Loader.java b/Plugin/src/main/java/com/willfp/ecoenchants/loader/Loader.java index c490b128..631f4e08 100644 --- a/Plugin/src/main/java/com/willfp/ecoenchants/loader/Loader.java +++ b/Plugin/src/main/java/com/willfp/ecoenchants/loader/Loader.java @@ -111,91 +111,6 @@ public class Loader { new PacketSetSlot().register(); new PacketWindowItems().register(); - /* - Load land management support - */ - - Logger.info("Scheduling Integration Loading..."); - - Bukkit.getScheduler().runTaskLater(EcoEnchantsPlugin.getInstance(), () -> { - - Logger.info("Loading Integrations..."); - - if(Bukkit.getPluginManager().isPluginEnabled("WorldGuard")) { - AntigriefManager.registerAntigrief(new AntigriefWorldGuard()); - Logger.info("WorldGuard: §aENABLED"); - } else { - Logger.info("WorldGuard: §9DISABLED"); - } - - if(Bukkit.getPluginManager().isPluginEnabled("GriefPrevention")) { - AntigriefManager.registerAntigrief(new AntigriefGriefPrevention()); - Logger.info("GriefPrevention: §aENABLED"); - } else { - Logger.info("GriefPrevention: §9DISABLED"); - } - - if(Bukkit.getPluginManager().isPluginEnabled("FactionsUUID")) { - AntigriefManager.registerAntigrief(new AntigriefFactionsUUID()); - Logger.info("FactionsUUID: §aENABLED"); - } else { - Logger.info("FactionsUUID: §9DISABLED"); - } - - if(Bukkit.getPluginManager().isPluginEnabled("Towny")) { - AntigriefManager.registerAntigrief(new AntigriefTowny()); - Logger.info("Towny: §aENABLED"); - } else { - Logger.info("Towny: §9DISABLED"); - } - - if(Bukkit.getPluginManager().isPluginEnabled("Lands")) { - AntigriefManager.registerAntigrief(new AntigriefLands()); - Logger.info("Lands: §aENABLED"); - } else { - Logger.info("Lands: §9DISABLED"); - } - - if(Bukkit.getPluginManager().isPluginEnabled("Essentials")) { - EssentialsManager.registerEssentials(new IntegrationEssentials()); - Logger.info("Essentials: §aENABLED"); - EssentialsManager.registerEnchantments(); - } else { - Logger.info("Essentials: §9DISABLED"); - } - - if(Bukkit.getPluginManager().isPluginEnabled("AAC")) { - AnticheatManager.registerAnticheat(new AnticheatAAC()); - Logger.info("AAC: §aENABLED"); - } else { - Logger.info("AAC: §9DISABLED"); - } - - if(Bukkit.getPluginManager().isPluginEnabled("Matrix")) { - AnticheatManager.registerAnticheat(new AnticheatMatrix()); - Logger.info("Matrix: §aENABLED"); - } else { - Logger.info("Matrix: §9DISABLED"); - } - - if(Bukkit.getPluginManager().isPluginEnabled("NoCheatPlus")) { - AnticheatManager.registerAnticheat(new AnticheatNCP()); - Logger.info("NCP: §aENABLED"); - } else { - Logger.info("NCP: §9DISABLED"); - } - - if(Bukkit.getPluginManager().isPluginEnabled("Spartan")) { - AnticheatManager.registerAnticheat(new AnticheatSpartan()); - Logger.info("Spartan: §aENABLED"); - } else { - Logger.info("Spartan: §9DISABLED"); - } - - Logger.info(""); - - }, 1); - Logger.info(""); /* @@ -248,18 +163,6 @@ public class Loader { Bukkit.getPluginManager().registerEvents(new WatcherTriggers(), EcoEnchantsPlugin.getInstance()); Logger.info(""); - /* - Add Block Populators - */ - - Logger.info("Scheduling Adding Block Populators..."); - Bukkit.getScheduler().runTaskLater(EcoEnchantsPlugin.getInstance(), () -> { - Bukkit.getServer().getWorlds().forEach((world -> { - world.getPopulators().add(new LootPopulator()); - })); - }, 1); - Logger.info(""); - /* Load Extensions */ @@ -390,9 +293,25 @@ public class Loader { Logger.info(""); /* - Start update checker + Finish */ + Bukkit.getScheduler().runTaskLater(EcoEnchantsPlugin.getInstance(), Loader::postLoad, 1); + + Logger.info("Loaded §aEcoEnchants!"); + } + + /** + * Called after server is loaded + */ + public static void postLoad() { + Logger.info("Adding block populators..."); + + Bukkit.getServer().getWorlds().forEach((world -> { + world.getPopulators().add(new LootPopulator()); + })); + + Logger.info(""); new UpdateChecker(EcoEnchantsPlugin.getInstance(), 79573).getVersion((version) -> { DefaultArtifactVersion currentVersion = new DefaultArtifactVersion(EcoEnchantsPlugin.getInstance().getDescription().getVersion()); @@ -417,11 +336,81 @@ public class Loader { Logger.info("----------------------------"); }); - /* - Finish - */ + Logger.info(""); + Logger.info("Loading Integrations..."); - Logger.info("Loaded §aEcoEnchants!"); + if(Bukkit.getPluginManager().isPluginEnabled("WorldGuard")) { + AntigriefManager.registerAntigrief(new AntigriefWorldGuard()); + Logger.info("WorldGuard: §aENABLED"); + } else { + Logger.info("WorldGuard: §9DISABLED"); + } + + if(Bukkit.getPluginManager().isPluginEnabled("GriefPrevention")) { + AntigriefManager.registerAntigrief(new AntigriefGriefPrevention()); + Logger.info("GriefPrevention: §aENABLED"); + } else { + Logger.info("GriefPrevention: §9DISABLED"); + } + + if(Bukkit.getPluginManager().isPluginEnabled("FactionsUUID")) { + AntigriefManager.registerAntigrief(new AntigriefFactionsUUID()); + Logger.info("FactionsUUID: §aENABLED"); + } else { + Logger.info("FactionsUUID: §9DISABLED"); + } + + if(Bukkit.getPluginManager().isPluginEnabled("Towny")) { + AntigriefManager.registerAntigrief(new AntigriefTowny()); + Logger.info("Towny: §aENABLED"); + } else { + Logger.info("Towny: §9DISABLED"); + } + + if(Bukkit.getPluginManager().isPluginEnabled("Lands")) { + AntigriefManager.registerAntigrief(new AntigriefLands()); + Logger.info("Lands: §aENABLED"); + } else { + Logger.info("Lands: §9DISABLED"); + } + + if(Bukkit.getPluginManager().isPluginEnabled("Essentials")) { + EssentialsManager.registerEssentials(new IntegrationEssentials()); + Logger.info("Essentials: §aENABLED"); + EssentialsManager.registerEnchantments(); + } else { + Logger.info("Essentials: §9DISABLED"); + } + + if(Bukkit.getPluginManager().isPluginEnabled("AAC")) { + AnticheatManager.registerAnticheat(new AnticheatAAC()); + Logger.info("AAC: §aENABLED"); + } else { + Logger.info("AAC: §9DISABLED"); + } + + if(Bukkit.getPluginManager().isPluginEnabled("Matrix")) { + AnticheatManager.registerAnticheat(new AnticheatMatrix()); + Logger.info("Matrix: §aENABLED"); + } else { + Logger.info("Matrix: §9DISABLED"); + } + + if(Bukkit.getPluginManager().isPluginEnabled("NoCheatPlus")) { + AnticheatManager.registerAnticheat(new AnticheatNCP()); + Logger.info("NCP: §aENABLED"); + } else { + Logger.info("NCP: §9DISABLED"); + } + + if(Bukkit.getPluginManager().isPluginEnabled("Spartan")) { + AnticheatManager.registerAnticheat(new AnticheatSpartan()); + Logger.info("Spartan: §aENABLED"); + } else { + Logger.info("Spartan: §9DISABLED"); + } + + Logger.info(""); } /**