Easy config for exclusions

Discussion in 'Spigot Plugin Development' started by moocow9m, Jun 10, 2015.

  1. I have this code:
    Code (Text):
    package cf.moostich.www;

    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener {
        FileConfiguration config = getConfig();
        @Override
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            config.addDefault("youAreAwesome", true);
            config.options().copyDefaults(true);
            saveConfig();
        }
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            Player player = event.getPlayer();
            if(player.getName().equalsIgnoreCase("armystich")){
                //code here
            if (config.getBoolean("youAreAwesome")) {
                player.sendMessage("You are awesome!");
                player.sendMessage("I hope we can still be freinds even though I got mad and ran.");
                player.sendMessage("I hope this plugin suits your need as a lag preventer.");
                player.sendMessage("open config and turn youAreAwsome to false to disable this message.");
            } else {
             
            }
            } else {
               
            }
        }

        @SuppressWarnings("deprecation")
        @EventHandler
        public void onBlockBreak(BlockBreakEvent e){
            Player p = e.getPlayer();
            Block b = e.getBlock();
            p.getInventory().addItem(new ItemStack(b.getType(), 1, b.getData()));
            b.setType(Material.AIR);
        }
    }
    and I would like to have an exclusion list to config but cant fiqure out how.
     
  2. Why do you have two empty else statements in PlayerJoinEvent? You don't need an else statement for each if statement.

    In BlockBreakEvent, check if the block is not null straight away to prevent null pointer exceptions.

    Code (Java):
    if (b != null) {
    Is there any reason you're setting the broken block in BlockBreakEvent to air? You haven't cancelled the event, so the block will be broken and nothing will be in its place anyways (Unless another plugin has cancelled the event, such as WorldGuard).

    What do you mean by exclusion list? What do you want to exclude?
     
  3. like spawners and leaves.
    the blocks set to air is to prevent drops
     
  4. Like I said, you haven't cancelled the event, so the block will still be dropped, because BlockBreakEvent is when the block has been fully broken. If you'd like to prevent drops, cancel the event before setting the block to air using e.setCancelled(true);

    To make an exclusion list, create a list of excluded materials inside your configuration file, these won't be added to the inventory:
    Code (Text):
    blacklist:
    - DIRT
    - STONE
    I've implemented the rest of the blacklist stuff into your code, I put in some comments to explain everything:

    Code (Java):
        @SuppressWarnings("deprecation")
        @EventHandler
        public void onBlockBreak(BlockBreakEvent e) {
            Player p = e.getPlayer();
            if (!e.isCancelled()) {
                // Make sure another plugin hasn't already cancelled the block being broken
                if (e.getBlock() != null) {
                    // Check the block isn't null
                    Block b = e.getBlock();
                    e.setCancelled(true);
                    b.setType(Material.AIR);
                    Material material;
                    // Define the blacklisted material
                    for (String blacklist : getConfig().getStringList("blacklist")) {
                        // Iterate over blacklist
                        if (blacklist.contains(":")) {
                            // We must check if the blacklisted item has data, and
                            // define it differently if so
                            material = Material
                                    .getMaterial(blacklist.split(":")[0]);
                        } else {
                            // If it doesn't have data, define it normally
                            material = Material.getMaterial(blacklist);
                        }
                        if (b.getType() == material) {
                            // If the broken block is in our blacklist, stop our
                            // code here using the return;
                            return;
                        }
                    }
                    p.getInventory().addItem(
                            new ItemStack(b.getType(), 1, b.getData()));
                }
            }
        }
     
  5. You can check this page on the Wiki on how to work with Configuration files. As for how to implement that blacklist, Arman has provided a working solution but to improve efficiency and to reduce frequent reading and writing to the disk, I suggest that in your onEnable() method, you should read the config and store the blacklisted materials in a List or Set. This way, the performance increases and the load on your hard drive decreases, especially if this plugin will be used on a large server with a lot of people breaking blocks.
     
  6. fiquerd it out but thanks anways