Advertisement
Guest User

TestClass

a guest
Mar 31st, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. package me.defiancecoding.defiantsecurity;
  2.  
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6.  
  7. import org.bukkit.ChatColor;
  8. import org.bukkit.command.Command;
  9. import org.bukkit.command.CommandSender;
  10. import org.bukkit.entity.Player;
  11. import org.bukkit.event.EventHandler;
  12. import org.bukkit.event.Listener;
  13. import org.bukkit.event.entity.PlayerDeathEvent;
  14. import org.bukkit.inventory.ItemStack;
  15. import org.bukkit.plugin.java.JavaPlugin;
  16.  
  17. public class TestClass extends JavaPlugin implements Listener {
  18.  
  19.     public void onEnable() {
  20.  
  21.     }
  22.  
  23.     public void onDisable() {
  24.         //clear the list once the server crashes to prevent anything nasty
  25.         playerItems.clear();
  26.     }
  27.  
  28.     Map<String, List<ItemStack>> playerItems = new HashMap<String, List<ItemStack>>();
  29.    
  30.     @Override
  31.     public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
  32.  
  33.         if (sender instanceof Player) {
  34.             //casting to a player to get players class
  35.             Player player = (Player) sender;
  36.             if (sender.hasPermission("PermissionsHere")) {
  37.                 //we will make a for loop to scan all the items stored in the players map
  38.                 for (ItemStack items : playerItems.get(player.getUniqueId().toString())) {
  39.                     //and process the item, adding it to the players inventory.
  40.                     player.getInventory().addItem(items);
  41.                     //now the player has gotten the items we can clear the map so they dont take up space
  42.                     playerItems.remove(player.getUniqueId().toString());
  43.                 }
  44.                
  45.             } else {
  46.                 //No PermissionMessage here
  47.             }
  48.            
  49.         } else {
  50.             sender.sendMessage("Sender is console");
  51.         }
  52.         return false;
  53.     }
  54.  
  55.     @EventHandler
  56.     public void clearItemsOnDeath(PlayerDeathEvent e) {
  57.         //cast entity to a player to grab permissions
  58.         Player player = e.getEntity();
  59.         //check if the player has permissions
  60.         if (player.hasPermission("Permission")) {
  61.             //if the player dies and has permission we will now add the players UUID as a string to the map
  62.             //, with their items stored as a list
  63.             playerItems.put(player.getUniqueId().toString(), e.getDrops());
  64.             //now we can clear out the normal players drops
  65.             e.getDrops().clear();
  66.         }
  67.     }
  68.  
  69.     //this method only colors messages such as &3TestText <- will translate &3 to its color printing a colored input message
  70.    
  71.     public String colorMessage(String message) {
  72.         return ChatColor.translateAlternateColorCodes('&', message);
  73.     }
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement