Need Serious Help, at Dead End

Discussion in 'Plugin Development' started by 3ptO, Oct 5, 2013.

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

    3ptO

    Hello all,

    This plugin is used to spawn an aesthetic LeashHitch (no leashed entity), the LeashHitch in most cases will despawn on chunk reload (unload then load). When placing a "Tiki Torch" the initial LeashHitch UUID & Location are stored to a HashMap. On the event ChunkLoadEvent the map is iterated through to spawn the LeashHitch, remove the existing data and add the new data.

    Code:
    [SEVERE] Could not pass event ChunkLoadEvent to Tiki_Torches v1.0
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427)
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62)
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:477)
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:462)
        at net.minecraft.server.v1_6_R3.ChunkProviderServer.getChunkAt(ChunkProviderServer.java:138)
        at net.minecraft.server.v1_6_R3.ChunkProviderServer.getChunkAt(ChunkProviderServer.java:84)
        at net.minecraft.server.v1_6_R3.PlayerList.a(PlayerList.java:173)
        at net.minecraft.server.v1_6_R3.PlayerList.c(PlayerList.java:222)
        at net.minecraft.server.v1_6_R3.PlayerList.a(PlayerList.java:103)
        at net.minecraft.server.v1_6_R3.PendingConnection.e(PendingConnection.java:132)
        at net.minecraft.server.v1_6_R3.PendingConnection.d(PendingConnection.java:43)
        at net.minecraft.server.v1_6_R3.DedicatedServerConnectionThread.a(DedicatedServerConnectionThread.java:41)
        at net.minecraft.server.v1_6_R3.DedicatedServerConnection.b(SourceFile:29)
        at net.minecraft.server.v1_6_R3.MinecraftServer.t(MinecraftServer.java:592)
        at net.minecraft.server.v1_6_R3.DedicatedServer.t(DedicatedServer.java:227)
        at net.minecraft.server.v1_6_R3.MinecraftServer.s(MinecraftServer.java:488)
        at net.minecraft.server.v1_6_R3.MinecraftServer.run(MinecraftServer.java:421)
        at net.minecraft.server.v1_6_R3.ThreadServerApplication.run(SourceFile:583)
    Caused by: java.lang.IllegalArgumentException: Cannot spawn an entity for org.bukkit.entity.LeashHitch
        at org.bukkit.craftbukkit.v1_6_R3.CraftWorld.spawn(CraftWorld.java:1026)
        at org.bukkit.craftbukkit.v1_6_R3.CraftWorld.spawn(CraftWorld.java:791)
        at me.version3ptO.Tiki_Torches.Main.onChunkLoad(Main.java:218)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425)
        ... 17 more
    Code:
    Code:java
    1. package me.version3ptO.Tiki_Torches;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import java.util.HashSet;
    6. import java.util.Iterator;
    7. import java.util.List;
    8. import java.util.Map;
    9. import java.util.Set;
    10. import java.util.UUID;
    11. import java.util.logging.Logger;
    12.  
    13. import org.bukkit.Bukkit;
    14. import org.bukkit.ChatColor;
    15. import org.bukkit.Chunk;
    16. import org.bukkit.Location;
    17. import org.bukkit.Material;
    18. import org.bukkit.World;
    19. import org.bukkit.block.Block;
    20. import org.bukkit.block.BlockFace;
    21. import org.bukkit.command.Command;
    22. import org.bukkit.command.CommandSender;
    23. import org.bukkit.entity.Entity;
    24. import org.bukkit.entity.LeashHitch;
    25. import org.bukkit.entity.Player;
    26. import org.bukkit.event.EventHandler;
    27. import org.bukkit.event.Listener;
    28. import org.bukkit.event.block.Action;
    29. import org.bukkit.event.hanging.HangingBreakEvent;
    30. import org.bukkit.event.player.PlayerInteractEntityEvent;
    31. import org.bukkit.event.player.PlayerInteractEvent;
    32. import org.bukkit.event.world.ChunkLoadEvent;
    33. import org.bukkit.plugin.PluginDescriptionFile;
    34. import org.bukkit.plugin.PluginManager;
    35. import org.bukkit.plugin.java.JavaPlugin;
    36.  
    37. public class Main extends JavaPlugin
    38. implements Listener
    39. {
    40. public static Main plugin;
    41. public static Logger log = Logger.getLogger("Minecraft");
    42.  
    43. public String indent;
    44. public String indentDbl;
    45. public String labelError;
    46.  
    47. public Set<String> toggle = new HashSet<>();
    48. public Map<UUID, Location> entLoc = new HashMap<>();
    49.  
    50. @Override
    51. public void onDisable()
    52. {
    53. this.saveConfig();
    54. PluginDescriptionFile pd = this.getDescription();
    55. getLogger().info("Disabled v" + pd.getVersion());
    56. }
    57.  
    58. @Override
    59. public void onEnable()
    60. {
    61. PluginDescriptionFile pd = this.getDescription();
    62. getLogger().info("Enabled v" + pd.getVersion());
    63. PluginManager pm = Bukkit.getPluginManager();
    64. pm.registerEvents(this, this);
    65. this.saveDefaultConfig();
    66.  
    67. indent = " ";
    68. indentDbl = " ";
    69. labelError = ChatColor.DARK_RED + " Error: " + ChatColor.RESET;
    70. }
    71.  
    72. public void saveMap(Map<UUID, Location> entLoc)
    73. {
    74. List<String> list = new ArrayList<String>();
    75.  
    76. for (Map.Entry<UUID, Location> entry : entLoc.entrySet()) {
    77. Location loc = entry.getValue();
    78. String string = loc.getWorld()
    79. + "," + loc.getBlockX()
    80. + "," + loc.getBlockY()
    81. + "," + loc.getBlockZ();
    82.  
    83. list.add(entry.getKey().toString() + ":" + string);
    84. }
    85. this.getConfig().set("entLoc", list);
    86. }
    87.  
    88. public void loadMap(Map<UUID, Location> entLoc)
    89. {
    90. List<String> list = getConfig().getStringList("entLoc");
    91.  
    92. for (String string : list) {
    93. String one = string.split(":")[0];
    94. String two = string.split(":")[1];
    95. Location loc = new Location(
    96. Bukkit.getWorld(two.split(",")[0]),
    97. Integer.parseInt(two.split(",")[1]),
    98. Integer.parseInt(two.split(",")[2]),
    99. Integer.parseInt(two.split(",")[3]));
    100.  
    101. entLoc.put(UUID.fromString(one), loc);
    102. }
    103. }
    104.  
    105. // Toggle tiki torch mode.
    106. public boolean onCommand(CommandSender sender, Command cmd, String command, String args[])
    107. {
    108. if (sender instanceof Player) {
    109. Player player = (Player) sender;
    110. String name = player.getName();
    111.  
    112. if (command.equalsIgnoreCase("tt")
    113. && player.hasPermission("tt.create")) {
    114. if (args.length == 0) {
    115. if (!toggle.contains(name)) {
    116. toggle.add(name);
    117. player.sendMessage(indent + ChatColor.GREEN + "Tiki Torch mode enabled.");
    118. } else {
    119. toggle.remove(name);
    120. player.sendMessage(indent + ChatColor.RED + "Tiki Torch mode disabled.");
    121. }
    122. } else {
    123. player.sendMessage(labelError + ChatColor.RED + "Too many arugments.");
    124. }
    125. } if (!player.hasPermission("tt.create")) {
    126. player.sendMessage(labelError + ChatColor.RED + "You can't do that.");
    127. }
    128. }
    129. return false;
    130. }
    131.  
    132. @EventHandler
    133. // Tiki torch creation and location registration.
    134. public void onInteract(PlayerInteractEvent event)
    135. {
    136. Player player = event.getPlayer();
    137. Block block = event.getClickedBlock();
    138. Action action = event.getAction();
    139. String name = player.getName();
    140. Material item = player.getItemInHand().getType();
    141.  
    142. if (toggle.contains(name)
    143. && item == Material.TORCH
    144. && action == Action.RIGHT_CLICK_BLOCK
    145. && player.hasPermission("tt.create")) {
    146. World world = player.getWorld();
    147.  
    148. for (int i = 1; i <= 3; i++) {
    149. block.getRelative(BlockFace.UP, i).setType(Material.FENCE);
    150. }
    151. Location hitchLoc = block.getLocation().add(0, 3, 0);
    152. LeashHitch hitch = world.spawn(hitchLoc, LeashHitch.class);
    153.  
    154. entLoc.put(hitch.getUniqueId(), hitchLoc);
    155.  
    156. this.saveMap(entLoc); // Save data
    157.  
    158. block.getRelative(BlockFace.UP, 4).setType(Material.TORCH);;
    159. }
    160. }
    161.  
    162. @EventHandler
    163. public void onEntityInteract(PlayerInteractEntityEvent event)
    164. {
    165. Player player = event.getPlayer();
    166. Entity entity = event.getRightClicked();
    167. UUID uuid = entity.getUniqueId();
    168. String name = player.getName();
    169. Material item = player.getItemInHand().getType();
    170.  
    171. // Unregistering hitch:
    172. if (toggle.contains(name)
    173. && item == Material.TORCH
    174. && entity instanceof LeashHitch
    175. && entLoc.containsKey(uuid)
    176. && player.hasPermission("tt.remove")) {
    177. entLoc.remove(uuid);
    178. player.sendMessage(indent + ChatColor.YELLOW + "Data removed. Removing Hitch...");
    179.  
    180. entity.remove();
    181. } if (entity instanceof LeashHitch
    182. && entLoc.containsKey(uuid)) {
    183. event.setCancelled(true);
    184. player.sendMessage(labelError + ChatColor.RED + "You can't do that.");
    185. }
    186. }
    187.  
    188. @EventHandler
    189. // Hitch OR hitched post is broken:
    190. public void onHangingBreak(HangingBreakEvent event)
    191. {
    192. Entity entity = event.getEntity();
    193. UUID uuid = entity.getUniqueId();
    194.  
    195. if (entLoc.containsKey(uuid)) {
    196. entLoc.remove(uuid);
    197. System.out.println("Data removed (onBreak)");
    198. }
    199. }
    200.  
    201. @EventHandler
    202. public void onChunkLoad(ChunkLoadEvent event)
    203. {
    204. Chunk chunk = event.getChunk();
    205. World world = event.getWorld();
    206. Map<UUID, Location> tempLoc = new HashMap<>();
    207.  
    208. this.loadMap(entLoc); // Load data
    209.  
    210. // Iterate through stored entity data.
    211. for (Iterator<Map.Entry<UUID, Location>> iterator = entLoc.entrySet().iterator();
    212. iterator.hasNext();) {
    213. Map.Entry<UUID, Location> entry = iterator.next();
    214.  
    215. // Is chunk with stored entity equal to current chunk?
    216. // Spawn entity, add new data to temporary map and remove existing data.
    217. if (world.getChunkAt(entry.getValue()).equals(chunk)) {
    218. Location hitchLoc = entry.getValue();
    219. LeashHitch hitch = world.spawn(hitchLoc, LeashHitch.class);
    220.  
    221. tempLoc.put(hitch.getUniqueId(), hitchLoc);
    222.  
    223. iterator.remove();
    224. }
    225. } if (tempLoc != null) {
    226. entLoc.putAll(tempLoc);
    227.  
    228. this.saveMap(entLoc); // Save data
    229. }
    230. }
    231. }


    Thanks
     
  2. Offline

    molenzwiebel

    Well, the problem is literally in the error. You can't spawn a LeashHitch. It might be possible with NMS, but you can't with just Bukkit.
     
  3. Offline

    3ptO

    molenzwiebel The plugin has already spawned a LeashHitch once, I'm trying to spawn it again.
     
  4. Offline

    molenzwiebel

    Code:
    Caused by: java.lang.IllegalArgumentException: Cannot spawn an entity for org.bukkit.entity.LeashHitch
    Seems pretty clear to me...
     
  5. Offline

    3ptO

    molenzwiebel The plugin has already spawned a LeashHitch once at line 152 no problem.
     
  6. Offline

    molenzwiebel


    Well, there is no difference between
    Code:
    LeashHitch hitch = world.spawn(hitchLoc, LeashHitch.class);
    and
    Code:
    LeashHitch hitch = world.spawn(hitchLoc, LeashHitch.class);
    This means either Bukkit is derping, or the spawning on line 152 isn't successful either.
     
  7. Offline

    3ptO

    molenzwiebel As I have said twice, it has spawned at line 152 and therefore Bukkit is derping.
     
  8. Offline

    molenzwiebel

    Alright, you are right. You can spawn the leash. Looking at craftbukkit's code, it seems that the leash object in spawn() is null, which then throws the exception. It might have to do with the chunk not being loaded yet
     
  9. Offline

    3ptO

    molenzwiebel A fellow on bukkitdev pointed out this thread to me, I can't make sense of it however.
     
Thread Status:
Not open for further replies.

Share This Page