Skip to content

Commit

Permalink
v2.2 (#27)
Browse files Browse the repository at this point in the history
+ add new simple region location condition. This is the replacement for region condition on legacy Versions.
* Fixed a bug which prevents BDO from starting when using a legacy (<7) World Guard version.
- Disabled Region Condition for Versions <1.13. (All Versions incompatible to WG7)
  • Loading branch information
rainbowdashlabs authored Jul 19, 2020
1 parent 4c94e65 commit 33fa0d5
Show file tree
Hide file tree
Showing 31 changed files with 391 additions and 254 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.eldoria</groupId>
<artifactId>BigDoorsOpener</artifactId>
<version>2.1.1-BETA</version>
<version>2.2-BETA</version>
<name>BigDoorsOpener</name>
<url>https://github.com/eldoriarpg/BigDoorOpener</url>
<description>Open and close doors automatically on certain conditions</description>
Expand Down
71 changes: 40 additions & 31 deletions src/main/java/de/eldoria/bigdoorsopener/BigDoorsOpener.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import de.eldoria.bigdoorsopener.doors.conditions.item.interacting.ItemClick;
import de.eldoria.bigdoorsopener.doors.conditions.location.Proximity;
import de.eldoria.bigdoorsopener.doors.conditions.location.Region;
import de.eldoria.bigdoorsopener.doors.conditions.location.SimpleRegion;
import de.eldoria.bigdoorsopener.doors.conditions.standalone.Permission;
import de.eldoria.bigdoorsopener.doors.conditions.standalone.Placeholder;
import de.eldoria.bigdoorsopener.doors.conditions.standalone.Time;
Expand Down Expand Up @@ -53,18 +54,16 @@
public class BigDoorsOpener extends JavaPlugin {

private static Logger logger;
private static CachingJSEngine JS;
private static boolean placeholderEnabled = false;
@Getter
private static RegionContainer regionContainer = null;
private final BukkitScheduler scheduler = Bukkit.getScheduler();
private Config config;
private boolean initialized;
private Localizer localizer;
private static CachingJSEngine JS;
private static boolean placeholderEnabled = false;


// External instances.
private BigDoors doors;
@Getter
private static RegionContainer regionContainer = null;
private Commander commander;

// scheduler
Expand All @@ -74,6 +73,26 @@ public class BigDoorsOpener extends JavaPlugin {
private WeatherListener weatherListener;
private RegisterInteraction registerInteraction;

/**
* Get the plugin logger instance.
*
* @return plugin logger instance
*/
@SuppressWarnings("StaticVariableUsedBeforeInitialization")
@NotNull
public static Logger logger() {
return logger;
}

@SuppressWarnings("StaticVariableUsedBeforeInitialization")
public static CachingJSEngine JS() {
return JS;
}

public static boolean isPlaceholderEnabled() {
return placeholderEnabled;
}

@Override
public void onDisable() {
super.onDisable();
Expand Down Expand Up @@ -173,14 +192,20 @@ private void loadExternalSources() throws InstantiationException {
// check if world guard is loaded
if (Bukkit.getPluginManager().isPluginEnabled("WorldGuard")) {
logger().info("World Guard found. Trying to get a hook.");
regionContainer = WorldGuard.getInstance().getPlatform().getRegionContainer();
if (regionContainer != null) {
logger().info("Hooked into world guard successfully.");

String worldGuard = Bukkit.getPluginManager().getPlugin("WorldGuard").getDescription().getVersion();
if (worldGuard.startsWith("7")) {
regionContainer = WorldGuard.getInstance().getPlatform().getRegionContainer();
if (regionContainer != null) {
logger().info("Hooked into world guard successfully.");
} else {
logger().warning("Failed to hook into world guard.");
}
} else {
logger().warning("Failed to hook into world guard.");
logger().info("Found legacy World Guard Version. Region conditions can't be used.");
}
} else {
logger().info("World guard not found. Region conditions cant be used.");
logger().info("World guard not found. Region conditions can't be used.");
}

// check if placeholder api is present.
Expand All @@ -192,6 +217,9 @@ private void loadExternalSources() throws InstantiationException {
}
}

private void worldGuardHook() throws ClassNotFoundException {
}

/**
* Register the serializer classes.
* When a provided alias should be used the class needs the {@link org.bukkit.configuration.serialization.SerializableAs}
Expand All @@ -208,6 +236,7 @@ private void buildSerializer() {
ConfigurationSerialization.registerClass(ItemOwning.class, "itemOwningCondition");
ConfigurationSerialization.registerClass(Proximity.class, "proximityCondition");
ConfigurationSerialization.registerClass(Region.class, "regionCondition");
ConfigurationSerialization.registerClass(SimpleRegion.class, "simpleRegionCondition");
ConfigurationSerialization.registerClass(Permission.class, "permissionCondition");
ConfigurationSerialization.registerClass(Time.class, "timeCondition");
ConfigurationSerialization.registerClass(Weather.class, "weatherCondition");
Expand Down Expand Up @@ -279,24 +308,4 @@ private Pair<String, String> getDoorsVersion(String ver) {
}
return new Pair<>(versionString, buildString);
}

/**
* Get the plugin logger instance.
*
* @return plugin logger instance
*/
@SuppressWarnings("StaticVariableUsedBeforeInitialization")
@NotNull
public static Logger logger() {
return logger;
}

@SuppressWarnings("StaticVariableUsedBeforeInitialization")
public static CachingJSEngine JS() {
return JS;
}

public static boolean isPlaceholderEnabled() {
return placeholderEnabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
import de.eldoria.bigdoorsopener.doors.conditions.item.interacting.ItemClick;
import de.eldoria.bigdoorsopener.doors.conditions.location.Proximity;
import de.eldoria.bigdoorsopener.doors.conditions.location.Region;
import de.eldoria.bigdoorsopener.doors.conditions.location.SimpleRegion;
import de.eldoria.bigdoorsopener.doors.conditions.standalone.Permission;
import de.eldoria.bigdoorsopener.doors.conditions.standalone.Placeholder;
import de.eldoria.bigdoorsopener.doors.conditions.standalone.Time;
import de.eldoria.bigdoorsopener.doors.conditions.standalone.Weather;
import de.eldoria.bigdoorsopener.listener.registration.InteractionRegistrationObject;
import de.eldoria.bigdoorsopener.listener.registration.RegisterInteraction;
import de.eldoria.bigdoorsopener.scheduler.DoorChecker;
import de.eldoria.bigdoorsopener.util.C;
Expand Down Expand Up @@ -52,6 +54,7 @@
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.util.BlockVector;
Expand All @@ -69,6 +72,13 @@
import java.util.stream.Collectors;

public class BigDoorsOpenerCommand implements TabExecutor {
private static final CachingJSEngine ENGINE;
// Tabcomplete utils
private static final String[] CONDITION_TYPES;
private static final String[] CONDITION_GROUPS;
private static final String[] PROXIMITY_FORM;
private static final String[] WEATHER_TYPE;
private static final String[] EVALUATOR_TYPES;
private final BigDoorsOpener plugin;
private final Commander commander;
private final Config config;
Expand All @@ -77,18 +87,8 @@ public class BigDoorsOpenerCommand implements TabExecutor {
private final MessageSender messageSender;
private final RegisterInteraction registerInteraction;
private final RegionContainer regionContainer;

private final BukkitAudiences bukkitAudiences;

private static final CachingJSEngine ENGINE;

// Tabcomplete utils
private static final String[] CONDITION_TYPES;
private static final String[] CONDITION_GROUPS;
private static final String[] PROXIMITY_FORM;
private static final String[] WEATHER_TYPE;
private static final String[] EVALUATOR_TYPES;

static {
ENGINE = BigDoorsOpener.JS();
CONDITION_TYPES = Arrays.stream(ConditionType.values())
Expand Down Expand Up @@ -358,10 +358,10 @@ private boolean setCondition(Player player, String[] args) {
return true;
}

if (argumentsInvalid(player, args, 3,
if (argumentsInvalid(player, args, 2,
"<" + localizer.getMessage("syntax.doorId") + "> <"
+ localizer.getMessage("syntax.condition") + "> <"
+ localizer.getMessage("syntax.conditionValues") + ">")) {
+ localizer.getMessage("syntax.condition") + "> ["
+ localizer.getMessage("syntax.conditionValues") + "]")) {
return true;
}

Expand Down Expand Up @@ -395,8 +395,10 @@ private boolean setCondition(Player player, String[] args) {

ConditionChain conditionChain = conditionalDoor.getConditionChain();


String[] conditionArgs = Arrays.copyOfRange(args, 2, args.length);
String[] conditionArgs = new String[0];
if (args.length > 2) {
conditionArgs = Arrays.copyOfRange(args, 2, args.length);
}

switch (type) {
// <amount> <consumed>
Expand Down Expand Up @@ -556,6 +558,33 @@ private boolean setCondition(Player player, String[] args) {
conditionChain.setLocation(new Region(region, player.getWorld()));
messageSender.sendMessage(player, localizer.getMessage("setCondition.region"));
break;
case SIMPLE_REGION:
messageSender.sendMessage(player, localizer.getMessage("setCondition.firstPoint"));
registerInteraction.register(player, new InteractionRegistrationObject() {
private String world;
private BlockVector first;

@Override
public boolean register(PlayerInteractEvent event) {
if (event.getAction() != Action.LEFT_CLICK_BLOCK) {
return false;
}
BlockVector vec = event.getClickedBlock().getLocation().toVector().toBlockVector();
if (first == null) {
world = event.getPlayer().getWorld().getName();
first = vec;
event.setCancelled(true);
messageSender.sendMessage(player, localizer.getMessage("setCondition.secondPoint"));
return false;
}
conditionalDoor.getConditionChain().setLocation(new SimpleRegion(first, vec, world));
config.safeConfig();
event.setCancelled(true);
messageSender.sendMessage(player, localizer.getMessage("setCondition.simpleRegionRegisterd"));
return true;
}
});
break;
// permission
case PERMISSION:
if (argumentsInvalid(player, conditionArgs, 1,
Expand Down Expand Up @@ -1437,6 +1466,10 @@ private boolean denyAccess(CommandSender sender, boolean silent, String... permi
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
String cmd = args[0];

if (args.length > 10) {
return Collections.singletonList("(╯°□°)╯︵ ┻━┻");
}

if (args.length == 1) {
return ArrayUtil.startingWithInArray(cmd,
new String[] {"help", "about",
Expand All @@ -1450,7 +1483,7 @@ public List<String> onTabComplete(CommandSender sender, Command command, String


if (args.length == 2) {
return Collections.singletonList(localizer.getMessage("tabcomplete.doorId"));
return Collections.singletonList("<" + localizer.getMessage("syntax.doorId") + ">");
}
if (args.length == 3) {
return ArrayUtil.startingWithInArray(args[2], CONDITION_TYPES).collect(Collectors.toList());
Expand All @@ -1476,7 +1509,7 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
}
if (args.length == 5) {
if (args[4].isEmpty()) {
return Arrays.asList("true", "false");
return Arrays.asList("true", "false");
}
return Arrays.asList("[" + localizer.getMessage("tabcomplete.consumed") + "]", "true", "false");
}
Expand Down Expand Up @@ -1622,6 +1655,7 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
return Collections.singletonList("<" + localizer.getMessage("syntax.amount") + ">");
}
}

return Collections.emptyList();
}
}
40 changes: 20 additions & 20 deletions src/main/java/de/eldoria/bigdoorsopener/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ public Config(Plugin plugin) {
loadConfig();
}

private static void setIfAbsent(FileConfiguration config, String path, Object value) {
if (!config.isSet(path)) {
config.set(path, value);
}
}

private static void setIfAbsent(ConfigurationSection section, String path, Object value) {
if (!section.isSet(path)) {
section.set(path, value);
}
}

private static ConfigurationSection createSectionIfAbsent(FileConfiguration config, String path) {
ConfigurationSection section = config.getConfigurationSection(path);
if (section == null) {
return config.createSection(path);
}
return section;
}

public void loadConfig() {
updateConfig();
reloadConfig();
Expand Down Expand Up @@ -186,24 +206,4 @@ public void safeConfig() {
plugin.saveConfig();
}

private static void setIfAbsent(FileConfiguration config, String path, Object value) {
if (!config.isSet(path)) {
config.set(path, value);
}
}

private static void setIfAbsent(ConfigurationSection section, String path, Object value) {
if (!section.isSet(path)) {
section.set(path, value);
}
}

private static ConfigurationSection createSectionIfAbsent(FileConfiguration config, String path) {
ConfigurationSection section = config.getConfigurationSection(path);
if (section == null) {
return config.createSection(path);
}
return section;
}

}
26 changes: 13 additions & 13 deletions src/main/java/de/eldoria/bigdoorsopener/config/TimedDoor.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ public TimedDoor(long doorUID, String world, Vector position, String permission,
this.invertOpen = invertOpen;
}

public static TimedDoor deserialize(Map<String, Object> map) {
TypeResolvingMap resolvingMap = SerializationUtil.mapOf(map);
long doorUID = Long.parseLong(resolvingMap.getValue("doorUID"));
String world = resolvingMap.getValue("world");
Vector position = resolvingMap.getValue("position");
String permission = resolvingMap.getValue("permission");
int ticksClose = resolvingMap.getValue("ticksClose");
int ticksOpen = resolvingMap.getValue("ticksOpen");
double range = resolvingMap.getValue("range");
boolean invertOpen = resolvingMap.getValue("invertOpen");
return new TimedDoor(doorUID, world, position, permission, ticksClose, ticksOpen, range, invertOpen);
}

@Override
public Map<String, Object> serialize() {
return SerializationUtil.newBuilder()
Expand All @@ -82,19 +95,6 @@ public Map<String, Object> serialize() {
.build();
}

public static TimedDoor deserialize(Map<String, Object> map) {
TypeResolvingMap resolvingMap = SerializationUtil.mapOf(map);
long doorUID = Long.parseLong(resolvingMap.getValue("doorUID"));
String world = resolvingMap.getValue("world");
Vector position = resolvingMap.getValue("position");
String permission = resolvingMap.getValue("permission");
int ticksClose = resolvingMap.getValue("ticksClose");
int ticksOpen = resolvingMap.getValue("ticksOpen");
double range = resolvingMap.getValue("range");
boolean invertOpen = resolvingMap.getValue("invertOpen");
return new TimedDoor(doorUID, world, position,permission, ticksClose, ticksOpen, range, invertOpen);
}

public boolean shouldBeOpen(long fulltime) {
// This is a permanent closed door.
if (isPermanentlyClosed()) {
Expand Down
Loading

0 comments on commit 33fa0d5

Please sign in to comment.