Plugin permissions are turned off by default?

Discussion in 'Plugin Development' started by TomTom1997, Oct 15, 2011.

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

    TomTom1997

    Hello,

    I want to ask you how change this plugin code, because permissions (now) is required to drop, pick block by default. I want to change it as if player have permission he can't drop, pick, but if he haven't permission he can drop, pick. With java editing I encounter first time :), also plugin not mine :D

    Code:

    main file:
    Code:
    package me.trc202.ItemDrop;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.logging.Logger;
    
    import com.nijiko.permissions.PermissionHandler;
    import com.nijikokun.bukkit.Permissions.Permissions;
    
    import org.bukkit.ChatColor;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.command.ConsoleCommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import java.io.*;
    
    public class ItemD extends JavaPlugin {
        private final IDItemListener PlayerListener = new IDItemListener(this);
        private final InvInteract InvListener = new InvInteract(this);
        private static final Logger log = Logger.getLogger("Minecraft");
        public static PermissionHandler Permissions = null;
        List<Integer> blockdisabled = new ArrayList<Integer>(); //Contains all blocks disabled by ItemDrop
        public int IDEnable = 1; // 1 for enabled 0 for disabled
    
        public static PermissionHandler permissionHandler;
        public void onEnable() {
            PluginManager pm = getServer().getPluginManager();
            pm.registerEvent(Event.Type.PLAYER_DROP_ITEM, PlayerListener, Event.Priority.Normal, this);
            pm.registerEvent(Event.Type.PLAYER_INTERACT, InvListener, Event.Priority.Normal, this);
            IDEnable = 1;
            setupPermissions();
            //String myfilelocation = "plugins/ItemDrop/dblocks.txt";
            try
             { //Read from a file
                    // Open the file that is the first
                    // command line parameter
                    FileInputStream fstream = new FileInputStream("plugins/ItemDrop/dblocks.txt");
                    // Get the object of DataInputStream
                    DataInputStream in = new DataInputStream(fstream);
                        BufferedReader br = new BufferedReader(new InputStreamReader(in));
                    String strLine;
                    //Read File Line By Line
                    while ((strLine = br.readLine()) != null)   //read line until it reaches the null character
                    {
                      // Print the content on the console
                        setnewblock(strLine); //Put blocks in list
                    }
                   //Close the input stream
                    in.close();
            }
             catch (Exception e){//Catch exception if any
                 log.info("Error: " + e.getMessage()); //Print error message to console if there is one
                    }
                    String numberblocksloaded = Integer.toString(blockdisabled.size()); //Convert blockdisabled to string for use in blocks loaded
            log.info(getDescription().getName() + " " + getDescription().getVersion()+ " is loaded " + numberblocksloaded + " block(s) loaded"); //Show enable message & list blocks loaded
        }
        public void onDisable()
        {
            log.info("Item drop 1.0 disabled");
            try{ //Write to a file
                // Create file
                FileWriter fstream = new FileWriter("plugins/ItemDrop/dblocks.txt");
                    BufferedWriter out = new BufferedWriter(fstream);
                    out.write("");
                    for (int counter = 0; counter < blockdisabled.size(); counter = counter+1)
                    {
                        String stringwriteout = Integer.toString(blockdisabled.get(counter));//convert to string block number
                        //append it to file
    
                        out.append(stringwriteout);
                        out.append(System.getProperty("line.separator"));
                    }
                //Close the output stream
                out.close();
                }catch (Exception e){//Catch exception if any
                  System.err.println("Error: " + e.getMessage());
                }
            IDEnable = 0;
        }
    
        @SuppressWarnings("static-access") //Supresses a static access warning (unsure of what the warning is)
        private void setupPermissions() {
            Plugin permissionsPlugin = this.getServer().getPluginManager().getPlugin("Permissions");
    
            if (this.permissionHandler == null) {
                if (permissionsPlugin != null) {
                    this.permissionHandler = ((Permissions) permissionsPlugin).getHandler();
                }
                else {
                    log.info("Permission system not detected. Defaulting to OP");
                }
            }
        }
    
    public boolean check(CommandSender sender, String permNode)
    
        {
            if (sender instanceof Player)
            {
                if (Permissions == null)
                {
                    if (sender.isOp()) { return true; }
                    else
                    {
                    return false;
                    }
                }
                else
                {
                    Player player = (Player) sender;
                    return Permissions.has(player, permNode);
                }
            }
            else if (sender instanceof ConsoleCommandSender)
            {
                return false;
            }
            else
            {
                return false;
            }
        }
     
        @Override
        public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args)
        {
            if ((command.getName().equalsIgnoreCase("itemd")) || (command.getName().equalsIgnoreCase("itemdrop"))) //Check to see if the sender sent the itemdrop argument
            {
                if (sender instanceof Player) //Checks to see if sender is a player
                {
                    Player player = (Player) sender;
                    if (check(player, "ItemDrop.add")) //Player has permission to add/remove items
                    {
                        if (args[0].equalsIgnoreCase("add")) //Add block
                        {
                                player.sendMessage(ChatColor.RED + "Item id " + args[1] + " added");
                                setnewblock(args[1]);
                                return true;
    
                        }
                        else if(args[0].equalsIgnoreCase("remove")) //Remove block
                        {
                            removeblock(args[1]);
                            player.sendMessage("Block removed");
                        }
                        else if(args[0].equalsIgnoreCase("list"))
                        {
                            player.sendMessage(ChatColor.RED + "The disabled blocks are: " + blockdisabled.toString());
                            return true;
                        }
                        else //Player did not send the correct argument
                        {
                            return false;
                        }
                    }
                    else //Player has no permission
                    {
                        if(args[0].equalsIgnoreCase("list")) //Allows players who do not have the required permissions to see disabled blocks
                        {
                            player.sendMessage(ChatColor.RED + "The disabled blocks are: " + blockdisabled.toString()); //Sends a message to the player
                            return true; //Report that the command was entered successfully
                        }
    
                        return false; //Report that the command was not entered successfully
                    }
                }
                else //Sender is console
                {
                    if(args[0].equalsIgnoreCase("add"))
                    {
                        setnewblock(args[1]); //add new block id
                        log.info("item " + args[1] + " added");
                        return true;
                    }
                    else if(args[0].equalsIgnoreCase("remove"))
                    {
                        removeblock(args[1]);
                        log.info("Block removed");
                        return true;
                    }
                    if(args[0].equalsIgnoreCase("list"))
                    {
                        log.info("The disabled blocks are: " + blockdisabled.toString());
                        return true;
                    }
                    else
                    {
                        return false;
                    }
    
                }
                return false;
            }
            else //Sender entered the wrong command
            {
                return false;
            }
        }
        public void setnewblock(int blockid) //Accepts int's to add to blockdisabled
        {
            if (!blockdisabled.contains(blockid)) //Only add the element if it does not allready exist in the list
            {
                blockdisabled.add(new Integer(blockid)); //Adds new int to blockdisabled
            }
            return;
        }
        public void setnewblock(String blockid) //Accepts strings and converts it to int and calls setnewitem to place it into list blockdisabled
        {
            int newblockid = Integer.parseInt(blockid); //Converts string into int (needs a way to check for errors)
            setnewblock(newblockid); //calls setnewblock
            return;
    
        }
        public void removeblock(String blockr)
        {
            int removeblockid = Integer.parseInt(blockr);
            if (blockdisabled.contains(removeblockid))
            {
                blockdisabled.remove(blockdisabled.indexOf(removeblockid));
            }
    
        }
    
    }
    Code:
    package me.trc202.ItemDrop;
    import org.bukkit.ChatColor;  //Allows the chat color to be red
    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.event.player.PlayerListener;
    public class IDItemListener extends PlayerListener {
        public static ItemD plugin;
        public IDItemListener(ItemD id){
            plugin = id;
        }
        public void onPlayerDropItem(PlayerDropItemEvent event){ //Overrides the default Item drop event
            Player player = event.getPlayer(); //Creates object player from class Player, Retrieves player who triggered event
            if(plugin.IDEnable == 1)
            {
                    if(plugin.blockdisabled.contains(event.getItemDrop().getItemStack().getTypeId())) //Checks to see if item dropped is bedrock
                    {
                        player.sendMessage(ChatColor.RED + "offending item destroyed"); //Notify's the player
                        event.getItemDrop().remove(); //Removes block upon drop
                    }
                    else
                    {
                        //do nothing
                    }
            }
        }
    }
    Code:
    package me.trc202.ItemDrop;
    import org.bukkit.ChatColor;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerListener;
    public class InvInteract extends PlayerListener {
        public static ItemD plugin;
        public InvInteract(ItemD instance){
            plugin = instance;
        }
        @Override
        public void onPlayerInteract(PlayerInteractEvent event){
            Player player = event.getPlayer();
            if(plugin.IDEnable == 1)
            {
                //player.sendMessage(ChatColor.GREEN + event.getEventName());
                if(event.getAction() == Action.RIGHT_CLICK_BLOCK)  //Checks to see if player right clicked a block, otherwise does nothing
                {
                    Block myblock = event.getClickedBlock();
                    if(myblock.getTypeId() == 54 || myblock.getTypeId() == 61 || myblock.getTypeId() == 62 || myblock.getTypeId() == 23 || myblock.getTypeId() == 58)
                    {
                        //player.sendMessage(ChatColor.RED + "You clicked a chest, furnace, despencer, or craftingtable!");
                        for (int counter = 0; counter < plugin.blockdisabled.size(); counter = counter+1)
                        {
                            if(player.getInventory().contains(plugin.blockdisabled.get(counter)))
                            {
                                player.getInventory().remove(plugin.blockdisabled.get(counter));
                                player.sendMessage(ChatColor.RED + "Offending Item removed");
                            }
                        }
                    }
                }
            }
        }
    }
    
     
  2. You should update that plugin and ditch Permission support, rather use the Bukkit API's permission system.
    Anyway, if you want players not to have some effect when having a certain permissions, then you just negate the player.hasPermission(...) by using !player.hasPermission(...)
     
    r3Fuze likes this.
  3. Offline

    TomTom1997

    As I say its my first plugin edit at all (with java). Thanks :)
     
Thread Status:
Not open for further replies.

Share This Page