Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.captain.SimpleGive; // Package
- // Java Imports
- import java.util.logging.Logger;
- // Import Items.java
- import me.captain.SimpleGive.Items;
- // Bukkit Imports
- import org.bukkit.ChatColor;
- import org.bukkit.command.Command;
- import org.bukkit.command.CommandSender;
- import org.bukkit.entity.Player;
- import org.bukkit.inventory.ItemStack;
- import org.bukkit.inventory.PlayerInventory;
- import org.bukkit.plugin.java.JavaPlugin;
- import org.bukkit.plugin.Plugin;
- // Permissions Imports
- import com.nijiko.permissions.PermissionHandler;
- import com.nijikokun.bukkit.Permissions.Permissions;
- /*
- * Before you read this plugin source tutorial/sample, please consider the following:
- * This plugin is super simple, no listeners, and only two class files,
- * This plugin uses onCommand to execute code,
- * This plugin uses a configuration file called Items.txt to load Strings and replace them with item ids
- * When I refer to "item" I mean the string that we send to Items.java
- * Read this class first then Items.java
- * Have a nice day :)
- *
- * Now that you have considered the aforementioned, you may begin :)
- */
- //Class
- public class SimpleGive extends JavaPlugin {
- private boolean UsePermissions; // Create the boolean UsePermissions (will tell wether to use isOP or permissions nodes)
- public static PermissionHandler Permissions; // Creates a static PermissionHandler, part of Permissions (you have to do this)
- // Make the method setupPermissions, you can copy+paste this :D
- private void setupPermissions() {
- Plugin test = this.getServer().getPluginManager().getPlugin("Permissions");
- if (this.Permissions == null) {
- if (test != null) {
- UsePermissions = true;
- this.Permissions = ((Permissions) test).getHandler();
- System.out.println("[SimpleGive] Version 1.2 Permissions system detected!");
- } else {
- log.info("[SimpleGive] Version 1.2 Permissions system not detected, defaulting to OP");
- UsePermissions = false;
- }
- }
- }
- // This is how you create a permissions boolean, it returns false if they don't have the node :)
- // You can also copy+paste this :D
- public boolean canUseGive(Player p) {
- if (UsePermissions) {
- return this.Permissions.has(p, "simplegive.give"); //checking to see if they have simpleadmin.give
- }
- return p.isOp(); //returns isOp() if UsePermissions is false
- }
- public boolean canUseAGive(Player p, Integer id) {
- if (UsePermissions) {
- return this.Permissions.has(p, "simplegive.give." + id); //checking to see if they have simpleadmin.give
- }
- return p.isOp(); //returns isOp() if UsePermissions is false
- }
- // Same as above, but for simplegive.item
- public boolean canUseI(Player p) {
- if (UsePermissions) {
- return this.Permissions.has(p, "simplegive.item");
- }
- return p.isOp();
- }
- public boolean canUseAnI(Player p, Integer id) {
- if (UsePermissions) {
- return this.Permissions.has(p, "simplegive.item." + id);
- }
- return p.isOp();
- }
- // Get the logger, this is how we send messages to the console
- private static final Logger log = Logger.getLogger("Minecraft");
- // Instancey stuff, idk why you have to do this, but you do ... copy+paste this if you like :D
- private static SimpleGive instance;
- public static SimpleGive getInstance()
- {
- return instance;
- }
- // This is necessary, it is what you tell your plugin to do when the server starts/the plugin is enabled
- public void onEnable() {
- setupPermissions();// Run the method above, necessary if you are using permissions
- 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
- log.info("[SimpleGive] SimpleGive v1.2 by captainawesome7 has been enabled!");// Sends this to the console, basically a startup message
- }
- public Boolean givecm = Config.getMsg("GiveConfirmMessage");
- public Boolean icm = Config.getMsg("ItemConfigMessage");
- // This is the serious shit! onCommand!!
- public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
- if(commandLabel.equalsIgnoreCase("give")){ // If the command is /give, it will do the following:
- if(!(sender instanceof Player)) {
- String gname = args[0];//Make the first arguments (/give firstargs) a string
- Player givee = this.getServer().getPlayer(gname);//Get the player from the string, used to specify a player in the command
- 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
- PlayerInventory pinv = givee.getInventory();//Gets the player's inventory, necessary if we want to edit it
- String itemid = args[1];//Sets the second arguments to a string
- Integer id = Integer.parseInt(Items.getItem(itemid));//This is a little complicated, refer to the below:
- //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
- ItemStack is = new ItemStack(0, 0);//Makes a new itemstack, this is how we add things to the inventory
- is.setTypeId(id);//Sets the ID of the itemstack to whatever was specified earlier
- String itemName = is.getType().name().toLowerCase().replace('_', ' ');//This is used to send the player a message saying "Enjoy the new log" or whatever
- //The following is run if there are more than 2 arguments:
- if(args.length > 2 ) {
- Integer am = Integer.parseInt(args[2]);//Parses the third arg (string) into an integer
- is.setAmount(am);//Sets the amount of the item in the itemstack to the third argument
- } else {
- is.setAmount(64);//If no Item amount was specified, sets it to 64
- }
- //The following is run if there are more than 3 arguments:
- if(args.length > 3 ) {
- String dmg = args[3];//Sets the 4 arguments to a string
- 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
- }
- pinv.addItem(is);//Finally adds the itemstack to the player's inventory
- log.info("[SimpleGive] " + ggname + " has received the " + itemName + "(s)");
- givee.sendMessage(ChatColor.BLUE + "Enjoy your new " + itemName + "(s)");//Sends the player that is receiving items a message, uses the name from earlier
- return true;
- } else {
- if (canUseGive((Player) sender)) {
- Player player = (Player) sender;
- try {
- String gname = args[0];//Make the first arguments (/give firstargs) a string
- Player givee = this.getServer().getPlayer(gname);//Get the player from the string, used to specify a player in the command
- 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
- PlayerInventory pinv = givee.getInventory();//Gets the player's inventory, necessary if we want to edit it
- String itemid = args[1];//Sets the second arguments to a string
- Integer id = Integer.parseInt(Items.getItem(itemid));//This is a little complicated, refer to the below:
- //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
- if(!canUseAGive(player, id)) {
- player.sendMessage(ChatColor.RED + "You aren't allowed to give " + id.toString());
- return true;
- }
- ItemStack is = new ItemStack(0, 0);//Makes a new itemstack, this is how we add things to the inventory
- is.setTypeId(id);//Sets the ID of the itemstack to whatever was specified earlier
- String itemName = is.getType().name().toLowerCase().replace('_', ' ');//This is used to send the player a message saying "Enjoy the new log" or whatever
- //The following is run if there are more than 2 arguments:
- if(args.length > 2 ) {
- Integer am = Integer.parseInt(args[2]);//Parses the third arg (string) into an integer
- is.setAmount(am);//Sets the amount of the item in the itemstack to the third argument
- } else {
- is.setAmount(64);//If no Item amount was specified, sets it to 64
- }
- //The following is run if there are more than 3 arguments:
- if(args.length > 3 ) {
- String dmg = args[3];//Sets the 4 arguments to a string
- 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
- }
- pinv.addItem(is);//Finally adds the itemstack to the player's inventory
- givee.sendMessage(ChatColor.BLUE + "Enjoy your new " + itemName);//Sends the player that is receiving items a message, uses the name from earlier
- player.sendMessage(ChatColor.RED + ggname + ChatColor.BLUE + " Has received the " + itemName);//Sends the command sender a message, uses the name from earlier
- } catch (NullPointerException e) {
- player.sendMessage(ChatColor.RED + "That Item Doesn't Exist!");
- }
- } else {
- Player s = (Player) sender;//sets the sender to s
- 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
- }
- return true;//onCommand must return true or false
- }
- }
- // All of the below is the same as above but without the player argument. Basically exactly the same :)
- if(commandLabel.equalsIgnoreCase("i")){
- if (canUseI((Player) sender)) {
- Player player = (Player) sender;
- try {
- PlayerInventory pinv = player.getInventory();
- String itemid = args[0].replace("_", "");
- Integer id = Integer.parseInt(Items.getItem(itemid));
- if(!canUseAnI(player, id)) {
- player.sendMessage(ChatColor.RED+ "You aren't allowed to give yourself a " + id.toString());
- return true;
- }
- ItemStack is = new ItemStack(0, 0);
- is.setTypeId(id);
- String itemName = is.getType().name().toLowerCase().replace('_', ' ');
- if(args.length > 1 ) {
- Integer am = Integer.parseInt(args[1]);
- is.setAmount(am);
- } else {
- is.setAmount(64);
- }
- if(args.length > 2 ) {
- String dmg = args[2];
- is.setDurability(Short.parseShort(dmg));
- }
- pinv.addItem(is);
- player.sendMessage(ChatColor.BLUE + "Enjoy your new " + itemName);
- } catch(NullPointerException e) {
- player.sendMessage(ChatColor.RED + "That Item Doesn't Exist!");
- }
- } else {
- Player s = (Player) sender;
- s.sendMessage(ChatColor.RED + "You don't have permisson to: " + ChatColor.AQUA + "Itemize!");
- }
- return true;
- }
- return false;
- }
- //This is what the plugin does when the server shuts down, or the plugin is disabled
- public void onDisable() {
- log.info("[SimpleGive] SimpleGive v1.2 has been disabled!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement