EntityArmorStand Hologramme ohne flackern updaten

Wenn ihr bei eurem Plugin nicht weiter kommt, könnt ihr hier euer Problem beschreiben und um Hilfe bitten.

EntityArmorStand Hologramme ohne flackern updaten

Beitragvon DevToLearn » Di 12. Jul 2016, 09:08

Hallo. Ich momentan das Problem, dass bei mir die Hologramme aus EntityArmorStands mit einem hesslichen flackern updaten. Ich forsche schon sehr lange daran, finde aber leider keine Lösung. Ich weis das es möglich ist.

Vielen Dank für eure Hilfe.
Benutzeravatar
DevToLearn
 
Beiträge: 15
Registriert: So 13. Mär 2016, 10:20

Re: EntityArmorStand Hologramme ohne flackern updaten

Beitragvon Letsplaybar » Mi 13. Jul 2016, 07:06

Wie meinst du das Flakern wenn es verändert wird also erst zerstört und dann neugeladen wird? wenn ja hast du hier einen sichtbaren delay drin. ich habe beimeinen Hologrammsystem nicht das Problem beim Verändern also neuladen das es Flackert.
Code: Alles auswählen
  1. package me.Letsplaybar.Lobbysystem.Features;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.bukkit.Bukkit;
  5. import org.bukkit.Location;
  6. import java.lang.reflect.Constructor;
  7. import java.lang.reflect.Field;
  8. import java.lang.reflect.InvocationTargetException;
  9. import java.lang.reflect.Method;
  10. import java.util.Arrays;
  11. import java.util.UUID;
  12. import org.bukkit.World;
  13. import org.bukkit.entity.Player;
  14. public class Hologramms {
  15.  
  16.     private List<Object> destroyCache;
  17.     private List<Object> spawnCache;
  18.     private List<UUID> players;
  19.     private List<String> lines;
  20.     private Location loc;
  21.  
  22.     private static final double ABS = 0.23D;
  23.     private static String path;
  24.     private static String version;
  25.  
  26.     /*
  27.      * Cache for getPacket()-Method
  28.      */
  29.     private static Class<?> armorStand;
  30.     private static Class<?> worldClass;
  31.     private static Class<?> nmsEntity;
  32.     private static Class<?> craftWorld;
  33.     private static Class<?> packetClass;
  34.     private static Class<?> entityLivingClass;
  35.     private static Constructor<?> armorStandConstructor;
  36.    
  37.     /*
  38.      * Cache for getDestroyPacket()-Method
  39.      */
  40.     private static Class<?> destroyPacketClass;
  41.     private static Constructor<?> destroyPacketConstructor;
  42.  
  43.     /*
  44.      * Cache for sendPacket()-Method
  45.      */
  46.     private static Class<?> nmsPacket;
  47.  
  48.  
  49.     static {
  50.         path = Bukkit.getServer().getClass().getPackage().getName();
  51.         version = path.substring(path.lastIndexOf(".")+1, path.length());
  52.      
  53.         try {
  54.             armorStand = Class.forName("net.minecraft.server." + version + ".EntityArmorStand");
  55.             worldClass = Class.forName("net.minecraft.server." + version + ".World");
  56.             nmsEntity = Class.forName("net.minecraft.server." + version + ".Entity");
  57.             craftWorld = Class.forName("org.bukkit.craftbukkit." + version + ".CraftWorld");
  58.             packetClass = Class.forName("net.minecraft.server." + version + ".PacketPlayOutSpawnEntityLiving");
  59.             entityLivingClass = Class.forName("net.minecraft.server." + version + ".EntityLiving");
  60.             armorStandConstructor = armorStand.getConstructor(new Class[] { worldClass });
  61.          
  62.             destroyPacketClass = Class.forName("net.minecraft.server." + version + ".PacketPlayOutEntityDestroy");
  63.             destroyPacketConstructor = destroyPacketClass.getConstructor(int[].class);
  64.            
  65.             nmsPacket = Class.forName("net.minecraft.server." + version + ".Packet");
  66.         } catch (ClassNotFoundException | NoSuchMethodException | SecurityException ex) {
  67.             System.err.println("Error - Classes not initialized!");
  68.             ex.printStackTrace();
  69.         }
  70.     }
  71.     /**
  72.      * Create a new hologram
  73.      * Note: The constructor will automatically initialize the internal cache; it may take some millis
  74.      * @param loc The location where this hologram is shown
  75.      * @param lines The text-lines, from top to bottom, farbcodes are possible
  76.      */
  77.     public Holograms(Location loc, String... lines) {
  78.         this(loc, Arrays.asList(lines));
  79.     }
  80.    
  81.     /**
  82.      * Create a new hologram
  83.      * Note: The constructor will automatically initialize the internal cache; it may take some millis
  84.      * @param loc The location where this hologram is shown
  85.      * @param lines The text-lines, from top to bottom, farbcodes are possible
  86.      */
  87.     public Holograms(Location loc, List<String> lines) {
  88.         this.lines = lines;
  89.         this.loc = loc;
  90.         this.players = new ArrayList<>();
  91.         this.spawnCache = new ArrayList<>();
  92.         this.destroyCache = new ArrayList<>();
  93.        
  94.         // Init
  95.         Location displayLoc = loc.clone().add(0, (ABS * lines.size()) - 1.97D, 0);
  96.         for (int i = 0; i < lines.size(); i++) {
  97.             Object packet = this.getPacket(this.loc.getWorld(), displayLoc.getX(), displayLoc.getY(), displayLoc.getZ(), this.lines.get(i));
  98.             this.spawnCache.add(packet);
  99.             try {
  100.                 Field field = packetClass.getDeclaredField("a");
  101.                 field.setAccessible(true);
  102.                 this.destroyCache.add(this.getDestroyPacket(new int[] { (int) field.get(packet) }));
  103.             } catch (Exception ex) {
  104.                 ex.printStackTrace();
  105.             }
  106.             displayLoc.add(0, ABS * (-1), 0);
  107.         }
  108.     }
  109.  
  110.     /**
  111.      * Shows this hologram to the given player
  112.      * @param p The player who will see this hologram at the location specified by calling the constructor
  113.      * @return true, if the action was successful, else false
  114.      */
  115.     public boolean display(Player p) {
  116.         for (int i = 0; i < spawnCache.size(); i++) {
  117.             this.sendPacket(p, spawnCache.get(i));
  118.         }
  119.      
  120.         this.players.add(p.getUniqueId());
  121.         return true;
  122.     }
  123.    
  124.     /**
  125.      * Removes this hologram from the players view
  126.      * @param p The target player
  127.      * @return true, if the action was successful, else false (including the try to remove a non-existing hologram)
  128.      */
  129.     public boolean destroy(Player p) {
  130.         if (this.players.contains(p.getUniqueId())) {
  131.             for (int i = 0; i < this.destroyCache.size(); i++) {
  132.                 this.sendPacket(p, this.destroyCache.get(i));
  133.             }
  134.             this.players.remove(p.getUniqueId());
  135.             return true;
  136.         }
  137.         return false;
  138.     }
  139.  
  140.     private Object getPacket(World w, double x, double y, double z, String text) {
  141.         try {
  142.             Object craftWorldObj = craftWorld.cast(w);
  143.             Method getHandleMethod = craftWorldObj.getClass().getMethod("getHandle", new Class<?>[0]);
  144.             Object entityObject = armorStandConstructor.newInstance(new Object[] { getHandleMethod.invoke(craftWorldObj, new Object[0]) });
  145.             Method setCustomName = entityObject.getClass().getMethod("setCustomName", new Class<?>[] { String.class });
  146.             setCustomName.invoke(entityObject, new Object[] { text });
  147.             Method setCustomNameVisible = nmsEntity.getMethod("setCustomNameVisible", new Class[] { boolean.class });
  148.             setCustomNameVisible.invoke(entityObject, new Object[] { true });
  149.             Method setGravity = entityObject.getClass().getMethod("setGravity", new Class<?>[] { boolean.class });
  150.             setGravity.invoke(entityObject, new Object[] { false });
  151.             Method setLocation = entityObject.getClass().getMethod("setLocation", new Class<?>[] { double.class, double.class, double.class, float.class, float.class });
  152.             setLocation.invoke(entityObject, new Object[] { x, y, z, 0.0F, 0.0F });
  153.             Method setInvisible = entityObject.getClass().getMethod("setInvisible", new Class<?>[] { boolean.class });
  154.             setInvisible.invoke(entityObject, new Object[] { true });
  155.             Constructor<?> cw = packetClass.getConstructor(new Class<?>[] { entityLivingClass });
  156.             Object packetObject = cw.newInstance(new Object[] { entityObject });
  157.             return packetObject;
  158.         } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
  159.             e.printStackTrace();
  160.         }
  161.         return null;
  162.     }
  163.    
  164.     private Object getDestroyPacket(int... id) {
  165.         try {
  166.             return destroyPacketConstructor.newInstance(id);
  167.         } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
  168.             e.printStackTrace();
  169.         }
  170.         return null;
  171.     }
  172.  
  173.     private void sendPacket(Player p, Object packet) {
  174.         try {
  175.            Method getHandle = p.getClass().getMethod("getHandle");
  176.            Object entityPlayer = getHandle.invoke(p);
  177.            Object pConnection = entityPlayer.getClass().getField("playerConnection").get(entityPlayer);
  178.            Method sendMethod = pConnection.getClass().getMethod("sendPacket", new Class[] { nmsPacket });
  179.            sendMethod.invoke(pConnection, new Object[] { packet });
  180.         } catch (Exception e) {
  181.            e.printStackTrace();
  182.         }
  183.      }
  184. }

das ist meine Methode die ich verwende wo ich noch nie ein Problem mit flackern hatte.
Bild
Helfe Wo ich kann.
Mein MinecraftServer: Letsplaybaro.de - Suchen Developer/Bilder

I hacked 127.0.0.1!!! :)
Benutzeravatar
Letsplaybar
 
Beiträge: 195
Registriert: Mo 1. Feb 2016, 17:21
Wohnort: Berlin


Zurück zu Hilfe und Support

Wer ist online?

Mitglieder in diesem Forum: 0 Mitglieder und 26 Gäste