This commit is contained in:
		
					parent
					
						
							
								ad36edd5cd
							
						
					
				
			
			
				commit
				
					
						e409d3e4df
					
				
			
		
					 3 changed files with 80 additions and 2 deletions
				
			
		| 
						 | 
				
			
			@ -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<String, Object> 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<String, Object> 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;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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<EntityType, Double> 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));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue