World Guard support

Discussion in 'Plugin Development' started by TotallyNotWalshy, Oct 24, 2014.

Thread Status:
Not open for further replies.
  1. I made a plugin but people can break in worldguard protected areas. Can anyone help me out, I have checked the worldguard api it didn't help I hate to ask this but anyone have any code?

    This is my current code:
    Code:java
    1. public static boolean canBuild(Location loc){
    2. boolean canBuild = false;
    3. RegionManager rm = WorldGuard.getRegionManager(loc.getWorld());
    4. ApplicableRegionSet region = rm.getApplicableRegions(loc);
    5. if((region.getFlag(DefaultFlag.BUILD) != null) && ((StateFlag.State)region.getFlag(DefaultFlag.BUILD)).equals(StateFlag.State.ALLOW)){
    6. canBuild = true;
    7. }else if(region.getFlag(DefaultFlag.BUILD) != null){
    8. canBuild = false;
    9. }else{
    10. canBuild = true;
    11. }
    12. System.out.println("" + canBuild);
    13. return canBuild;
    14. }


    Code:java
    1. if(!WorldGuard.canBuild(e.getBlock().getLocation())){
    2. e.getBlock().getDrops().clear();
    3. ItemStack drops = new ItemStack(Material.IRON_INGOT);
    4. player.getInventory().addItem(drops);
    5. e.setBlock().setType(Material.AIR);


    Tried true and false didn't work.
     
  2. Offline

    JordyPwner

  3. Offline

    nlthijs48

    TotallyNotWalshy JordyPwner Normally the build flag of a region should not be set, building is disabled by default for all players that are not an owner/member of the region. So that is why you method of checking does not work. In one of my plugins I wanted to check if a player was in PVP area, I did this with the following:
    Code:java
    1. RegionManager manager = worldGuard.getRegionManager(Bukkit.getWorld("world"));
    2. ApplicableRegionSet regions = manager.getApplicableRegions(player.getLocation());
    3. boolean pvpAllowed = regions.allows(DefaultFlag.PVP, worldGuard.wrapPlayer(player));

    You already have the first two lines, after that you need the allows() method to check if the set of regions allows the specified action. You need to use DefaultFlag.BUILD instead of PVP, it also seems that you don't have access to the player object, you will need that for using the allows() method, so you probably want to change the Location argument to a Player argument.
     
  4. nlthijs48 This is my code
    Code:java
    1. public static boolean canBuild(Player player){
    2. Location loc = player.getLocation();
    3. RegionManager manager = WorldGuard.getRegionManager(loc.getWorld()); //NPE!!!
    4. ApplicableRegionSet regions = manager.getApplicableRegions(loc);
    5. boolean canBuild = regions.allows(DefaultFlag.BUILD, WorldGuard.wrapPlayer(player));
    6.  
    7. return canBuild;
    8. }


    But it is throwing a npe, any idea how to fix?
     
  5. Offline

    mythbusterma

  6. Offline

    nlthijs48

    TotallyNotWalshy Are you sure it is that line causing the NPE? If it is then loc is null, which is not possible if I'm correct or WorldGuard is null, which is more likely. Assuming WorldGuard is a variable (named incorrectly, should be starting with a lowercase character) then that is null. If you show the complete class I can help you more with it.
     
  7. nlthijs48
    Code:
    28) [custom.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
    Caused by: java.lang.NullPointerException
            at com.murderxtreme.test.WorldGuard.canBuild(WorldGuard.java:17) ~[?
    :?]
            at com.murderxtreme.test.Dummy.onWorldTest(Dummy.java:14) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _51]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _51]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_51]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_51]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:292) ~[custom.jar:git-Bukkit-1.7.9-R0.1-10-g8688bd4-b3092jnks]
     
            ... 15 more
     
  8. Offline

    nlthijs48

  9. nlthijs48
    Code:java
    1. package com.murderxtreme.test;
    2.  
    3. import org.bukkit.Location;
    4. import org.bukkit.entity.Player;
    5.  
    6. import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
    7. import com.sk89q.worldguard.protection.ApplicableRegionSet;
    8. import com.sk89q.worldguard.protection.flags.DefaultFlag;
    9. import com.sk89q.worldguard.protection.managers.RegionManager;
    10.  
    11. public class WorldGuard {
    12.  
    13. public static WorldGuardPlugin WorldGuard;
    14.  
    15. public static boolean canBuild(Player player){
    16. Location loc = player.getLocation();
    17. RegionManager manager = WorldGuard.getRegionManager(loc.getWorld()); //NPE!!!
    18. ApplicableRegionSet regions = manager.getApplicableRegions(loc);
    19. boolean canBuild = regions.allows(DefaultFlag.BUILD, WorldGuard.wrapPlayer(player));
    20.  
    21. return canBuild;
    22. }
    23. }
    24.  
     
  10. Offline

    nlthijs48

    TotallyNotWalshy Your WorldGuard variable is null, you have never assigned something to it. And you still need to change the name of that to 'worldGuard', following the conventions.
     
  11. nlthijs48 Ah I am super dumb the class and the WorldGuard worldGuard class are the same, that's probably why.


    So what should I assign it to?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 14, 2016
  12. Offline

    nlthijs48

    TotallyNotWalshy You need to get the WorldGuardPlugin instance through Bukkit, check this page to see how that works. Because you don't have a reference to your main class you need to use 'Bukkit.getServer()' instead of 'getServer()' or give a reference to your main class with the constructor.
     
Thread Status:
Not open for further replies.

Share This Page