Advertisement
captainawesome7

SimpleGive.class

May 29th, 2011
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 11.32 KB | None | 0 0
  1. package me.captain.SimpleGive; // Package
  2. // Java Imports
  3. import java.util.logging.Logger;
  4. // Import Items.java
  5. import me.captain.SimpleGive.Items;
  6. // Bukkit Imports
  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.inventory.ItemStack;
  12. import org.bukkit.inventory.PlayerInventory;
  13. import org.bukkit.plugin.java.JavaPlugin;
  14. import org.bukkit.plugin.Plugin;
  15. // Permissions Imports
  16. import com.nijiko.permissions.PermissionHandler;
  17. import com.nijikokun.bukkit.Permissions.Permissions;
  18. /*
  19.  * Before you read this plugin source tutorial/sample, please consider the following:
  20.  * This plugin is super simple, no listeners, and only two class files,
  21.  * This plugin uses onCommand to execute code,
  22.  * This plugin uses a configuration file called Items.txt to load Strings and replace them with item ids
  23.  * When I refer to "item" I mean the string that we send to Items.java
  24.  * Read this class first then Items.java
  25.  * Have a nice day :)
  26.  *
  27.  * Now that you have considered the aforementioned, you may begin :)
  28.  */
  29.  
  30.  
  31.  
  32. //Class
  33. public class SimpleGive extends JavaPlugin {
  34.    
  35.     private boolean UsePermissions; // Create the boolean UsePermissions (will tell wether to use isOP or permissions nodes)
  36.      
  37.     public static PermissionHandler Permissions; // Creates a static PermissionHandler, part of Permissions (you have to do this)
  38.    
  39.     // Make the method setupPermissions, you can copy+paste this :D
  40.     private void setupPermissions() {
  41.         Plugin test = this.getServer().getPluginManager().getPlugin("Permissions");
  42.         if (this.Permissions == null) {
  43.             if (test != null) {
  44.                 UsePermissions = true;
  45.                 this.Permissions = ((Permissions) test).getHandler();
  46.                 System.out.println("[SimpleGive] Version 1.2 Permissions system detected!");
  47.             } else {
  48.                 log.info("[SimpleGive] Version 1.2 Permissions system not detected, defaulting to OP");
  49.                 UsePermissions = false;
  50.             }
  51.         }
  52.     }
  53.    
  54.     // This is how you create a permissions boolean, it returns false if they don't have the node :)
  55.     // You can also copy+paste this :D
  56.     public boolean canUseGive(Player p) {
  57.         if (UsePermissions) {
  58.             return this.Permissions.has(p, "simplegive.give"); //checking to see if they have simpleadmin.give
  59.         }
  60.         return p.isOp(); //returns isOp() if UsePermissions is false
  61.     }
  62.    
  63.     public boolean canUseAGive(Player p, Integer id) {
  64.         if (UsePermissions) {
  65.             return this.Permissions.has(p, "simplegive.give." + id); //checking to see if they have simpleadmin.give
  66.         }
  67.         return p.isOp(); //returns isOp() if UsePermissions is false
  68.     }
  69.    
  70.     // Same as above, but for simplegive.item
  71.     public boolean canUseI(Player p) {
  72.         if (UsePermissions) {
  73.             return this.Permissions.has(p, "simplegive.item");
  74.         }
  75.         return p.isOp();
  76.     }
  77.    
  78.     public boolean canUseAnI(Player p, Integer id) {
  79.         if (UsePermissions) {
  80.             return this.Permissions.has(p, "simplegive.item." + id);
  81.         }
  82.         return p.isOp();
  83.     }
  84.    
  85.  
  86.    
  87.     // Get the logger, this is how we send messages to the console
  88.     private static final Logger log = Logger.getLogger("Minecraft");
  89.    
  90.     // Instancey stuff, idk why you have to do this, but you do ... copy+paste this if you like :D
  91.     private static SimpleGive instance;
  92.     public static SimpleGive getInstance()
  93.     {
  94.         return instance;
  95.     }
  96.  
  97.     // This is necessary, it is what you tell your plugin to do when the server starts/the plugin is enabled
  98.     public void onEnable() {
  99.         setupPermissions();// Run the method above, necessary if you are using permissions
  100.         Items.getItem("Random");// Runs the getItem(String item) method in Items.java, so that if the Items.txt doesn't exist it will be created, not necessary, but a good idea
  101.         log.info("[SimpleGive] SimpleGive v1.2 by captainawesome7 has been enabled!");// Sends this to the console, basically a startup message
  102.     }
  103.    
  104.     public Boolean givecm = Config.getMsg("GiveConfirmMessage");
  105.  
  106.     public Boolean icm = Config.getMsg("ItemConfigMessage");
  107.    
  108.     // This is the serious shit! onCommand!!
  109.     public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
  110.         if(commandLabel.equalsIgnoreCase("give")){ // If the command is /give, it will do the following:
  111.             if(!(sender instanceof Player)) {
  112.                 String gname = args[0];//Make the first arguments (/give firstargs) a string
  113.                
  114.                 Player givee = this.getServer().getPlayer(gname);//Get the player from the string, used to specify a player in the command
  115.                
  116.                 String ggname = givee.getDisplayName();//Gets the player's display name, only because 2 of my plugins have setDisplayName() stuff, so this is just an added bonus
  117.                
  118.                 PlayerInventory pinv = givee.getInventory();//Gets the player's inventory, necessary if we want to edit it
  119.                
  120.                 String itemid = args[1];//Sets the second arguments to a string
  121.  
  122.                                
  123.                 Integer id = Integer.parseInt(Items.getItem(itemid));//This is a little complicated, refer to the below:
  124.                 //This basically uses the second arguments and runs it through getItem from Items.java, and returns an ID, which it then parses into an Integer
  125.  
  126.                
  127.                 ItemStack is = new ItemStack(0, 0);//Makes a new itemstack, this is how we add things to the inventory
  128.                
  129.                 is.setTypeId(id);//Sets the ID of the itemstack to whatever was specified earlier
  130.                
  131.                 String itemName = is.getType().name().toLowerCase().replace('_', ' ');//This is used to send the player a message saying "Enjoy the new log" or whatever
  132.                
  133.                 //The following is run if there are more than 2 arguments:
  134.                 if(args.length > 2 ) {
  135.                
  136.                     Integer am = Integer.parseInt(args[2]);//Parses the third arg (string) into an integer
  137.                    
  138.                     is.setAmount(am);//Sets the amount of the item in the itemstack to the third argument
  139.                    
  140.                 } else {
  141.                    
  142.                     is.setAmount(64);//If no Item amount was specified, sets it to 64
  143.                 }
  144.                
  145.                 //The following is run if there are more than 3 arguments:
  146.                 if(args.length > 3 ) {
  147.                    
  148.                     String dmg = args[3];//Sets the 4 arguments to a string
  149.                    
  150.                     is.setDurability(Short.parseShort(dmg));//Parses the string into a short (the format durability uses), and sets the durability of the itemstack to the 4 arguments
  151.                    
  152.                 }
  153.                
  154.                 pinv.addItem(is);//Finally adds the itemstack to the player's inventory
  155.                
  156.                
  157.                 log.info("[SimpleGive] " + ggname + " has received the " + itemName + "(s)");
  158.                     givee.sendMessage(ChatColor.BLUE + "Enjoy your new " + itemName + "(s)");//Sends the player that is receiving items a message, uses the name from earlier
  159.                 return true;
  160.             } else {
  161.             if (canUseGive((Player) sender)) {
  162.                 Player player = (Player) sender;
  163.                 try {
  164.                
  165.                 String gname = args[0];//Make the first arguments (/give firstargs) a string
  166.                
  167.                 Player givee = this.getServer().getPlayer(gname);//Get the player from the string, used to specify a player in the command
  168.                
  169.                 String ggname = givee.getDisplayName();//Gets the player's display name, only because 2 of my plugins have setDisplayName() stuff, so this is just an added bonus
  170.                
  171.                 PlayerInventory pinv = givee.getInventory();//Gets the player's inventory, necessary if we want to edit it
  172.                
  173.                 String itemid = args[1];//Sets the second arguments to a string
  174.  
  175.                                
  176.                 Integer id = Integer.parseInt(Items.getItem(itemid));//This is a little complicated, refer to the below:
  177.                 //This basically uses the second arguments and runs it through getItem from Items.java, and returns an ID, which it then parses into an Integer
  178.                
  179.                 if(!canUseAGive(player, id)) {
  180.                     player.sendMessage(ChatColor.RED + "You aren't allowed to give " + id.toString());
  181.                     return true;
  182.                 }
  183.                
  184.                 ItemStack is = new ItemStack(0, 0);//Makes a new itemstack, this is how we add things to the inventory
  185.                
  186.                 is.setTypeId(id);//Sets the ID of the itemstack to whatever was specified earlier
  187.                
  188.                 String itemName = is.getType().name().toLowerCase().replace('_', ' ');//This is used to send the player a message saying "Enjoy the new log" or whatever
  189.                
  190.                 //The following is run if there are more than 2 arguments:
  191.                 if(args.length > 2 ) {
  192.                
  193.                     Integer am = Integer.parseInt(args[2]);//Parses the third arg (string) into an integer
  194.                    
  195.                     is.setAmount(am);//Sets the amount of the item in the itemstack to the third argument
  196.                    
  197.                 } else {
  198.                    
  199.                     is.setAmount(64);//If no Item amount was specified, sets it to 64
  200.                 }
  201.                
  202.                 //The following is run if there are more than 3 arguments:
  203.                 if(args.length > 3 ) {
  204.                    
  205.                     String dmg = args[3];//Sets the 4 arguments to a string
  206.                    
  207.                     is.setDurability(Short.parseShort(dmg));//Parses the string into a short (the format durability uses), and sets the durability of the itemstack to the 4 arguments
  208.                    
  209.                 }
  210.                
  211.                 pinv.addItem(is);//Finally adds the itemstack to the player's inventory
  212.                
  213.  
  214.                     givee.sendMessage(ChatColor.BLUE + "Enjoy your new " + itemName);//Sends the player that is receiving items a message, uses the name from earlier
  215.  
  216.                 player.sendMessage(ChatColor.RED + ggname + ChatColor.BLUE + " Has received the " + itemName);//Sends the command sender a message, uses the name from earlier
  217.                 } catch (NullPointerException e) {
  218.                     player.sendMessage(ChatColor.RED + "That Item Doesn't Exist!");
  219.                 }
  220.                 } else {
  221.                
  222.                 Player s = (Player) sender;//sets the sender to s
  223.                
  224.                 s.sendMessage(ChatColor.RED + "You don't have permisson to: " + ChatColor.AQUA + "Give Items!");//Shows this if the player doesn't have simpleadmin.give node
  225.             }
  226.                 return true;//onCommand must return true or false
  227.             }
  228.         }
  229.        
  230.         // All of the below is the same as above but without the player argument. Basically exactly the same :)
  231.         if(commandLabel.equalsIgnoreCase("i")){
  232.             if (canUseI((Player) sender)) {
  233.                 Player player = (Player) sender;
  234.                 try {
  235.                 PlayerInventory pinv = player.getInventory();
  236.                
  237.                 String itemid = args[0].replace("_", "");
  238.                
  239.                 Integer id = Integer.parseInt(Items.getItem(itemid));
  240.                
  241.                 if(!canUseAnI(player, id)) {
  242.                     player.sendMessage(ChatColor.RED+ "You aren't allowed to give yourself a " + id.toString());
  243.                     return true;
  244.                 }
  245.                
  246.                 ItemStack is = new ItemStack(0, 0);
  247.                
  248.                 is.setTypeId(id);
  249.                
  250.                 String itemName = is.getType().name().toLowerCase().replace('_', ' ');
  251.                
  252.                 if(args.length > 1 ) {
  253.                     Integer am = Integer.parseInt(args[1]);
  254.                     is.setAmount(am);
  255.                 } else {
  256.                     is.setAmount(64);
  257.                 }
  258.                 if(args.length > 2 ) {
  259.                     String dmg = args[2];
  260.                    
  261.                     is.setDurability(Short.parseShort(dmg));
  262.                    
  263.                 }
  264.                 pinv.addItem(is);
  265.                
  266.                 player.sendMessage(ChatColor.BLUE + "Enjoy your new " + itemName);
  267.                 } catch(NullPointerException e) {
  268.                     player.sendMessage(ChatColor.RED + "That Item Doesn't Exist!");
  269.                 }
  270.                 } else {
  271.                     Player s = (Player) sender;
  272.                     s.sendMessage(ChatColor.RED + "You don't have permisson to: " + ChatColor.AQUA + "Itemize!");
  273.                 }
  274.                 return true;
  275.         }
  276.             return false;
  277.     }
  278.  
  279.     //This is what the plugin does when the server shuts down, or the plugin is disabled
  280.     public void onDisable() {
  281.         log.info("[SimpleGive] SimpleGive v1.2 has been disabled!");
  282.     }
  283.    
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement