mrkirby153

Untitled

Dec 24th, 2014
892
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 18.49 KB | None | 0 0
  1. package me.mrkirby153.swc.api.nms;
  2.  
  3. import me.mrkirby153.swc.api.player.SWCPlayers;
  4. import me.mrkirby153.swc.hub.player.SWCPlayer;
  5. import net.minecraft.server.v1_7_R4.*;
  6. import org.bukkit.ChatColor;
  7. import org.bukkit.Location;
  8. import org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer;
  9. import org.bukkit.entity.EntityType;
  10. import org.bukkit.entity.Player;
  11. import org.bukkit.event.EventHandler;
  12. import org.bukkit.event.Listener;
  13. import org.bukkit.event.player.PlayerMoveEvent;
  14. import org.bukkit.plugin.java.JavaPlugin;
  15. import org.bukkit.scheduler.BukkitRunnable;
  16.  
  17. import java.lang.reflect.Field;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20.  
  21.  
  22. public class BarAPI implements Listener{
  23.  
  24.  
  25.     public static final int ENTITY_ID = 1234;
  26.     private static JavaPlugin plugin;
  27.     private static HashMap<String, Boolean> hasHealthBar = new HashMap<String, Boolean>();
  28.     private static Map<Player, Integer> percentageMap = new HashMap<Player, Integer>();
  29.     private static Map<Player, Boolean> stopPercentage = new HashMap<Player, Boolean>();
  30.  
  31.     private static Map<String, Integer> direction = new HashMap<String, Integer>();
  32.     private static Map<String, String> displayText = new HashMap<String, String>();
  33.  
  34.     public static void sendPacket(Player player, Packet packet) {
  35.         EntityPlayer entityPlayer = ((CraftPlayer) player).getHandle();
  36.  
  37.         entityPlayer.playerConnection.sendPacket(packet);
  38.     }
  39.  
  40.     /**
  41.      * CALL THIS BEFORE DOING ANYTHING. PASS IN AN INSTANCE TO YOUR PLUGIN
  42.      *
  43.      * @param plugin
  44.      */
  45.     public static void init(JavaPlugin plugin) {
  46.         BarAPI.plugin = plugin;
  47.         plugin.getServer().getPluginManager().registerEvents(new BarAPI(), plugin);
  48.     }
  49.  
  50.     public static Field getField(Class<?> cl, String field_name) {
  51.         try {
  52.             Field field = cl.getDeclaredField(field_name);
  53.             return field;
  54.         } catch (SecurityException e) {
  55.             e.printStackTrace();
  56.         } catch (NoSuchFieldException e) {
  57.             e.printStackTrace();
  58.         }
  59.         return null;
  60.     }
  61.  
  62.     //Accessing packets
  63.     @SuppressWarnings("deprecation")
  64.     public static PacketPlayOutSpawnEntityLiving getMobPacket(String text, Location loc, Player player) {
  65.         PacketPlayOutSpawnEntityLiving mobPacket = new PacketPlayOutSpawnEntityLiving();
  66.         boolean is18 = (((CraftPlayer) player).getHandle().playerConnection.networkManager.getVersion() == 47);
  67.         try {
  68.  
  69.             Field a = getField(mobPacket.getClass(), "a");
  70.             a.setAccessible(true);
  71.             a.set(mobPacket, ENTITY_ID);
  72.  
  73.             Field b = getField(mobPacket.getClass(), "b");
  74.             b.setAccessible(true);
  75.             if (is18) {
  76.                 b.set(mobPacket, (byte) EntityType.WITHER.getTypeId());
  77.             } else {
  78.                 b.set(mobPacket, EntityType.ENDER_DRAGON.getTypeId());
  79.             }
  80.             // Calculate 30 blocks in fromt
  81.             if (!is18) {
  82.                 SWCPlayer swcPlayer = SWCPlayers.findPlayer(player);
  83.                 if (swcPlayer != null) {
  84.                     Location hubSpawn = swcPlayer.getCurrentHub().getSpawnLocation();
  85.                     loc = new Location(hubSpawn.getWorld(), hubSpawn.getX(), 1, hubSpawn.getZ());
  86.                 } else {
  87.                     loc = new Location(player.getLocation().getWorld(), player.getLocation().getX(), 1, player.getLocation().getZ());
  88.                 }
  89.                 Field c = getField(mobPacket.getClass(), "c");
  90.                 c.setAccessible(true);
  91.                 c.set(mobPacket, (int) Math.floor(loc.getBlockX() * 32.0D));
  92.  
  93.                 Field d = getField(mobPacket.getClass(), "d");
  94.                 d.setAccessible(true);
  95.                 d.set(mobPacket, (int) Math.floor(loc.getBlockY() * 32.0D));
  96.  
  97.                 Field e = getField(mobPacket.getClass(), "e");
  98.                 e.setAccessible(true);
  99.                 e.set(mobPacket, (int) Math.floor(loc.getBlockZ() * 32.0D));
  100.             } else {
  101.                 Location newLoc;
  102.                 newLoc = player.getLocation().add(player.getLocation().getDirection().setY(0).normalize().multiply(30));
  103.                 loc = newLoc;
  104.                 int x = (int) Math.floor(loc.getBlockX() * 32.0D);
  105.                 int y = (int) Math.floor(loc.getBlockY() * 32.0D);
  106.                 int z = (int) Math.floor(loc.getBlockZ() * 32.0D);
  107.                 System.out.println("Spawned at " + loc.getBlockX() + ", " + loc.getBlockY() + ", " + loc.getBlockZ());
  108.                 Field c = getField(mobPacket.getClass(), "c");
  109.                 c.setAccessible(true);
  110.                 c.set(mobPacket, x);
  111.  
  112.                 Field d = getField(mobPacket.getClass(), "d");
  113.                 d.setAccessible(true);
  114.                 d.set(mobPacket, y);
  115.  
  116.                 Field e = getField(mobPacket.getClass(), "e");
  117.                 e.setAccessible(true);
  118.                 e.set(mobPacket, z);
  119.             }
  120.  
  121.             Field f = getField(mobPacket.getClass(), "f");
  122.             f.setAccessible(true);
  123.             f.set(mobPacket, (byte) 0);
  124.  
  125.             Field g = getField(mobPacket.getClass(), "g");
  126.             g.setAccessible(true);
  127.             g.set(mobPacket, (byte) 0);
  128.  
  129.             Field h = getField(mobPacket.getClass(), "h");
  130.             h.setAccessible(true);
  131.             h.set(mobPacket, (byte) 0);
  132.  
  133.             Field i = getField(mobPacket.getClass(), "i");
  134.             i.setAccessible(true);
  135.             i.set(mobPacket, (byte) 0);
  136.  
  137.             Field j = getField(mobPacket.getClass(), "j");
  138.             j.setAccessible(true);
  139.             j.set(mobPacket, (byte) 0);
  140.  
  141.             Field k = getField(mobPacket.getClass(), "k");
  142.             k.setAccessible(true);
  143.             k.set(mobPacket, (byte) 0);
  144.  
  145.         } catch (IllegalArgumentException | IllegalAccessException e1) {
  146.             e1.printStackTrace();
  147.         }
  148.  
  149.         DataWatcher watcher = getWatcher(text, 300, ((CraftPlayer) player).getHandle().playerConnection.networkManager.getVersion());
  150.  
  151.         try {
  152.             Field t = PacketPlayOutSpawnEntityLiving.class.getDeclaredField("l");
  153.             t.setAccessible(true);
  154.             t.set(mobPacket, watcher);
  155.         } catch (Exception ex) {
  156.             ex.printStackTrace();
  157.         }
  158.  
  159.         return mobPacket;
  160.     }
  161.  
  162.     public static PacketPlayOutEntityDestroy getDestroyEntityPacket() {
  163.         PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy();
  164.  
  165.         Field a = getField(packet.getClass(), "a");
  166.         a.setAccessible(true);
  167.         try {
  168.             a.set(packet, new int[]{ENTITY_ID});
  169.         } catch (IllegalArgumentException | IllegalAccessException e) {
  170.             e.printStackTrace();
  171.         }
  172.  
  173.         return packet;
  174.     }
  175.  
  176.     public static PacketPlayOutEntityMetadata getMetadataPacket(DataWatcher watcher) {
  177.         PacketPlayOutEntityMetadata metaPacket = new PacketPlayOutEntityMetadata();
  178.  
  179.         Field a = getField(metaPacket.getClass(), "a");
  180.         a.setAccessible(true);
  181.         try {
  182.             a.set(metaPacket, ENTITY_ID);
  183.         } catch (IllegalArgumentException | IllegalAccessException e1) {
  184.             e1.printStackTrace();
  185.         }
  186.  
  187.         try {
  188.             Field b = PacketPlayOutEntityMetadata.class.getDeclaredField("b");
  189.             b.setAccessible(true);
  190.             b.set(metaPacket, watcher.c());
  191.         } catch (Exception e) {
  192.             e.printStackTrace();
  193.         }
  194.  
  195.         return metaPacket;
  196.     }
  197.  
  198.     public static PacketPlayInClientCommand getRespawnPacket() {
  199.         PacketPlayInClientCommand packet = new PacketPlayInClientCommand();
  200.  
  201.         Field a = getField(packet.getClass(), "a");
  202.         a.setAccessible(true);
  203.         try {
  204.             a.set(packet, 1);
  205.         } catch (IllegalArgumentException | IllegalAccessException e) {
  206.             e.printStackTrace();
  207.         }
  208.  
  209.         return packet;
  210.     }
  211.  
  212.     public static DataWatcher getWatcher(String text, int health, int protocolVersion) {
  213.         DataWatcher watcher = new DataWatcher(null);
  214.         boolean is18 = (protocolVersion == 47);
  215.       //  watcher.a(0, (byte) 0x20); //Flags, 0x20 = invisible
  216.         if (!is18)
  217.             health -= 100;
  218.         watcher.a(6, (float) health);
  219.         watcher.a((is18) ? 2 : 10, text); //Entity name
  220.         watcher.a((is18) ? 3 : 11, (byte) 1); //Show name, 1 = show, 0 = don't show
  221.         if (is18) {
  222.             watcher.a(20, 0);
  223.         }
  224.         //watcher.a(16, health); //Wither health, 300 = full health
  225.  
  226.         return watcher;
  227.     }
  228.  
  229.     //Other methods
  230.  
  231.     /**
  232.      * use the bar as a simple text display.
  233.      *
  234.      * @param text
  235.      * @param player
  236.      */
  237.     public static void displayTextBar(String text, final Player player) {
  238.         if (plugin == null) return;
  239.         createBar(player, text);
  240.  
  241.         /*new BukkitRunnable() {
  242.             @Override
  243.             public void run() {
  244.                 destroyBar(player);
  245.             }
  246.         }.runTaskLater(plugin, 120L);*/
  247.     }
  248.  
  249.     /**
  250.      * after we're finished with the bar (either from our countdown
  251.      * or the percentage handler reached 100 (or was stopped) )
  252.      * call this to remove the bar from the players HUD
  253.      *
  254.      * @param player
  255.      */
  256.     public static void destroyBar(Player player) {
  257.         PacketPlayOutEntityDestroy destroyEntityPacket = getDestroyEntityPacket();
  258.  
  259.         sendPacket(player, destroyEntityPacket);
  260.         hasHealthBar.put(player.getName(), false);
  261.     }
  262.  
  263.     /**
  264.      * creates a bar with the text given. Use this before
  265.      * starting to send data about the bar (like a percentage)
  266.      *
  267.      * @param player
  268.      * @param text
  269.      */
  270.     public static void createBar(Player player, String text) {
  271.         PacketPlayOutSpawnEntityLiving mobPacket = getMobPacket(text, player.getLocation(), player);
  272.  
  273.         sendPacket(player, mobPacket);
  274.         hasHealthBar.put(player.getName(), true);
  275.         displayText.put(player.getName(), text);
  276.  
  277.     }
  278.  
  279.     static ChatColor percentageColor = ChatColor.GREEN;
  280.  
  281.     /**
  282.      * A dev-controlled percentage bar. The only automation is the
  283.      * update procedure. The dev must use setPercentage() to keep the
  284.      * display updated and, once the percentage is 100, the bar will destroy
  285.      * itself and briefly display the endText
  286.      * This should be used in the case of the bar being a "percentage complete"
  287.      * bar or the like
  288.      *
  289.      * @param text                text to display above the bar
  290.      * @param player
  291.      * @param percentage
  292.      * @param appendPercentage    if true the percentage will be appended to the end of the text
  293.      * @param appendEndPercentage if true the percentage will be appended when the bar is finished, whether it
  294.      *                            finished through automation or through being cancelled
  295.      * @author backspace119
  296.      */
  297.     public static void displayPercentageBar(String text, String endText, final Player player, int percentage, boolean appendPercentage, boolean appendEndPercentage) {
  298.         if (plugin == null) return;
  299.         stopPercentage.put(player, false);
  300.         setPercentage(player, percentage);
  301.         final String dText = appendPercentage ? appendPercentage(text, percentage) : text;
  302.         createBar(player, text);
  303.         final String dEndText = appendEndPercentage ? appendPercentage(endText, percentage) : endText;
  304.  
  305.         new BukkitRunnable() {
  306.  
  307.             @Override
  308.             public void run() {
  309.                 if (getPercentage(player) >= 100 || getStop(player)) {
  310.                     destroyBar(player);
  311.  
  312.                     createBar(player, dEndText);
  313.  
  314.                     new BukkitRunnable() {
  315.                         @Override
  316.                         public void run() {
  317.                             destroyBar(player);
  318.                         }
  319.                     }.runTaskLater(plugin, 60L);
  320.                     this.cancel();
  321.                 }
  322.                 setBarHealth(player, getPercentage(player) * 3, dText);
  323.             }
  324.         }.runTaskTimer(plugin, 10L, 10L);
  325.     }
  326.  
  327.     public static void stopPercentage(Player player) {
  328.         stopPercentage.put(player, true);
  329.     }
  330.  
  331.     public static boolean getStop(Player player) {
  332.         return stopPercentage.get(player);
  333.     }
  334.  
  335.     public static void setPercentage(Player player, int percentage) {
  336.         percentageMap.put(player, percentage);
  337.     }
  338.  
  339.     public static int getPercentage(Player player) {
  340.         return percentageMap.get(player);
  341.     }
  342.  
  343.     /**
  344.      * set the color of the percentage appended to the bar if appendPercentage is true in displayPercentageBar()
  345.      * default color is Green.
  346.      *
  347.      * @param pColor
  348.      */
  349.     public static void setPercentageColor(ChatColor pColor) {
  350.         percentageColor = pColor;
  351.     }
  352.  
  353.     private static String appendPercentage(String message, int percentage) {
  354.         return message + percentageColor + " - " + percentage + "%";
  355.     }
  356.  
  357.     /**
  358.      * This sets the bars "health fill" amount. 300 is full, 0 is empty.
  359.      *
  360.      * @param player
  361.      * @param health
  362.      * @param text   text that is on the bar to set health on
  363.      */
  364.     public static void setBarHealth(Player player, int health, String text) {
  365.         DataWatcher watcher = getWatcher(text, health, ((CraftPlayer) player).getHandle().playerConnection.networkManager.getVersion());
  366.         PacketPlayOutEntityMetadata metaPacket = getMetadataPacket(watcher);
  367.  
  368.         sendPacket(player, metaPacket);
  369.     }
  370.  
  371.     /**
  372.      * used internally, the extra parameter healthAdd determines the amount of health
  373.      * to add or subtract each second and the delay is for the update speed in ticks.
  374.      *
  375.      * @param text
  376.      * @param completeText
  377.      * @param player
  378.      * @param healthAdd
  379.      * @param delay
  380.      * @param loadUp
  381.      */
  382.     public static void displayLoadingBar(final String text, final String completeText, final Player player, final int healthAdd, final long delay, final boolean loadUp) {
  383.         if (plugin == null) return;
  384.  
  385.         createBar(player, text);
  386.  
  387.         new BukkitRunnable() {
  388.             int health = (loadUp ? 0 : 300);
  389.  
  390.             @Override
  391.             public void run() {
  392.                 //fires if time is left on the "loading bar" loop
  393.                 if ((loadUp ? health < 300 : health > 0)) {
  394.                     setBarHealth(player, health, text);
  395.                     if (loadUp) {
  396.                         health += healthAdd;
  397.                     } else {
  398.                         health -= healthAdd;
  399.                     }
  400.                     //fires once the bar is done loading, cleans up the bar and
  401.                     //puts the "complete text" on the bar for the player to see
  402.                 } else {
  403.                     //this code will remain uncleaned until I find out
  404.                     //whether the order of these lines is important-backspace119
  405.                     DataWatcher watcher = getWatcher(text, (loadUp ? 300 : 0), ((CraftPlayer) player).getHandle().playerConnection.networkManager.getVersion());
  406.                     PacketPlayOutEntityMetadata metaPacket = getMetadataPacket(watcher);
  407.                     PacketPlayOutEntityDestroy destroyEntityPacket = getDestroyEntityPacket();
  408.  
  409.                     sendPacket(player, metaPacket);
  410.                     sendPacket(player, destroyEntityPacket);
  411.                     hasHealthBar.put(player.getName(), false);
  412.  
  413.                     //Complete text
  414.                     createBar(player, completeText);
  415.  
  416.                     setBarHealth(player, 300, completeText);
  417.  
  418.                     new BukkitRunnable() {
  419.                         @Override
  420.                         public void run() {
  421.                             destroyBar(player);
  422.                         }
  423.                     }.runTaskLater(plugin, 40L);
  424.  
  425.                     this.cancel();
  426.                 }
  427.             }
  428.         }.runTaskTimer(plugin, delay, delay);
  429.     }
  430.  
  431.     /**
  432.      * Use this to display a loading bar to the player with the given delay.
  433.      *
  434.      * @param text         text to show before load is complete
  435.      * @param completeText text to show after load is complete
  436.      * @param player
  437.      * @param secondsDelay
  438.      * @param loadUp       load towards left ("up") if true, right if false
  439.      */
  440.     public static void displayLoadingBar(final String text, final String completeText, final Player player, final int secondsDelay, final boolean loadUp) {
  441.  
  442.         final int healthChangePerSecond = 300 / secondsDelay;
  443.  
  444.         displayLoadingBar(text, completeText, player, healthChangePerSecond, 20L, loadUp);
  445.     }
  446.  
  447.     public static int getCardinalDirection(Player player) {
  448.         Location location = player.getLocation();
  449.         float rot = location.getYaw();
  450.         if (0 <= rot && rot < 22.5) {
  451.             return 1; // North
  452.         } else if (22.5 <= rot && rot < 67.5) {
  453.             return 2; // Northeast
  454.         } else if (67.5 <= rot && rot < 112.5) {
  455.             return 3; // East
  456.         } else if (112.5 <= rot && rot < 157.5) {
  457.             return 4; // Southeast
  458.         } else if (157.5 <= rot && rot < 202.5) {
  459.             return 5; // South
  460.         } else if (202.5 <= rot && rot < 247.5) {
  461.             return 6; // Southwest
  462.         } else if (247.5 <= rot && rot < 292.5) {
  463.             return 7; // West
  464.         } else if (292.5 <= rot && rot < 337.5) {
  465.             return 8; // Northwest
  466.         } else if (337.5 <= rot && rot < 360.0) {
  467.             return 1; // North
  468.         } else {
  469.             return -1;
  470.         }
  471.     }
  472.  
  473.     @EventHandler
  474.     public void onMove(PlayerMoveEvent event){
  475.         if(event.isCancelled())
  476.             return;
  477.         if(((CraftPlayer) event.getPlayer()).getHandle().playerConnection.networkManager.getVersion() != 47)
  478.             return;
  479.         int currentDirection = getCardinalDirection(event.getPlayer());
  480.         if(direction.get(event.getPlayer().getName()) == null){
  481.             direction.put(event.getPlayer().getName(),currentDirection);
  482.         }
  483.         if(direction.get(event.getPlayer().getName()) != currentDirection){
  484.             direction.put(event.getPlayer().getName(), currentDirection);
  485.         }
  486.         if(hasHealthBar.get(event.getPlayer().getName()) != null){
  487.             if(!hasHealthBar.get(event.getPlayer().getName()))
  488.                 hasHealthBar.remove(event.getPlayer().getName());
  489.             else {
  490.                 destroyBar(event.getPlayer());
  491.                 displayTextBar(displayText.get(event.getPlayer().getName()), event.getPlayer());
  492.             }
  493.         }
  494.     }
  495. }
Advertisement
Add Comment
Please, Sign In to add comment