Checking for Entity (LeashHitch) Existence

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

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

    3ptO

    Hello all,

    What would be the best method to check for an entity's existence using the entity's stored location? The entity is a LeashHitch, being used aesthetically (not leashed to an entity) and as a result despawns when there are no players around. I need to check for the entity's existence so that I may respawn the entity.

    Thanks
     
  2. Offline

    Goblom

    Code:java
    1. private void LeashHitch(LeashHitch event) {
    2. Location LeashLocation = event.getLocation();
    3. }

    Code:java
    1. player.getWorld().spawn(LeashLocation), LeashHitch.class/);

    Don't know if that would work...
     
  3. Offline

    3ptO

    Goblom I already have a method to spawn the LeashHitch, that is what this is doing right?
     
  4. Offline

    Goblom

    No, that is getting the location and setting the location


    then spawning a Leash at the LeashLocation
     
  5. Offline

    3ptO

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

    I currently use ChunkLoadEvent, however, it has proven to be unreliable.
    I need an alternative such as checking for the LeashHitch's existence to respawn.
     
Thread Status:
Not open for further replies.

Share This Page