Solved Need help with saving in config

Discussion in 'Plugin Development' started by Urag, Jul 23, 2017.

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

    Urag

    Error while rightclick:
    Show Spoiler
    Code:
    [Server thread/ERROR]: Could not pass event PlayerInteractEvent to RestoringBlocks v1.0
    org.bukkit.event.EventException: null
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:499) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:484) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at org.bukkit.craftbukkit.v1_12_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:234) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.PlayerInteractManager.a(PlayerInteractManager.java:448) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.PlayerConnection.a(PlayerConnection.java:944) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:37) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:1) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_102]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_102]
        at net.minecraft.server.v1_12_R1.SystemUtils.a(SourceFile:46) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:747) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:405) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_102]
    Caused by: java.lang.NullPointerException
        at me.miloszgnk.RestoringBlocks.myEventExecutor.onBlockInteract(myEventExecutor.java:60) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_102]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_102]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_102]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_102]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot-1.12.jar:git-Spigot-7228328-6659ddf]
        ... 17 more


    Main:

    Show Spoiler
    Code:
    public class Main extends JavaPlugin {
       
        public List<Location> locations;
       
        HashMap<Player, Boolean> blockplacingactiv;
        HashMap<Player, Boolean> blockbreakingactiv;
       
        private static Main instance;
       
        public static Main getInstance() {
            return instance;
        }   
       
        public List<Location> getLocations(){
            return locations;
        }
    
       
        private void registerConfig(){
            getConfig().options().copyDefaults(true);
            saveConfig();
            reloadConfig();
        }
       
        public void setRestoring_Blocks_Placing_Activated(Player p, Boolean b){
            blockplacingactiv.put(p, b);
            return;
        }
       
        public Boolean getRestoring_Blocks_Placing_Activated(Player p){
            if (!blockplacingactiv.keySet().contains(p)) {
                blockplacingactiv.put(p, false);
            }
            return blockplacingactiv.get(p);
        }
       
        public void setRestoring_Blocks_Breaking_Activated(Player p, Boolean b){
            blockbreakingactiv.put(p, b);
        }
       
        public Boolean getRestoring_Blocks_Breaking_Activated(Player p){
            if (!blockbreakingactiv.keySet().contains(p)) {
                blockbreakingactiv.put(p, false);
            }
            return blockbreakingactiv.get(p);
        }
    
       
       
        @Override
        public void onEnable(){
            locations = (List<Location>) getConfig().getConfigurationSection("locations");
            blockbreakingactiv = new HashMap<Player, Boolean>();
            blockplacingactiv = new HashMap<Player, Boolean>();
            instance=this;
            getCommand("rb").setExecutor(new myCommandExecutor());
            Bukkit.getServer().getPluginManager().registerEvents(new myEventExecutor(), this);
            registerConfig();
        }
       
        @Override
        public void onDisable(){
           
        }
    
    }


    MyEventExecutor:
    Show Spoiler
    Code:
    public class myEventExecutor implements Listener {
       
        private Main main = Main.getInstance();
       
        List<Location> locations = main.getLocations();
       
       
        @EventHandler
        public void onBlockPlaceEvent(BlockPlaceEvent e){
            if (main.getRestoring_Blocks_Placing_Activated(e.getPlayer())==true){
                if(!(locations.contains(e.getBlock().getLocation()))){
                    locations.add(e.getBlock().getLocation());
                    main.getConfig().set("locations", locations);
                    main.saveConfig();
                    main.reloadConfig();
                }
            }
        }
           
        @EventHandler
        public void onBlockBreakEvent(BlockBreakEvent e){
            if(main.getRestoring_Blocks_Breaking_Activated(e.getPlayer())==true){
                if (locations.contains(e.getBlock().getLocation())){
                locations.remove(e.getBlock().getLocation());
                main.getConfig().set("locations", locations);
                main.saveConfig();
                main.reloadConfig();
                }
            }
            else if(locations.contains(e.getBlock().getLocation())){
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
                    @Override   
                    public void run(){
                        e.getBlock().getState().update(true);
                    }
                }, 60);
            }
        }
    
       
        @EventHandler
        public void onBlockInteract(PlayerInteractEvent e){
            if (e.getPlayer().getInventory().getItemInMainHand().getType() != Material.AIR && e.getPlayer().getInventory().getItemInMainHand().getType() == Material.FEATHER && e.getPlayer().getInventory().getItemInMainHand().getItemMeta().getDisplayName().equals("Restoring Blocks' Editor")){
               
                if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                    if (!(locations.contains(e.getClickedBlock().getLocation()))){
                        locations.add(e.getClickedBlock().getLocation());
                        main.getConfig().set("locations", locations);
                        e.getPlayer().sendMessage("Added block");
                        main.saveConfig();
                        main.reloadConfig();
                    }
                }
                if (e.getAction() == Action.LEFT_CLICK_BLOCK) {
                    if (locations.contains(e.getClickedBlock().getLocation())){
                        locations.remove(e.getClickedBlock().getLocation());
                        main.getConfig().set("locations", locations);
                        e.getPlayer().sendMessage("Removed block");
                        main.saveConfig();
                        main.reloadConfig();
                    }
                }
            }
        }
    
           
    
    }   
    


    What's wrong here?
    Help! :)
     
  2. Offline

    Horsey

    Give us line 60 of MyEventListener
     
  3. Offline

    Urag

    it's
    Code:
                    if (!(locations.contains(e.getClickedBlock().getLocation()))){
    there's a problem, because the config is null and there is not "locations" section, because i don't know how to make it and maybe that's the reason
     
  4. Offline

    Machine Maker

    @Urag
    1. You are using the "==" and "!=" operators to compare materials. Those operators do not check if the value is the same, but if they take up the same memory space. Use the .equals() method to make sure the values are the same

    2. You are also doing the above for the actions. Change the "==" to .equals().

    3. The problem is the locations varable. And probably the main variable as well. The code that sets them isn't getting run. Try initializing them where they are, and setting them in a constructor.
     
    Urag likes this.
  5. Offline

    Urag

    main is working well
    I've changed =='s to .equals(), but what do i have to do with locations? You see my code.
     
  6. Offline

    Machine Maker

    @Urag
    Create a constructor for your class. A constructor is a method that is called when a new instance of the class is created. Now you create a new instance of the class when you register the events and that happens right at the beginning so that's a perfect time to set the variables. Heres what one looks like:

    Code:
    public class EventListener implements Listener {
    
            private List<Location> locations;
    
            public EventListnener() { //This is the constructor
                    locations = main.getLocations();
            }
    }
    Notice the differences between a constructor and a normal method. We use the name of the class to define the constructor.
     
    Last edited: Jul 23, 2017
    Urag likes this.
  7. Offline

    Urag

  8. Offline

    Machine Maker

    @Urag In your onEnable method, can you run a quick if(locations == null) check right after you set it to that configuration section?
     
  9. Offline

    Urag

    upload_2017-7-23_17-50-58.png

    And config.yml in my project contains "locations:"
     
  10. Offline

    Machine Maker

    @Urag Hmm, ok so now we know that's the issue. You'll have to use a different way to get a list of locations out of the configuration. The bukkit Location object has a pretty cool feature that almost no one uses. Theres a method you can call on a location #serialize() which returns a Map<String, Object> which can be set to a path in the configuration.
    Code:
    ...#getConfig().set("location", Location.serialize());
    Then to get the Location out of the config do this
    Code:
    Map<String, Object> locMap = ...#getConfig().getConfigurationSection("location").getValues(true);
    Location loc = Location.deserialize(locMap);
    This will work for Lists of locations as well. But when you are setting the config, you'll need to convert your List<Location> to a List<Map<String, Object>>.

    Code:
     List<Map<String, Object>> locsMap = new ArrayList<Map<String, Object>>();
    for (Location loc : locations) {
        locsMap.add(loc.serialize());
    }
    ...#getConfig().set("location-list", locsMap);
    And to get the Map list

    Code:
    List<Map<String, Object>> locsMap = ...#getConfig().getMapList("locations-list"); //You'll need to convert them to locations
    
    List<Location> locs = new ArrayList<Location>();
    for (Map<String, Object> map : locsMap) {
        locs.add(Location.deserialize(map);
    }
    **Whew that was a lot!
     
    Urag likes this.
  11. Offline

    Urag

    Sorry, but I do not understand. Here it was easier:
    https://bukkit.org/threads/need-hel...locations-in-config-file.458039/#post-3532976

    I have sth like that
    upload_2017-7-23_18-25-41.png
    And what should I replace locations variable with?
     
  12. Offline

    Machine Maker

    @Urag All of this should be in your onEnable() method. It won't get run if it's not.

    The locations variable should be replaces with locs (the new List<Location) but I forgot one step.

    Code:
    List<Map<String, Object>> locsMap2 = main.getConfig().getMapList("locations");
    should be
    Code:
    List<Map<?,?>> //(the same for everything else)
    and
    Code:
    for (Map<String, Object> map : locsMap2) {
        locs.add(map);
    }
    should be
    Code:
    for (Map<?,?> map : locsMap2) {
        locs.add((Map<String, Object>) map);
    }
     
    Urag likes this.
  13. Offline

    Urag

    upload_2017-7-23_18-44-58.png
    How to get locs in another class?
     
  14. Offline

    Machine Maker

    @Urag
    1. You don't need locMap or locatoin or locsMap. That was just an example of how you could do it will one location.

    and
    locs.add((Map<String, Object>) map);
    should be
    locs.add(Locations.deserialize((Map<String, Object>) map);

    And to get locs to another class do the same thing you did for the other things you wanted in different classes
     
    Urag likes this.
  15. Offline

    Urag

    So what just I need to put in Main class? It's so annoying when you dont know what to do ;_;
     
  16. Offline

    Machine Maker

    @Urag
    You should have:
    Code:
    List<Map<?,?>> listLocsMap;
    List<Locations> locations;
    public void onEnable() {
        listLocsMap = getConfig().getMapList("locations");
        locations = new ArrayList<Location>();
        for (Map<?,?> map : listLocsMap) {
            locations.add(Location.deserialize((Map<String, Object>) map);
        }
    }
    
    public List<Location> getLocationList() {
        return locations;
    }
    
    public void setLocationList(List<Location> locations) {
         this.locations = locations
    }
    To add a location to that list write this in any class
    Code:
    List<Location> otherList = main.getLocationList();
    otherList.add(otherLocation);
    main.setLocationList(otherList);
    And to check if a location is in that list,

    Code:
    if (main.getLocationList().contains(otherLocation) {
        //if the list has that location
    }
    @Urag I added a whole bunch more to that post so I tagged you again
     
    Last edited: Jul 23, 2017
    Urag likes this.
  17. Offline

    Urag

  18. Offline

    Machine Maker

    locs should be locations

    And finally put this in your onDisable() method to save all the locations to the config.

    Code:
    public void onDisable() {
        List<Map<String, Object>> tempList = new ArrayList<Map<String, Object>>();
        for (Location loc : locations) {
            tempList.add(loc.serialize());
        }
        getConfig().set("locations", tempList);
     
    Last edited by a moderator: Jul 26, 2017
    Urag likes this.
  19. Offline

    Urag

    upload_2017-7-23_19-7-22.png
     
  20. Offline

    Machine Maker

    @Urag the otherLocation was an example. idk what you are doing with the locations but I wanted to show you how to add a location in case you needed to anywhere
     
    Urag likes this.
  21. Offline

    Urag

    Oh yes, sorry. I was really angry and i wasn't looking what was I doing xd
    How should now look EventListener class? I need to save locations instantly, not only onDisable

    Show Spoiler
    Code:
    package me.miloszgnk.RestoringBlocks;
    
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    
    public class myEventExecutor implements Listener {
       
        private Main main = Main.getInstance();
        List<Location> locations = main.getLocationList();
       
        @EventHandler
        public void onBlockPlaceEvent(BlockPlaceEvent e){
            if (main.getRestoring_Blocks_Placing_Activated(e.getPlayer()).equals(true)){
                if(!(locations.contains(e.getBlock().getLocation()))){
                    locations.add(e.getBlock().getLocation());
                    main.setLocationList(locations);
                }
            }
        }
           
        @EventHandler
        public void onBlockBreakEvent(BlockBreakEvent e){
            if(main.getRestoring_Blocks_Breaking_Activated(e.getPlayer()).equals(true)){
                if (locations.contains(e.getBlock().getLocation())){
                locations.remove(e.getBlock().getLocation());
                main.setLocationList(locations);
    
                }
            }
            else if(locations.contains(e.getBlock().getLocation())){
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
                    @Override   
                    public void run(){
                        e.getBlock().getState().update(true);
                    }
                }, 60);
            }
        }
       
        @EventHandler
        public void onBlockInteract(PlayerInteractEvent e){
            if (e.getPlayer().getInventory().getItemInMainHand().getType() != Material.AIR && e.getPlayer().getInventory().getItemInMainHand().getType() == Material.FEATHER && e.getPlayer().getInventory().getItemInMainHand().getItemMeta().getDisplayName().equals("Restoring Blocks' Editor")){
               
                if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                    if (!(locations.contains(e.getClickedBlock().getLocation()))){
                        locations.add(e.getClickedBlock().getLocation());
                        main.setLocationList(locations);
                        e.getPlayer().sendMessage("Added block");
                    }
                }
                if (e.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
                    if (locations.contains(e.getClickedBlock().getLocation())){
                        locations.remove(e.getClickedBlock().getLocation());
                        e.getPlayer().sendMessage("Removed block");
                    }
                }
            }
        }
    
           
    
    }   
    
     
  22. Offline

    Machine Maker

    @Urag Hmm, is it an issue if your config doesn't update real time? The list of locations will be updated in real time but the config will be updated onDisable() that way the config isnt constantly changing.

    Also, in the Event class I would create a new locations variable in each method instead of one outside all the methods. That way the list will update each time the event is called.
     
    Urag likes this.
  23. Offline

    Urag

    Show Spoiler
    Code:
    package me.miloszgnk.RestoringBlocks;
    
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    
    public class myEventExecutor implements Listener {
      
        private Main main = Main.getInstance();
      
        @EventHandler
        public void onBlockPlaceEvent(BlockPlaceEvent e){
            if (main.getRestoring_Blocks_Placing_Activated(e.getPlayer()).equals(true)){
                List<Location> locations = main.getLocationList();
                if(!(locations.contains(e.getBlock().getLocation()))){
                    locations.add(e.getBlock().getLocation());
                }
            }
        }
          
        @EventHandler
        public void onBlockBreakEvent(BlockBreakEvent e){
            if(main.getRestoring_Blocks_Breaking_Activated(e.getPlayer()).equals(true)){
                List<Location> locations = main.getLocationList();
                if (locations.contains(e.getBlock().getLocation())){
                locations.remove(e.getBlock().getLocation());
    
                }
            }
            else if(main.getLocationList().contains(e.getBlock().getLocation())){
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
                    @Override  
                    public void run(){
                        e.getBlock().getState().update(true);
                    }
                }, 60);
            }
        }
      
        @EventHandler
        public void onBlockInteract(PlayerInteractEvent e){
            if (e.getPlayer().getInventory().getItemInMainHand().getType() != Material.AIR && e.getPlayer().getInventory().getItemInMainHand().getType() == Material.FEATHER && e.getPlayer().getInventory().getItemInMainHand().getItemMeta().getDisplayName().equals("Restoring Blocks' Editor")){
                List<Location> locations = main.getLocationList();
                if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                    if (!(locations.contains(e.getClickedBlock().getLocation()))){
                        locations.add(e.getClickedBlock().getLocation());
                        e.getPlayer().sendMessage("Added block");
                    }
                }
                if (e.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
                    if (locations.contains(e.getClickedBlock().getLocation())){
                        locations.remove(e.getClickedBlock().getLocation());
                        e.getPlayer().sendMessage("Removed block");
                    }
                }
            }
        }
    
          
    
    }  
    


    When i rightclick it sends me "Added block" and it's in a list, but isn't in the config.
    config.yml still doesn't contain "locations" btw
     
  24. Offline

    Machine Maker

    @Urag Like I said, the list will update when you reload the server.
     
  25. Offline

    Urag

    Reload doesn't save locations in config
     
  26. Offline

    Machine Maker

    @Urag Did you put what I said in onDisable() (look a few posts up)?
     
  27. Offline

    Urag

    Yep

    Code:
        public void saveLocations(){
            List<Map<String, Object>> tempList = new ArrayList<Map<String, Object>>();
            for (Location loc : locations) {
                tempList.add(loc.serialize());
            }
            getConfig().set("locations", tempList);
        }
    and

    Code:
        @Override
        public void onDisable(){
            saveLocations();
        }
     
  28. Offline

    Machine Maker

    @Urag I forgot to add the saveConfig(); method you need to call after any change to the config.

    add it after you set "locations" to the list
     
  29. Offline

    Urag

    @X1machinemaker1X
    Ok, works, but what should be improved?
    Main
    Show Spoiler
    Code:
    package me.miloszgnk.RestoringBlocks;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    
    public class Main extends JavaPlugin {
      
        HashMap<Player, Boolean> blockplacingactiv;
        HashMap<Player, Boolean> blockbreakingactiv;
      
        private static Main instance;
      
        public static Main getInstance() {
            return instance;
        }  
    
      
        private void registerConfig(){
            getConfig().options().copyDefaults(true);
            saveConfig();
            reloadConfig();
        }
      
        public void setRestoring_Blocks_Placing_Activated(Player p, Boolean b){
            blockplacingactiv.put(p, b);
            return;
        }
      
        public Boolean getRestoring_Blocks_Placing_Activated(Player p){
            if (!blockplacingactiv.keySet().contains(p)) {
                blockplacingactiv.put(p, false);
            }
            return blockplacingactiv.get(p);
        }
      
        public void setRestoring_Blocks_Breaking_Activated(Player p, Boolean b){
            blockbreakingactiv.put(p, b);
        }
      
        public Boolean getRestoring_Blocks_Breaking_Activated(Player p){
            if (!blockbreakingactiv.keySet().contains(p)) {
                blockbreakingactiv.put(p, false);
            }
            return blockbreakingactiv.get(p);
        }
    
      
        public List<Location> getLocationList() {
            return locations;
        }
      
        public void setLocationList(List<Location> locations) {
             this.locations = locations;
        }
      
        public void saveLocations(){
            List<Map<String, Object>> tempList = new ArrayList<Map<String, Object>>();
            for (Location loc : locations) {
                tempList.add(loc.serialize());
            }
            getConfig().set("locations", tempList);
            saveConfig();
        }
      
        List<Map<?,?>> listLocsMap;
        List<Location> locations;
        @Override
        public void onEnable() {
            listLocsMap = getConfig().getMapList("locations");
            locations = new ArrayList<Location>();
            for (Map<?,?> map : listLocsMap) {
                locations.add(Location.deserialize((Map<String, Object>) map));
            }
            blockbreakingactiv = new HashMap<Player, Boolean>();
            blockplacingactiv = new HashMap<Player, Boolean>();
            instance=this;
            getCommand("rb").setExecutor(new myCommandExecutor());
            Bukkit.getServer().getPluginManager().registerEvents(new myEventExecutor(), this);
            registerConfig();
        }
      
        @Override
        public void onDisable(){
            List<Map<String, Object>> tempList = new ArrayList<Map<String, Object>>();
            for (Location loc : locations) {
                tempList.add(loc.serialize());
            }
            getConfig().set("locations", tempList);
        }
    
    }
    


    myEventExecutor

    Show Spoiler
    Code:
    package me.miloszgnk.RestoringBlocks;
    
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.block.BlockState;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.event.block.BlockPlaceEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    
    public class myEventExecutor implements Listener {
      
        private Main main = Main.getInstance();
      
        @EventHandler
        public void onBlockPlaceEvent(BlockPlaceEvent e){
            if (main.getRestoring_Blocks_Placing_Activated(e.getPlayer()).equals(true)){
                List<Location> locations = main.getLocationList();
                if(!(locations.contains(e.getBlock().getLocation()))){
                    locations.add(e.getBlock().getLocation());
                    main.saveLocations();
                }
            }
        }
          
        @EventHandler
        public void onBlockBreakEvent(BlockBreakEvent e){
            if(main.getRestoring_Blocks_Breaking_Activated(e.getPlayer()).equals(true)){
                List<Location> locations = main.getLocationList();
                if (locations.contains(e.getBlock().getLocation())){
                locations.remove(e.getBlock().getLocation());
                main.saveLocations();
    
                }
            }
            if(main.getLocationList().contains(e.getBlock().getLocation())){
                BlockState b = e.getBlock().getState();
                Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(main, new Runnable() {
                    @Override  
                    public void run(){
                        b.update(true);
                        e.getPlayer().sendMessage("lol");
                    }
                }, 15);
            }
        }
      
        @EventHandler
        public void onBlockInteract(PlayerInteractEvent e){
            if (e.getPlayer().getInventory().getItemInMainHand().getType() != Material.AIR && e.getPlayer().getInventory().getItemInMainHand().getType() == Material.FEATHER && e.getPlayer().getInventory().getItemInMainHand().getItemMeta().getDisplayName().equals("Restoring Blocks' Editor")){
                List<Location> locations = main.getLocationList();
                if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
                    if (!(locations.contains(e.getClickedBlock().getLocation()))){
                        locations.add(e.getClickedBlock().getLocation());
                        e.getPlayer().sendMessage("Added block");
                        main.saveLocations();
                    }
                }
                if (e.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
                    if (locations.contains(e.getClickedBlock().getLocation())){
                        locations.remove(e.getClickedBlock().getLocation());
                        e.getPlayer().sendMessage("Removed block");
                        main.saveLocations();
                    }
                }
            }
        }
    
          
    
    }  
    
     
  30. Offline

    Machine Maker

    @Urag idk, looks good to me. Please mark this thread as solved but if you encounter any more errors pertaining to this issue, please post here.
     
    Urag likes this.
Thread Status:
Not open for further replies.

Share This Page