diff --git a/README.md b/README.md index 8b6db43..7d830ff 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,9 @@ Control knockback dealt by entities Quickly kills (terminates) the server on trigger, via command or HTTP request. [KILLSWITCH.md for more info](/Minecon724/tweaks724/src/branch/master/docs/KILLSWITCH.md) +### Swing through grass + + ### Utility commands diff --git a/src/main/java/eu/m724/tweaks/TweaksConfig.java b/src/main/java/eu/m724/tweaks/TweaksConfig.java index 7a791cc..011e74e 100644 --- a/src/main/java/eu/m724/tweaks/TweaksConfig.java +++ b/src/main/java/eu/m724/tweaks/TweaksConfig.java @@ -60,7 +60,11 @@ public record TweaksConfig( Map knockbackModifiers, boolean killswitchEnabled, - String killswitchListen + String killswitchListen, + + boolean swingEnabled, + boolean swingSword, + int swingMode ) { public static final int CONFIG_VERSION = 2; private static TweaksConfig config; @@ -135,6 +139,10 @@ public record TweaksConfig( boolean killswitchEnabled = config.getBoolean("killswitch.enabled"); 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, worldborderExpand, worldborderHide, @@ -150,7 +158,8 @@ public record TweaksConfig( authEnabled, authForce, authHostname, redstoneEnabled, redstoneListen, knockbackModifiers, - killswitchEnabled, killswitchListen + killswitchEnabled, killswitchListen, + swingEnabled, swingSword, swingMode ); return TweaksConfig.config; diff --git a/src/main/java/eu/m724/tweaks/TweaksPlugin.java b/src/main/java/eu/m724/tweaks/TweaksPlugin.java index 575b935..b0abd9a 100644 --- a/src/main/java/eu/m724/tweaks/TweaksPlugin.java +++ b/src/main/java/eu/m724/tweaks/TweaksPlugin.java @@ -22,6 +22,7 @@ import eu.m724.tweaks.ping.PingChecker; import eu.m724.tweaks.pomodoro.PomodoroManager; import eu.m724.tweaks.redstone.RedstoneManager; import eu.m724.tweaks.sleep.SleepManager; +import eu.m724.tweaks.swing.SwingManager; import eu.m724.tweaks.updater.UpdaterManager; import eu.m724.tweaks.worldborder.WorldBorderExpander; import eu.m724.tweaks.worldborder.WorldBorderHider; @@ -135,6 +136,11 @@ public class TweaksPlugin extends MStatsPlugin { new KillswitchManager(this).init(getCommand("servkill")); } + if (config.swingEnabled()) { + DebugLogger.fine("Enabling Swing"); + new SwingManager(this).init(); + } + /* end modules */ if (config.metrics()) { diff --git a/src/main/java/eu/m724/tweaks/swing/SwingManager.java b/src/main/java/eu/m724/tweaks/swing/SwingManager.java new file mode 100644 index 0000000..6acfd75 --- /dev/null +++ b/src/main/java/eu/m724/tweaks/swing/SwingManager.java @@ -0,0 +1,89 @@ +/* + * 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.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; +import org.bukkit.entity.LivingEntity; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.plugin.Plugin; + +import java.util.Arrays; +import java.util.HashSet; +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<>(); + + public SwingManager(Plugin plugin) { + this.plugin = plugin; + + this.mode = TweaksConfig.getConfig().swingMode(); + + 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()); + } + + public void init() { + plugin.getServer().getPluginManager().registerEvents(this, plugin); + } + + @EventHandler + public void onBreak(BlockBreakEvent event) { + var type = event.getBlock().getType(); + if (type.isOccluding()) return; + + var player = event.getPlayer(); + + if (useTools) { + var tool = player.getInventory().getItemInMainHand().getType(); + if (!tools.contains(tool)) return; + } + + 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) { + var result = player.getWorld().rayTraceEntities( + player.getEyeLocation(), + player.getEyeLocation().getDirection(), + player.getAttribute(Attribute.ENTITY_INTERACTION_RANGE).getValue(), + e -> e != player + ); + + if (result != null) + entity = result.getHitEntity(); + } + + if (entity != null) { + player.attack(entity); + DebugLogger.fine("Swing " + player.getName() + " hit " + entity.getName()); + } + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 9c49759..4524099 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -126,6 +126,15 @@ killswitch: # To disable HTTP server, set to null listen: 127.0.0.1:57932 +# Swing through grass (and alike) +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!