Solved Sign reset on reload/stop

Discussion in 'Spigot Plugin Development' started by Chemi, May 7, 2015.

Thread Status:
Not open for further replies.
  1. I made a plugin that shows the url to the player that clicks a special sign.
    I saved the sign location in a hashmap
    Code (Text):
          public final HashMap<Location, String> signs = new HashMap<Location, String>();
    and added the sign location when a player creates the special sign.

    But the problem is - whenever the server stops/reloads the hashmap resets and the sign is no longer working
    I think i could create a file with the loctions but i'm not so good at files xD

    Is there a way to do this without files?
    or if there is no way to do this without files, can you help me with it?
     
  2. You can eg. save it to a yml file and load it when the plugin will be enabled.
     
  3. This ^
     
  4. Old me asked for spoonfeed
     
    #4 Chemi, May 7, 2015
    Last edited: Sep 16, 2016
  5. clip

    Benefactor

    Just convert the location to a string and save it. It is pretty simple stuff.

    getConfig().set("signs."+signIdentifier+".location", loc.getBlockX()+","+loc.getBlockY()+","+loc.getBlockZ()+","+loc.getWorld().getName());

    SaveConfig();

    To load your sign just grab the location string and split it by ','

    String[] parts = getConfig.getString ("signs."+name+".location").split(",");

    Ofc you would need to do a few other things such as make sure the String exists in the config and also validate the location string to make sure each part os an Integer but it isn't hard at all. Id help more but im on my phone.
     
    #5 clip, May 7, 2015
    Last edited: May 7, 2015
  6. I'm not so good at explaining, but I'm sure you can google it and find a tutorial.
    Now I'd do that for you, but I'm at school and Google wont work at my school for whatever reason. :p
    Google something like "bukkit coding config file"
     
  7. Do i put the getConfig.set
    in the onEnable?
     
  8. clip

    Benefactor

    If you don't know the answer to that I highly suggest you find a tutorial on how to create/load/save a configuration file. I will link one when I get off work and get to my pc.
     
  9. Hey, you can't do this without saving your hashmap to a file.

    Do what @extended_clip does. So save the location to a config.yml by setting a , between worldxyz. Then split it and you have the worldxyz and you have your sign back. Try doing this by yourself.
     
  10. Its solved. i did it finally.
     
  11. Sure, I don't want to spoonfeed but if you can tell me what you're trying to do it may help.
    You can save everything into a list with <Location> and then u should add an count int, and every time ur adding a sign, you add 1 to this int.
    and than when you save it into the file you should take the integer and save in the integer the location.
    Code (Text):
    public static void saveLoc(Location loc)
    {
    count++;
    getConfig().set("Signs." + count + ".x", loc.getX());
    getConfig().set("Signs." + count + ".y", loc.getY());
    getConfig().set("Signs." + count + ".z", loc.getZ());
    }
    and onEnable()
    you should set the the count to "Signs.Number" and
    onDisable()
    you should save the count into the config
    getConfig().set("Signs.Number",  count);
    not tested but try to use this
     
    #12 Chemi, Jun 7, 2015
    Last edited: Sep 16, 2016
  12. Please, I'm not going to even attempt to look at this code without putting it in a
    Code (Text):
    Code Box
    Thank You.
     
    • Agree Agree x 1
    • Funny Funny x 1
  13. Im trying to make a elevator plugin, and i posted the code above this post! Please help me, even if its spoon feed, this is blocking 5 other plugins i make!
     
  14. I'm happy to help. But what's your problem here?
     
  15. ........ Sir, you don't know how to spell disable.
    Code (Text):
    public void onDisble(){
    That's what you wrote.
    Code (Text):
    public void onDisable(){
    That's what it should be. Tell me if it doesn't work, because that's a big error.
     
  16. So simply, my problem is that the hashmap <Location, String> should be saved on reload/disable and loaded on enable. So lets say i put down the sign using "[Lift]" on line 1 and "up" on line 2, the lift is created (This works already) and then after a reload the sign doesent work anymore! I need this fixed!

    My listener code is (I removed some copy and pasting from hashmaps called up1, up 2 & up 3, witch almost do the same as up):
    Code (Text):
    package com.Vision.Lift;

    import java.util.HashMap;

    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    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.block.SignChangeEvent;
    import org.bukkit.event.player.PlayerInteractEvent;

    public class AListener implements Listener {
       
        public final static HashMap<Location, String> up = new HashMap<Location, String>();
        public final static HashMap<Location, String> down = new HashMap<Location, String>();
        public final static HashMap<Location, String> up1 = new HashMap<Location, String>();
        public final static HashMap<Location, String> up2 = new HashMap<Location, String>();
        public final static HashMap<Location, String> up3 = new HashMap<Location, String>();
        public final static HashMap<Location, String> down1 = new HashMap<Location, String>();
        public final static HashMap<Location, String> down2 = new HashMap<Location, String>();
        public final static HashMap<Location, String> down3 = new HashMap<Location, String>();
       
        public main plugin;
        public AListener(main plugin){
            this.plugin = plugin;
        }
       
        @EventHandler
        public void SignChange(SignChangeEvent e){
            if(e.getLine(0).equalsIgnoreCase("[lift]") && (e.getLine(1).equalsIgnoreCase("up")) && e.getPlayer().hasPermission("Lift.make")){
                up.put(e.getBlock().getLocation(), e.getPlayer().getName());      
                e.getPlayer().sendMessage(ChatColor.GOLD + "Lift sign gemaakt!");
                e.setLine(0, ChatColor.BLACK + "[" + ChatColor.WHITE + "Lift" + ChatColor.BLACK + "]" );
                e.setLine(1, ChatColor.BOLD + "UP");
                plugin.getConfig().set("liftup", up);
                plugin.saveConfig();
            }
            if(e.getLine(0).equalsIgnoreCase("[lift]") && (e.getLine(1).equalsIgnoreCase("down"))){
                down.put(e.getBlock().getLocation(), e.getPlayer().getName());
                e.getPlayer().sendMessage(ChatColor.GOLD + "Lift sign gemaakt!");
                e.setLine(0, ChatColor.BLACK + "[" + ChatColor.WHITE + "Lift" + ChatColor.BLACK + "]" );
                e.setLine(1, ChatColor.BOLD + "DOWN");
                plugin.getConfig().set("liftdown", down);
                plugin.saveConfig();
            }
        }

        @EventHandler
        public void BlockBreak(BlockBreakEvent e){
            Player p = e.getPlayer();
            if(up.containsKey(e.getBlock().getLocation()) && !up.containsValue(e.getPlayer()) || !p.isOp() || !p.hasPermission("lift.signbreak")){
                e.setCancelled(true);
            }else{
                up.remove(e.getBlock().getLocation(), e.getPlayer().getName());
            }
            if(down.containsKey(e.getBlock().getLocation()) && !down.containsValue(e.getPlayer()) || !p.isOp() || !p.hasPermission("lift.signbreak")){
                e.setCancelled(true);
            }else{
                down.remove(e.getBlock().getLocation(), e.getPlayer().getName());
            }
        }
       
        @EventHandler
        public void Interact(PlayerInteractEvent e){
            Player p = e.getPlayer();
           
            if(up.containsKey(e.getClickedBlock().getLocation())){
                p.sendMessage(ChatColor.AQUA + "Je bent 1 verdieping omhoog getpt!");
                Location y = p.getLocation().add(0, +6, 0);
                p.teleport(y);
               
            }
            if(down.containsKey(e.getClickedBlock().getLocation())){
                p.sendMessage(ChatColor.AQUA + "Je bent 1 verdieping omlaag getpt!");
                Location y = p.getLocation().add(0, -6, 0);
                p.teleport(y);
               
            }  
        }
    }
     
    This is THE SAME CODE without any removes:
    Code (Text):
    package com.Vision.Lift;

    import java.util.HashMap;

    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    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.block.SignChangeEvent;
    import org.bukkit.event.player.PlayerInteractEvent;

    public class AListener implements Listener {
       
        public final static HashMap<Location, String> up = new HashMap<Location, String>();
        public final static HashMap<Location, String> down = new HashMap<Location, String>();
        public final static HashMap<Location, String> up1 = new HashMap<Location, String>();
        public final static HashMap<Location, String> up2 = new HashMap<Location, String>();
        public final static HashMap<Location, String> up3 = new HashMap<Location, String>();
        public final static HashMap<Location, String> down1 = new HashMap<Location, String>();
        public final static HashMap<Location, String> down2 = new HashMap<Location, String>();
        public final static HashMap<Location, String> down3 = new HashMap<Location, String>();
       
        public main plugin;
        public AListener(main plugin){
            this.plugin = plugin;
        }
       
        @EventHandler
        public void SignChange(SignChangeEvent e){
            if(e.getLine(0).equalsIgnoreCase("[lift]") && (e.getLine(1).equalsIgnoreCase("up")) && e.getPlayer().hasPermission("Lift.make")){
                up.put(e.getBlock().getLocation(), e.getPlayer().getName());      
                e.getPlayer().sendMessage(ChatColor.GOLD + "Lift sign gemaakt!");
                e.setLine(0, ChatColor.BLACK + "[" + ChatColor.WHITE + "Lift" + ChatColor.BLACK + "]" );
                e.setLine(1, ChatColor.BOLD + "UP");
                plugin.getConfig().set("liftup", up);
                plugin.saveConfig();
            }
            if(e.getLine(0).equalsIgnoreCase("[lift]") && (e.getLine(1).equalsIgnoreCase("down"))){
                down.put(e.getBlock().getLocation(), e.getPlayer().getName());
                e.getPlayer().sendMessage(ChatColor.GOLD + "Lift sign gemaakt!");
                e.setLine(0, ChatColor.BLACK + "[" + ChatColor.WHITE + "Lift" + ChatColor.BLACK + "]" );
                e.setLine(1, ChatColor.BOLD + "DOWN");
                plugin.getConfig().set("liftdown", down);
                plugin.saveConfig();
            }
            if(e.getLine(0).equalsIgnoreCase("[lift]") && (e.getLine(1).equalsIgnoreCase("up bestuur")) && e.getPlayer().hasPermission("Lift.make")){
                up1.put(e.getBlock().getLocation(), e.getPlayer().getName());      
                e.getPlayer().sendMessage(ChatColor.GOLD + "Lift sign gemaakt!");
                e.setLine(0, ChatColor.BLACK + "[" + ChatColor.WHITE + "Lift" + ChatColor.BLACK + "]" );
                e.setLine(1, ChatColor.BOLD + "UP");
                e.setLine(2, "Overig Stadsbestuur");
                plugin.getConfig().set("liftup", up1);
                plugin.saveConfig();
            }
            if(e.getLine(0).equalsIgnoreCase("[lift]") && (e.getLine(1).equalsIgnoreCase("up CEO")) && e.getPlayer().hasPermission("Lift.make")){
                up2.put(e.getBlock().getLocation(), e.getPlayer().getName());      
                e.getPlayer().sendMessage(ChatColor.GOLD + "Lift sign gemaakt!");
                e.setLine(0, ChatColor.BLACK + "[" + ChatColor.WHITE + "Lift" + ChatColor.BLACK + "]" );
                e.setLine(1, ChatColor.BOLD + "UP");
                e.setLine(2, "CEO &");
                e.setLine(3, "Directeur");
                plugin.getConfig().set("liftup", up2);
                plugin.saveConfig();
            }
            if(e.getLine(0).equalsIgnoreCase("[lift]") && (e.getLine(1).equalsIgnoreCase("up President")) && e.getPlayer().hasPermission("Lift.make")){
                up3.put(e.getBlock().getLocation(), e.getPlayer().getName());      
                e.getPlayer().sendMessage(ChatColor.GOLD + "Lift sign gemaakt!");
                e.setLine(0, ChatColor.BLACK + "[" + ChatColor.WHITE + "Lift" + ChatColor.BLACK + "]" );
                e.setLine(1, ChatColor.BOLD + "UP");
                e.setLine(2, "President &");
                e.setLine(3, "Burgemeester");
                plugin.getConfig().set("liftup", up3);
                plugin.saveConfig();
            }
            if(e.getLine(0).equalsIgnoreCase("[lift]") && (e.getLine(1).equalsIgnoreCase("down bestuur")) && e.getPlayer().hasPermission("Lift.make")){
                down1.put(e.getBlock().getLocation(), e.getPlayer().getName());      
                e.getPlayer().sendMessage(ChatColor.GOLD + "Lift sign gemaakt!");
                e.setLine(0, ChatColor.BLACK + "[" + ChatColor.WHITE + "Lift" + ChatColor.BLACK + "]" );
                e.setLine(1, ChatColor.BOLD + "DOWN");
                e.setLine(2, "Overig Stadsbestuur");
                plugin.getConfig().set("liftup", down1);
                plugin.saveConfig();
            }
            if(e.getLine(0).equalsIgnoreCase("[lift]") && (e.getLine(1).equalsIgnoreCase("down ceo")) && e.getPlayer().hasPermission("Lift.make")){
                down2.put(e.getBlock().getLocation(), e.getPlayer().getName());      
                e.getPlayer().sendMessage(ChatColor.GOLD + "Lift sign gemaakt!");
                e.setLine(0, ChatColor.BLACK + "[" + ChatColor.WHITE + "Lift" + ChatColor.BLACK + "]" );
                e.setLine(1, ChatColor.BOLD + "DOWN");
                e.setLine(2, "CEO &");
                e.setLine(3, "Directeur");
                plugin.getConfig().set("liftup", down2);
                plugin.saveConfig();
            }
            if(e.getLine(0).equalsIgnoreCase("[lift]") && (e.getLine(1).equalsIgnoreCase("down president")) && (e.getLine(3).equalsIgnoreCase("President")) && e.getPlayer().hasPermission("Lift.make")){
                down3.put(e.getBlock().getLocation(), e.getPlayer().getName());      
                e.getPlayer().sendMessage(ChatColor.GOLD + "Lift sign gemaakt!");
                e.setLine(0, ChatColor.BLACK + "[" + ChatColor.WHITE + "Lift" + ChatColor.BLACK + "]" );
                e.setLine(1, ChatColor.BOLD + "DOWN");
                e.setLine(2, "President &");
                e.setLine(3, "Burgemeester");
                plugin.getConfig().set("liftup", down3);
                plugin.saveConfig();
            }
        }

        @EventHandler
        public void BlockBreak(BlockBreakEvent e){
            Player p = e.getPlayer();
            if(up.containsKey(e.getBlock().getLocation()) && !up.containsValue(e.getPlayer()) || !p.isOp() || !p.hasPermission("lift.signbreak")){
                e.setCancelled(true);
            }else{
                up.remove(e.getBlock().getLocation(), e.getPlayer().getName());
            }
            if(up1.containsKey(e.getBlock().getLocation()) && !up1.containsValue(e.getPlayer()) || !p.isOp() || !p.hasPermission("lift.signbreak")){
                e.setCancelled(true);
            }else{
                up1.remove(e.getBlock().getLocation(), e.getPlayer().getName());
            }
            if(up2.containsKey(e.getBlock().getLocation()) && !up2.containsValue(e.getPlayer()) || !p.isOp() || !p.hasPermission("lift.signbreak")){
                e.setCancelled(true);
            }else{
                up2.remove(e.getBlock().getLocation(), e.getPlayer().getName());
            }
            if(up3.containsKey(e.getBlock().getLocation()) && !up3.containsValue(e.getPlayer()) || !p.isOp() || !p.hasPermission("lift.signbreak")){
                e.setCancelled(true);
            }else{
                up3.remove(e.getBlock().getLocation(), e.getPlayer().getName());
            }
            if(down.containsKey(e.getBlock().getLocation()) && !down.containsValue(e.getPlayer()) || !p.isOp() || !p.hasPermission("lift.signbreak")){
                e.setCancelled(true);
            }else{
                down.remove(e.getBlock().getLocation(), e.getPlayer().getName());
            }
            if(down1.containsKey(e.getBlock().getLocation()) && !down1.containsValue(e.getPlayer()) || !p.isOp() || !p.hasPermission("lift.signbreak")){
                e.setCancelled(true);
            }else{
                down1.remove(e.getBlock().getLocation(), e.getPlayer().getName());
            }
            if(down2.containsKey(e.getBlock().getLocation()) && !down2.containsValue(e.getPlayer()) || !p.isOp() || !p.hasPermission("lift.signbreak")){
                e.setCancelled(true);
            }else{
                down2.remove(e.getBlock().getLocation(), e.getPlayer().getName());
            }
            if(down3.containsKey(e.getBlock().getLocation()) && !down3.containsValue(e.getPlayer()) || !p.isOp() || !p.hasPermission("lift.signbreak")){
                e.setCancelled(true);
            }else{
                down3.remove(e.getBlock().getLocation(), e.getPlayer().getName());
            }
        }
       
        @EventHandler
        public void Interact(PlayerInteractEvent e){
            Player p = e.getPlayer();
           
            if(up.containsKey(e.getClickedBlock().getLocation())){
                p.sendMessage(ChatColor.AQUA + "Je bent 1 verdieping omhoog getpt!");
                Location y = p.getLocation().add(0, +6, 0);
                p.teleport(y);
               
            }
            if(up1.containsKey(e.getClickedBlock().getLocation())){
                p.sendMessage(ChatColor.AQUA + "Je bent 1 verdieping omhoog getpt!");
                Location y = p.getLocation().add(0, +8, 0);
                p.teleport(y);
               
            }
            if(up2.containsKey(e.getClickedBlock().getLocation())){
                p.sendMessage(ChatColor.AQUA + "Je bent 1 verdieping omhoog getpt!");
                Location y = p.getLocation().add(0, +18, 0);
                p.teleport(y);
               
            }
            if(up3.containsKey(e.getClickedBlock().getLocation())){
                p.sendMessage(ChatColor.AQUA + "Je bent 1 verdieping omhoog getpt!");
                Location y = p.getLocation().add(0, +29, 0);
                p.teleport(y);
               
            }
            if(down.containsKey(e.getClickedBlock().getLocation())){
                p.sendMessage(ChatColor.AQUA + "Je bent 1 verdieping omlaag getpt!");
                Location y = p.getLocation().add(0, -6, 0);
                p.teleport(y);
               
            }
            if(down1.containsKey(e.getClickedBlock().getLocation())){
                p.sendMessage(ChatColor.AQUA + "Je bent 1 verdieping omlaag getpt!");
                Location y = p.getLocation().add(0, -8, 0);
                p.teleport(y);
               
            }
            if(down2.containsKey(e.getClickedBlock().getLocation())){
                p.sendMessage(ChatColor.AQUA + "Je bent 1 verdieping omlaag getpt!");
                Location y = p.getLocation().add(0, -18, 0);
                p.teleport(y);
               
            }
            if(down3.containsKey(e.getClickedBlock().getLocation())){
                p.sendMessage(ChatColor.AQUA + "Je bent 1 verdieping omlaag getpt!");
                Location y = p.getLocation().add(0, -29, 0);
                p.teleport(y);
               
            }
           
        }
    }
     
    And just in case you need the Main class & Plugin.yml....
    MAIN:
    Code (Text):
    package com.Vision.Lift;

    import org.bukkit.Bukkit;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;

    import com.Vision.Lift.AListener;

    public class main extends JavaPlugin {
       
        public void onEnable(){
        PluginManager pm = Bukkit.getServer().getPluginManager();
        pm.registerEvents(new AListener(this), this);
        }
    }
    PLUGIN.YML (Descriptions are dutch, sorry!)
    Code (Text):
    name: CustomLift
    version: 1.0
    main: com.Vision.Lift.main
    description: Lift plugin voor NLS!

    permissions:
        lift.signbreak:
            description: Deze permissie geeft je toegang tot het breken van een lift sign!
            default: OP
        lift.make:
            description: Deze permissie geeft je toegang tot het breken van een lift sign!
            default: OP
    EXTRA INFO:
    Config.yml saves a sign like this:

    Code (Text):
    liftdown:
      Location{world=CraftWorld{name=world},x=-1105:
        0,y=60:
          0,z=-479:
            0,pitch=0:
              0,yaw=0:
                0}: Vision_SkipKip
    liftup:
      ? ==: org.bukkit.Location
        yaw: 0.0
        pitch: 0.0
        z: -477.0
        y: 60.0
        world: world
        x: -1111.0
      : Vision_SkipKip
     
    But on reload it STILL doesent reload!

    I HOPE I PROVIDED ENOUGH INFORMATION ON EVERYTHING!
    Thanks for all the help !
     
  17. Did you read anything I just told you? Your onDisable doesn't work. That might not be the issue but can you at least tell me? I'm really sorry for sounding like an asshole.
     
  18. Please take a look at the code i posted above here (WITHOUT EDITS)! I removed the onDisable(); in an atempt to fix it. No succes tho!
     
  19. Okay looking at it.
     
Thread Status:
Not open for further replies.