Can someone tell me what I'm doing wrong in my code.

Discussion in 'Spigot Plugin Development' started by CyberCrafter, Aug 19, 2017.

  1. I intent to add an item to the hopper inventory when itemspawnevent is called. But for some reason it dosen't work.
    The hopper should be named "Jumper" for the item to go inside it and the distance between the item and jumper should be at max 4 blocks.
    Code (Text):

    package com.Torpedo.Jumper;

    import java.util.ArrayList;
    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.block.BlockState;
    import org.bukkit.block.Hopper;
    import org.bukkit.entity.Item;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.ItemSpawnEvent;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Jumper extends JavaPlugin implements Listener {

        public void onEnable()
        {
            Bukkit.getPluginManager().registerEvents(this,  this);
        }
     
        @EventHandler
        public void onItemSpawn(ItemSpawnEvent e)
        {
            Bukkit.getLogger().info("Item Dropped!");
            Location loc = e.getLocation();
            List<Hopper> jumpers = new ArrayList<Hopper>();
            for(BlockState bs : loc.getChunk().getTileEntities())
            {
                if(bs instanceof Hopper){
                    Bukkit.getLogger().info("Nearby Hopper Found!");
                    if(((Hopper) bs).getInventory().getName() == "Jumper"){
                        Bukkit.getLogger().info("Jumper confirmed!");
                       jumpers.add((Hopper)bs);
                       Bukkit.getLogger().info("Jumper added to list!");
                    }
                }
            }
            if(!(jumpers.isEmpty())){
            Hopper hopper = jumpers.get(0);
            for(Hopper h : jumpers){
                if(h.getLocation().distanceSquared(loc) <= 16){
                    if(h.getLocation().distanceSquared(loc) < hopper.getLocation().distanceSquared(loc)){
                        hopper = h;
                    }
                }
            }
            Bukkit.getLogger().info("Closest Jumper found!");
            if(hopper.getLocation().distanceSquared(loc) <= 16){
                Item i = (Item)e;
                hopper.getInventory().addItem(i.getItemStack());
                Bukkit.getLogger().info("Item added!");
            }
        }
        }

    }
     
    It only displays till "Nearby Hopper found".
     
  2. for Strings, try using String#equals(String) instead of ==

    Code (Text):
    if(((Hopper) bs).getInventory().getName().equals("Jumper"){
     
    • Agree Agree x 1
  3. Thanks @roro1506
    But now it only gets till "Closest Jumper Found" and gives me an error:
    java.lang.ClassCastException: org.bukkit.event.entity.ItemSpawnEvent cannot be cast to org.bukkit.entity.Item at com.Torpedo.Jumper.Jumper.onItemSpawn(Jumper.java:51) ~[Jumper.jar:?]
     
  4. Then jump to Line 51 of class Jumper and try to guess, what "ClassCastException" means. This aint hard. If you dont know how to interpret errors I'd suggest, you better learn exception handling first.

    Greetings.
     
    • Useful Useful x 1
  5. Yeah, nvm. I just posted it while eclipse was opening.
    Problem tho, I don't want anyone to just rename their Hopper as Jumper and then get benefits.
    But instead I want the actual Name of the block..the entity jumper as some colored code with special lores so people actually buy it. Any ideas how I could do that?
     
  6. NBTTags.

    Save the information in the block via NBTTags.

    https://www.spigotmc.org/threads/how-would-you-access-a-blocks-nbt.142475/

    https://www.spigotmc.org/threads/tu...guide-to-itemstack-nbttags-attributes.131458/
     
  7. I don't understand how to do it. Can you give an example.
     
  8. You get a castexeption when you try to cast something what you cant cast into the type you want it.
     
  9. In your case you try to cast the spawnevent into an item. Thats simply not possible.
    Its just like you want to cast a string to a char or sth like that
     
  10. Read the links I gave you. Theres plenty of information on NBT there.