Player Switches to and From Sword

Discussion in 'Plugin Development' started by TheManiacGamers, Nov 8, 2016.

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

    TheManiacGamers

    Hello Everyone!

    Currently coding a Hub plugin for my server, and I'm trying to figure out how I would check if the player switches FROM a Sword TO another hotbar slot.
    I am trying to make it say "PVP Disabled" and remove all their armor.

    At this moment,
    this is all I have:

    [
    Code:
        @EventHandler
        public void onPlayerSwitchItems(PlayerItemHeldEvent e) {
            if (e.getNewSlot() == 8 && (e.getPlayer().getGameMode().equals(GameMode.SURVIVAL))) {
                Player p = e.getPlayer();
                ItemStack helmet = new ItemStack(Material.DIAMOND_HELMET, 1);
                ItemStack chestplate = new ItemStack(Material.DIAMOND_CHESTPLATE, 1);
                ItemStack leggings = new ItemStack(Material.DIAMOND_LEGGINGS, 1);
                ItemStack boots = new ItemStack(Material.DIAMOND_BOOTS, 1);
                List<String> helmetLore = new ArrayList<>();
                ItemMeta helmetMeta = helmet.getItemMeta();
                helmetMeta.setDisplayName(ChatColor.RED + "PVPers Helmet!");
                helmetLore.add(ChatColor.DARK_PURPLE + "This helps protect your head when in those PVP battles!");
                helmetMeta.setLore(helmetLore);
                helmet.setItemMeta(helmetMeta);
                List<String> chestplateLore = new ArrayList<>();
                ItemMeta chestplateMeta = chestplate.getItemMeta();
                chestplateMeta.setDisplayName(ChatColor.RED + "PVPers Chestplate!");
                chestplateLore.add(ChatColor.DARK_PURPLE + "This helps protect your chest when in those PVP battles!");
                chestplateMeta.setLore(chestplateLore);
                chestplate.setItemMeta(chestplateMeta);
                List<String> leggingsLore = new ArrayList<>();
                ItemMeta leggingsMeta = leggings.getItemMeta();
                leggingsMeta.setDisplayName(ChatColor.RED + "PVPers Leggings!");
                leggingsLore.add(ChatColor.DARK_PURPLE + "This helps protect your legs when in those PVP battles!");
                leggingsMeta.setLore(leggingsLore);
                leggings.setItemMeta(leggingsMeta);
                List<String> bootsLore = new ArrayList<>();
                ItemMeta bootsMeta = boots.getItemMeta();
                bootsMeta.setDisplayName(ChatColor.RED + "PVPers Boots!");
                bootsLore.add(ChatColor.DARK_PURPLE + "This helps protect your feet when in those PVP battles!");
                bootsMeta.setLore(bootsLore);
                boots.setItemMeta(bootsMeta);
                p.getInventory().setHelmet(helmet);
                p.getInventory().setChestplate(chestplate);
                p.getInventory().setLeggings(leggings);
                p.getInventory().setBoots(boots);
                p.sendMessage(ChatColor.GREEN + "PVP Enabled!");
            }
        }
    but that's only for enabling pvp (Switching TO the Sword).

    How would I go about detecting when the player switches from the sword?

    Thanks in advance. :)
    EDIT:// Forgot there was a e.getPreviousSlot().

    Fixed it. :)

    Code:
    
    if (e.getPreviousSlot() == 8 && (e.getPlayer().getGameMode().equals(GameMode.SURVIVAL))) {
    Player p = e.getPlayer();ItemStack air = new ItemStack(Material.AIR);
    p.getInventory().setHelmet(air);
    p.getInventory().setChestplate(air);
    p.getInventory().setLeggings(air);
    p.getInventory().setBoots(air);
    p.sendMessage(ChatColor.DARK_RED + "PVP Disabled!");
    
    
     
    Last edited: Nov 8, 2016
  2. Offline

    Lordloss

    Please mark this thread as solved.
     
Thread Status:
Not open for further replies.

Share This Page