certain level to wear certain armor

Discussion in 'Plugin Development' started by rhunter, Apr 27, 2015.

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

    rhunter

    Hi im trying to make a plugin where you need to be certain levels to wear certain armor. for example, you need to be atleast level 10 to wear iron armor. Heres the code I have so far:

    public class Main extends JavaPlugin{
    @EventHandler
    public void levelPermissions(PlayerInteractEvent event){
    Player player = event.getPlayer();
    if(player.getLevel() < 10){
    if(player.getInventory().getBoots().getType() == Material.IRON_BOOTS){
    player.sendMessage(ChatColor.RED+"You aren't high enough level to wear this!");
    }
    }
    }
    }

    This code doesn't send the message that you aren't allowed to wear this when I try to put it on. My questions are:
    1. why doesn't it send the message
    2. how would I make it so you can't wear all iron armor without putting them all in individually
    3. how would i make it cancel the event if you can't wear it?

    Thanks!
     
    Last edited: Apr 27, 2015
  2. Offline

    Payment_Option

    If you're sure that you've registered this class for events, then some other things I think might be the problem:

    * I'm not sure if this is true, but I don't think that the player's armor is updated before the interact event to put it on takes place. So, the player doesn't have the boots in their boots slot until after all event handlers have done their stuff. Therefore, your first condition will always be false unless they already have the boots.

    Here's some code that might work for what you want, although there may be bugs in it:
    Code:
    @EventHandler
    public void levelPermissions(PlayerInteractEvent event) {
        if (event.getAction() == Action.RIGHT_CLICK) {
            Player player = event.getPlayer();
            Material handItemMaterial = player.getItemInHand().getType();
    
            if (player.getLevel() < 10 && handItemMaterial.name().contains("IRON") && isArmor(handItemMaterial)) {
                player.sendMessage(ChatColor.RED + "You aren't high enough level to equip this!");
                event.setCancelled(true);
            }
        }
    }
    
    private static boolean isArmor(Material type) {
        if (type == null) {
            return false;
        }
    
        String name = type.name();
    
        return name.contains("CHESTPLATE") || name.contains("BOOTS") ||
               name.contains("HELMET") || name.contains("LEGGINGS");
    }
     
  3. Offline

    ninjaboy323

    Hey I am currently on my phone but I will do my best to help you. So the way you are doing this currently is wrong. You would need to check for ALL the ways a player could put armor on, check what armor it is, check level and set cancelled if you need to. I will post code once I get on tomorrow. But the ways someone could put armor on include:

    Right click
    Shift click
    Drag and drop
    Dispenser

    I will post code for each of these once I can

    ----

    for the physically put the armor on:
    Code (Java):
    1. public void potionEffectEquip(InventoryClickEvent event) {
    2. ItemStack cursor = event.getCursor();
    3. if (event.getSlotType() == InventoryType.SlotType.ARMOR) {
    4. if (cursor != null && cursor.getType() != Material.AIR) {
    5. }
    6. }
    7. }

    for the shift click:
    Code (Java):
    1. public void potionEffectEquip2(InventoryClickEvent event) {
    2. ItemStack cursor = event.getCursor();
    3. if (event.getClick().isShiftClick()) {

    4. }
    5. }
    now for the right click:
    Code (Java):
    1. @EventHandler
    2. public void potionEffectEquip3(PlayerInteractEvent e) {
    3. if (e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    4. if (e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equals("Blindness")) {
    5. * KEEP IN MIND THAT RIGHT CLICK WITH ARMOR SOMETIMES GETS SUPER GLITCHY SO I SUGGEST TO JUST TURN THIS OFF
    6. * TO DO SO JUST PUT THIS IN:
    7. * e.setCancelled(true);
    8. * e.getPlayer().updateInventory();
    9. */
    10. }
    11. }
    12. }
    Now you can figure the dispenser on your own. Just search up the event named onDispense or something of the sort. :)Also the potion effect seems fine to me so your problem is probz the fact that you didn't register the events
    btw to do that just put this in your onEnable:


    {{Posts merged by Myrathi -- please edit consecutive posts!}}
     
    Last edited by a moderator: Apr 27, 2015
  4. Offline

    mine-care

    @Payment_Option Please avoid spoonfeeding, most people will get nothing out of it :- )
    @rhunter please put your code into [c0de] ... [/c0de] brackets, (i misspeled 0 on purpose.)
    @ninjaboy323 you are missing @EventHandler annotation in some of your events (mabe because you are on phone (- :) Also i recomend to avoid naming stuff like name, name1, name2 to skip the chance of calling the wrong method/using the wrong variable and so on by mistake. Lastly the same about spoon feeding :3 ^
     
  5. Offline

    ninjaboy323

    @mine-care
    I see the @EventHandler that was because on phone, and the names are examples for him
     
  6. Offline

    mine-care

    @ninjaboy323 i see :- ) but prefer not to spoonfeed :p
    Cheers.
     
Thread Status:
Not open for further replies.

Share This Page