[TUTORIAL] Setting Armor/Player Invisible When ShiftClicking

Discussion in 'Resources' started by FatAussieFatBoy, Sep 17, 2013.

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

    FatAussieFatBoy

    I am back with another Tutorial for the people of Bukkit, this time I am teaching you how to Hide your Player and Armor when Shift Clicking, But before we begin this Tutorial I must encourage you to visit http://forums.bukkit.org/threads/tutorial-saving-player-inventories.175032/ First so you can learn How HashMaps work with Inventories.

    But without much else to Say lets Begin, So Firstly we will want to add extends JavaPlugin and implements Listener to the Main Class

    Code:
    public HashMap<String, ItemStack[]> armorsave = new HashMap<String, ItemStack[]>();
    If you have been reading the Other Post you will Realize that all we are using the Same Method for this Tutorial (except the invsave HashMap), but Now this is where we will Be Mixing the Code up a Bit, So now we are adding Event for when the Player Shift Clicks -

    Code:
    //Shift Click Checker
    @EventHandler
    public void PlayerShiftClick(PlayerToggleSneakEvent e) {
        Player p = e.getPlayer();
    //NOTE YOU CAN LIMIT THE INVISIBILITY WITH THE 20000 NUMBER BY SETTING IT LOWER, NOTE ALSO THAT 20 EQUALS ONE SECOND, SO LIMITING A PLAYER TO 6 SECONDS WOULD BE 120
        PotionEffect effect = new PotionEffect(PotionEffectType.INVISIBILITY, 20000, 0);
        if(e.isSneaking()) {
            if(p.hasPermission("permission")) {
                p.addPotionEffect(effect);
                armorsave.put(p, p.getInventory().getArmorContents());
                p.getInventory().setArmorContents(null);
            }else{
                e.setCancelled(true);
            }
        }
        if(!e.isSneaking()) {
            if(p.hasPermission("permission")) {
                p.removePotionEffect(PotionEffectType.INVISIBILITY);
                p.getInventory().setArmorContents(armorsave.get(p));
            }
        }
    }
    That's it nothing more nothing less, I hope you enjoyed the Tutorial and I hope it Has helped.

    FatAussieFatBoy <3
     
  2. Offline

    CodenameTitan

    FatAussieFatBoy Very nice tutorial :) This will be of use to me in the future! Also try not to save the Player in a hashmap. It causes memory leek issues from what I have heard :)
    Happy Coding :)
     
  3. Offline

    CreatorTom

    Hmm, nice idea to let players become invisible when they shiftclick.
    Nice tutorial for those new bukkit coders!
     
  4. Offline

    FatAussieFatBoy

    CodenameTitan
    Thanks for the Suggestion but we are only adding the Players Armor to the HashMap and only His Name so the Armor may be returned when he UnShifts, but Thanks glad you Liked the Tutorial and can't wait to see what people can make with this Code :p
     
  5. Offline

    bobacadodl



    No, you're saving the player object as a key in the hashmap. Instead, you should just store his name!
    Code:
    public HashMap<Player, ItemStack[]> armorsave = new HashMap<Player, ItemStack[]>();
     
    Change to
     
    public HashMap<String, ItemStack[]> armorsave = new HashMap<String, ItemStack[]>();
     
  6. Offline

    TheTinySpider

    This isn't a really useful, lets say the player get hits by a heavy enchanted sword, with his armor he would survive, without like this does he will die instanly... a better way todo this is changing packages send to players, rather advanced but better if you want a good plugin and fair gameplay.
     
  7. Offline

    Ultimate_n00b

    *packets
    And it's quite easy with ProtocolLib.
     
  8. Offline

    FatAussieFatBoy

    @bobacadodl

    I will Edit the Code now to match what you have Put in your Post above, Thanks for Pointing out the Flaw I had in my Code :p
     
  9. Offline

    CeramicTitan

    You can only do that with Protocol Lib. You need to intercept the before they reach the server.
     
  10. Offline

    malikdbuseck

    Im getting an error on the .put

    Code:
    package me.malikdbuseck;
    
    import java.util.HashMap;
    
    import org.bukkit.World;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    
    public class PlayerListener extends JavaPlugin implements Listener{
       
         Main plugin;
        
         public PlayerListener(Main plugin) {
          this.plugin = plugin;
         }
        
        public HashMap<String, ItemStack[]> armorsave = new HashMap<String, ItemStack[]>();
        
        @SuppressWarnings("deprecation")
        @EventHandler
         public void onPlayerMove(PlayerMoveEvent event){
          Player p = event.getPlayer();
         
          World w = (World) p.getWorld();
          Block b = w.getBlockAt(p.getLocation().add(0, 0, 0));
               
          if(b.getTypeId() == 175){
          p.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 9999, 2, true));
          armorsave.put(p, p.getInventory().getArmorContents());
          p.getInventory().setArmorContents(null);
          }else{
              event.setCancelled(true);
         
          if(b.getTypeId() != 175){
               p.removePotionEffect(PotionEffectType.INVISIBILITY);
               p.getInventory().setArmorContents(armorsave.get(p));
              }
         }
    }
    
    }
     
  11. Offline

    teej107

  12. Offline

    malikdbuseck

    okay I changed it too
    armorsave.put(p.getName(), p.getInventory().getArmorContents());
    but it dont clear
     
Thread Status:
Not open for further replies.

Share This Page