Skip to content

Commit

Permalink
Updating config to support blocked regions
Browse files Browse the repository at this point in the history
This adds in the feature request from #1. We will now check before teleporting if the player is teleporting into a region that is defined as blocked.

This commit also adds code to first check if certain config values exist before we try to parse them. Overall just optimization changes.
  • Loading branch information
CoolLord22 committed Aug 21, 2020
1 parent 98ec187 commit 985f9bf
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 33 deletions.
109 changes: 76 additions & 33 deletions src/main/java/com/coollord22/otheranimalteleport/assets/OATConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
Expand All @@ -17,7 +18,12 @@
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.EntityType;

import com.coollord22.otheranimalteleport.OtherAnimalTeleport;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;

public class OATConfig {
private final OtherAnimalTeleport plugin;
Expand All @@ -26,12 +32,13 @@ public class OATConfig {
public boolean gColorLogMessages;
public boolean globalUpdateChecking;
public boolean usePrefix = true;

public int radius;

public List<Set<World>> worldGroup = new ArrayList<Set<World>>();
public List<EntityType> allowedEnts = new ArrayList<EntityType>();

public HashMap<World, Set<ProtectedRegion>> blockedRegions = new HashMap<>();

public String prefix;
public String failedTeleportMessage;

Expand Down Expand Up @@ -121,52 +128,88 @@ public void loadConfig() throws FileNotFoundException, IOException, InvalidConfi

worldGroup.clear();
allowedEnts.clear();
blockedRegions.clear();

verbosity = OATCommon.getConfigVerbosity(globalConfig);
globalUpdateChecking = globalConfig.getBoolean("update_checker", true);
gColorLogMessages = globalConfig.getBoolean("color_log_messages", true);
radius = globalConfig.getInt("radius", 2);

usePrefix = globalConfig.getBoolean("use_prefix", true);
prefix = globalConfig.getString("prefix", "&7[&aOtherAnimalTeleport&7] ");

//messages
failedTeleportMessage = globalConfig.getString("fail_teleport", "&7An entity could not be teleported and is located near (&c%x&7, &c%y&7, &c%z&7).");

for(Object input : globalConfig.getList("world_groups")) {
Set<World> worldList = new HashSet<World>();
for(String inputWorld : (ArrayList<String>)input) {
boolean foundMatch = false;
for(World knownWorld : Bukkit.getWorlds()) {
if(knownWorld.getName().equalsIgnoreCase(inputWorld)) {
foundMatch = true;
worldList.add(knownWorld);
}
if(globalConfig.contains("blocked_regions")) {
for(String input : globalConfig.getStringList("blocked_regions")) {
String[] splitRegion = input.split("@", 2);
if(splitRegion.length != 2) {
plugin.log.logWarning("Improper world@region formatting, please check input again (" + input + ")! Skipping...");
continue;
}

World regionWorld = Bukkit.getWorld(splitRegion[0]);
if(regionWorld == null) {
plugin.log.logWarning("Unrecognized world for blocked_regions, please check world name (" + input + ")! Skipping...");
continue;
}
if(!foundMatch) {
plugin.log.logWarning("Unrecognized world name specified (" + inputWorld + ")! Skipping...");

RegionManager regionManager = WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(regionWorld));
ProtectedRegion regionToAdd = regionManager.getRegion(splitRegion[1]);
if(regionToAdd == null) {
plugin.log.logWarning("Unrecognized region for blocked_regions, please check region name (" + input + ")! Skipping...");
continue;
}

Set<ProtectedRegion> regionList = new HashSet<>();
if(blockedRegions.containsKey(regionWorld)) {
regionList = blockedRegions.get(regionWorld);
}
regionList.add(regionToAdd);
blockedRegions.put(regionWorld, regionList);

}
worldGroup.add(worldList);
}
for(String input : globalConfig.getStringList("allowed_entities")) {
if(input.equals("ANY") || input.equals("ALL")) {
for(EntityType entType : EntityType.values()) {
if(entType.isAlive() && !(entType.equals(EntityType.valueOf("PLAYER"))))
allowedEnts.add(entType);
}
}
else {
boolean foundMatch = false;
for(EntityType entType : EntityType.values()) {
if(input.equalsIgnoreCase(entType.toString())) {
foundMatch = true;
allowedEnts.add(entType);

if(globalConfig.contains("world_groups")) {
for(Object input : globalConfig.getList("world_groups")) {
Set<World> worldList = new HashSet<World>();
for(String inputWorld : (ArrayList<String>)input) {
boolean foundMatch = false;
for(World knownWorld : Bukkit.getWorlds()) {
if(knownWorld.getName().equalsIgnoreCase(inputWorld)) {
foundMatch = true;
worldList.add(knownWorld);
}
}
if(!foundMatch) {
plugin.log.logWarning("Unrecognized world name specified (" + inputWorld + ")! Skipping...");
}
}
if(!foundMatch) {
plugin.log.logWarning("Unrecognized entity type (" + input + ")! Skipping...");
worldGroup.add(worldList);
}
}

if(globalConfig.contains("allowed_entities")) {
for(String input : globalConfig.getStringList("allowed_entities")) {
if(input.equals("ANY") || input.equals("ALL")) {
for(EntityType entType : EntityType.values()) {
if(entType.isAlive() && !(entType.equals(EntityType.valueOf("PLAYER"))))
allowedEnts.add(entType);
}
}
else {
boolean foundMatch = false;
for(EntityType entType : EntityType.values()) {
if(input.equalsIgnoreCase(entType.toString())) {
foundMatch = true;
allowedEnts.add(entType);
}
}
if(!foundMatch) {
plugin.log.logWarning("Unrecognized entity type (" + input + ")! Skipping...");
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import com.coollord22.otheranimalteleport.OATMethods;
import com.coollord22.otheranimalteleport.OtherAnimalTeleport;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;

public class OATListeners implements Listener {

Expand All @@ -39,6 +41,21 @@ public void onTeleport(PlayerTeleportEvent event) {
boolean sameGroup = false;
World fromWorld = event.getFrom().getWorld();
World toWorld = event.getTo().getWorld();
if(plugin.config.blockedRegions.containsKey(toWorld)) {
for(ProtectedRegion region : plugin.config.blockedRegions.get(toWorld)) {
if(region.contains(BlockVector3.at(event.getTo().getX(), event.getTo().getY(), event.getTo().getZ()))) {
if(plugin.config.failedTeleportMessage != null) {
if(!plugin.config.failedTeleportMessage.isEmpty()) {
plugin.common.sendMessage(plugin.config.usePrefix, event.getPlayer(), plugin.config.failedTeleportMessage
.replaceAll("%x", df.format(event.getFrom().getBlockX()))
.replaceAll("%y", df.format(event.getFrom().getBlockY()))
.replaceAll("%z", df.format(event.getFrom().getBlockZ())));
}
}
return;
}
}
}
if(fromWorld.equals(toWorld)) {
sameGroup = true;
} else {
Expand Down
9 changes: 9 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ world_groups:
# allowed_entities: [COW, PIG, CHICKEN, WOLF]
allowed_entities: [ANY]

#######################
# List of regions to deny teleporting animals into and out of. Requires world and region name separated by @
#
# The format is "worldName@regionName" where worldName is the name of world containing the regionName. Note if a
# region cannot be found it will be logged in console so please check that the name and region are written correctly!
#
# Ex. blocked_regions: ["world@region1", "world@region2"]
blocked_regions: []

#######################
# Message handling
#
Expand Down

0 comments on commit 985f9bf

Please sign in to comment.