Wands plugin help

Discussion in 'Plugin Development' started by Randomguy, Aug 26, 2015.

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

    Randomguy

    Code:
    package com.Zach.LauncherWand;
    
    import java.util.Random;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Effect;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Fireball;
    import org.bukkit.entity.Item;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Skeleton;
    import org.bukkit.entity.Snowman;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerItemConsumeEvent;
    import org.bukkit.event.player.PlayerPickupItemEvent;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    import org.bukkit.util.Vector;
    
    import com.Zach.CowMissileWand.CowWand;
    import com.Zach.WandsOfMagic.Wands;
    
    public class LaunchWandEvents implements Listener
    {
        private String letters = ChatColor.BLUE + "dhkkjahdiuwhdhauidhiwuhduiahdibwhdbaibdqwbdiwahduhabdhivbawiudhawidguaihwfywagfhwaidguhkwahdyuawigdwauyidkjahwiy";
     
        @EventHandler
        public void onRightClickLauncherWand(PlayerInteractEvent e)
        {
            Player player = (Player) e.getPlayer();
         
            if(e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.RIGHT_CLICK_AIR)
            {
                if(player.getItemInHand().getItemMeta().getDisplayName() == Wands.launcherWand.getItemMeta().getDisplayName())
                {
                    if(player.hasPermission("Wands.LaunchWand") || player.isOp())
                    { 
                        Location loc = player.getLocation().multiply(2);
                        Random random = new Random();
                        int randInt = random.nextInt(28);
                        ItemStack itemsRand = Wands.items[randInt];
                     
                        if(itemsRand.getType() == Material.FIREBALL)
                            player.getWorld().spawn(loc, Fireball.class);
                     
                        if(itemsRand.getType() == Material.ARROW)
                            player.getWorld().spawnArrow(loc, Wands.vec, 3, 3);
                        else
                        {
                            Item dropItem = player.getWorld().dropItem(loc, itemsRand);
                            ItemStack droppedItem = dropItem.getItemStack();
                            Vector vect = loc.getDirection();
                            vect.setX(vect.getX() * 4);
                            vect.setY(vect.getY() * 4);
                            vect.setZ(vect.getZ() * 4);
                            dropItem.setVelocity(vect);
                            dropItem.setCustomName(letters);
                            droppedItem.getItemMeta().addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
                        }
                    }
                }
                if(player.getItemInHand().getItemMeta().getDisplayName() == CowWand.cowWand.getItemMeta().getDisplayName())
                    player.sendMessage(ChatColor.DARK_GREEN + "" + ChatColor.BOLD + "Why u right click me?");
            }
        }
        @EventHandler
        public void onGrouncCollide(PlayerDropItemEvent e)
        {
            Location loc;
            Item ent = e.getItemDrop();
         
            if(ent.getCustomName() == letters)
            {
                ent.setCustomName("");
                if(ent.isOnGround())
                {
                    if(ent.getItemStack() == new ItemStack(Material.GOLDEN_APPLE))
                        ent.setCustomName("Gold Apple");
                    if(ent.getItemStack() == new ItemStack(Material.RECORD_10))
                    {
                        loc = ent.getLocation();
                        ent.getWorld().playEffect(loc, Effect.RECORD_PLAY, 1); //is last number the sound or repeat this? probably 2nd
                    }
                    if(ent.getItemStack() == new ItemStack(Material.SNOW_BALL))
                    {
                        loc = ent.getLocation();
                        ent.getWorld().spawn(loc, Snowman.class);
                    }
                    if(ent.getItemStack() == new ItemStack(Material.BOWL))
                    {
                        loc = ent.getLocation();
                        ItemStack soup = new ItemStack(Material.MUSHROOM_SOUP);
                        ent.getWorld().dropItem(loc, soup);
                    }
                    if(ent.getItemStack() == new ItemStack(Material.BONE))
                    {
                        loc = ent.getLocation();
                        ent.getWorld().spawn(loc, Skeleton.class);
                    }
                    if(ent.getItemStack() == new ItemStack(Material.CLAY_BRICK))
                    {
                        loc = ent.getLocation();
                        if(loc.getBlock().equals(Material.AIR))
                            loc.getBlock().setType(Material.BRICK);
                    }
                    if(ent.getItemStack() == new ItemStack(Material.APPLE))
                        ent.setCustomName("EAT ME");
                 
                    if(ent.getItemStack() == new ItemStack(Material.COMPASS))
                    {
                        ent.setCustomName("PICK ME UP PLEASE");
                     
                        if(ent.getName().equalsIgnoreCase("I POINT TO YOUR BED") || ent.getName().equalsIgnoreCase("I AM BROKEN"))
                        {
                            ent.setItemStack(new ItemStack(Material.DEAD_BUSH));
                            ent.setCustomName(ChatColor.RED + "" + ChatColor.BOLD + "WHY DID U DROP ME!!");
                        }
                    }
                    if(ent.getItemStack() == new ItemStack(Material.COAL))
                        ent.setCustomName("PICK ME UP PLEASE");
                    if(ent.getItemStack() == new ItemStack(Material.TNT))
                    {
                        loc = ent.getLocation();
                        ent.getWorld().createExplosion(loc, 4f);
                    }
                    if(ent.getItemStack() == new ItemStack(Material.FIRE))
                    {
                        //TODO How Spawn Fire
                    }
                    else
                    {
                        //TODO how set damage
                    }
                }
            }
        }
        @EventHandler
        public void eat(PlayerItemConsumeEvent e)
        {
            Player player = e.getPlayer();
            ItemStack eaten = e.getItem();
         
            if(eaten.equals(Material.GOLDEN_APPLE) && Material.GOLDEN_APPLE.name().equalsIgnoreCase("Gold Apple"))
            {
                player.addPotionEffect(new PotionEffect(PotionEffectType.ABSORPTION, 6000, 1));
                player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 6000, 1));
                player.addPotionEffect(new PotionEffect(PotionEffectType.WITHER, 2400, 1));
                player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 6000, 1));
                player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 6000, 1));
                player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 6000, 1));
                player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 6000, 1));
            }
            if(eaten.equals(Material.APPLE) && Material.APPLE.name().equalsIgnoreCase("EAT ME"))
            {
                player.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 1200, 1));
                player.sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "HAHA. LOSER!!");
            }
        }
        @EventHandler
        public void pickup(PlayerPickupItemEvent e)
        {
            if(e.getItem().getName().equals("EAT ME") && e.getItem().getItemStack() == new ItemStack(Material.APPLE))
                e.getPlayer().sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "EAT ME!!!!!! AN APPLE A DAY KEEPS THE DOCTOR AWAY!!");
            if(e.getItem().getName().equals("PICK ME UP PLEASE") && e.getItem().getItemStack() == new ItemStack(Material.COAL))
            {
                e.getPlayer().sendMessage(ChatColor.RED + "" + ChatColor.BOLD + "Santa doesn't like you...");
                e.getItem().setCustomName("Santa doesn't like you...");
            }
            if(e.getItem().getName().equals("PICK ME UP PLEASE") && e.getItem().getItemStack() == new ItemStack(Material.COMPASS))
            {
                Random random = new Random();
                int rand = random.nextInt(3);
                if(rand == 0)
                {
                    Location loc = e.getPlayer().getLocation();
                    loc.setX(loc.getX() + 1);
                    e.getPlayer().setCompassTarget(loc);
                    e.getItem().setCustomName("I AM BROKEN!");
                }
                else
                {
                    Location loc = e.getPlayer().getBedSpawnLocation();
                    e.getPlayer().setCompassTarget(loc);
                    e.getItem().setCustomName("I POINT TO YOUR BED");
                }
            }
        }
    }
    That is the code. Well, there are a lot of errors, and I'm sorry if it's hard to read... But I'm developing a wands plugin, that has many classes that work. This is the one class that doesn't. There are no errors.
    A few things that don't work:
    1. The launcher wand doesn't drop items...
    2. When the launcher did work, none of the names or effects worked...
    3. If you leave and re-join, the wands don't do anything.
    4. This could be a glitch, but sometimes, not often, the wands stop working and you have to get them again.
    A few things:
    1. If you see undeclared stuff, that's because it's in a different class. (this isn't my main class, if you need others, tell me).
    2. Don't spoon feed me, please. I want to learn. Just please tell me the method and if there are args, what the args do (But I usually know, but there are occasions where I don't...)
    A few questions:
    1. I have the play effect method somewhere in there, if not, in a different class. The method is like
    Code:
    player.playEffect(loc, effect, int)
    What is the int? how many times to repeat?
    2. Is there an onCollision event or method? I have a wand that launches stuff but all it has is the isOnGround() so if it bounces off a block.
    3. What is the Duplicate Entry Item error I'm getting?

    Anything else that's wrong, please tell me. I'd prefer if you tell me what's wrong instead of change my code, but any help appreciated. Thanks!
    Ik it's a lot and I'm sorry for that...
     
    Last edited: Aug 26, 2015
  2. Offline

    au2001

    @Randomguy You should null check for ItemMeta and displayName before using them.
    Also, use Cancelable#setCanceled(true) to prevent the items of being used.

    Have you registered these events in your main class?

    For playEffect: see
    http://jd.bukkit.org/org/bukkit/entity/Player.html#playEffect(org.bukkit.Location, org.bukkit.Effect, T)
    The one you use is deprecated: http://jd.bukkit.org/org/bukkit/entity/Player.html#playEffect(org.bukkit.Location, org.bukkit.Effect, int)

    Can we see the full error log for the Duplicate Entry Item?

    Also, you better use ItemStack#isSimilar(ItemStack) rather than "ent.getItemStack() == new ItemStack(Material...)"
     
  3. Offline

    Randomguy

    thx...
    At the bottom the Item entity 1069 usually says random numbers and usually spams that... but I did something so it does it like once... probably because wand stopped launching... thx for help
     
  4. Offline

    au2001

    @Randomguy In your "Wands" class, you have a variable "vec", it's null.
    You must initialize it to use it in your spawn Arrow code.

    I suggest you learn how to debug your plugin, look:
    Code:
    Caused by: java.lang.IllegalArgumentException: Can not spawn arrow with a null velocity
    This is the error, which explains clearly the problem.
    Then, a few lines under you can find that:
    Code:
    at com.Zach.LauncherWand.LaunchWandEvents.onRightClickLauncherWand(LaunchWandEvents.java:54)
    "com.Zach.LauncherWand.LaunchWandEvents" is your package and class
    "onRightClickLauncherWand" is the method causing the error
    "LaunchWandEvents.java:54" is "in the file LaunchWandEvents.java at line 54"

    Line 54:
    Code:
    player.getWorld().spawnArrow(loc, Wands.vec, 3, 3);
    Wands.vec being the velocity of the arrow, the error says it's null

    For more help/details: http://bukkit.org/threads/32457
     
  5. Offline

    Randomguy

    I usually know the basics of debugging, but when I looked, it was spammed with item entity... thx!

    I would love to say this worked, but it doesn't.

    The console still does this:
    Code:
    [16:08:45 ERROR]: Item entity 3051 has no item?!
    [16:08:46 ERROR]: Item entity 3052 has no item?!
    [16:08:50 ERROR]: Item entity 3140 has no item?!
    [16:08:58 ERROR]: Item entity 3330 has no item?!
    [16:08:58 ERROR]: Item entity 3331 has no item?!
    [16:09:00 ERROR]: Item entity 3403 has no item?!
    [16:09:01 ERROR]: Item entity 3412 has no item?!
    [16:09:02 ERROR]: Item entity 3460 has no item?!
    [16:09:03 ERROR]: Item entity 3482 has no item?!
    Not as often but still does it... Also if you look at the code, there is a check if it is a fireball, and else... Those do absolutely nothing. No error message in console...
     
    Last edited: Aug 30, 2015
  6. Offline

    au2001

    @Randomguy I also have this error in another plugin, and I don't really know why :|

    Here is a quick and dirty fix (but the only I found for now):
    Code:
    Bukkit.getLogger().setFilter(new Filter() {
        public boolean isLoggable(LogRecord record) {
            return !record.getMessage().matches("Item entity [0-9]+ has no item\\?!");
        }
    });
    It just removes all the messages from the log that say "Item entity <number> has no item?!"
     
  7. Offline

    Randomguy

    This will probably make me look like an idiot, but where should I add it? I understand it for the most part, but not sure where to put it. Also sorry for taking forever to respond, I'm working on game development...
     
  8. Offline

    au2001

    @Randomguy In the onEnable method of your Main class
     
Thread Status:
Not open for further replies.

Share This Page