Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package uk.sheepcraft.api;
- import java.lang.reflect.Constructor;
- import java.lang.reflect.Field;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Random;
- import net.milkbowl.vault.economy.Economy;
- import net.minecraft.server.v1_8_R1.ChatSerializer;
- import net.minecraft.server.v1_8_R1.EnumTitleAction;
- import net.minecraft.server.v1_8_R1.IChatBaseComponent;
- import net.minecraft.server.v1_8_R1.Packet;
- import net.minecraft.server.v1_8_R1.PacketPlayOutChat;
- import net.minecraft.server.v1_8_R1.PacketPlayOutTitle;
- import net.minecraft.server.v1_8_R1.PlayerConnection;
- import org.apache.commons.lang.math.RandomUtils;
- import org.bukkit.Bukkit;
- import org.bukkit.ChatColor;
- import org.bukkit.Color;
- import org.bukkit.FireworkEffect;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.craftbukkit.v1_8_R1.entity.CraftPlayer;
- import org.bukkit.entity.Firework;
- import org.bukkit.entity.Player;
- import org.bukkit.inventory.meta.FireworkMeta;
- import org.bukkit.plugin.RegisteredServiceProvider;
- import org.bukkit.util.Vector;
- import com.google.common.collect.Lists;
- public class API {
- //You are welcome to use this in your plugins
- //but please leave this in the Code :D
- //Made by Sheepcraft
- //more soon
- public void moresoon()
- {
- }
- //Depend
- public static boolean VAULT;
- public static Economy VAULT_ECONOMY;
- public static final void checkDependencies()
- {
- VAULT = Bukkit.getPluginManager().getPlugin("Vault") != null;
- if (VAULT)
- {
- for (RegisteredServiceProvider<?> t : Bukkit.getServicesManager().getRegistrations(Bukkit.getPluginManager().getPlugin("Vault"))) {
- System.out.println(t.getProvider().getClass());
- }
- RegisteredServiceProvider<Economy> economyProvider = Bukkit.getServicesManager().getRegistration(Economy.class);
- VAULT_ECONOMY = economyProvider != null ? economyProvider.getProvider() : null;
- VAULT = VAULT_ECONOMY != null;
- }
- }
- //Money
- public static void giveMoneyString(Player p, double string)
- {
- if (VAULT)
- {
- if (!VAULT_ECONOMY.hasAccount(p)) {
- VAULT_ECONOMY.createPlayerAccount(p);
- }
- if (string > 0.0D) {
- VAULT_ECONOMY.depositPlayer(p, string);
- } else if (string > 0.0D) {
- VAULT_ECONOMY.withdrawPlayer(p, -string);
- }
- }
- }
- public static void giveMoney(Player p, double price)
- {
- if (VAULT)
- {
- if (!VAULT_ECONOMY.hasAccount(p)) {
- VAULT_ECONOMY.createPlayerAccount(p);
- }
- if (price > 0.0D) {
- VAULT_ECONOMY.depositPlayer(p, price);
- } else if (price < 0.0D) {
- VAULT_ECONOMY.withdrawPlayer(p, -price);
- }
- }
- }
- public static void setMoney(Player p, double price)
- {
- if (VAULT)
- {
- if (!VAULT_ECONOMY.hasAccount(p)) {
- VAULT_ECONOMY.createPlayerAccount(p);
- }
- double balance = VAULT_ECONOMY.getBalance(p);
- if (balance > 0.0D) {
- VAULT_ECONOMY.withdrawPlayer(p, balance);
- } else if (balance < 0.0D) {
- VAULT_ECONOMY.depositPlayer(p, balance);
- }
- if (price > 0.0D) {
- VAULT_ECONOMY.depositPlayer(p, price);
- } else {
- VAULT_ECONOMY.withdrawPlayer(p, -price);
- }
- }
- }
- public static double getMoney(Player p)
- {
- if (VAULT)
- {
- if (!VAULT_ECONOMY.hasAccount(p)) {
- VAULT_ECONOMY.createPlayerAccount(p);
- }
- return VAULT_ECONOMY.getBalance(p);
- }
- return 0.0D;
- }
- public static boolean hasMoney(Player p, double price)
- {
- if ((VAULT) && (getMoney(p) >= price)) {
- return true;
- }
- return false;
- }
- //ReflectionUtils
- public static Constructor<?> getConstructor(Class<?> clazz, Class<?>... parameterTypes)
- throws NoSuchMethodException
- {
- Class<?>[] primitiveTypes = DataType.getPrimitive(parameterTypes);
- for (Constructor<?> constructor : clazz.getConstructors()) {
- if (DataType.compare(DataType.getPrimitive(constructor.getParameterTypes()), primitiveTypes)) {
- return constructor;
- }
- }
- throw new NoSuchMethodException("There is no such constructor in this class with the specified parameter types");
- }
- public static Constructor<?> getConstructor(String className, PackageType packageType, Class<?>... parameterTypes)
- throws NoSuchMethodException, ClassNotFoundException
- {
- return getConstructor(packageType.getClass(className), parameterTypes);
- }
- public static Object instantiateObject(Class<?> clazz, Object... arguments)
- throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException
- {
- return getConstructor(clazz, DataType.getPrimitive(arguments)).newInstance(arguments);
- }
- public static Object instantiateObject(String className, PackageType packageType, Object... arguments)
- throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException
- {
- return instantiateObject(packageType.getClass(className), arguments);
- }
- public static Method getMethod(Class<?> clazz, String methodName, Class<?>... parameterTypes)
- throws NoSuchMethodException
- {
- Class<?>[] primitiveTypes = DataType.getPrimitive(parameterTypes);
- for (Method method : clazz.getMethods()) {
- if ((method.getName().equals(methodName)) && (DataType.compare(DataType.getPrimitive(method.getParameterTypes()), primitiveTypes))) {
- return method;
- }
- }
- throw new NoSuchMethodException("There is no such method in this class with the specified name and parameter types");
- }
- public static Method getMethod(String className, PackageType packageType, String methodName, Class<?>... parameterTypes)
- throws NoSuchMethodException, ClassNotFoundException
- {
- return getMethod(packageType.getClass(className), methodName, parameterTypes);
- }
- public static Object invokeMethod(Object instance, String methodName, Object... arguments)
- throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException
- {
- return getMethod(instance.getClass(), methodName, DataType.getPrimitive(arguments)).invoke(instance, arguments);
- }
- public static Object invokeMethod(Object instance, Class<?> clazz, String methodName, Object... arguments)
- throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException
- {
- return getMethod(clazz, methodName, DataType.getPrimitive(arguments)).invoke(instance, arguments);
- }
- public static Object invokeMethod(Object instance, String className, PackageType packageType, String methodName, Object... arguments)
- throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException
- {
- return invokeMethod(instance, packageType.getClass(className), methodName, arguments);
- }
- public static Field getField(Class<?> clazz, boolean declared, String fieldName)
- throws NoSuchFieldException, SecurityException
- {
- Field field = declared ? clazz.getDeclaredField(fieldName) : clazz.getField(fieldName);
- field.setAccessible(true);
- return field;
- }
- public static Field getField(String className, PackageType packageType, boolean declared, String fieldName)
- throws NoSuchFieldException, SecurityException, ClassNotFoundException
- {
- return getField(packageType.getClass(className), declared, fieldName);
- }
- public static Object getValue(Object instance, Class<?> clazz, boolean declared, String fieldName)
- throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException
- {
- return getField(clazz, declared, fieldName).get(instance);
- }
- public static Object getValue(Object instance, String className, PackageType packageType, boolean declared, String fieldName)
- throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, ClassNotFoundException
- {
- return getValue(instance, packageType.getClass(className), declared, fieldName);
- }
- public static Object getValue(Object instance, boolean declared, String fieldName)
- throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException
- {
- return getValue(instance, instance.getClass(), declared, fieldName);
- }
- public static void setValue(Object instance, Class<?> clazz, boolean declared, String fieldName, Object value)
- throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException
- {
- getField(clazz, declared, fieldName).set(instance, value);
- }
- public static void setValue(Object instance, String className, PackageType packageType, boolean declared, String fieldName, Object value)
- throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, ClassNotFoundException
- {
- setValue(instance, packageType.getClass(className), declared, fieldName, value);
- }
- public static void setValue(Object instance, boolean declared, String fieldName, Object value)
- throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException
- {
- setValue(instance, instance.getClass(), declared, fieldName, value);
- }
- public static enum PackageType
- {
- MINECRAFT_SERVER("net.minecraft.server." + getServerVersion()),
- CRAFTBUKKIT("org.bukkit.craftbukkit." + getServerVersion()),
- CRAFTBUKKIT_BLOCK(CRAFTBUKKIT, "block"),
- CRAFTBUKKIT_CHUNKIO(CRAFTBUKKIT, "chunkio"),
- CRAFTBUKKIT_COMMAND(CRAFTBUKKIT, "command"),
- CRAFTBUKKIT_CONVERSATIONS(CRAFTBUKKIT, "conversations"),
- CRAFTBUKKIT_ENCHANTMENS(CRAFTBUKKIT, "enchantments"),
- CRAFTBUKKIT_ENTITY(CRAFTBUKKIT, "entity"),
- CRAFTBUKKIT_EVENT(CRAFTBUKKIT, "event"),
- CRAFTBUKKIT_GENERATOR(CRAFTBUKKIT, "generator"),
- CRAFTBUKKIT_HELP(CRAFTBUKKIT, "help"),
- CRAFTBUKKIT_INVENTORY(CRAFTBUKKIT, "inventory"),
- CRAFTBUKKIT_MAP(CRAFTBUKKIT, "map"),
- CRAFTBUKKIT_METADATA(CRAFTBUKKIT, "metadata"),
- CRAFTBUKKIT_POTION(CRAFTBUKKIT, "potion"),
- CRAFTBUKKIT_PROJECTILES(CRAFTBUKKIT, "projectiles"),
- CRAFTBUKKIT_SCHEDULER(CRAFTBUKKIT, "scheduler"),
- CRAFTBUKKIT_SCOREBOARD(CRAFTBUKKIT, "scoreboard"),
- CRAFTBUKKIT_UPDATER(CRAFTBUKKIT, "updater"),
- CRAFTBUKKIT_UTIL(CRAFTBUKKIT, "util");
- private final String path;
- private PackageType(String path)
- {
- this.path = path;
- }
- private PackageType(PackageType parent, String path)
- {
- this(parent + "." + path);
- }
- public String getPath()
- {
- return this.path;
- }
- public Class<?> getClass(String className)
- throws ClassNotFoundException
- {
- return Class.forName(this + "." + className);
- }
- public String toString()
- {
- return this.path;
- }
- public static String getServerVersion()
- {
- return Bukkit.getServer().getClass().getPackage().getName().substring(23);
- }
- }
- public static enum DataType
- {
- BYTE(Byte.TYPE, Byte.class),
- SHORT(Short.TYPE, Short.class),
- INTEGER(Integer.TYPE, Integer.class),
- LONG(Long.TYPE, Long.class),
- CHARACTER(Character.TYPE, Character.class),
- FLOAT(Float.TYPE, Float.class),
- DOUBLE(Double.TYPE, Double.class),
- BOOLEAN(Boolean.TYPE, Boolean.class);
- private static final Map<Class<?>, DataType> CLASS_MAP;
- private final Class<?> primitive;
- private final Class<?> reference;
- static
- {
- CLASS_MAP = new HashMap<Class<?>, DataType>();
- for (DataType type : values())
- {
- CLASS_MAP.put(type.primitive, type);
- CLASS_MAP.put(type.reference, type);
- }
- }
- private DataType(Class<?> primitive, Class<?> reference)
- {
- this.primitive = primitive;
- this.reference = reference;
- }
- public Class<?> getPrimitive()
- {
- return this.primitive;
- }
- public Class<?> getReference()
- {
- return this.reference;
- }
- public static DataType fromClass(Class<?> clazz)
- {
- return CLASS_MAP.get(clazz);
- }
- public static Class<?> getPrimitive(Class<?> clazz)
- {
- DataType type = fromClass(clazz);
- return type == null ? clazz : type.getPrimitive();
- }
- public static Class<?> getReference(Class<?> clazz)
- {
- DataType type = fromClass(clazz);
- return type == null ? clazz : type.getReference();
- }
- public static Class<?>[] getPrimitive(Class<?>[] classes)
- {
- int length = classes == null ? 0 : classes.length;
- Class<?>[] types = new Class[length];
- for (int index = 0; index < length; index++) {
- types[index] = getPrimitive(classes[index]);
- }
- return types;
- }
- public static Class<?>[] getReference(Class<?>[] classes)
- {
- int length = classes == null ? 0 : classes.length;
- Class<?>[] types = new Class[length];
- for (int index = 0; index < length; index++) {
- types[index] = getReference(classes[index]);
- }
- return types;
- }
- public static Class<?>[] getPrimitive(Object[] objects)
- {
- int length = objects == null ? 0 : objects.length;
- Class<?>[] types = new Class[length];
- for (int index = 0; index < length; index++) {
- types[index] = getPrimitive(objects[index].getClass());
- }
- return types;
- }
- public static Class<?>[] getReference(Object[] objects)
- {
- int length = objects == null ? 0 : objects.length;
- Class<?>[] types = new Class[length];
- for (int index = 0; index < length; index++) {
- types[index] = getReference(objects[index].getClass());
- }
- return types;
- }
- public static boolean compare(Class<?>[] primary, Class<?>[] secondary)
- {
- if ((primary == null) || (secondary == null) || (primary.length != secondary.length)) {
- return false;
- }
- for (int index = 0; index < primary.length; index++)
- {
- Class<?> primaryClass = primary[index];
- Class<?> secondaryClass = secondary[index];
- if ((!primaryClass.equals(secondaryClass)) && (!primaryClass.isAssignableFrom(secondaryClass))) {
- return false;
- }
- }
- return true;
- }
- }
- //cooldown time
- private static HashMap<String, Long> cooldown = new HashMap<String, Long>();
- public static void putCooldown(Player p, int Seconds) {
- cooldown.put(p.getName(), System.currentTimeMillis() + (Seconds * 1000L));
- }
- public static int getCooldown(Player p) {
- if (cooldown.containsKey(p.getName())) {
- return Long.valueOf(((System.currentTimeMillis() - cooldown.get(p.getName())) / 1000)).intValue();
- } else {
- return 0;
- }
- }
- public static boolean isOnCooldown(Player p) {
- if (cooldown.containsKey(p.getName()) && cooldown.get(p.getName()) > System.currentTimeMillis()) {
- return true;
- } else {
- return false;
- }
- }
- public static void removeCooldown(Player p) {
- if (cooldown.containsKey(p.getName())) {
- cooldown.remove(p.getName());
- }
- }
- //Colorize
- private static final Random RANDOM = new Random();
- private static final char[] COLORS = {'1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
- private static final char[] STYLES = {'l', 'n', 'o', 'k', 'm'};
- private static final char[] ALL_COLORS = {'1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'l', 'n', 'o', 'k', 'm'};
- public static String getRandomColorCode(boolean withExtra) {
- char[] toRandomize = (withExtra ? ALL_COLORS : COLORS);
- return "&" + String.valueOf(toRandomize[RANDOM.nextInt(toRandomize.length)]);
- }
- public static Stylize style(String toStyle) {
- return new Stylize(toStyle);
- }
- public static char[] getStyles() {
- return STYLES;
- }
- public static class Stylize {
- private final String toStyle;
- protected Stylize(String toStyle) {
- this.toStyle = toStyle;
- }
- public String toRainbow() {
- StringBuilder sb = new StringBuilder();
- for (char c : toStyle.toCharArray()) {
- sb.append(API.getRandomColorCode(false) + String.valueOf(c));
- }
- return ChatColor.translateAlternateColorCodes('&', sb.toString());
- }
- public String toGarbage() {
- List<Character> characters = Lists.newArrayList();
- for (char c : toStyle.toCharArray()) {
- characters.add(c);
- }
- StringBuilder sb = new StringBuilder(toStyle.length());
- while (!characters.isEmpty()) {
- int randPicker = (int) (Math.random() * characters.size());
- sb.append(characters.remove(randPicker));
- }
- return sb.toString();
- }
- public String toStripe(ChatColor colorOne, ChatColor colorTwo) {
- StringBuilder sb = new StringBuilder();
- boolean a = true;
- for (char c : toStyle.toCharArray()) {
- sb.append(a ? colorOne : colorTwo);
- sb.append(c);
- }
- return sb.toString();
- }
- }
- //Titles
- public static void sendTitle(Player player, String title, String subtitle, int fadeIn, int stay, int fadeOut)
- {
- CraftPlayer craftplayer = (CraftPlayer)player;
- PlayerConnection connection = craftplayer.getHandle().playerConnection;
- IChatBaseComponent titleJSON = ChatSerializer.a("{'text': '" + ChatColor.translateAlternateColorCodes('&', title) + "'}");
- IChatBaseComponent subtitleJSON = ChatSerializer.a("{'text': '" + ChatColor.translateAlternateColorCodes('&', subtitle) + "'}");
- Packet length = new PacketPlayOutTitle(EnumTitleAction.TIMES, titleJSON, fadeIn, stay, fadeOut);
- Packet titlePacket = new PacketPlayOutTitle(EnumTitleAction.TITLE, titleJSON, fadeIn, stay, fadeOut);
- Packet subtitlePacket = new PacketPlayOutTitle(EnumTitleAction.SUBTITLE, subtitleJSON, fadeIn, stay, fadeOut);
- connection.sendPacket(titlePacket);
- connection.sendPacket(length);
- connection.sendPacket(subtitlePacket);
- }
- //Actionbar
- public static boolean sendActionBar(Player player, String message)
- {
- CraftPlayer p = (CraftPlayer)player;
- IChatBaseComponent cbc = ChatSerializer.a("{\"text\": \"" + message + "\"}");
- PacketPlayOutChat ppoc = new PacketPlayOutChat(cbc, (byte)2);
- p.getHandle().playerConnection.sendPacket(ppoc);
- return false;
- }
- //Firework
- public static void LaunchRandomFirework(Location location)
- {
- FireworkEffect.Builder builder = FireworkEffect.builder();
- if (RandomUtils.nextInt(3) == 0) {
- builder.withTrail();
- } else if (RandomUtils.nextInt(2) == 0) {
- builder.withFlicker();
- }
- builder.with(org.bukkit.FireworkEffect.Type.values()[RandomUtils.nextInt(org.bukkit.FireworkEffect.Type.values().length)]);
- int colorCount = 17;
- builder.withColor(Color.fromRGB(RandomUtils.nextInt(255), RandomUtils.nextInt(255), RandomUtils.nextInt(255)));
- while (RandomUtils.nextInt(colorCount) != 0)
- {
- builder.withColor(Color.fromRGB(RandomUtils.nextInt(255), RandomUtils.nextInt(255), RandomUtils.nextInt(255)));
- colorCount--;
- }
- Firework firework = location.getWorld().spawn(location, Firework.class);
- FireworkMeta data = firework.getFireworkMeta();
- data.addEffects(new FireworkEffect[] { builder.build() });
- data.setPower(RandomUtils.nextInt(3));
- firework.setFireworkMeta(data);
- }
- //ParticleEffect
- public enum ParticleEffect
- {
- EXPLOSION_NORMAL("explode", 0, -1),
- EXPLOSION_LARGE("largeexplode", 1, -1),
- EXPLOSION_HUGE("hugeexplosion", 2, -1),
- FIREWORKS_SPARK("fireworksSpark", 3, -1),
- WATER_BUBBLE("bubble", 4, -1, false, true),
- WATER_SPLASH("splash", 5, -1),
- WATER_WAKE("wake", 6, 7),
- SUSPENDED("suspended", 7, -1, false, true),
- SUSPENDED_DEPTH("depthSuspend", 8, -1),
- CRIT("crit", 9, -1),
- CRIT_MAGIC("magicCrit", 10, -1),
- SMOKE_NORMAL("smoke", 11, -1),
- SMOKE_LARGE("largesmoke", 12, -1),
- SPELL("spell", 13, -1),
- SPELL_INSTANT("instantSpell", 14, -1),
- SPELL_MOB("mobSpell", 15, -1),
- SPELL_MOB_AMBIENT("mobSpellAmbient", 16, -1),
- SPELL_WITCH("witchMagic", 17, -1),
- DRIP_WATER("dripWater", 18, -1),
- DRIP_LAVA("dripLava", 19, -1),
- VILLAGER_ANGRY("angryVillager", 20, -1),
- VILLAGER_HAPPY("happyVillager", 21, -1),
- TOWN_AURA("townaura", 22, -1),
- NOTE("note", 23, -1),
- PORTAL("portal", 24, -1),
- ENCHANTMENT_TABLE("enchantmenttable", 25, -1),
- FLAME("flame", 26, -1),
- LAVA("lava", 27, -1),
- FOOTSTEP("footstep", 28, -1),
- CLOUD("cloud", 29, -1),
- REDSTONE("reddust", 30, -1),
- SNOWBALL("snowballpoof", 31, -1),
- SNOW_SHOVEL("snowshovel", 32, -1),
- SLIME("slime", 33, -1),
- HEART("heart", 34, -1),
- BARRIER("barrier", 35, 8),
- ITEM_CRACK("iconcrack", 36, -1, true),
- BLOCK_CRACK("blockcrack", 37, -1, true),
- BLOCK_DUST("blockdust", 38, 7, true),
- WATER_DROP("droplet", 39, 8),
- ITEM_TAKE("take", 40, 8),
- CHEESE("cheese",41, 8),
- MOB_APPEARANCE("mobappearance", 41, 8);
- private static final Map<String, ParticleEffect> NAME_MAP;
- private static final Map<Integer, ParticleEffect> ID_MAP;
- private final String name;
- private final int id;
- private final int requiredVersion;
- private final boolean requiresData;
- private final boolean requiresWater;
- static
- {
- NAME_MAP = new HashMap<String, ParticleEffect>();
- ID_MAP = new HashMap<Integer, ParticleEffect>();
- for (ParticleEffect effect : values())
- {
- NAME_MAP.put(effect.name, effect);
- ID_MAP.put(Integer.valueOf(effect.id), effect);
- }
- }
- private ParticleEffect(String name, int id, int requiredVersion, boolean requiresData, boolean requiresWater)
- {
- this.name = name;
- this.id = id;
- this.requiredVersion = requiredVersion;
- this.requiresData = requiresData;
- this.requiresWater = requiresWater;
- }
- private ParticleEffect(String name, int id, int requiredVersion, boolean requiresData)
- {
- this(name, id, requiredVersion, requiresData, false);
- }
- private ParticleEffect(String name, int id, int requiredVersion)
- {
- this(name, id, requiredVersion, false);
- }
- public String getName()
- {
- return this.name;
- }
- public int getId()
- {
- return this.id;
- }
- public int getRequiredVersion()
- {
- return this.requiredVersion;
- }
- public boolean getRequiresData()
- {
- return this.requiresData;
- }
- public boolean getRequiresWater()
- {
- return this.requiresWater;
- }
- public boolean isSupported()
- {
- if (this.requiredVersion == -1) {
- return true;
- }
- return ParticlePacket.getVersion() >= this.requiredVersion;
- }
- public static ParticleEffect fromName(String name)
- {
- for (Map.Entry<String, ParticleEffect> entry : NAME_MAP.entrySet()) {
- if (((String)entry.getKey()).equalsIgnoreCase(name)) {
- return (ParticleEffect)entry.getValue();
- }
- }
- return null;
- }
- public static ParticleEffect fromId(int id)
- {
- for (Map.Entry<Integer, ParticleEffect> entry : ID_MAP.entrySet()) {
- if (((Integer)entry.getKey()).intValue() == id) {
- return (ParticleEffect)entry.getValue();
- }
- }
- return null;
- }
- private static boolean isWater(Location location)
- {
- Material material = location.getBlock().getType();
- return (material == Material.WATER) || (material == Material.STATIONARY_WATER);
- }
- private static boolean isLongDistance(Location location, List<Player> players)
- {
- for (Player player : players) {
- if (player.getLocation().distanceSquared(location) >= 65536.0D) {
- return true;
- }
- }
- return false;
- }
- private static boolean isDataCorrect(ParticleEffect effect, ParticleData data)
- {
- return ((effect != BLOCK_CRACK) && (effect != BLOCK_DUST)) || (((data instanceof BlockData)) || ((effect == ITEM_CRACK) && ((data instanceof ItemData))));
- }
- public void display(float offsetX, float offsetY, float offsetZ, float speed, int amount, Location center, double range)
- throws ParticleEffect.ParticleVersionException, ParticleEffect.ParticleDataException, IllegalArgumentException
- {
- if (!isSupported()) {
- throw new ParticleVersionException("This particle effect is not supported by your server version");
- }
- if (this.requiresData) {
- throw new ParticleDataException("This particle effect requires additional data");
- }
- if ((this.requiresWater) && (!isWater(center))) {
- throw new IllegalArgumentException("There is no water at the center location");
- }
- new ParticlePacket(this, offsetX, offsetY, offsetZ, speed, amount, range > 256.0D, null).sendTo(center, range);
- }
- public void display(float offsetX, float offsetY, float offsetZ, float speed, int amount, Location center, List<Player> players)
- throws ParticleEffect.ParticleVersionException, ParticleEffect.ParticleDataException, IllegalArgumentException
- {
- if (!isSupported()) {
- throw new ParticleVersionException("This particle effect is not supported by your server version");
- }
- if (this.requiresData) {
- throw new ParticleDataException("This particle effect requires additional data");
- }
- if ((this.requiresWater) && (!isWater(center))) {
- throw new IllegalArgumentException("There is no water at the center location");
- }
- new ParticlePacket(this, offsetX, offsetY, offsetZ, speed, amount, isLongDistance(center, players), null).sendTo(center, players);
- }
- public void display(float offsetX, float offsetY, float offsetZ, float speed, int amount, Location center, Player... players)
- throws ParticleEffect.ParticleVersionException, ParticleEffect.ParticleDataException, IllegalArgumentException
- {
- display(offsetX, offsetY, offsetZ, speed, amount, center, Arrays.asList(players));
- }
- public void display(Vector direction, float speed, Location center, double range)
- throws ParticleEffect.ParticleVersionException, ParticleEffect.ParticleDataException, IllegalArgumentException
- {
- if (!isSupported()) {
- throw new ParticleVersionException("This particle effect is not supported by your server version");
- }
- if (this.requiresData) {
- throw new ParticleDataException("This particle effect requires additional data");
- }
- if ((this.requiresWater) && (!isWater(center))) {
- throw new IllegalArgumentException("There is no water at the center location");
- }
- new ParticlePacket(this, direction, speed, range > 256.0D, null).sendTo(center, range);
- }
- public void display(Vector direction, float speed, Location center, List<Player> players)
- throws ParticleEffect.ParticleVersionException, ParticleEffect.ParticleDataException, IllegalArgumentException
- {
- if (!isSupported()) {
- throw new ParticleVersionException("This particle effect is not supported by your server version");
- }
- if (this.requiresData) {
- throw new ParticleDataException("This particle effect requires additional data");
- }
- if ((this.requiresWater) && (!isWater(center))) {
- throw new IllegalArgumentException("There is no water at the center location");
- }
- new ParticlePacket(this, direction, speed, isLongDistance(center, players), null).sendTo(center, players);
- }
- public void display(Vector direction, float speed, Location center, Player... players)
- throws ParticleEffect.ParticleVersionException, ParticleEffect.ParticleDataException, IllegalArgumentException
- {
- display(direction, speed, center, Arrays.asList(players));
- }
- public void display(ParticleData data, float offsetX, float offsetY, float offsetZ, float speed, int amount, Location center, double range)
- throws ParticleEffect.ParticleVersionException, ParticleEffect.ParticleDataException
- {
- if (!isSupported()) {
- throw new ParticleVersionException("This particle effect is not supported by your server version");
- }
- if (!this.requiresData) {
- throw new ParticleDataException("This particle effect does not require additional data");
- }
- if (!isDataCorrect(this, data)) {
- throw new ParticleDataException("The particle data type is incorrect");
- }
- new ParticlePacket(this, offsetX, offsetY, offsetZ, speed, amount, range > 256.0D, data).sendTo(center, range);
- }
- public void display(ParticleData data, float offsetX, float offsetY, float offsetZ, float speed, int amount, Location center, List<Player> players)
- throws ParticleEffect.ParticleVersionException, ParticleEffect.ParticleDataException
- {
- if (!isSupported()) {
- throw new ParticleVersionException("This particle effect is not supported by your server version");
- }
- if (!this.requiresData) {
- throw new ParticleDataException("This particle effect does not require additional data");
- }
- if (!isDataCorrect(this, data)) {
- throw new ParticleDataException("The particle data type is incorrect");
- }
- new ParticlePacket(this, offsetX, offsetY, offsetZ, speed, amount, isLongDistance(center, players), data).sendTo(center, players);
- }
- public void display(ParticleData data, float offsetX, float offsetY, float offsetZ, float speed, int amount, Location center, Player... players)
- throws ParticleEffect.ParticleVersionException, ParticleEffect.ParticleDataException
- {
- display(data, offsetX, offsetY, offsetZ, speed, amount, center, Arrays.asList(players));
- }
- public void display(ParticleData data, Vector direction, float speed, Location center, double range)
- throws ParticleEffect.ParticleVersionException, ParticleEffect.ParticleDataException
- {
- if (!isSupported()) {
- throw new ParticleVersionException("This particle effect is not supported by your server version");
- }
- if (!this.requiresData) {
- throw new ParticleDataException("This particle effect does not require additional data");
- }
- if (!isDataCorrect(this, data)) {
- throw new ParticleDataException("The particle data type is incorrect");
- }
- new ParticlePacket(this, direction, speed, range > 256.0D, data).sendTo(center, range);
- }
- public void display(ParticleData data, Vector direction, float speed, Location center, List<Player> players)
- throws ParticleEffect.ParticleVersionException, ParticleEffect.ParticleDataException
- {
- if (!isSupported()) {
- throw new ParticleVersionException("This particle effect is not supported by your server version");
- }
- if (!this.requiresData) {
- throw new ParticleDataException("This particle effect does not require additional data");
- }
- if (!isDataCorrect(this, data)) {
- throw new ParticleDataException("The particle data type is incorrect");
- }
- new ParticlePacket(this, direction, speed, isLongDistance(center, players), data).sendTo(center, players);
- }
- public void display(ParticleData data, Vector direction, float speed, Location center, Player... players)
- throws ParticleEffect.ParticleVersionException, ParticleEffect.ParticleDataException
- {
- display(data, direction, speed, center, Arrays.asList(players));
- }
- public static abstract class ParticleData
- {
- private final Material material;
- private final byte data;
- private final int[] packetData;
- @SuppressWarnings("deprecation")
- public ParticleData(Material material, byte data)
- {
- this.material = material;
- this.data = data;
- this.packetData = new int[] { material.getId(), data };
- }
- public Material getMaterial()
- {
- return this.material;
- }
- public byte getData()
- {
- return this.data;
- }
- public int[] getPacketData()
- {
- return this.packetData;
- }
- public String getPacketDataString()
- {
- return "_" + this.packetData[0] + "_" + this.packetData[1];
- }
- }
- public static final class ItemData
- extends ParticleEffect.ParticleData
- {
- public ItemData(Material material, byte data)
- {
- super(material, data);
- }
- }
- public static final class BlockData
- extends ParticleEffect.ParticleData
- {
- public BlockData(Material material, byte data)
- throws IllegalArgumentException
- {
- super(material, data);
- if (!material.isBlock()) {
- throw new IllegalArgumentException("The material is not a block");
- }
- }
- }
- private static final class ParticleDataException
- extends RuntimeException
- {
- private static final long serialVersionUID = 3203085387160737484L;
- public ParticleDataException(String message)
- {
- super();
- }
- }
- private static final class ParticleVersionException
- extends RuntimeException
- {
- private static final long serialVersionUID = 3203085387160737484L;
- public ParticleVersionException(String message)
- {
- super();
- }
- }
- public static final class ParticlePacket
- {
- private static int version;
- private static Class<?> enumParticle;
- private static Constructor<?> packetConstructor;
- private static Method getHandle;
- private static Field playerConnection;
- private static Method sendPacket;
- private static boolean initialized;
- private final ParticleEffect effect;
- private final float offsetX;
- private final float offsetY;
- private final float offsetZ;
- private final float speed;
- private final int amount;
- private final boolean longDistance;
- private final ParticleEffect.ParticleData data;
- private Object packet;
- public ParticlePacket(ParticleEffect effect, float offsetX, float offsetY, float offsetZ, float speed, int amount, boolean longDistance, ParticleEffect.ParticleData data)
- throws IllegalArgumentException
- {
- initialize();
- if (speed < 0.0F) {
- throw new IllegalArgumentException("The speed is lower than 0");
- }
- if (amount < 1) {
- throw new IllegalArgumentException("The amount is lower than 1");
- }
- this.effect = effect;
- this.offsetX = offsetX;
- this.offsetY = offsetY;
- this.offsetZ = offsetZ;
- this.speed = speed;
- this.amount = amount;
- this.longDistance = longDistance;
- this.data = data;
- }
- public ParticlePacket(ParticleEffect effect, Vector direction, float speed, boolean longDistance, ParticleEffect.ParticleData data)
- throws IllegalArgumentException
- {
- initialize();
- if (speed < 0.0F) {
- throw new IllegalArgumentException("The speed is lower than 0");
- }
- this.effect = effect;
- this.offsetX = ((float)direction.getX());
- this.offsetY = ((float)direction.getY());
- this.offsetZ = ((float)direction.getZ());
- this.speed = speed;
- this.amount = 0;
- this.longDistance = longDistance;
- this.data = data;
- }
- public static void initialize()
- throws ParticleEffect.ParticlePacket.VersionIncompatibleException
- {
- if (initialized) {
- return;
- }
- try
- {
- version = Integer.parseInt(Character.toString(PackageType.getServerVersion().charAt(3)));
- if (version > 7) {
- enumParticle = PackageType.MINECRAFT_SERVER.getClass("EnumParticle");
- }
- Class<?> packetClass = PackageType.MINECRAFT_SERVER.getClass(version < 7 ? "Packet63WorldParticles" : "PacketPlayOutWorldParticles");
- packetConstructor = getConstructor(packetClass, new Class[0]);
- getHandle = getMethod("CraftPlayer", API.PackageType.CRAFTBUKKIT_ENTITY, "getHandle", new Class[0]);
- playerConnection = getField("EntityPlayer", API.PackageType.MINECRAFT_SERVER, false, "playerConnection");
- sendPacket = getMethod(playerConnection.getType(), "sendPacket", new Class[] { PackageType.MINECRAFT_SERVER.getClass("Packet") });
- }
- catch (Exception exception)
- {
- throw new VersionIncompatibleException("Your current bukkit version seems to be incompatible with this library", exception);
- }
- initialized = true;
- }
- public static int getVersion()
- {
- return version;
- }
- public static boolean isInitialized()
- {
- return initialized;
- }
- public void sendTo(Location center, Player player)
- throws ParticleEffect.ParticlePacket.PacketInstantiationException, ParticleEffect.ParticlePacket.PacketSendingException
- {
- if (this.packet == null) {
- try
- {
- this.packet = packetConstructor.newInstance(new Object[0]);
- Object id;
- if (version < 8) {
- id = this.effect.getName() + (this.data == null ? "" : this.data.getPacketDataString());
- } else {
- id = enumParticle.getEnumConstants()[this.effect.getId()];
- }
- setValue(this.packet, true, "a", id);
- setValue(this.packet, true, "b", Float.valueOf((float)center.getX()));
- setValue(this.packet, true, "c", Float.valueOf((float)center.getY()));
- setValue(this.packet, true, "d", Float.valueOf((float)center.getZ()));
- setValue(this.packet, true, "e", Float.valueOf(this.offsetX));
- setValue(this.packet, true, "f", Float.valueOf(this.offsetY));
- setValue(this.packet, true, "g", Float.valueOf(this.offsetZ));
- setValue(this.packet, true, "h", Float.valueOf(this.speed));
- setValue(this.packet, true, "i", Integer.valueOf(this.amount));
- if (version > 7)
- {
- API.setValue(this.packet, true, "j", Boolean.valueOf(this.longDistance));
- API.setValue(this.packet, true, "k", this.data == null ? new int[0] : this.data.getPacketData());
- }
- }
- catch (Exception exception)
- {
- throw new PacketInstantiationException("Packet instantiation failed", exception);
- }
- }
- try
- {
- sendPacket.invoke(playerConnection.get(getHandle.invoke(player, new Object[0])), new Object[] { this.packet });
- }
- catch (Exception exception)
- {
- throw new PacketSendingException("Failed to send the packet to player '" + player.getName() + "'", exception);
- }
- }
- public void sendTo(Location center, List<Player> players)
- throws IllegalArgumentException
- {
- if (players.isEmpty()) {
- throw new IllegalArgumentException("The player list is empty");
- }
- for (Player player : players) {
- sendTo(center, player);
- }
- }
- public void sendTo(Location center, double range)
- throws IllegalArgumentException
- {
- if (range < 1.0D) {
- throw new IllegalArgumentException("The range is lower than 1");
- }
- String worldName = center.getWorld().getName();
- double squared = range * range;
- for (Player player : Bukkit.getOnlinePlayers()) {
- if ((player.getWorld().getName().equals(worldName)) && (player.getLocation().distanceSquared(center) <= squared)) {
- sendTo(center, player);
- }
- }
- }
- private static final class VersionIncompatibleException
- extends RuntimeException
- {
- private static final long serialVersionUID = 3203085387160737484L;
- public VersionIncompatibleException(String message, Throwable cause)
- {
- super(cause);
- }
- }
- private static final class PacketInstantiationException
- extends RuntimeException
- {
- private static final long serialVersionUID = 3203085387160737484L;
- public PacketInstantiationException(String message, Throwable cause)
- {
- super(cause);
- }
- }
- private static final class PacketSendingException
- extends RuntimeException
- {
- private static final long serialVersionUID = 3203085387160737484L;
- public PacketSendingException(String message, Throwable cause)
- {
- super(cause);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement