[1.5.2] Else statement only appears to read one line

Discussion in 'Spigot Plugin Development' started by gordon301, May 20, 2016.

  1. Hello!

    I'm fairly new to java (Been learning over the last week) and I'm having an issue with a plugin that I'm trying to code for a modded server (FTB Unleashed) that I work for Basically, there is a dupe bug with the crafting station (ID: 3223) that I'm trying to fix by only allowing the player to break the block if it contains no items.
    This is the code I have so far and it doesn't appear to be working.

    Code (Text):
    package me.gordon301.dupefixer.event.block;

    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.*;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.inventory.Inventory;

    public class BlockBreak implements Listener {
       
        @EventHandler
        public void onBlockBreak(BlockBreakEvent event) {
            Player player = event.getPlayer();
           
            Block block = event.getBlock();
            Chest block2 = (Chest) block.getState();
            int material = block.getTypeId();
           
            if(material == 54) {
                Inventory inv = block2.getInventory();
                //if( inv.getContents() != null)
                for (int i = 0; i < inv.getSize(); i++) {
                    {if (inv.getItem(i) == null || inv.getItem(i).getType().equals(Material.AIR))
                        event.setCancelled(false);
                    else
                        event.setCancelled(true);
                        player.sendMessage(ChatColor.GOLD + "[Gordons Police Hamster 2.0]" + ChatColor.RED + " Please empty before breaking");

            }
        }
        }
        }
    }
    What am I doing wrong? Ideally it should stop the block from breaking if it contains items and give the message, if its empty it should just remove the block no problems

    Sidenote: As I'm currently testing this plugin on my non modded server I have to use a chest instead of the crafting station but I will change that before moving over to the modded server :)
     
  2. FormallyMyles

    Supporter

    When programming if you don't use brackets it will only include 1 line. Use brackets.

    eg.
    Code (Text):
    if (lemon) {

    }else
        System.out.println("one line");
    vs

    Code (Text):
    if (lemon) {

    } else {
        System.out.println("one line");
        System.out.println("two lines");
    }

    Hope this helps,
     
  3. On that second line, you have an unnecessary bracket before the if statement.
    Other than that, make sure you're opening and closing brackets after if, else, and else if statements.
    The post above explains it pretty well :)
     
  4. I'm sorry, your code formatting is off, so I don't really understand the code path you have set up.
     
  5. We don't support versions lower than 1.8.
     
  6. Yup, why would the OP even be using 1.5.2 anyways?
     
  7. I think he said he was running a FTB server, so you would have to play on earlier Minecraft versions to play on old modpacks like that.
     
  8. Indeed, I administrate an FTB Unleashed server running the 1.5.2 version of minecraft hence why I needed help for that specific version :)
     
  9. I'm sorry you feel that way? Thanks for sharing?

    Lol

    Try setting it to lower priority maybe there is another plugin unblocking it?
     
  10. That's Nice.

    I understand that my formatting isn't the best. and as I did state. I've been learning Java for all of a week. So if you could kindly not post such meaningless content on this thread I'd much appreciate it. Thank you to the rest of you for helping me, I'm still learning :)
     
  11. Did you even register it in onEnable()?
     
  12. Am I the only one who think someone should just break out the spoon and show him how to fix it? I mean he has been waiting since Friday. xD Or has the main issue been resolved already? @gordon301
     
  13. Do you use a IDE? :)
    If not, I can recommend you to Eclipse. It is AMAZING for beginners!
     
  14. @Mylescomputer pretty much fixed my problem :)

    Yep! I've been using eclipse since I first started learning, great tool!
     
  15. Don't you get errors? When you get a red line underneath something, you can just hover over it with your cursor and get the reason why it is being underlined. And another tool. To format your code, do cONTROL + SHIFT + F