diff --git a/src/main/java/eu/m724/tweaks/TweaksConfig.java b/src/main/java/eu/m724/tweaks/TweaksConfig.java index 2c71c1f..b835162 100644 --- a/src/main/java/eu/m724/tweaks/TweaksConfig.java +++ b/src/main/java/eu/m724/tweaks/TweaksConfig.java @@ -9,6 +9,8 @@ package eu.m724.tweaks; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.Plugin; +import java.util.Map; + public record TweaksConfig( boolean metrics, boolean debug, @@ -53,7 +55,9 @@ public record TweaksConfig( String authDomain, boolean redstoneEnabled, - String redstoneListen + String redstoneListen, + + Map knockbackModifiers ) { public static final int CONFIG_VERSION = 1; private static TweaksConfig config; @@ -121,6 +125,9 @@ public record TweaksConfig( boolean redstoneEnabled = config.getBoolean("retstone.enabled"); String redstoneListen = config.getString("retstone.listen"); + // this is processed when initing + Map knockbackModifiers = config.getConfigurationSection("knockback").getValues(false); + TweaksConfig.config = new TweaksConfig( debug, metrics, locale, worldborderExpand, worldborderHide, @@ -134,7 +141,8 @@ public record TweaksConfig( hardcoreEnabled, hardcoreChance, sleepEnabled, sleepInstant, authEnabled, authForce, authHostname, - redstoneEnabled, redstoneListen + redstoneEnabled, redstoneListen, + knockbackModifiers ); return TweaksConfig.config; diff --git a/src/main/java/eu/m724/tweaks/knockback/KnockbackListener.java b/src/main/java/eu/m724/tweaks/knockback/KnockbackListener.java new file mode 100644 index 0000000..14e4764 --- /dev/null +++ b/src/main/java/eu/m724/tweaks/knockback/KnockbackListener.java @@ -0,0 +1,62 @@ +/* + * Copyright (C) 2024 Minecon724 + * Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file + * in the project root for the full license text. + */ + +package eu.m724.tweaks.knockback; + +import eu.m724.tweaks.DebugLogger; +import eu.m724.tweaks.TweaksConfig; +import org.bukkit.entity.EntityType; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityKnockbackByEntityEvent; +import org.bukkit.plugin.Plugin; + +import java.util.HashMap; +import java.util.Map; + +public class KnockbackListener implements Listener { + private final Map modifiers = new HashMap<>(); + + public KnockbackListener(Plugin plugin) { + TweaksConfig.getConfig().knockbackModifiers().forEach((k, v) -> { + EntityType type; + double mod; + + String line = "(%s: %s)".formatted(k, v); + + if (v instanceof Double d) { + mod = d; + } else if (v instanceof Integer i) { + mod = i; + } else { + DebugLogger.warning("In " + line + " the value is not a number "); + return; + } + + try { + type = EntityType.valueOf(k); + } catch (IllegalArgumentException e) { + DebugLogger.warning("In" + line + " the key is not a valid entity type"); + DebugLogger.warning("Valid entity types: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/entity/EntityType.html"); + return; + } + + if (mod == 1) return; + modifiers.put(type, mod); + }); + + if (!modifiers.isEmpty()) + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + @EventHandler + public void onEntityKnockbackByEntity(EntityKnockbackByEntityEvent event) { + var modifier = modifiers.get(event.getSourceEntity().getType()); + if (modifier != null) { + event.setFinalKnockback(event.getKnockback().multiply(modifier)); + } + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 6f58952..6c0844a 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -109,6 +109,14 @@ retstone: # This takes host:port, listens on UDP listen: 127.0.0.1:57931 +# Knockback dealt BY those entities is multiplied by value +# Entity must be one of https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/entity/EntityType.html +# 1 means no change +knockback: + player: 0.7 + tnt: 5 + creeper: 0.7 + # Finally, thank you for downloading Tweaks724, I hope you enjoy! # Don't modify unless told to