Double class execution.. glitch

Discussion in 'Plugin Development' started by kev3200, Feb 1, 2015.

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

    kev3200

    So I'm trying to make a plugin that will "de-craft" a sword when a player right clicks on an iron_block. I just started coding in Bukkit.. yesterday so if there are some more conventional means of achieving my results please let me know. (mainly if they lower the line count :D)

    My problem is that when I right click an Iron Sword on the Iron_Block it will run the "unCraft" class fine. But when I right click the iron_ingot (with 2 in my inventory and more than 1 stick) it will run the "reCraft" class, discover the Iron Sword in my inventory now, and then run the "unCraft" class, resulting in 2 iron_ingots and the same stack of sticks.

    So my question is why does it seem to run both classes? It should check that on my right-click I don't have the sword in my inventory so should skip the deCraft section and just perform the reCraft stuff right? Explanation would be great, not just solution please. Thanks.

    Code (open)

    Code:
    package me.kev3200.itemp.listeners;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    
    import static org.bukkit.ChatColor.*;
    
    public class deCrafting implements Listener{
        @EventHandler
        public void unCraft(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            Material item = p.getItemInHand().getType();
           
            if(e.getAction() == (Action.RIGHT_CLICK_BLOCK)) {
                Material block = e.getClickedBlock().getType();
                if(item == (Material.DIAMOND_SWORD) && block == Material.IRON_BLOCK) {
                    p.setItemInHand(new ItemStack(Material.AIR));
                    p.getInventory().addItem(new ItemStack(Material.STICK, 1));
                    p.getInventory().addItem(new ItemStack(Material.DIAMOND, 2));
                }
                if(item == (Material.IRON_SWORD) && block == Material.IRON_BLOCK) {
                    p.setItemInHand(new ItemStack(Material.AIR));
                    p.getInventory().addItem(new ItemStack(Material.STICK, 1));
                    p.getInventory().addItem(new ItemStack(Material.IRON_INGOT, 2));
                }
                if(item == (Material.GOLD_SWORD) && block == Material.IRON_BLOCK) {
                    p.setItemInHand(new ItemStack(Material.AIR));
                    p.getInventory().addItem(new ItemStack(Material.STICK, 1));
                    p.getInventory().addItem(new ItemStack(Material.GOLD_INGOT, 2));
                }
                if(item == (Material.STONE_SWORD) && block == Material.IRON_BLOCK) {
                    p.setItemInHand(new ItemStack(Material.AIR));
                    p.getInventory().addItem(new ItemStack(Material.STICK, 1));
                    p.getInventory().addItem(new ItemStack(Material.COBBLESTONE, 2));
                }
                if(item == (Material.WOOD_SWORD) && block == Material.IRON_BLOCK) {
                    p.setItemInHand(new ItemStack(Material.AIR));
                    p.getInventory().addItem(new ItemStack(Material.STICK, 1));
                    p.getInventory().addItem(new ItemStack(Material.WOOD, 2));
                }
            } p.updateInventory();
        }
        @EventHandler
        public void reCraft(PlayerInteractEvent e) {
            Player p = e.getPlayer();
            String pName = p.getDisplayName();
            Material item = p.getItemInHand().getType();
            String negativeMessage = (GOLD + "You do not have the right ingredients to make a ");
           
            if(e.getAction() == (Action.RIGHT_CLICK_BLOCK)) {
                Material block = e.getClickedBlock().getType();
                if(item == (Material.DIAMOND) && block == Material.IRON_BLOCK) {
                    if(p.getInventory().contains(Material.DIAMOND, 2) && p.getInventory().contains(Material.STICK, 1)) {
                        p.getInventory().removeItem(new ItemStack(Material.DIAMOND, 2));
                        p.getInventory().removeItem(new ItemStack(Material.STICK, 1));
                        p.getInventory().addItem(new ItemStack(Material.DIAMOND_SWORD, 1));
                        p.sendMessage("You have successfully built a diamond sword!");
                    } else {
                        p.sendMessage(negativeMessage + RED + "diamond sword.");
                    }
                }
                if(item == (Material.IRON_INGOT) && block == Material.IRON_BLOCK) {
                    if(p.getInventory().contains(Material.IRON_INGOT, 2) && p.getInventory().contains(Material.STICK, 1)) {
                        p.getInventory().removeItem(new ItemStack(Material.IRON_INGOT, 2));
                        p.getInventory().removeItem(new ItemStack(Material.STICK, 1));
                        p.getInventory().addItem(new ItemStack(Material.IRON_SWORD, 1));
                        p.sendMessage("You have successfully built an iron sword!");
                    } else {
                        p.sendMessage(negativeMessage + RED + "iron sword.");
                    }
                }
                if(item == (Material.GOLD_INGOT) && block == Material.IRON_BLOCK) {
                    if(p.getInventory().contains(Material.GOLD_INGOT, 2) && p.getInventory().contains(Material.STICK, 1)) {
                        p.getInventory().removeItem(new ItemStack(Material.GOLD_INGOT, 2));
                        p.getInventory().removeItem(new ItemStack(Material.STICK, 1));
                        p.getInventory().addItem(new ItemStack(Material.GOLD_SWORD, 1));
                        p.sendMessage("You have successfully built a gold sword!");
                    } else {
                        p.sendMessage(negativeMessage + RED + "gold sword.");
                    }
                }
                if(item == (Material.COBBLESTONE) && block == Material.IRON_BLOCK) {
                    if(p.getInventory().contains(Material.COBBLESTONE, 2) && p.getInventory().contains(Material.STICK, 1)) {
                        p.getInventory().removeItem(new ItemStack(Material.COBBLESTONE, 2));
                        p.getInventory().removeItem(new ItemStack(Material.STICK, 1));
                        p.getInventory().addItem(new ItemStack(Material.STONE_SWORD, 1));
                        p.sendMessage("You have successfully built a stone sword!");
                    } else {
                        p.sendMessage(negativeMessage + RED + "stone sword.");
                    }
                }
                if(item == (Material.WOOD) && block == Material.IRON_BLOCK) {
                    if(p.getInventory().contains(Material.WOOD, 2) && p.getInventory().contains(Material.STICK, 1)) {
                        p.getInventory().removeItem(new ItemStack(Material.WOOD, 2));
                        p.getInventory().removeItem(new ItemStack(Material.STICK, 1));
                        p.getInventory().addItem(new ItemStack(Material.WOOD_SWORD, 1));
                        p.sendMessage("You have successfully built a wooden sword!");
                    } else {
                        p.sendMessage(negativeMessage + RED + "wooden sword.");
                    }
                }
            } p.updateInventory();
        }
    }
    
     
  2. @kev3200
    [1] I believe it's because the PlayerInteractEvent is still being run (if you know what I mean). It runs the uncraft and since (for a very very brief moment), it instantly recognizes that the iron ingots are in the player's inventory because the event is still being run (since the player is still technically right clicking the block). Try cancelling the event at the end.
    [2] Check if the block is an iron block ONCE. Do it at the beginning so you don't have to keep checking it every single time!
    [3] They are methods, not classes.
     
    kev3200 likes this.
  3. Offline

    kev3200

    Sorry my bad. Method :) And thanks for the quick reply. I'll edit that and get back to you.

    Eureka! It works :) Thanks a bunch man.
     
    Last edited: Feb 2, 2015
    DJSkepter likes this.
  4. Offline

    kev3200

    Nvm.. false positive. It still glitches.

    I have this right now for one of the swords:
    Diamond Sword (open)

    Code:
    if(e.getAction() == (Action.RIGHT_CLICK_BLOCK) && e.getClickedBlock().getType() == Material.IRON_BLOCK) {
                if(item == Material.DIAMOND_SWORD) {
                    e.setCancelled(true);
                    p.setItemInHand(new ItemStack(Material.AIR));
                    p.getInventory().addItem(new ItemStack(Material.STICK, 1));
                    p.getInventory().addItem(new ItemStack(Material.DIAMOND, 1));
                    p.sendMessage("You lost 1 resource for taking apart your diamond sword.");
                }
    }
    
    if(e.getAction() == (Action.RIGHT_CLICK_BLOCK) && e.getClickedBlock().getType() == Material.IRON_BLOCK) {
                if(item == Material.DIAMOND) {
                    if(p.getInventory().contains(Material.DIAMOND, 2) && p.getInventory().contains(Material.STICK, 1)) {
                        e.setCancelled(true);
                        p.getInventory().removeItem(new ItemStack(Material.DIAMOND, 2));
                        p.getInventory().removeItem(new ItemStack(Material.STICK, 1));
                        p.getInventory().addItem(new ItemStack(Material.DIAMOND_SWORD, 1));
                        p.sendMessage("You have successfully built a diamond sword!");
                    } else {
                        p.sendMessage("You don't have the right resources to make a diamond sword.");
                    }
                }
    }

    And this works, but, when I try to craft an iron sword with 2 iron_ingots in my first hotbar slot, and a stick in my second, it makes the sword, and then un-crafts it again. Same problem.
    In my code, you can see I am cancelling the event, so why does it still not work?
     
  5. Offline

    Burnett

    @kev3200 The last snippet of code is applicable for diamond swords and diamonds. Not iron_ingots like you stated. Full code would be nice to see what can be fixed. As for a quick glance at the code above, you have two if statements that check the same thing twice. Combine them. Next what you want to do is have an else for the next checks. Retest with these changes, post full code and give detailed analysis of what happened in steps.
     
  6. Offline

    1Rogue

  7. Offline

    kev3200

  8. Offline

    Burnett

    @kev3200 By the sounds of it you didn't start Java 2 days ago. You skipped the Java and went to the bukkit.
     
  9. Offline

    kev3200

    Sorry. Bad wording. I started Bukkit two days ago. Been doing java for.. maybe half a year now. Took an introductory class at community college.
    Speaking of which, I'll be in class most of today, but I'll try and get that updated code to you by tonight.
     
  10. Offline

    Burnett

  11. Offline

    kev3200

    Thanks @Burnett That did it! Code seems to be working fine now. Guess my problem was like you said, I had two if statements doing the same thing twice. So.. the code did the same thing twice... figures. Thanks for the help :)
     
Thread Status:
Not open for further replies.

Share This Page