Enchantment Help

Discussion in 'Plugin Development' started by StyL_TwisT, Feb 11, 2013.

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

    StyL_TwisT

    Hello,

    I'm making a plugin that pays players for mining ores, but i want to check if their pickaxe has silk touch and if it does nothing will happen, but if it doesn't it will run the econ.deposit + player.sendMessage.

    Does anyone know how i could do this in an if + else statement?

    Thanks,

    StyL TwisT

    Come on, I really need to know this :/

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  2. Offline

    danslayerx

    If you want it so that when they break a block such as diamond ore and it WILL break that block, but do nothing else when using a pickaxe with said enchantment. Just return on an if statement that's checking for the enchantment. Otherwise, if you want it to cancel then cancel the event or don't and it will break the block like a normal pickaxe with Silk Touch would.

    Code:
        private void DetectBreak(BlockBreakEvent e){
            Player p = e.getPlayer();
            if(p.getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH)){
                e.setCancelled(true);
                // Cancel event, nothing happens with Silk Touch.
                return;
            }
         
            // Code here that pay player for breaking block WITHOUT enchantment.
        }
     
  3. Offline

    ZeusAllMighty11

    iterate over enchantments


    And you seriously bumped a thread 30 minutes after posting, on a monday morning at 7am????
     
    danslayerx likes this.
  4. Offline

    StyL_TwisT

    danslayerx Thanks, you really helped me out :)

    Can i use it like this?
    Code:
    if(block == Material.DIAMOND_ORE){
                        Player p = e.getPlayer();
                        if(p.getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH)){
                            e.setCancelled(true);
                        }else{
                            econ.depositPlayer(player.getName(), 300);
                            player.sendMessage("§b+ 300");
                        }
                }
        }
    }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  5. Offline

    danslayerx

    I'm not 100% sure, but unless block is defined as block.getType or getTypeId it will not work correctly.

    You can make one change to that if statement and it should fix that, again not certain it could be the problem.

    Code:
            if(block.getType().equals(Material.DIAMOND_ORE)){
               
                }
           
                      // OR Use it this way (56 is the ITEMID for Diamond Ore)
     
            if(block.getTypeId() == 56){
               
                }
    Otherwise that should work
     
  6. Offline

    StyL_TwisT

    danslayerx It doesn't work.
    Shall i paste my whole BlockListener class + Main class?
     
  7. Offline

    danslayerx

    What is the variable datatype for block? On the method do you have it defined like so?

    Code:
        @EventHandler
        public void Example(BlockBreakEvent e){
            Player p = e.getPlayer();
            Block block = e.getBlock();
              }
     
    // Block block = e.getBlock(); <- Like that? 
     
  8. Offline

    StyL_TwisT

    danslayerx No, it is this.
    Code:
    Material block = e.getBlock().getType();
    And my if statement is this:
    Code:
    if(block.equals(Material.DIAMOND_ORE)){
    The event handler:
    Code:
    @EventHandler(priority=EventPriority.NORMAL)
        public void onBlockBreak(BlockBreakEvent e){
            Material block = e.getBlock().getType();
            Player player = e.getPlayer();
     
  9. Offline

    danslayerx

    Ah, I see what you did there. Sadly I couldn't see what type of DataType you used before.

    Code:
    if(block == Material.DIAMOND_ORE){
                        Player p = e.getPlayer();
                        if(p.getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH)){
                            e.setCancelled(true);
                        }else{
                            econ.depositPlayer(player.getName(), 300);
                            player.sendMessage("§b+ 300");
                        }
                }
        }
    }
    Your original code should work fine, I'm used to defining the block broken as Block block = e.getBlock();

    Where as you defined it as the Material which is one step ahead, e.getBlock().getType(); hence why my thought processing wouldn't work, heh. Anyway that should in theory work. If you have any issues I'm here to help.
     
  10. Offline

    StyL_TwisT

    It didn't work do you think there could be a fault in my main class?
    Code:
    package me.styltwist.minepay;
    
    import java.util.logging.Logger;
    
    import me.styltwist.minepay.MyBlockListener;
    import net.milkbowl.vault.chat.Chat;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.permission.Permission;
    
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class MinePay extends JavaPlugin {
    
        public static final Logger log = Logger.getLogger("Minecraft");
        public static Economy econ = null;
        public static Permission perms = null;
        public static Chat chat = null;
        public final MyBlockListener bl = new MyBlockListener();
    
        @Override
        public void onDisable() {
            log.info(String.format("[%s] Disabled Version %s", getDescription().getName(), getDescription().getVersion()));
        }
    
        @Override
        public void onEnable() {
            if (!setupEconomy() ) {
                log.severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
                getServer().getPluginManager().disablePlugin(this);
                PluginManager pm = getServer().getPluginManager();
                pm.registerEvents(new MyBlockListener(), this);
                return;
            }
            setupPermissions();
            setupChat();
        }
    
        public boolean setupEconomy() {
            if (getServer().getPluginManager().getPlugin("Vault") == null) {
                return false;
            }
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
            if (rsp == null) {
                return false;
            }
            econ = rsp.getProvider();
            return econ != null;
        }
    
        public boolean setupChat() {
            RegisteredServiceProvider<Chat> rsp = getServer().getServicesManager().getRegistration(Chat.class);
            chat = rsp.getProvider();
            return chat != null;
        }
    
        public boolean setupPermissions() {
            RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
            perms = rsp.getProvider();
            return perms != null;
        }
    }
     
  11. Offline

    danslayerx

    Could you give me a specific error?

    It's very unlikely, the problem would most likely be in the class itself. If it still remains do you mind if I see your full class for your 'MyBlockListener'?
     
  12. Offline

    StyL_TwisT

    MyBlockListener
    Code:
    package me.styltwist.minepay;
     
    import net.milkbowl.vault.chat.Chat;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.permission.Permission;
    import org.bukkit.enchantments.Enchantment;
    
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
     
    public class MyBlockListener implements Listener {
        public static Economy econ = null;
        public static Permission perms = null;
        public static Chat chat = null;
        public static MinePay plugin;
        public static Material[] orelist = {Material.DIAMOND_ORE, Material.GOLD_ORE, Material.IRON_ORE, Material.REDSTONE_ORE, Material.COAL_ORE, Material.LAPIS_ORE};
        
        @EventHandler(priority=EventPriority.NORMAL)
        public void onBlockBreak(BlockBreakEvent e){
            Material block = e.getBlock().getType();
            Player player = e.getPlayer();
            
            if(block.equals(Material.DIAMOND_ORE)){
                        Player p = e.getPlayer();
                        if(p.getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH)){
                            e.setCancelled(true);
                        }else{
                            econ.depositPlayer(player.getName(), 300);
                            player.sendMessage("§b+ 300");
                        }
                }
        }
    }
     
     
  13. Offline

    danslayerx

    This works for me, at least the method 'onBlockBreak' does, where is the specific error? Could you give me a line number?
     
  14. Offline

    StyL_TwisT

    There aren't any errors in eclipse its just it doesn't work in-game.
     
  15. Offline

    danslayerx

    Do you get an error in-game? If so could you post the stacktrace?
     
Thread Status:
Not open for further replies.

Share This Page