Tutorial WorldGuard API - Set Flag deny-spawn

Discussion in 'Resources' started by SteveZR963, Jun 21, 2021.

  1. Offline

    SteveZR963

    I had a lot of trouble with trying to do this, because the Docs don't explain that setting the flag for deny-spawn is different to setting other flags, and the Docs don't explain what generic types are required. I was also unable to find any case uses despite hours of searching online. This is the reason that I'm writing this tutorial, and I hope it helps someone.

    First of all, here are the versions I was using when I tested this:

    MC: Paper 1.16.5
    Java: 11
    WorldEdit: 7.2.5
    WorldGuard: 7.0.5

    I don't know if this will work on older versions because I haven't tested it. However, it should work for WorldEdit and WorldGuard that are compatible with 1.12 and above.

    I'm going to assume that you know how to include WorldEdit and WorldGuard in your build paths, as this is basic. If you don't, then a quick search online will give you the answer.

    In this case use I am assuming that you are trying to create a region.

    Code:
    // Get world instance. World instance MUST be a WorldEdit instance.
    // wgPlugin is an instance of WorldGuardPlugin
    
    Region selection = wgPlugin.getWorldEdit().getSession(player).getSelection();
    com.sk89q.worldedit.world.World world = selection.getWorld();
    
    // Set up region manager
    
    RegionContainer rgContain = WorldGuard.getInstance().getPlatform().getRegionContainer();
    RegionManager regions = rgContain.get(world);
    
    // Capture your selection here
    
    BlockVector3 min = selection.getMinimumPoint();
    BlockVector3 max = selection.getMaximumPoint();
    
    // Create your protected region
    
    ProtectedRegion region = new ProtectedCuboidRegion("Region Name String", min, max);
    
    // Add deny-spawn flag
    
    // You must place the entities into a Set
    
    // Entities MUST be an instance of com.sk89q.worldedit.world.entity.EntityType. If it isn't then you'll get the
    // follow error
    
    /*
    *
    * The method setFlag(T, V) in the type ProtectedRegion is not applicable for the arguments
    * (SetFlag<EntityType>, Set<EntityType>)
    *
    */
    
    Set<com.sk89q.worldedit.world.entity.EntityType> entities = new HashSet<com.sk89q.worldedit.world.entity.EntityType>();
    
    // Add entities to Set. You MUST adapt the Bukkit EntityType to the WorldEdit EntityType, otherwise
    // you'll get the error as above
    
    entities.add(BukkitAdapter.adapt(EntityType.ZOMBIE));
    entities.add(BukkitAdapter.adapt(EntityType.BEE));
    
    // Now set the flag
    
    region.setFlag(Flags.DENY_SPAWN, entities);
    
    // Now create your region
    
    regions.addRegion(region);
    And that's it!
     

Share This Page