This commit is contained in:
坏黑 2018-09-15 00:21:58 +08:00
parent dbf319addb
commit a9700dfdd3
8 changed files with 170 additions and 121 deletions

View File

@ -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;
}
}

View File

@ -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;
}

View File

@ -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<String, List<Container>> 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<Container> 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());
}
}
}
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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();
}
}

View File

@ -18,7 +18,7 @@ public class VectorUtils {
/**
* 物品丢弃
*
* <p>
* 常用参数
* itemDrop(player, itemStack, 0.2, 0.5)
*
@ -57,7 +57,7 @@ public class VectorUtils {
/**
* 生物抛射
*
* <p>
* 常用参数
* 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);

View File

@ -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);
}
}
}