How to store/read ArrayList to/from the config?

Discussion in 'Plugin Development' started by DarknessXIII, Sep 2, 2012.

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

    DarknessXIII

    Hi.

    I'm making an another plugin for my server - ChestLockAndPick(for locking chests and lockpicking them)

    I made the code for protection, it's working perfectly, but tthe protection removes when the server reloads :( So I want to store ArrayList (with protection location and owner) to a config.yml, but I do not know how :/

    I want to config.yml look like this(if possible):
    Code:
    location : 120,64,120
    owner: DarknessXIII
    
    Here's my code:
    ChestLockAndPick.java
    Code:java
    1.  
    2. package DarknessXIII.ChestLockAndPick;
    3.  
    4. import java.util.ArrayList;
    5. import java.util.logging.Logger;
    6. import org.bukkit.Location;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class ChestLockAndPick extends JavaPlugin {
    10.  
    11. protected static final Logger log = Logger.getLogger("Minecraft");
    12.  
    13. public ArrayList<Location> chestProtection = new ArrayList<Location>();
    14. public ArrayList<String> chestPlayers = new ArrayList<String>();
    15.  
    16. @Override
    17. public void onEnable() {
    18. getServer().getPluginManager().registerEvents(new ChestListener(this), this);
    19. log.info("[ChestLockAndPick] ChestLockAndPickv0.1 wlaczony!");
    20. }
    21. }
    22.  
    23.  


    ChestListener.java
    Code:java
    1.  
    2. package DarknessXIII.ChestLockAndPick;
    3.  
    4. import org.bukkit.event.Listener;
    5. import org.bukkit.ChatColor;
    6. import org.bukkit.Location;
    7. import org.bukkit.event.EventHandler;
    8. import org.bukkit.Material;
    9. import org.bukkit.event.block.Action;
    10. import org.bukkit.event.block.BlockBreakEvent;
    11. import org.bukkit.event.player.PlayerInteractEvent;
    12.  
    13. public class ChestListener implements Listener {
    14.  
    15. ChestLockAndPick plugin;
    16.  
    17. ChestListener(ChestLockAndPick p){
    18. plugin = p;
    19. }
    20.  
    21. @EventHandler
    22. public void stopBreak(BlockBreakEvent event){
    23. if(plugin.chestProtection.contains(event.getBlock().getLocation()) && event.getBlock().getTypeId() == 54){
    24. event.getPlayer().sendMessage(ChatColor.RED + "Ta skrzynia jest zabezpieczona!");
    25. event.setCancelled(true);
    26. }
    27. }
    28.  
    29. @EventHandler
    30. public void chestProtect(PlayerInteractEvent event){
    31. Location l = event.getClickedBlock().getLocation();
    32. String playerName = event.getPlayer().getName().toString();
    33. if(event.getPlayer().getItemInHand().getType() == Material.LEATHER && event.getAction() == Action.LEFT_CLICK_BLOCK && event.getClickedBlock().getTypeId() == 54 && event.getPlayer().isSneaking()){
    34. if(plugin.chestProtection.contains(l) && !plugin.chestPlayers.contains(playerName)){
    35. event.getPlayer().sendMessage(ChatColor.RED + "Ta skrzynia ma juz zabezpieczenie!");
    36. }else{
    37. plugin.chestProtection.add(l);
    38. plugin.chestPlayers.add(playerName);
    39. event.getPlayer().sendMessage(ChatColor.GREEN + "Skrzynia zabezpieczona pomyslnie!");
    40. event.getPlayer().getItemInHand().setAmount((event.getPlayer().getItemInHand().getAmount()) - 1);
    41. }
    42. }
    43. if(event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getClickedBlock().getTypeId() == 54){
    44. if(plugin.chestProtection.contains(l) && plugin.chestPlayers.contains(playerName)){
    45. event.setCancelled(false);
    46. }else if(!plugin.chestProtection.contains(l) && !plugin.chestPlayers.contains(playerName)){
    47. event.setCancelled(false);
    48. }else{
    49. event.getPlayer().sendMessage(ChatColor.RED + "Ta skrzynia jest zabezpieczona!");
    50. event.setCancelled(true);
    51. }
    52. }
    53. }
    54.  

    Thanks in advance.
     
  2. Offline

    calebbfmv

    Your welcome in advance!
    Check out LockPick on BukkitDev, it does what you need!
     
  3. Offline

    DarknessXIII

    Soryy, I'm not going to use your plugin, because I don't want to use LWC or Lockette :C
     
  4. Offline

    calebbfmv

    It's compatible with all protection plugins :D
     
  5. Offline

    DarknessXIII

    Yep, but I'm making my own protection plugin, because I want to make 5 tiers of protection :F

    #Edit:
    Anyone knows the answer for my question? (storing and reading ArrayList in config.yml)
     
  6. Offline

    calebbfmv

    Yeah Let me get it for you, it isnt that hard really!

    EDIT:
    I am noticing a lack of HashSets<String> in your plugin. Do you intend to allow other people to open chest if the owner does a command? Or just only the player who placed the chest, can open it?

    Code:
    ArrayList<String> a = new ArrayList<String>();
    
        @Override
        public void onEnable() {
            log.info("[Plugin] is now enabled!");
            a.add("your text");
            a.add("your text");
            a.add("your text");
            a.add("your text");
            FileConfiguration cfg = getConfig();
            cfg.addDefault"PathTo.Config", a);
    
    Thats all you need! :D Have fun!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
Thread Status:
Not open for further replies.

Share This Page