SHARE
TWEET

Untitled

a guest May 21st, 2016 35 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package de.klein5.TowerDefense.utils;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.util.List;
  5. import java.util.Random;
  6. import java.util.UUID;
  7.  
  8. import org.bukkit.Bukkit;
  9. import org.bukkit.Location;
  10. import org.bukkit.craftbukkit.v1_9_R2.entity.CraftEntity;
  11. import org.bukkit.craftbukkit.v1_9_R2.entity.CraftPlayer;
  12. import org.bukkit.entity.Entity;
  13. import org.bukkit.entity.LivingEntity;
  14. import org.bukkit.entity.Player;
  15.  
  16. import de.klein5.TowerDefense.datawatcher.EnumDataWatcherObjects;
  17. import de.klein5.TowerDefense.datawatcher.NullDataWatcher;
  18. import net.minecraft.server.v1_9_R2.DataWatcher;
  19. import net.minecraft.server.v1_9_R2.Packet;
  20. import net.minecraft.server.v1_9_R2.PacketPlayOutEntityDestroy;
  21. import net.minecraft.server.v1_9_R2.PacketPlayOutEntityMetadata;
  22. import net.minecraft.server.v1_9_R2.PacketPlayOutSpawnEntityLiving;
  23.  
  24. public class Laser {
  25.  
  26.     protected int entityId;
  27.     protected NullDataWatcher dataWatcher;
  28.  
  29.     private Location location;
  30.  
  31.     private byte changeMask(byte bitMask, int bit, boolean state) {
  32.         if (state) {
  33.             return bitMask |= 1 << bit;
  34.         } else {
  35.             return bitMask &= ~(1 << bit);
  36.         }
  37.     }
  38.    
  39.     public Laser(Location location) {
  40.         entityId = new Random().nextInt(Integer.MAX_VALUE);
  41.         NullDataWatcher w = new NullDataWatcher();
  42.         byte status = 0;
  43.         status = changeMask(status, 0, false); // FIRE
  44.         status = changeMask(status, 1, false); // SNEAK
  45.         status = changeMask(status, 2, false); // NOT_SET
  46.         status = changeMask(status, 3, false); // SPRINT
  47.         status = changeMask(status, 4, false); // USE_ITEM
  48.         status = changeMask(status, 5, false); // INVISABLE
  49.         w.add(EnumDataWatcherObjects.LIVING_HEALTH_06, (float) 20F);
  50.         w.add(EnumDataWatcherObjects.LIVING_POITION_COLOR_07, 0);
  51.         w.add(EnumDataWatcherObjects.ENTITY_BITMASK_00, status);
  52.         w.add(EnumDataWatcherObjects.HUMAN_SKIN_12, (byte) 127);
  53.         this.location = location;
  54.     }
  55.  
  56.     private void sendPacket(Packet packet) {
  57.         for (Player player : Bukkit.getOnlinePlayers()) {
  58.             ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
  59.         }
  60.     }
  61.  
  62.     private void sendPacket(Player player, Packet packet) {
  63.         ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
  64.     }
  65.  
  66.     private void spawn(Player show) {
  67.         try {
  68.             PacketPlayOutSpawnEntityLiving packet = new PacketPlayOutSpawnEntityLiving();
  69.             set(packet, "a", entityId);
  70.             set(packet, "b", UUID.randomUUID());
  71.             set(packet, "c", org.bukkit.entity.EntityType.GUARDIAN.getTypeId());
  72.             set(packet, "d", toFixedPointNumber(location.getX()));
  73.             set(packet, "e", toFixedPointNumber(location.getY()));
  74.             set(packet, "f", toFixedPointNumber(location.getZ()));
  75.             set(packet, "g", (int) toPackedByte(location.getYaw()));
  76.             set(packet, "h", (int) toPackedByte(location.getPitch()));
  77.             set(packet, "i", (int) toPackedByte(location.getPitch()));
  78.             set(packet, "j", (byte) 0);
  79.             set(packet, "k", (byte) 0);
  80.             set(packet, "l", (byte) 0);
  81.             set(packet, "m", dataWatcher.toNMS());
  82.  
  83.             if (show == null) {
  84.                 sendPacket(packet);
  85.             } else {
  86.                 sendPacket(show, packet);
  87.             }
  88.         } catch (Exception e) {
  89.             e.printStackTrace();
  90.         }
  91.     }
  92.  
  93.     public void setTarget(Entity en) {
  94.         try {
  95.             spawn(null);
  96.             PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata();
  97.             set(packet, "a", entityId);
  98.             set(packet, "b", dataWatcher.toNMS().c());
  99.             sendPacket(packet);
  100.            
  101.         } catch (Exception e) {
  102.             e.printStackTrace();
  103.         }
  104.     }
  105.  
  106.     public void despawn(Player show) {
  107.         PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(new int[] { entityId });
  108.         if (show == null) {
  109.             sendPacket(packet);
  110.         } else {
  111.             sendPacket(show, packet);
  112.         }
  113.     }
  114.  
  115.     protected int calcData(int data, int id, boolean flag) {
  116.         if (flag) {
  117.             return Integer.valueOf(data | 1 << id);
  118.         } else {
  119.             return Integer.valueOf(data & ~(1 << id));
  120.         }
  121.     }
  122.  
  123.     protected int calcType(int type, int id, boolean flag) {
  124.         if (flag) {
  125.             return Integer.valueOf(type | id);
  126.         } else {
  127.             return Integer.valueOf(type & ~id);
  128.         }
  129.     }
  130.  
  131.     protected byte toPackedByte(float f) {
  132.         return (byte) ((int) (f * 256.0F / 360.0F));
  133.     }
  134.  
  135.     protected int toFixedPointNumber(Double d) {
  136.         return (int) (d * 32D);
  137.     }
  138.  
  139.     protected void set(Object instance, String name, Object value) throws Exception {
  140.         Field field = instance.getClass().getDeclaredField(name);
  141.         field.setAccessible(true);
  142.         field.set(instance, value);
  143.     }
  144. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top