diff --git a/src/main/java/me/youhavetrouble/preventstabby/config/ConfigCache.java b/src/main/java/me/youhavetrouble/preventstabby/config/ConfigCache.java index ac78ac0..6820bf7 100644 --- a/src/main/java/me/youhavetrouble/preventstabby/config/ConfigCache.java +++ b/src/main/java/me/youhavetrouble/preventstabby/config/ConfigCache.java @@ -2,16 +2,20 @@ package me.youhavetrouble.preventstabby.config; import me.youhavetrouble.preventstabby.PreventStabby; import org.bukkit.configuration.file.FileConfiguration; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import java.io.File; +import java.io.IOException; import java.util.Collections; import java.util.HashSet; +import java.util.List; import java.util.Set; public class ConfigCache { - public final boolean pvp_enabled_by_default, lava_and_fire_stopper_enabled, channeling_enchant_disabled, - punish_for_combat_logout, punish_for_combat_logout_announce, only_owner_can_interact_with_pet, - snowballs_knockback, egg_knockback, block_commands_in_combat, block_teleports_in_combat, allow_fishing_rod_pull; + public final boolean pvp_enabled_by_default, bucket_stopper_enabled, fire_stopper_enabled, punish_for_combat_logout, + punish_for_combat_logout_announce, block_teleports_in_combat, allow_fishing_rod_pull; public final String pvp_enabled, pvp_disabled, cannot_attack_victim, cannot_attack_attacker, cannot_attack_pets_victim, cannot_attack_pets_attacker, no_permission, no_such_command, pvp_enabled_other, pvp_disabled_other, punish_for_combat_logout_message, entering_combat, leaving_combat, @@ -23,77 +27,130 @@ public class ConfigCache { cannot_attack_pets_teleport_or_spawn_protection_attacker, cannot_attack_mounts_teleport_or_spawn_protection_attacker, cannot_attack_teleport_or_spawn_protection_victim; - public final double lava_and_fire_stopper_radius; - public final long combat_time, login_protection_time, teleport_protection_time, cache_time; + public final double combat_time, login_protection_time, teleport_protection_time, bucket_stopper_radius, + fire_stopper_radius; private final Set combatBlockedCommands = new HashSet<>(); - public ConfigCache(PreventStabby plugin) { + private final FileConfiguration config; + public ConfigCache(PreventStabby plugin) { plugin.reloadConfig(); - FileConfiguration config = plugin.getConfig(); + config = plugin.getConfig(); // Settings - this.pvp_enabled_by_default = config.getBoolean("settings.pvp_enabled_by_default", false); + this.pvp_enabled_by_default = getBoolean( + "settings.pvp_enabled_by_default", + false, + List.of("Should pvp be enabled by default when the player first joins?") + ); - this.lava_and_fire_stopper_enabled = config.getBoolean("settings.lava_and_fire_stopper.enabled", true); - this.lava_and_fire_stopper_radius = config.getDouble("settings.lava_and_fire_stopper.radius", 2.5); + this.combat_time = getDouble( + "settings.combat_time", + 25, + List.of("How long in seconds should combat last since the last hit") + ); + this.punish_for_combat_logout = getBoolean( + "settings.punish_for_combat_logout.enabled", + true, + List.of("Should players be killed if they log out during combat?") + ); + this.punish_for_combat_logout_announce = getBoolean( + "settings.punish_for_combat_logout.announce", + true, + List.of("Should killing of a player that logged out of combat be announced?") + ); - this.channeling_enchant_disabled = config.getBoolean("settings.channeling_enchant_disabled", true); + this.combatBlockedCommands.addAll(getList( + "settings.block_in_combat.commands", + List.of("spawn", "tpa", "home"), + List.of("Commands to block when player is in combat") + ) + ); - this.combat_time = config.getLong("settings.combat_time", 25L); - this.punish_for_combat_logout = config.getBoolean("settings.punish_for_combat_logout.enabled", true); - this.punish_for_combat_logout_announce = config.getBoolean("settings.punish_for_combat_logout.announce", true); - this.punish_for_combat_logout_message = config.getString("settings.punish_for_combat_logout.message", "%player% logged out while in combat. What a loser."); - this.only_owner_can_interact_with_pet = config.getBoolean("settings.only_owner_can_interact_with_pet", false); + this.block_teleports_in_combat = getBoolean( + "settings.block_in_combat.teleports", + false, + List.of("Block teleportation triggered by plugins while in combat") + ); - this.allow_fishing_rod_pull = config.getBoolean("settings.allow_fishing_rod_pull", false); - this.snowballs_knockback = config.getBoolean("settings.snowballs_do_knockback", false); - this.egg_knockback = config.getBoolean("settings.eggs_do_knockback", false); - this.block_commands_in_combat = config.getBoolean("settings.block_in_combat.block_commands", true); - if (block_commands_in_combat) { - this.combatBlockedCommands.addAll(config.getStringList("settings.block_in_combat.block_commands.commands")); - } - this.block_teleports_in_combat = config.getBoolean("settings.block_in_combat.block_teleports", false); + this.login_protection_time = getDouble( + "settings.login_protection_time", + 0, + List.of("Protection time after player logs in in seconds") + ); + this.teleport_protection_time = getDouble( + "settings.teleport_protection_time", + 0, + List.of("Protection time after player is teleported in seconds") + ); - this.cache_time = config.getLong("settings.cache_time", 30L); - - this.login_protection_time = config.getLong("settings.login_protection_time", 0); - this.teleport_protection_time = config.getLong("settings.teleport_protection_time", 0); + this.allow_fishing_rod_pull = getBoolean( + "settings.allow_fishing_rod_pull", + false, + List.of("Should fishing rod pulling be allowed regardless of players pvp state?") + ); + this.bucket_stopper_enabled = getBoolean( + "settings.environmental.bucket_stopper.enabled", + true, + List.of("Should plugin block dumping buckets with dangers near players with pvp off?") + ); + this.bucket_stopper_radius = getDouble( + "settings.environmental.bucket_stopper.radius", + 2.5, + List.of("Distance from the player where dumping buckets will be disallowed") + ); + this.fire_stopper_enabled = getBoolean( + "settings.environmental.fire_stopper.enabled", + true, + List.of("Should plugin block igniting blocks near players with pvp off?") + ); + this.fire_stopper_radius = getDouble( + "settings.environmental.fire_stopper.radius", + 2.5, + List.of("Distance from the player where igniting blocks will be disallowed") + ); // Messages - this.pvp_enabled = config.getString("messages.pvp_enabled", "You enabled PvP!"); - this.pvp_disabled = config.getString("messages.pvp_disabled", "You disabled PvP!"); - this.cannot_attack_victim = config.getString("messages.cannot_attack_victim", "You can't attack players that have PvP turned off!"); - this.cannot_attack_attacker = config.getString("messages.cannot_attack_attacker", "You can't attack players while you have PvP turned off!"); - this.cannot_attack_pets_victim = config.getString("messages.cannot_attack_pets_victim", "You can't attack pets while you have PvP turned off"); - this.cannot_attack_pets_attacker = config.getString("messages.cannot_attack_pets_attacker", "You can't attack pets of players that have PvP turned off"); - this.cannot_attack_mounts_victim = config.getString("messages.cannot_attack_mounts_victim", "You can't attack mounts of players that have PvP turned off"); - this.cannot_attack_mounts_attacker = config.getString("messages.cannot_attack_mounts_attacker", "You can't attack mounts while you have PvP turned off"); - this.cannot_attack_pvp_force_off = config.getString("messages.cannot_attack_pvp_force_off", "PvP is forcibly disabled"); + this.pvp_enabled = getString("messages.pvp_enabled", "You enabled PvP!"); + this.pvp_disabled = getString("messages.pvp_disabled", "You disabled PvP!"); + this.cannot_attack_victim = getString("messages.cannot_attack_victim", "You can't attack players that have PvP turned off!"); + this.cannot_attack_attacker = getString("messages.cannot_attack_attacker", "You can't attack players while you have PvP turned off!"); + this.cannot_attack_pets_victim = getString("messages.cannot_attack_pets_victim", "You can't attack pets while you have PvP turned off"); + this.cannot_attack_pets_attacker = getString("messages.cannot_attack_pets_attacker", "You can't attack pets of players that have PvP turned off"); + this.cannot_attack_mounts_victim = getString("messages.cannot_attack_mounts_victim", "You can't attack mounts of players that have PvP turned off"); + this.cannot_attack_mounts_attacker = getString("messages.cannot_attack_mounts_attacker", "You can't attack mounts while you have PvP turned off"); + this.cannot_attack_pvp_force_off = getString("messages.cannot_attack_pvp_force_off", "PvP is forcibly disabled"); - this.no_permission = config.getString("messages.no_permission", "You don't have permission to use that."); - this.no_such_command = config.getString("messages.no_such_command", "No such command."); - this.pvp_enabled_other = config.getString("messages.pvp_enabled_others", "You've enabled %player%'s PvP."); - this.pvp_disabled_other = config.getString("messages.pvp_disabled_others", "You've disabled %player%'s PvP."); - this.entering_combat = config.getString("messages.entering_combat", "Entering combat"); - this.leaving_combat = config.getString("messages.leaving_combat", "Leaving combat"); - this.cant_do_that_during_combat = config.getString("messages.cant_do_that_during_combat", "You can't do that while in combat!"); - this.force_pvp_on = config.getString("messages.force_pvp_on", "PvP is now force enabled"); - this.force_pvp_off = config.getString("messages.force_pvp_off", "PvP is now force disabled"); - this.force_pvp_none = config.getString("messages.force_pvp_none", "PvP state is not forced now"); + this.no_permission = getString("messages.no_permission", "You don't have permission to use that."); + this.no_such_command = getString("messages.no_such_command", "No such command."); + this.pvp_enabled_other = getString("messages.pvp_enabled_others", "You've enabled %player%'s PvP."); + this.pvp_disabled_other = getString("messages.pvp_disabled_others", "You've disabled %player%'s PvP."); + this.entering_combat = getString("messages.entering_combat", "Entering combat"); + this.leaving_combat = getString("messages.leaving_combat", "Leaving combat"); + this.cant_do_that_during_combat = getString("messages.cant_do_that_during_combat", "You can't do that while in combat!"); + this.force_pvp_on = getString("messages.force_pvp_on", "PvP is now force enabled"); + this.force_pvp_off = getString("messages.force_pvp_off", "PvP is now force disabled"); + this.force_pvp_none = getString("messages.force_pvp_none", "PvP state is not forced now"); - this.placeholder_combat_time = config.getString("placeholder.placeholder_combat_time", "Combat time: %time%"); - this.placeholder_not_in_combat = config.getString("placeholder.not_in_combat", "Not in combat"); + this.punish_for_combat_logout_message = getString("messages.punish_for_combat_logout.message", "%player% logged out while in combat. What a loser."); - this.placeholder_pvp_forced_true = config.getString("placeholder.pvp_forced_true", "PvP is forced on"); - this.placeholder_pvp_forced_false = config.getString("placeholder.pvp_forced_false", "PvP is forced off"); - this.placeholder_pvp_forced_none = config.getString("placeholder.pvp_forced_none", "PvP is not forced"); + this.placeholder_combat_time = getString("placeholder.placeholder_combat_time", "Combat time: %time%"); + this.placeholder_not_in_combat = getString("placeholder.not_in_combat", "Not in combat"); - this.cannot_attack_forced_pvp_off = config.getString("messages.cannot_attack_pvp_force_off", "PvP is forcibly disabled"); - this.cannot_attack_teleport_or_spawn_protection_attacker = config.getString("messages.cannot_attack_teleport_or_spawn_protection_attacker", "You can't attack players while they have teleport or spawn protection"); - this.cannot_attack_pets_teleport_or_spawn_protection_attacker = config.getString("messages.cannot_attack_pets_teleport_or_spawn_protection_attacker", "You can't attack pets while you have teleport or spawn protection"); - this.cannot_attack_mounts_teleport_or_spawn_protection_attacker = config.getString("messages.cannot_attack_mounts_teleport_or_spawn_protection_attacker", "You can't attack mounts while you have teleport or spawn protection"); - this.cannot_attack_teleport_or_spawn_protection_victim = config.getString("messages.cannot_attack_teleport_or_spawn_protection_victim", "You can't attack players while you have teleport or spawn protection"); + this.placeholder_pvp_forced_true = getString("placeholder.pvp_forced_true", "PvP is forced on"); + this.placeholder_pvp_forced_false = getString("placeholder.pvp_forced_false", "PvP is forced off"); + this.placeholder_pvp_forced_none = getString("placeholder.pvp_forced_none", "PvP is not forced"); + + this.cannot_attack_forced_pvp_off = getString("messages.cannot_attack_pvp_force_off", "PvP is forcibly disabled"); + this.cannot_attack_teleport_or_spawn_protection_attacker = getString("messages.cannot_attack_teleport_or_spawn_protection_attacker", "You can't attack players while they have teleport or spawn protection"); + this.cannot_attack_pets_teleport_or_spawn_protection_attacker = getString("messages.cannot_attack_pets_teleport_or_spawn_protection_attacker", "You can't attack pets while you have teleport or spawn protection"); + this.cannot_attack_mounts_teleport_or_spawn_protection_attacker = getString("messages.cannot_attack_mounts_teleport_or_spawn_protection_attacker", "You can't attack mounts while you have teleport or spawn protection"); + this.cannot_attack_teleport_or_spawn_protection_victim = getString("messages.cannot_attack_teleport_or_spawn_protection_victim", "You can't attack players while you have teleport or spawn protection"); + try { + config.save(new File(plugin.getDataFolder(), "config.yml")); + } catch (IOException e) { + plugin.getLogger().severe("Failed to save configuration file! - " + e.getLocalizedMessage()); + } } @@ -101,5 +158,48 @@ public class ConfigCache { return Collections.unmodifiableSet(combatBlockedCommands); } + private String getString(String path, @NotNull String def) { + return getString(path, def, null); + } + + private String getString(String path, @NotNull String def, @Nullable List comments) { + if (config.isSet(path)) return config.getString(path, def); + config.set(path, def); + if (comments != null) config.setComments(path, comments); + return def; + } + + private boolean getBoolean(String path, boolean def, @Nullable List comments) { + if (config.isSet(path)) return config.getBoolean(path, def); + config.set(path, def); + if (comments != null) config.setComments(path, comments); + return def; + } + + private double getDouble(String path, double def, @Nullable List comments) { + if (config.isSet(path)) return config.getDouble(path, def); + config.set(path, def); + if (comments != null) config.setComments(path, comments); + return def; + } + + private long getLong(String path, long def, @Nullable List comments) { + if (config.isSet(path)) return config.getLong(path, def); + config.set(path, def); + if (comments != null) config.setComments(path, comments); + return def; + } + + private List getList(String path, List def, @Nullable List comments) { + if (config.isSet(path)) return config.getStringList(path); + config.set(path, def); + if (comments != null) config.setComments(path, comments); + return def; + } + + private List getList(String path, List def) { + return getList(path, def, null); + } + } diff --git a/src/main/java/me/youhavetrouble/preventstabby/data/PlayerData.java b/src/main/java/me/youhavetrouble/preventstabby/data/PlayerData.java index c4c289d..e14136e 100644 --- a/src/main/java/me/youhavetrouble/preventstabby/data/PlayerData.java +++ b/src/main/java/me/youhavetrouble/preventstabby/data/PlayerData.java @@ -53,7 +53,7 @@ public class PlayerData { } protected boolean isCacheExpired() { - return System.currentTimeMillis() - lastAccessTimestamp > PreventStabby.getPlugin().getConfigCache().cache_time * 1000; + return System.currentTimeMillis() - lastAccessTimestamp > 60000; } /** @@ -85,6 +85,7 @@ public class PlayerData { */ public void markInCombat() { refreshCacheTime(); + if (PreventStabby.getPlugin().getConfigCache().combat_time <= 0) return; this.combatStartTimestamp = System.currentTimeMillis(); } @@ -134,7 +135,7 @@ public class PlayerData { public long getSecondsLeftUntilCombatEnd() { if (combatStartTimestamp == null) return -1; long timeSinceCombatStart = System.currentTimeMillis() - combatStartTimestamp; - long combatTimeConfigured = PreventStabby.getPlugin().getConfigCache().combat_time * 1000; + long combatTimeConfigured = (long) (PreventStabby.getPlugin().getConfigCache().combat_time * 1000); return (timeSinceCombatStart < combatTimeConfigured) ? (combatTimeConfigured - timeSinceCombatStart) / 1000 : -1; } diff --git a/src/main/java/me/youhavetrouble/preventstabby/data/PlayerManager.java b/src/main/java/me/youhavetrouble/preventstabby/data/PlayerManager.java index 3432325..44bceda 100644 --- a/src/main/java/me/youhavetrouble/preventstabby/data/PlayerManager.java +++ b/src/main/java/me/youhavetrouble/preventstabby/data/PlayerManager.java @@ -66,6 +66,7 @@ public class PlayerManager { continue; } // entering combat logic + if (plugin.getConfigCache().combat_time <= 0) return; // disable combat entering when combat time is <0 if (!playerData.getLastCombatCheckState() && playerData.isInCombat()) { PlayerEnterCombatEvent enterCombatEvent = null; if (PlayerEnterCombatEvent.getHandlerList().getRegisteredListeners().length > 0) { diff --git a/src/main/java/me/youhavetrouble/preventstabby/listeners/EnvironmentalListener.java b/src/main/java/me/youhavetrouble/preventstabby/listeners/EnvironmentalListener.java index 042e516..3d25af5 100644 --- a/src/main/java/me/youhavetrouble/preventstabby/listeners/EnvironmentalListener.java +++ b/src/main/java/me/youhavetrouble/preventstabby/listeners/EnvironmentalListener.java @@ -33,11 +33,11 @@ public class EnvironmentalListener implements Listener { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onDangerousBucketDump(PlayerBucketEmptyEvent event) { ConfigCache config = plugin.getConfigCache(); - if (!config.lava_and_fire_stopper_enabled) return; + if (!config.bucket_stopper_enabled) return; if (!dangerousBuckets.contains(event.getBucket())) return; Location location = event.getBlockClicked().getLocation(); Player placer = event.getPlayer(); - double radius = config.lava_and_fire_stopper_radius; + double radius = config.bucket_stopper_radius; BoundingBox boundingBox = BoundingBox.of(location.toVector(), radius, radius, radius); for (Entity victim : location.getWorld().getNearbyEntities(boundingBox)) { @@ -54,13 +54,13 @@ public class EnvironmentalListener implements Listener { @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onBlockIgnite(BlockIgniteEvent event) { ConfigCache config = plugin.getConfigCache(); - if (!config.lava_and_fire_stopper_enabled) return; + if (!config.fire_stopper_enabled) return; Entity igniter = event.getIgnitingEntity(); if (igniter == null) return; Target igniterTarget = Target.getTarget(igniter); if (igniterTarget == null) return; Location location = event.getBlock().getLocation(); - double radius = config.lava_and_fire_stopper_radius; + double radius = config.fire_stopper_radius; BoundingBox boundingBox = BoundingBox.of(location.toVector(), radius, radius, radius); for (Entity victim : location.getWorld().getNearbyEntities(boundingBox)) {