NegativeArraySizeException when trying to create holograms

Discussion in 'Plugin Development' started by jojodmo, Apr 23, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    jojodmo

    I'm trying to make animated holograms, and, after about 1 minute of the server being on, anyone who connects or tries to connect gets kicked for:

    Internal Exception: io.netty.handler.codec.DecoderException: java.lang.NegativeArraySizeException

    Here's my hologram code:

    Code:java
    1. package net.podzol.jojodmo.podzol.api;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.Arrays;
    5. import java.util.List;
    6.  
    7. import net.minecraft.server.v1_7_R3.EntityHorse;
    8. import net.minecraft.server.v1_7_R3.EntityPlayer;
    9. import net.minecraft.server.v1_7_R3.EntityWitherSkull;
    10. import net.minecraft.server.v1_7_R3.PacketPlayOutAttachEntity;
    11. import net.minecraft.server.v1_7_R3.PacketPlayOutEntityDestroy;
    12. import net.minecraft.server.v1_7_R3.PacketPlayOutSpawnEntityLiving;
    13. import net.minecraft.server.v1_7_R3.WorldServer;
    14.  
    15. import org.bukkit.craftbukkit.v1_7_R3.entity.CraftPlayer;
    16. import org.bukkit.craftbukkit.v1_7_R3.CraftWorld;
    17. import org.bukkit.Bukkit;
    18. import org.bukkit.Location;
    19. import org.bukkit.entity.Player;
    20.  
    21. public class Hologram {
    22. private static final double distance = 0.23;
    23. private List<String> lines = new ArrayList<String>();
    24. private List<Integer> ids = new ArrayList<Integer>();
    25. private List<EntityHorse> horses = new ArrayList<EntityHorse>();
    26. private boolean showing = false;
    27. private Location location;
    28.  
    29. public Hologram(String... lines) {
    30. this.lines.addAll(Arrays.asList(lines));
    31. }
    32.  
    33. public void setLines(String... lines){
    34. this.lines.addAll(Arrays.asList(lines));
    35. }
    36.  
    37. public List<String> getLines(){
    38. return this.lines;
    39. }
    40.  
    41. public Location getLocation(){
    42. return this.location;
    43. }
    44.  
    45. public void setLocation(Location l){
    46. this.location = l;
    47. }
    48.  
    49. public void update(){
    50. destroy();
    51. show(this.location);
    52. }
    53.  
    54. public boolean show(Player p, Location loc) {
    55. if (showing){
    56. return false;
    57. }
    58.  
    59. Location first = loc.clone().add(0, (this.lines.size() / 2) * distance, 0);
    60. for(int i = 0; i < this.lines.size(); i++){
    61. ids.addAll(showLine(p, first.clone(), this.lines.get(i)));
    62. first.subtract(0, distance, 0);
    63. }
    64.  
    65. showing = true;
    66. this.location = loc;
    67. return true;
    68. }
    69.  
    70. public boolean show(Location loc) {
    71. try{
    72. if(showing){
    73. return false;
    74. }
    75.  
    76. Location first = loc.clone().add(0, (this.lines.size() / 2) * distance, 0);
    77. for(int i = 0; i < this.lines.size(); i++){
    78. ids.addAll(showLine(first.clone(), this.lines.get(i)));
    79. first.subtract(0, distance, 0);
    80. }
    81.  
    82. showing = true;
    83. this.location = loc;
    84. }
    85. catch(Exception e){
    86. e.printStackTrace();
    87. }
    88. return true;
    89. }
    90.  
    91. public boolean destroy(){
    92. try{
    93. if(!showing){
    94. return false;
    95. }
    96.  
    97. for(EntityHorse h : this.horses){
    98. h.setCustomNameVisible(false);
    99. h.setHealth(0f);
    100. }
    101.  
    102. int[] ints = new int[(this.ids.size() < 0 ? 0 : this.ids.size())];
    103. for(int j = 0; j < ints.length; j++){
    104. ints[j] = ids.get(j);
    105. }
    106.  
    107. PacketPlayOutEntityDestroy packet = new PacketPlayOutEntityDestroy(ints);
    108. for(Player player : Bukkit.getOnlinePlayers()){
    109. ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
    110. }
    111.  
    112. showing = false;
    113. this.location = null;
    114. }catch(Exception e){
    115. e.printStackTrace();
    116. }
    117. return true;
    118. }
    119.  
    120. private List<Integer> showLine(Player p, Location loc, String text) {
    121. WorldServer world = ((CraftWorld) loc.getWorld()).getHandle();
    122. EntityWitherSkull skull = new EntityWitherSkull(world);
    123. skull.setLocation(loc.getX(), loc.getY() + 1 + 55, loc.getZ(), 0, 0);
    124. ((CraftWorld) loc.getWorld()).getHandle().addEntity(skull);
    125.  
    126. EntityHorse horse = new EntityHorse(world);
    127. horse.setLocation(loc.getX(), loc.getY() + 55, loc.getZ(), 0, 0);
    128. horse.setAge(-1700000);
    129. horse.setCustomName(text);
    130. horse.setCustomNameVisible(true);
    131. this.horses.add(horse);
    132. PacketPlayOutSpawnEntityLiving packedt = new PacketPlayOutSpawnEntityLiving(horse);
    133.  
    134. EntityPlayer nmsPlayer = ((CraftPlayer) p).getHandle();
    135. nmsPlayer.playerConnection.sendPacket(packedt);
    136.  
    137. PacketPlayOutAttachEntity pa = new PacketPlayOutAttachEntity(0, horse, skull);
    138. nmsPlayer.playerConnection.sendPacket(pa);
    139.  
    140. return Arrays.asList(skull.getId(), horse.getId());
    141. }
    142.  
    143. private static List<Integer> showLine(Location loc, String text) {
    144. WorldServer world = ((CraftWorld) loc.getWorld()).getHandle();
    145. EntityWitherSkull skull = new EntityWitherSkull(world);
    146. skull.setLocation(loc.getX(), loc.getY() + 1 + 55, loc.getZ(), 0, 0);
    147. ((CraftWorld) loc.getWorld()).getHandle().addEntity(skull);
    148.  
    149. EntityHorse horse = new EntityHorse(world);
    150. horse.setLocation(loc.getX(), loc.getY() + 55, loc.getZ(), 0, 0);
    151. horse.setAge(-1700000);
    152. horse.setCustomName(text);
    153. horse.setCustomNameVisible(true);
    154. PacketPlayOutSpawnEntityLiving packedt = new PacketPlayOutSpawnEntityLiving(horse);
    155.  
    156. for(Player p : Bukkit.getOnlinePlayers()){
    157. EntityPlayer nmsPlayer = ((CraftPlayer) p).getHandle();
    158. nmsPlayer.playerConnection.sendPacket(packedt);
    159.  
    160. PacketPlayOutAttachEntity pa = new PacketPlayOutAttachEntity(0, horse, skull);
    161. nmsPlayer.playerConnection.sendPacket(pa);
    162. }
    163. return Arrays.asList(skull.getId(), horse.getId());
    164. }
    165. }


    And here's my onEnable():

    Code:java
    1. Hologram holo = new Hologram(ChatColor.GREEN + "Test", ChatColor.GOLD + "" + ChatColor.BOLD + "Ello there mate!");
    2. holo.show(new Location(Bukkit.getWorld("world"), 0, 70, 0));
    3.  
    4. this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    5. public void run(){
    6. holo.destroy();
    7. holo.show(new Location(Bukkit.getWorld("world"), 0, 70, 0));
    8. }
    9. },60,60);


    I know it's because of this code because, first off, before I added this nothing like this was happening, and second off, you can connect, and then 3 seconds later (60 ticks / 20 tps = 3 seconds) you get kicked from the server for:

    Internal Exception: io.netty.handler.codec.DecoderException: java.lang.NegativeArraySizeException

    I get no errors in console, nor in eclipse. Any ideas?
     
  2. Offline

    Slikey

    jojodmo Have you tried restarting the server without your plugin? "netty" is a tool to handle connections in a network. Please include the complete stacktrace if there is on in the console.
     
  3. Offline

    jojodmo

    Read the entire post...
     
  4. Offline

    raGan.

    Have you tried debugging it? Print everything you can, especially what all outgoing packets look like. Try adding a delay between them, might help identifying which one is actually causing it. Because if client crashes, you are most probably sending something it can't digest.
     
  5. Offline

    RawCode

    add debugging to stated netty method, this will allow to track issue.

    current reason is "malformed network packet"
     
  6. Offline

    jojodmo

    Yes but I haven't printed very much... I'll print all the packets and see what happens
     
Thread Status:
Not open for further replies.

Share This Page