Stopping Blockplacing from config

Discussion in 'Plugin Development' started by firecombat4, Mar 19, 2012.

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

    firecombat4

    I have

    Code:
    package me.steven.Plugins;
     
    package me.steven.Plugins;
     
    import java.util.ArrayList;
    import java.util.Arrays;
     
    import javax.tools.JavaFileManager.Location;
     
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class BlockProtect extends JavaPlugin {
     
    String[] Break1 = { "Example1", "Example2", "Example3" };
    String[] Place1 = { "Example1", "Example2", "Example3" };
    public ArrayList<Location> blockProtection = new ArrayList<Location>();
     
    public void onEnable(){
    getServer().getPluginManager().registerEvents(new ProtectionListener(), this);
    final FileConfiguration config = this.getConfig();
    config.addDefault("Break", Arrays.asList(Break1));
    config.addDefault("Place", Arrays.asList(Place1));
    config.options().copyDefaults(true);
    saveConfig();
     
    }
     
    }
    
    And

    Code:
    package me.steven.Plugins;
     
    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockPlaceEvent;
     
    public class ProtectionListener implements Listener {
       
        public class ConfigListener implements Listener{
            BlockProtect plugin;
           
            public ConfigListener(BlockProtect instance) {
            plugin = instance;
            }
        }
       
       
        @EventHandler
        public void StopTntBlockBreak(BlockBreakEvent event){
            if(event.getBlock().getTypeId() == 46){
                event.setCancelled(true);
                System.out.println("A player tried to destory tnt");
                event.getPlayer().sendMessage(ChatColor.RED+"You cant destroy that block.");
            }
        }
     
    @EventHandler
    public void StopTntBlockPlace(BlockPlaceEvent event){
        if(event.getBlock().getTypeId() == 46){
            event.setCancelled(true);
            System.out.println("A player tried to place tnt");
            event.getPlayer().sendMessage(ChatColor.RED+"You cant place that block.");
            }
        }
    }
    
    Just wondering how i would make this if(event.getBlock().getTypeId() == 46){ instead of blocking tnt placing, it would read from the list in the config file and block them from being placed, ive looked at a few tuts and have what ive made so far.
    Thanks a lot!
     
  2. Offline

    tobindev

    First make your
    Code:
    String[] Break1 = { "Example1", "Example2", "Example3" };
    String[] Place1 = { "Example1", "Example2", "Example3" };
    
    to
    Code:
          int[] Break1 = { 50, 51, 52 };
          int[] Place1 = { 50, 51, 52 };
    
    because you want to have Arrays and in the end lists which contain Integers not Strings. Then for your config you need a
    Code:
          FileConfiguration config;
    
    I would change in the onEnable part
    Code:
    getServer().getPluginManager().registerEvents(new ProtectionListener(), this);
    
    to
    Code:
        getServer().getPluginManager().registerEvents(new playerListener(this), this);
    
    For the config you should use a try catch block with the following changes:
    Code:
        try {
           
            FileConfiguration config = getConfig();
           
            File file = new File("plugins/<yourPluginName>/config.yml");
           
            if(!file.exists()) {
               
                config.addDefault("Break", Arrays.asList(Break1));
                config.addDefault("Place", Arrays.asList(Place1));
               
                config.options().copyDefaults(true);
               
                saveConfig();
               
            }
           
        } catch(Exception e1) {
           
        e1.printStackTrace();
       
        }
    
    After that I see no onDisable. :D And you could also add a simple System.out.println("Plugin enabled!"); and a System.out.println("Plugin disabled!"); :)


    Then for your Listener:
    Change
    Code:
        public class ConfigListener implements Listener{
            BlockProtect plugin;
         
            public ConfigListener(BlockProtect instance) {
            plugin = instance;
            }
        }
    
    to
    Code:
        private BlockProtect plugin;
        public ProtectionListener(BlockProtect instance) {plugin = instance;}
    
    Now the part to get the things out of the config:
    Code:
        List<?> Break = plugin.getConfig().getList("Break");
        List<?> Place = plugin.getConfig().getList("Place");
    
    I have used List<?> but I'm now not sure if this works fine because I haven't tested the List<?> :D
    Just test it and then say if this works :)
    And now last but not least the checking with the config:
    For
    Code:
            if(event.getBlock().getTypeId() == 46){
    
    use
    Code:
    if(Break.contains(event.getBlock().getTypeId())){
    
    This should for work if still something causes an error just post it here :)
    Do you understand it now? ;)
     
Thread Status:
Not open for further replies.

Share This Page