Skip to content

Commit

Permalink
Adding ignore_causes config option
Browse files Browse the repository at this point in the history
Rather than manually overriding the BED_EXIT and DISMOUNT teleport reasons, made a config option to handle which causes to ignore. Accepts all Spigot TeleportCauses, see https://hub.spigotmc.org/javadocs/spigot/org/bukkit/event/player/PlayerTeleportEvent.TeleportCause.html for more info.
  • Loading branch information
CoolLord22 committed Oct 4, 2023
1 parent 1ea62d1 commit f2fc60b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand All @@ -25,6 +24,7 @@
import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.protection.managers.RegionManager;
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
import org.bukkit.event.player.PlayerTeleportEvent;

public class OATConfig {
private final OtherAnimalTeleport plugin;
Expand All @@ -33,11 +33,11 @@ public class OATConfig {
public boolean gColorLogMessages;
public boolean globalUpdateChecking;
public boolean usePrefix = true;
public boolean ignoreUnknownCauses = false;

public int radius;

public List<Set<World>> worldGroup = new ArrayList<>();
public List<PlayerTeleportEvent.TeleportCause> ignoreCauses = new ArrayList<>();

public HashMap<EntityType, Boolean> entityMap = new HashMap<>();

Expand Down Expand Up @@ -138,7 +138,6 @@ public void loadConfig() throws IOException, InvalidConfigurationException {
gColorLogMessages = globalConfig.getBoolean("color_log_messages", true);
radius = globalConfig.getInt("radius", 2);

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

Expand Down Expand Up @@ -225,15 +224,20 @@ else if(input.equalsIgnoreCase(entType.toString())) {
}
}

plugin.log.logInfo("Loaded global config (" + global + ") with (verbosity=" + verbosity + ")", Verbosity.HIGHEST);
}
if(globalConfig.contains("ignore_causes")) {
for(String input : globalConfig.getStringList("ignore_causes")) {
try {
ignoreCauses.add(PlayerTeleportEvent.TeleportCause.valueOf(input));
} catch (IllegalArgumentException e) {
plugin.log.logWarning("Unrecognized teleport cause (" + input + ")! Skipping...");
}
}
}

public static Verbosity getVerbosity() {
return verbosity;
}
if(globalConfig.contains("ignore_unknown_causes"))
plugin.log.logWarning("ignore_unknown_causes is no longer used! Please add the following line to your config: \nignore_causes: [UNKNOWN]");

public static void setVerbosity(Verbosity verbosity) {
OATConfig.verbosity = verbosity;
plugin.log.logInfo("Loaded global config (" + global + ") with (verbosity=" + verbosity + ")", Verbosity.HIGHEST);
}

private void copy(InputStream in, File file) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,11 @@ public void onJoinUpdateChecker(PlayerJoinEvent event) {
@EventHandler(priority = EventPriority.MONITOR)
public void onTeleport(PlayerTeleportEvent event) {
if(plugin.enabled && !event.isCancelled()) {
if(plugin.config.ignoreUnknownCauses && event.getCause() == PlayerTeleportEvent.TeleportCause.UNKNOWN) {
plugin.log.logInfo("Ignore unknown cause was enabled, ignoring this event.", Verbosity.HIGHEST);
if(plugin.config.ignoreCauses.contains(event.getCause())) {
plugin.log.logInfo("Teleport reason was set to be ignored, skipping this event.", Verbosity.HIGHEST);
return;
}

try {
if(event.getCause() == PlayerTeleportEvent.TeleportCause.DISMOUNT || event.getCause() == PlayerTeleportEvent.TeleportCause.EXIT_BED) {
plugin.log.logInfo("Player dismounted vehicle / exited bed, ignoring teleport event.", Verbosity.HIGHEST);
return;
}
} catch(NoSuchFieldError err) {
plugin.log.logInfo("Lower version detected, no DISMOUNT or EXIT_BED teleport cause found.", Verbosity.HIGHEST);
}

if(event.getPlayer().hasPermission("otheranimalteleport.player.use")) {
plugin.log.logInfo("Player use permission check passed, running world checks.", Verbosity.HIGH);

Expand Down
5 changes: 2 additions & 3 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ color_log_messages: true
radius: 2

#######################
# Should we ignore unknown teleport causes? This may help solve vehicle dismount left messages on versions < 1.19.3
# In version 1.19.3 and above, a dismount event was added to teleport cause, so this can safely be ignored.
ignore_unknown_causes: false
# Teleport causes we should ignore
ignore_causes: [DISMOUNT, EXIT_BED, UNKNOWN]

#######################
# World groups to prevent cross-flow of entities
Expand Down

0 comments on commit f2fc60b

Please sign in to comment.