Nothing happening when I right-click sign?

Discussion in 'Plugin Development' started by Axanite, Nov 22, 2013.

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

    Axanite

    Hi, I'm trying to make a plugin where if you right-click a sign, it will check if you got the information correct and if you have, when you right-click, it will give you the item with the set amount while taking away the set amount of XP on the sign. However, I am presented with no errors in Eclipse nor the console on the server. I am stumped on this one. Any help will be greatly appreciated!

    Code:
    package me.rf2minecraft.XPShop;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.block.Sign;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.SignChangeEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public final class XPShop extends JavaPlugin {
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(new SignListener(), this);
            Bukkit.getServer().getPluginManager().registerEvents(new PlayerInteract(), this);
        }
       
        public class SignListener implements Listener {
            @EventHandler
            public void onSignChange(SignChangeEvent e) {
                if(e.getLine(0).equalsIgnoreCase("[XPShop]")) {
                if(e.getLine(1).isEmpty() || e.getLine(2).isEmpty() || e.getLine(3).isEmpty()) {
                    e.getBlock().setType(Material.AIR);
                    e.getPlayer().sendMessage(ChatColor.DARK_RED + "[XPShop]" + ChatColor.RED + " You did not define required information.");
                    }
                }
                if(e.getLine(0).equalsIgnoreCase("[XPShop]")) {
                    e.setLine(0, ChatColor.DARK_RED + "[XPShop]");
                }
            }
        }
        public class PlayerInteract implements Listener {
            @SuppressWarnings("deprecation")
            @EventHandler
            public void onPlayerInteract(PlayerInteractEvent e) {
                if(e.getClickedBlock().getType() == Material.SIGN) {
                Sign s = (Sign) e.getClickedBlock();
                int item_id = Integer.parseInt(s.getLine(2));
                int item_amount = Integer.parseInt(s.getLine(1)); // Get the item amount from Line 2.
                int price = Integer.parseInt(s.getLine(3)); // Get the item price from Line 4.
                float player_xp = e.getPlayer().getExp();
                if(e.getAction() != Action.RIGHT_CLICK_BLOCK) return;
                if(player_xp < price) {
                    e.getPlayer().sendMessage(ChatColor.DARK_RED + "[XPShop]" + ChatColor.RED + " You do not have enough XP to buy this item!");
                    } else {
                        e.getPlayer().setExp(player_xp - price);
                        e.getPlayer().getInventory().addItem(new ItemStack(Material.getMaterial(item_id), item_amount));
                        e.getPlayer().sendMessage(ChatColor.DARK_RED + "[XPShop]" + ChatColor.RED + " You successfully bought " + item_amount + " of " + Material.getMaterial(item_id).getData().getName());
                    }
                }
            }
        }
    }
    
     
  2. Offline

    McMhz

    rf2minecraft
    Code:java
    1. @EventHandler
    2. public void onClickSign(PlayerInteractEvent e){
    3. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    4. if(e.getClickedBlock().getType() == Material.SIGN || e.getClickedBlock().getType() == Material.SIGN_POST || e.getClickedBlock().getType() == Material.WALL_SIGN){
    5. Sign sign = (Sign) e.getClickedBlock().getState()
    6. if(sign.getLine(0).equalsIgnoreCase("XPSHOP")){
    7. e.getPlayer().sendMessage("ยง3You clicked a sign that sais XPSHOP!");
    8. }
    9. }
    10. }
    11. }
     
  3. Offline

    OracleTarget

    rf2minecraft
    You should use a try and catch around the "int price = Integer.parseInt(s.getLine(2 & 3 ));"
    Use an IllegalArgumentException! You'll get a consol error if line 3 and/or 4 is not an int.

    Just a tip
     
  4. Offline

    boysnnoco

    rf2minecraft don't use == to compare the clicked block and the Material it is just bad, do e.getClickedBlock().getType().equals(Material.SIGN) ect.
     
  5. Offline

    krazytraynz

    That's not true, as he's comparing two objects to check for equality; .equals() is typically used for strings.
     
  6. Offline

    boysnnoco

    I've been yelled at it and i've seen other people get yelled at it for it so I was just spreading the news lol krazytraynz
     
  7. Offline

    PogoStick29

    The equals() method was not made for Strings only. The equals() method is defined in the Object class, which is inherited by all objects in Java. In fact, it could be better to use the equals() method as some classes override it with their own equality check that work better than ==.
     
  8. Offline

    Axanite

    Thanks for all the help guys! I think I got it sorted. :)
    Oh, hi PogoStick29! I watch your vids, keep up the good work :)
     
Thread Status:
Not open for further replies.

Share This Page