Disabling Enchanting Problem.

Discussion in 'Plugin Development' started by HyrulesLegend, Aug 14, 2013.

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

    HyrulesLegend

    So, I simply disabled enchanting by canceling the Enchant event. But, I want to be able to enchant Bows and Diamond Pickaxes aswell, so, I set the enchanting Dpicks priority higher, but now you can enchant everything else as-well, so, please help me out here. (DPick enchanting is in "PickEnchanting.java" and the disabling of enchanting is in the "EnchantingEvent.java")

    Main.java:
    Code:
    package me.kyrp.Elite;
     
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Main extends JavaPlugin {
        private EnchantingEvent PlayerListener = new EnchantingEvent(this);
        private PickEnchanting PlayerListener1 = new PickEnchanting(this);
    {
    }
     
       
       
       
        public void onEnable(){
            getConfig().options().copyDefaults(true);
            saveConfig();
           
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvents(this.PlayerListener, this);
           
            PluginManager pcm = getServer().getPluginManager();
            pcm.registerEvents(this.PlayerListener1, this);
        }
        }
        
    EnchantingEvent.java (This disables enchanting.)
    Code:
    package me.kyrp.Elite;
     
    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.enchantment.EnchantItemEvent;
     
    public class EnchantingEvent implements Listener{
     
            public Main plugin;
         
            public EnchantingEvent(Main instance){
                    plugin = instance;
            }
         
            @EventHandler(priority = EventPriority.LOWEST)
            public void EnchantItem(EnchantItemEvent e){
                e.getItem();
                e.setCancelled(true);
                e.getEnchanter().sendMessage(ChatColor.RED + "You cannot enchant " + e.getItem() + "on elite sonicpvp");
               
     
           
     
     
     
            {
            }
            }
    }
    
    PickEnchanting.java (Supposed to enable Enchanting of pickaxes.)

    Code:
    package me.kyrp.Elite;
     
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.enchantment.EnchantItemEvent;
     
    public class PickEnchanting implements Listener{
     
            public Main plugin;
         
            public PickEnchanting(Main instance){
                    plugin = instance;
            }
         
            @EventHandler(priority = EventPriority.HIGHEST)
            public void EnchantItem(EnchantItemEvent e){
                e.getItem().getType();
                Material.DIAMOND_PICKAXE.getData();
                e.setCancelled(false);
         
            }
    }
     
  2. Offline

    MistPhizzle

    Just use one event and check if the item that is being enchanted is a Diamond Pickaxe. If it is not, have it cancel. You only need one event here, not two different classes.

    Code:java
    1.  
    2. @EventHandler
    3. public void onEnchant(EnchantItemEvent e) {
    4.  
    5. if (e.getItem().getType() != Material.DIAMOND_PICKAXE)) { // This checks if the item is NOT a diamond pickaxe.
    6. e.setCancelled(true); //Cancels the event because it isn't a Diamond Pick.
    7. // Send any messages here.
    8. }
    9.  
    10.  

    This should do the trick. Or something like it. This is off the top of my head.
     
  3. Offline

    HyrulesLegend

    That worked, but what about bow enchants?
     
  4. Offline

    gameunited6155

    Maybe add
    1. if (e.getItem().getType() != Material.BOW)) { // This checks if the item is NOT a diamond pickaxe.
    2. e.setCancelled(true);
     
  5. Offline

    HyrulesLegend

    Nope.

    Anyone got any ideas?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  6. Offline

    MistPhizzle

    Separate arguments in your if statement with && (this means AND).

    Code:java
    1. @EventHandler
    2. public void onEnchant(EnchantItemEvent e) {
    3.  
    4. if (e.getItem().getType() != Material.DIAMOND_PICKAXE) && e.getItem().getType() != Material.BOW) { // This checks if the item is NOT a diamond pickaxe OR a bow.
    5. e.setCancelled(true); //Cancels the event because it isn't a Diamond Pick.
    6. // Send any messages here.
    7. }


    That checks if it is a DIAMOND_PICKAXE or a BOW. If it is neither, it runs the e.setCancelled(true) code.
     
  7. Offline

    HyrulesLegend

    Thanks, I'll test if it works! :)
     
Thread Status:
Not open for further replies.

Share This Page