diff --git a/src/main/java/com/froobworld/nabsuite/modules/mechs/MechsModule.java b/src/main/java/com/froobworld/nabsuite/modules/mechs/MechsModule.java index c7e85df..834e9f2 100644 --- a/src/main/java/com/froobworld/nabsuite/modules/mechs/MechsModule.java +++ b/src/main/java/com/froobworld/nabsuite/modules/mechs/MechsModule.java @@ -5,9 +5,9 @@ import com.froobworld.nabsuite.modules.mechs.border.WorldBorderManager; import com.froobworld.nabsuite.modules.mechs.chat.ClickableLinkReplacer; import com.froobworld.nabsuite.modules.mechs.command.BorderWarningCommand; -import com.froobworld.nabsuite.modules.mechs.command.EffectiveViewDistanceCommand; import com.froobworld.nabsuite.modules.mechs.command.PvpCommand; import com.froobworld.nabsuite.modules.mechs.command.ToggleViewDistanceCommand; +import com.froobworld.nabsuite.modules.mechs.command.NoReplantCommand; import com.froobworld.nabsuite.modules.mechs.config.MechsConfig; import com.froobworld.nabsuite.modules.mechs.signedit.SignEditDisabler; import com.froobworld.nabsuite.modules.mechs.mobgriefing.MobGriefingManager; @@ -50,7 +50,8 @@ public void onEnable() { new PvpCommand(this), new ToggleViewDistanceCommand(this), //new EffectiveViewDistanceCommand(), - new BorderWarningCommand(this) + new BorderWarningCommand(this), + new NoReplantCommand(this) ).forEach(getPlugin().getCommandManager()::registerCommand); } @@ -68,6 +69,10 @@ public PvpManager getPvpManager() { return pvpManager; } + public TreeManager getTreeManager() { + return treeManager; + } + public ViewDistanceManager getViewDistanceManager() { return viewDistanceManager; } diff --git a/src/main/java/com/froobworld/nabsuite/modules/mechs/command/NoReplantCommand.java b/src/main/java/com/froobworld/nabsuite/modules/mechs/command/NoReplantCommand.java new file mode 100644 index 0000000..9815c01 --- /dev/null +++ b/src/main/java/com/froobworld/nabsuite/modules/mechs/command/NoReplantCommand.java @@ -0,0 +1,42 @@ +package com.froobworld.nabsuite.modules.mechs.command; + +import cloud.commandframework.Command; +import cloud.commandframework.context.CommandContext; +import com.froobworld.nabsuite.command.NabCommand; +import com.froobworld.nabsuite.modules.mechs.MechsModule; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.format.NamedTextColor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class NoReplantCommand extends NabCommand { + private final MechsModule mechsModule; + + public NoReplantCommand(MechsModule mechsModule) { + super( + "noreplant", + "Toggle automatic tree replanting.", + "nabsuite.command.noreplant", + Player.class + ); + this.mechsModule = mechsModule; + } + + @Override + public void execute(CommandContext context) { + Player sender = (Player) context.getSender(); + boolean current = mechsModule.getTreeManager().replantEnabled(sender); + + mechsModule.getTreeManager().setReplantEnabled(sender, !current); + if (current) { + sender.sendMessage(Component.text("Automatic tree replanting disabled temporarily.", NamedTextColor.YELLOW)); + } else { + sender.sendMessage(Component.text("Automatic tree replanting enabled.", NamedTextColor.YELLOW)); + } + } + + @Override + public Command.Builder populateBuilder(Command.Builder builder) { + return builder; + } +} diff --git a/src/main/java/com/froobworld/nabsuite/modules/mechs/trees/TreeManager.java b/src/main/java/com/froobworld/nabsuite/modules/mechs/trees/TreeManager.java index eaf47bd..fe65cd7 100644 --- a/src/main/java/com/froobworld/nabsuite/modules/mechs/trees/TreeManager.java +++ b/src/main/java/com/froobworld/nabsuite/modules/mechs/trees/TreeManager.java @@ -8,13 +8,18 @@ import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Tag; +import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockPlaceEvent; +import org.bukkit.event.player.PlayerQuitEvent; import java.io.File; +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; import java.util.regex.Pattern; public class TreeManager implements Listener { @@ -22,6 +27,7 @@ public class TreeManager implements Listener { protected final DataSaver regionDataSaver; private final BiMap logRegionMap = HashBiMap.create(); private final File directory; + private final Set noreplantPlayers = new HashSet<>(); public TreeManager(MechsModule mechsModule) { directory = new File(mechsModule.getDataFolder(), "unnatural-logs/"); @@ -73,4 +79,26 @@ private void onBlockPlace(BlockPlaceEvent event) { } } + @EventHandler(priority = EventPriority.MONITOR) + private void onPlayerQuit(PlayerQuitEvent event) { + noreplantPlayers.remove(event.getPlayer().getUniqueId()); + } + + public Boolean replantEnabled(Player player) { + if (player == null) { + return true; + } + return !noreplantPlayers.contains(player.getUniqueId()); + } + + public void setReplantEnabled(Player player, boolean replant) { + if (player != null) { + if (!replant) { + noreplantPlayers.add(player.getUniqueId()); + } else { + noreplantPlayers.remove(player.getUniqueId()); + } + } + } + } diff --git a/src/main/java/com/froobworld/nabsuite/modules/mechs/trees/TreeReplanter.java b/src/main/java/com/froobworld/nabsuite/modules/mechs/trees/TreeReplanter.java index d06bce5..a8ce28c 100644 --- a/src/main/java/com/froobworld/nabsuite/modules/mechs/trees/TreeReplanter.java +++ b/src/main/java/com/froobworld/nabsuite/modules/mechs/trees/TreeReplanter.java @@ -1,10 +1,7 @@ package com.froobworld.nabsuite.modules.mechs.trees; import com.froobworld.nabsuite.modules.mechs.MechsModule; -import org.bukkit.Bukkit; -import org.bukkit.Location; -import org.bukkit.Material; -import org.bukkit.Tag; +import org.bukkit.*; import org.bukkit.block.Block; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -18,13 +15,12 @@ public class TreeReplanter implements Listener { public TreeReplanter(MechsModule mechsModule, TreeManager treeManager) { this.mechsModule = mechsModule; this.treeManager = treeManager; - } @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) private void onBlockBreak(BlockBreakEvent event) { if (Tag.LOGS.isTagged(event.getBlock().getType())) { - if (treeManager.isNaturalLog(event.getBlock().getLocation())) { + if (treeManager.replantEnabled(event.getPlayer()) && treeManager.isNaturalLog(event.getBlock().getLocation())) { Material saplingMaterial = saplingTypeForWood(event.getBlock().getType()); if (saplingMaterial != null) { schedulePlantTask(event.getBlock().getLocation(), saplingMaterial); diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index fc4987c..3d5ebfa 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -323,6 +323,8 @@ permissions: description: "Access to the /effectivevd command." nabsuite.command.borderwarning: description: "Access to the /borderwarning command." + nabsuite.command.noreplant: + description: "Access to the /noreplant command." # Mechs module other permissions nabsuite.nabmode: