[UNSOLVED]Need help whit anti-grief plugin!!!

Discussion in 'Plugin Development' started by jekeke123, Mar 23, 2013.

Thread Status:
Not open for further replies.
  1. This code works perfectly but i wanna add something else too
    I wanna add if the block location isn't logged in the yml file that it also just lets the player 'grief'.
    Code:
    public void onBreakBlock(BlockBreakEvent event){
        Player eventplayer = event.getPlayer();
        List<String> location = this.getConfig().getStringList("Data." + eventplayer);
        String blocklocation = (String) event.getBlock().getX() + ";" + event.getBlock().getX() + ";" + event.getBlock().getZ() + ";" + event.getBlock().getWorld();
        if(location == blocklocation){
          e.setCancelled(false);
        } else {
          e.setCancelled(true);
        }
        }
    BUMP

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  2. Offline

    MCForger

    Well if you save in your file the location.toString() then you can use this method I wrote for one of my plugins:
    Code:
        public Location stringToLoc(String s)
        {
            if (s == null)
            {
                return null;
            }
            try
            {
                String[] args = s.split(",");
                String wName = args[0];
                wName = wName.replace("CraftWorld", "").replace("Location", "")
                        .replace("name", "").replace("{", "").replace("}", "")
                        .replace("world==", "");
                String x = args[1];
                x = x.replace("x=", "");
                String y = args[2];
                y = y.replace("y=", "");
                String z = args[3];
                z = z.replace("z=", "");
                double arg1 = Double.parseDouble(x);
                double arg2 = Double.parseDouble(y);
                double arg3 = Double.parseDouble(z);
                return new Location(Bukkit.getWorld(wName), arg1, arg2, arg3);
            } catch (NumberFormatException e)
            {
                Logger.getLogger("Minecraft").severe(
                        "We had a slight problem: " + e);
            }
            return null;
        }
    I recommend onEnable you load all the locations into a list and then onDisable you save them to the config. So you don't have to call the getConfig().getString() method every time a block breaks.
     
  3. MCForger
    I just wanna save my location in a yml file i already converted the location into string i only need to know how to add it to a list without removing the location that is listed before the other so for exampleif i use this:
    Code:
    this.getConfig().set(eventplayer + ".blocks" , position);
    it will delete the one i first 'protected'

    And if it added the location then i wanna use it again:
    to see if my new position is the same as a position saved in my yml file
    and to who it belongs (eventplayer)
    BTW i'm making an anti-grief plugin if you place a block it protects one block at a time...
     
  4. Offline

    MCForger

    O Okay then do something like this:
    Code:
    getConfig().set(eventplayer + ".blocks" , List<Locations To String>);
    So your going to use like a HashMap<String, List<Locations> pLocations. I mean this is how I would do it.
     
  5. MCForger Tnx for that
    But then how do i check that the location is already saved and that who owns it?

    This code works perfectly but i wanna add something else too
    I wanna add if the block location isn't logged in the yml file that it also just lets the player 'grief'.
    Code:
    public void onBreakBlock(BlockBreakEvent event){
        Player eventplayer = event.getPlayer();
        List<String> location = this.getConfig().getStringList("Data." + eventplayer);
        String blocklocation = (String) event.getBlock().getX() + ";" + event.getBlock().getX() + ";" + event.getBlock().getZ() + ";" + event.getBlock().getWorld();
        if(location == blocklocation){
          e.setCancelled(false);
        } else {
          e.setCancelled(true);
        }
        }
    BUMP

    Topic updated
    So my problem now is i can't break blocks that belong to nobody

    Title changed

    I really hate this but BUMP

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  6. Offline

    MCForger

    jekeke123
    Sorry went to eat. Just check if this list contains this location then do this.
     
  7. MCForger
    My IDE Says something whit expression expected.
    What's wrong whit this code?
    Code:
        String blocklocation = event.getBlock().getX() + ";" + event.getBlock().getX() + ";" + event.getBlock().getZ() + ";" + event.getBlock().getWorld();
    Code:
        this.getConfig().set("Data." + eventplayer + ".blocks" , List.add<(blocklocation)>);
    And the full code:
    Code:
    @EventHandler
        public void onCreateBlock(BlockPlaceEvent event){
        Player eventplayer = event.getPlayer ();
        String blocklocation = event.getBlock().getX() + ";" + event.getBlock().getX() + ";" + event.getBlock().getZ() + ";" + event.getBlock().getWorld();
     
        this.getConfig().set("Data." + eventplayer + ".blocks" , List.add<(blocklocation)>);
        this.saveConfig();
        }
    Code:
    @EventHandler
        public void onBreakBlock(BlockBreakEvent event){
        Player eventplayer = event.getPlayer();
        List<String> playerdata = this.getConfig().getStringList("Data." + eventplayer);
        List<String> playerdata2 = this.getConfig().getStringList("Data." + eventplayer);
        String blocklocation = (String) (event.getBlock().getX() + ";" + event.getBlock().getX() + ";" + event.getBlock().getZ() + ";" + event.getBlock().getWorld());
        if(playerdata.contains(blocklocation)){
          event.setCancelled(false);
          this.getConfig().set("Data." + eventplayer + ".blocks", List.remove<(blocklocation)>);
        } else if (playerdata2.contains(blocklocation)){
          event.setCancelled(true);
        } else if (!playerdata2.contains(blocklocation)){
          event.setCancelled(false);
          this.getConfig().set("Data." + eventplayer + ".blocks", List.remove<(blocklocation)>);
        }
        }
     
Thread Status:
Not open for further replies.

Share This Page