From a9700dfdd34972b61a170daa5152105065bc9172 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=9D=8F=E9=BB=91?= Date: Sat, 15 Sep 2018 00:21:58 +0800 Subject: [PATCH] + update --- .../common/playercontanier/Container.java | 23 +++++ .../playercontanier/PlayerContainer.java | 18 ++++ .../PlayerContainerLoader.java | 92 +++++++++++++++++++ .../me/skymc/taboolib/damage/DamageUtils.java | 68 ++++++-------- .../me/skymc/taboolib/damage/GetDamager.java | 12 +-- .../me/skymc/taboolib/damage/GetKiller.java | 13 +-- .../me/skymc/taboolib/entity/VectorUtils.java | 12 +-- .../skymc/taboolib/events/DefaultEvent2.java | 53 ----------- 8 files changed, 170 insertions(+), 121 deletions(-) create mode 100644 src/main/java/me/skymc/taboolib/common/playercontanier/Container.java create mode 100644 src/main/java/me/skymc/taboolib/common/playercontanier/PlayerContainer.java create mode 100644 src/main/java/me/skymc/taboolib/common/playercontanier/PlayerContainerLoader.java delete mode 100644 src/main/java/me/skymc/taboolib/events/DefaultEvent2.java diff --git a/src/main/java/me/skymc/taboolib/common/playercontanier/Container.java b/src/main/java/me/skymc/taboolib/common/playercontanier/Container.java new file mode 100644 index 0000000..04cff06 --- /dev/null +++ b/src/main/java/me/skymc/taboolib/common/playercontanier/Container.java @@ -0,0 +1,23 @@ +package me.skymc.taboolib.common.playercontanier; + +/** + * @author sky + */ +public class Container { + + private final Object container; + private final boolean uniqueId; + + public Container(Object container, boolean uniqueId) { + this.container = container; + this.uniqueId = uniqueId; + } + + public Object getContainer() { + return container; + } + + public boolean isUniqueId() { + return uniqueId; + } +} \ No newline at end of file diff --git a/src/main/java/me/skymc/taboolib/common/playercontanier/PlayerContainer.java b/src/main/java/me/skymc/taboolib/common/playercontanier/PlayerContainer.java new file mode 100644 index 0000000..722953b --- /dev/null +++ b/src/main/java/me/skymc/taboolib/common/playercontanier/PlayerContainer.java @@ -0,0 +1,18 @@ +package me.skymc.taboolib.common.playercontanier; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * @Author sky + * @Since 2018-09-14 23:45 + */ +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +public @interface PlayerContainer { + + boolean uniqueId() default false; + +} \ No newline at end of file diff --git a/src/main/java/me/skymc/taboolib/common/playercontanier/PlayerContainerLoader.java b/src/main/java/me/skymc/taboolib/common/playercontanier/PlayerContainerLoader.java new file mode 100644 index 0000000..3047d10 --- /dev/null +++ b/src/main/java/me/skymc/taboolib/common/playercontanier/PlayerContainerLoader.java @@ -0,0 +1,92 @@ +package me.skymc.taboolib.common.playercontanier; + +import com.ilummc.tlib.logger.TLogger; +import me.skymc.taboolib.TabooLib; +import me.skymc.taboolib.TabooLibLoader; +import me.skymc.taboolib.listener.TListener; +import org.bukkit.Bukkit; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.event.server.PluginDisableEvent; +import org.bukkit.event.server.PluginEnableEvent; +import org.bukkit.plugin.Plugin; + +import java.lang.reflect.Field; +import java.util.*; +import java.util.concurrent.ConcurrentHashMap; + +/** + * @Author sky + * @Since 2018-09-14 23:45 + */ +@TListener +public class PlayerContainerLoader implements Listener { + + private static Map> pluginContainer = new ConcurrentHashMap<>(); + + PlayerContainerLoader() { + load(); + } + + public static void load() { + Arrays.stream(Bukkit.getPluginManager().getPlugins()).forEach(PlayerContainerLoader::load); + } + + public static void load(Plugin plugin) { + if (!(TabooLib.isTabooLib(plugin) || TabooLib.isDependTabooLib(plugin))) { + return; + } + TabooLibLoader.getPluginClasses(plugin).ifPresent(classes -> { + for (Class pluginClass : classes) { + for (Field field : pluginClass.getDeclaredFields()) { + PlayerContainer annotation = field.getAnnotation(PlayerContainer.class); + if (annotation == null) { + continue; + } + field.setAccessible(true); + try { + pluginContainer.computeIfAbsent(plugin.getName(), name -> new ArrayList<>()).add(new Container(field.get(pluginClass), annotation.uniqueId())); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + } + }); + } + + public static void unload() { + Arrays.stream(Bukkit.getPluginManager().getPlugins()).forEach(PlayerContainerLoader::unload); + } + + public static void unload(Plugin plugin) { + pluginContainer.remove(plugin.getName()); + } + + @EventHandler + public void onEnable(PluginEnableEvent e) { + load(e.getPlugin()); + } + + @EventHandler + public void onDisable(PluginDisableEvent e) { + unload(e.getPlugin()); + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onQuit(PlayerQuitEvent e) { + for (List containers : pluginContainer.values()) { + for (Container container : containers) { + if (container.getContainer() instanceof Map) { + ((Map) container.getContainer()).remove(container.isUniqueId() ? e.getPlayer().getUniqueId() : e.getPlayer().getName()); + } else if (container.getContainer() instanceof Collection) { + ((Collection) container.getContainer()).remove(container.isUniqueId() ? e.getPlayer().getUniqueId() : e.getPlayer().getName()); + } else { + TLogger.getGlobalLogger().error("Invalid Container: " + container.getContainer().getClass().getSimpleName()); + } + } + } + } + +} diff --git a/src/main/java/me/skymc/taboolib/damage/DamageUtils.java b/src/main/java/me/skymc/taboolib/damage/DamageUtils.java index a614445..1ffdd99 100644 --- a/src/main/java/me/skymc/taboolib/damage/DamageUtils.java +++ b/src/main/java/me/skymc/taboolib/damage/DamageUtils.java @@ -1,58 +1,44 @@ package me.skymc.taboolib.damage; -import me.skymc.taboolib.TabooLib; import org.bukkit.entity.Entity; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; +import org.bukkit.entity.Projectile; +import org.bukkit.event.entity.EntityDamageByEntityEvent; -import java.lang.reflect.InvocationTargetException; - +/** + * @author sky + */ public class DamageUtils { + public static Player getAttackerInDamageEvent(EntityDamageByEntityEvent e) { + if (e.getDamager() instanceof Player) { + return (Player) e.getDamager(); + } else if (e.getDamager() instanceof Projectile && ((Projectile) e.getDamager()).getShooter() instanceof Player) { + return (Player) ((Projectile) e.getDamager()).getShooter(); + } else { + return null; + } + } + + // ********************************* + // + // Deprecated + // + // ********************************* + + @Deprecated public static void damage(Player player, LivingEntity victim, double damage) { dmg(player, victim, damage); } + @Deprecated public static void damage(Player player, Entity victim, double damage) { - if (victim instanceof LivingEntity) { - dmg(player, (LivingEntity) victim, damage); - } + dmg(player, (LivingEntity) victim, damage); } - public static void dmg(LivingEntity paramLivingEntity1, LivingEntity paramLivingEntity2, double paramDouble) { - if ((paramLivingEntity2.hasMetadata("NPC")) || (paramLivingEntity1.hasMetadata("NPC"))) { - return; - } - - Object localObject1; - try { - localObject1 = paramLivingEntity1.getClass().getDeclaredMethod("getHandle").invoke(paramLivingEntity1); - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException localIllegalAccessException1) { - return; - } - - Object localObject2; - try { - localObject2 = paramLivingEntity2.getClass().getDeclaredMethod("getHandle").invoke(paramLivingEntity2); - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException localIllegalAccessException2) { - return; - } - - try { - Class DamageSource = nmsClass("DamageSource"); - Object localObject3 = DamageSource.getDeclaredMethod("playerAttack", nmsClass("EntityHuman")).invoke(DamageSource, localObject1); - - localObject2.getClass().getDeclaredMethod("damageEntity", new Class[]{DamageSource, Float.TYPE}).invoke(localObject2, localObject3, (float) paramDouble); - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException ignored) { - } - } - - private static Class nmsClass(String paramString) { - String str = "net.minecraft.server." + TabooLib.getVersion() + "." + paramString; - try { - return Class.forName(str); - } catch (ClassNotFoundException e) { - return null; - } + @Deprecated + public static void dmg(LivingEntity attacker, LivingEntity victim, double damage) { + attacker.damage(damage, victim); } } diff --git a/src/main/java/me/skymc/taboolib/damage/GetDamager.java b/src/main/java/me/skymc/taboolib/damage/GetDamager.java index aee2773..cb171d3 100644 --- a/src/main/java/me/skymc/taboolib/damage/GetDamager.java +++ b/src/main/java/me/skymc/taboolib/damage/GetDamager.java @@ -4,19 +4,11 @@ import org.bukkit.entity.Player; import org.bukkit.entity.Projectile; import org.bukkit.event.entity.EntityDamageByEntityEvent; +@Deprecated public class GetDamager { public static Player get(EntityDamageByEntityEvent e) { - Player p = null; - if (e.getDamager() instanceof Projectile) { - Projectile arrow = (Projectile) e.getDamager(); - if (arrow.getShooter() instanceof Player) { - p = (Player) arrow.getShooter(); - } - } else if (e.getDamager() instanceof Player) { - p = (Player) e.getDamager(); - } - return p; + return DamageUtils.getAttackerInDamageEvent(e); } } diff --git a/src/main/java/me/skymc/taboolib/damage/GetKiller.java b/src/main/java/me/skymc/taboolib/damage/GetKiller.java index 1206c85..0fcc95b 100644 --- a/src/main/java/me/skymc/taboolib/damage/GetKiller.java +++ b/src/main/java/me/skymc/taboolib/damage/GetKiller.java @@ -1,22 +1,13 @@ package me.skymc.taboolib.damage; import org.bukkit.entity.Player; -import org.bukkit.entity.Projectile; import org.bukkit.event.entity.EntityDeathEvent; +@Deprecated public class GetKiller { public static Player get(EntityDeathEvent e) { - Player p = null; - if (e.getEntity().getKiller() instanceof Projectile) { - Projectile arrow = (Projectile) e.getEntity().getKiller(); - if (arrow.getShooter() instanceof Player) { - p = (Player) arrow.getShooter(); - } - } else if (e.getEntity().getKiller() != null) { - p = e.getEntity().getKiller(); - } - return p; + return e.getEntity().getKiller(); } } diff --git a/src/main/java/me/skymc/taboolib/entity/VectorUtils.java b/src/main/java/me/skymc/taboolib/entity/VectorUtils.java index 2428757..e174206 100644 --- a/src/main/java/me/skymc/taboolib/entity/VectorUtils.java +++ b/src/main/java/me/skymc/taboolib/entity/VectorUtils.java @@ -18,7 +18,7 @@ public class VectorUtils { /** * 物品丢弃 - * + *

* 常用参数: * itemDrop(player, itemStack, 0.2, 0.5) * @@ -57,7 +57,7 @@ public class VectorUtils { /** * 生物抛射 - * + *

* 常用参数: * entityPush(entity, location, 15) * @@ -69,17 +69,17 @@ public class VectorUtils { Location from = entity.getLocation(); Vector test = to.clone().subtract(from).toVector(); - Double elevation = test.getY(); + double elevation = test.getY(); Double launchAngle = calculateLaunchAngle(from, to, velocity, elevation, 20.0D); - Double distance = Math.sqrt(Math.pow(test.getX(), 2.0D) + Math.pow(test.getZ(), 2.0D)); + double distance = Math.sqrt(Math.pow(test.getX(), 2.0D) + Math.pow(test.getZ(), 2.0D)); if (distance == 0.0D) { return; } if (launchAngle == null) { launchAngle = Math.atan((40.0D * elevation + Math.pow(velocity, 2.0D)) / (40.0D * elevation + 2.0D * Math.pow(velocity, 2.0D))); } - Double hangTime = calculateHangTime(launchAngle, velocity, elevation, 20.0D); + double hangTime = calculateHangTime(launchAngle, velocity, elevation, 20.0D); test.setY(Math.tan(launchAngle) * distance); test = normalizeVector(test); @@ -113,7 +113,7 @@ public class VectorUtils { private static Double calculateLaunchAngle(Location from, Location to, double v, double elevation, double g) { Vector vector = from.clone().subtract(to).toVector(); - Double distance = Math.sqrt(Math.pow(vector.getX(), 2.0D) + Math.pow(vector.getZ(), 2.0D)); + double distance = Math.sqrt(Math.pow(vector.getX(), 2.0D) + Math.pow(vector.getZ(), 2.0D)); double v2 = Math.pow(v, 2.0D); double v4 = Math.pow(v, 4.0D); double check = g * (g * Math.pow(distance, 2.0D) + 2.0D * elevation * v2); diff --git a/src/main/java/me/skymc/taboolib/events/DefaultEvent2.java b/src/main/java/me/skymc/taboolib/events/DefaultEvent2.java deleted file mode 100644 index def7099..0000000 --- a/src/main/java/me/skymc/taboolib/events/DefaultEvent2.java +++ /dev/null @@ -1,53 +0,0 @@ -package me.skymc.taboolib.events; - -import org.bukkit.entity.Player; -import org.bukkit.event.Cancellable; -import org.bukkit.event.HandlerList; -import org.bukkit.event.player.PlayerEvent; - -public class DefaultEvent2 extends PlayerEvent { - - private static final HandlerList handlers; - - static { - handlers = new HandlerList(); - } - - private DefaultEvent2(final Player who) { - super(who); - } - - public static HandlerList getHandlerList() { - return DefaultEvent2.handlers; - } - - @Override - public HandlerList getHandlers() { - return DefaultEvent2.handlers; - } - - public static class Pre extends DefaultEvent2 implements Cancellable { - private boolean cancelled; - - public Pre(Player who) { - super(who); - this.cancelled = false; - } - - @Override - public boolean isCancelled() { - return this.cancelled; - } - - @Override - public void setCancelled(final boolean cancelled) { - this.cancelled = cancelled; - } - } - - public static class Post extends DefaultEvent2 { - public Post(Player who) { - super(who); - } - } -}