From e59b95e2ef4e11dae173ed8c96546a760faf1d3f Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Thu, 2 Jan 2025 20:29:59 +0100 Subject: [PATCH] Auto swing mode --- .../java/eu/m724/tweaks/TweaksConfig.java | 8 +--- .../eu/m724/tweaks/sleep/SleepListener.java | 4 +- .../eu/m724/tweaks/swing/SwingManager.java | 38 ++++++------------- src/main/resources/config.yml | 7 +--- 4 files changed, 17 insertions(+), 40 deletions(-) diff --git a/src/main/java/eu/m724/tweaks/TweaksConfig.java b/src/main/java/eu/m724/tweaks/TweaksConfig.java index 4a34ec7..4196fab 100644 --- a/src/main/java/eu/m724/tweaks/TweaksConfig.java +++ b/src/main/java/eu/m724/tweaks/TweaksConfig.java @@ -63,9 +63,7 @@ public record TweaksConfig( boolean killswitchEnabled, String killswitchListen, - boolean swingEnabled, - boolean swingSword, - int swingMode + boolean swingEnabled ) { public static final int CONFIG_VERSION = 2; private static TweaksConfig config; @@ -142,8 +140,6 @@ public record TweaksConfig( String killswitchListen = config.getString("killswitch.listen"); boolean swingEnabled = config.getBoolean("swing.enabled"); - boolean swingSword = config.getBoolean("swing.sword"); - int swingMode = config.getInt("swing.mode"); TweaksConfig.config = new TweaksConfig( debug, metrics, locale, @@ -161,7 +157,7 @@ public record TweaksConfig( redstoneEnabled, redstoneListen, knockbackModifiers, killswitchEnabled, killswitchListen, - swingEnabled, swingSword, swingMode + swingEnabled ); return TweaksConfig.config; diff --git a/src/main/java/eu/m724/tweaks/sleep/SleepListener.java b/src/main/java/eu/m724/tweaks/sleep/SleepListener.java index ad3736e..8aa5579 100644 --- a/src/main/java/eu/m724/tweaks/sleep/SleepListener.java +++ b/src/main/java/eu/m724/tweaks/sleep/SleepListener.java @@ -38,13 +38,13 @@ public class SleepListener implements Listener { lastDay = day; if (!skippedCurrentNight.contains(event.getPlayer())) { - event.getPlayer().setHealth(event.getPlayer().getHealth() + heal); - if (instant) { double onePlayerRatio = 1 / (event.getPlayer().getServer().getOnlinePlayers().size() * (world.getGameRuleValue(GameRule.PLAYERS_SLEEPING_PERCENTAGE) / 100.0)); world.setTime(Math.min(world.getTime() + (long) (10917 * onePlayerRatio), 23459)); skippedCurrentNight.add(event.getPlayer()); } + + event.getPlayer().setHealth(event.getPlayer().getHealth() + heal); } } diff --git a/src/main/java/eu/m724/tweaks/swing/SwingManager.java b/src/main/java/eu/m724/tweaks/swing/SwingManager.java index b7ccb5f..b3cd2cb 100644 --- a/src/main/java/eu/m724/tweaks/swing/SwingManager.java +++ b/src/main/java/eu/m724/tweaks/swing/SwingManager.java @@ -7,7 +7,6 @@ package eu.m724.tweaks.swing; import eu.m724.tweaks.DebugLogger; -import eu.m724.tweaks.TweaksConfig; import org.bukkit.Material; import org.bukkit.attribute.Attribute; import org.bukkit.entity.Entity; @@ -24,25 +23,15 @@ import java.util.Set; public class SwingManager implements Listener { private final Plugin plugin; - private final int mode; - private final boolean useTools; - private Set tools = new HashSet<>(); + private final Set tools = new HashSet<>(); public SwingManager(Plugin plugin) { this.plugin = plugin; - this.mode = TweaksConfig.getConfig().swingMode(); + Arrays.stream(Material.values()) + .filter(m -> m.name().contains("SWORD")) + .forEach(tools::add); - if (mode != 0 && mode != 1) - throw new IllegalArgumentException("Mode " + mode + " is invalid. It must be 0 or 1."); - - if (TweaksConfig.getConfig().swingSword()) { - Arrays.stream(Material.values()) - .filter(m -> m.name().contains("SWORD")) - .forEach(m -> tools.add(m)); - } - - this.useTools = !tools.isEmpty(); DebugLogger.fine("Tools: " + tools.size()); } @@ -56,20 +45,10 @@ public class SwingManager implements Listener { if (type.isOccluding()) return; var player = event.getPlayer(); - - if (useTools) { - var tool = player.getInventory().getItemInMainHand().getType(); - if (!tools.contains(tool)) return; - } - + var tool = player.getInventory().getItemInMainHand().getType(); Entity entity = null; - if (mode == 0) { - entity = event.getBlock().getWorld() - .getNearbyEntities(event.getBlock().getLocation().add(0.5, 0.5, 0.5), 0.5, 0.5, 0.5) - .stream().filter(e -> (e instanceof LivingEntity && e != player)) - .findFirst().orElse(null); - } else if (mode == 1) { + if (tools.contains(tool)) { // if sword, raycast to hit farther var result = player.getWorld().rayTraceEntities( player.getEyeLocation(), player.getEyeLocation().getDirection(), @@ -79,6 +58,11 @@ public class SwingManager implements Listener { if (result != null) entity = result.getHitEntity(); + } else { + entity = event.getBlock().getWorld() + .getNearbyEntities(event.getBlock().getLocation().add(0.5, 0.5, 0.5), 0.5, 0.5, 0.5) + .stream().filter(e -> (e instanceof LivingEntity && e != player)) + .findFirst().orElse(null); } if (entity != null) { diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 4aa2426..dd439da 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -129,13 +129,10 @@ killswitch: listen: 127.0.0.1:57932 # Swing through grass (and alike) +# If using sword, you can also hit behind the grass +# If not, you can only hit the entity in the grass swing: enabled: true - # Only sword - sword: false - # 0 - entity inside broken block / can't attack entity behind the block - # 1 - raycasted from player / can attack entity behind the block (within normal reach) - mode: 0 # Finally, thank you for downloading Tweaks724, I hope you enjoy!