Make Items Not Despawn

Discussion in 'Plugin Development' started by KeybordPiano459, Sep 16, 2012.

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

    KeybordPiano459

    I'm pretty sure it takes items five minutes and then they despawn. Is there a way to make it so that they don't despawn? Actually not any item, I have a line that spawns an item in my code, is there a way to make it so that just that item doesn't despawn? And no I can't just spawn a new item after five minutes, because a player would pick up that item and I can't have it respawn so that there's unlimited of it.
     
  2. Offline

    gregthegeek

  3. Offline

    KeybordPiano459

  4. Offline

    LucasEmanuel

    Log the entity object and listen for the event gregthegeek posted, then compare the object that was trying to despawn, if it is the same object that you logged, deny the event, else allow it
     
  5. Offline

    KeybordPiano459

    I need to do that, but I don't know how.
     
  6. Offline

    darkmage0252

    add the item to a list/map then compare it to the item on the itemdespawn event
     
  7. Offline

    KeybordPiano459

    I suck at HashMaps =/ how would I go about doing that?
     
  8. Offline

    suckycomedian

    What're you using it for? You could make an ArrayList to save them in like this
    Code:
    public ArrayList<Integer> items = new ArrayList<Integer>();
     
    @EventHandler
        public void despawn(ItemDespawnEvent event){
            if(items.contains(event.getEntity().getItemStack())){
                event.setCancelled(true)
            }
    I'm just guess with this. I have no idea what you'd put in the array list...
     
  9. Offline

    KeybordPiano459

    Yeah me neither
     
  10. Offline

    suckycomedian

    Could you post more of your code? Specifically, where you spawn the item in
     
  11. Offline

    KeybordPiano459

    This is where I spawn the item:
    Code:java
    1. World world = Bukkit.getServer().getWorld("world");
    2. int x1 = 10;
    3. int y1 = 45;
    4. int z1 = 4351;
    5. final Location loc1 = new Location(world, x1, y1, z1);
    6. final ItemStack item1 = new ItemStack(Material.MELON, 1);
    7. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
    8. public void run() {
    9. Bukkit.getServer().getWorld("world").dropItemNaturally(loc1, item1);
    10. Bukkit.getServer().broadcastMessage("[" + ChatColor.LIGHT_PURPLE + "Apocalypse" + ChatColor.RESET + "] " + ChatColor.RED + "Reloading data, incoming lag.");
    11. Bukkit.getServer().reload();
    12. }
    13. }, 100L);
     
  12. Why are you triggering reload on the entire server ? :confused: Some plugins might break and most of all you're prone to memory leaks, using reload is a really bad idea.

    Also, I am unsure if cancelling ItemDespawnEvent works properly... you should test it before relying on it.

    And also, hashmaps are too easy... key-value pair.
    Basically you asign values to keys and then to get a value you give it the key, pretty simple stuff.
     
  13. Offline

    KeybordPiano459

    I've never used a HashMap before =/ Also, the reload thing was a test and I forgot to take it out.

    D: I have no idea where to start. First of all, how do I edit my code so that when the item is spawned it logs something to the hashmap, an int would be nice.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
Thread Status:
Not open for further replies.

Share This Page