tweaks724/src/main/java/eu/m724/tweaks/module/knockback/KnockbackModule.java
2025-01-24 10:05:49 +01:00

69 lines
2.3 KiB
Java

/*
* Copyright (C) 2025 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.module.knockback;
import eu.m724.tweaks.DebugLogger;
import eu.m724.tweaks.module.TweaksModule;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityKnockbackByEntityEvent;
import org.bukkit.util.Vector;
import java.util.HashMap;
import java.util.Map;
public class KnockbackModule extends TweaksModule implements Listener {
private final Map<EntityType, Vector> modifiers = new HashMap<>();
@Override
protected void onInit() {
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, new Vector(mod, mod >= 1 ? mod : 1, mod)); // don't touch vertical
});
if (!modifiers.isEmpty()) {
registerEvents(this);
try {
Class.forName("com.destroystokyo.paper.event.entity.EntityKnockbackByEntityEvent");
DebugLogger.warning("Ignore that. Server performance will NOT be affected.");
} catch (ClassNotFoundException ignored) { }
}
}
@EventHandler
public void onEntityKnockbackByEntity(EntityKnockbackByEntityEvent event) {
var modifier = modifiers.get(event.getSourceEntity().getType());
if (modifier != null) {
event.setFinalKnockback(event.getKnockback().multiply(modifier));
}
}
}