diff --git a/Minepacks/resources/config.yml b/Minepacks/resources/config.yml index 7708da9..cf1ed29 100644 --- a/Minepacks/resources/config.yml +++ b/Minepacks/resources/config.yml @@ -50,12 +50,15 @@ Cooldown: # Controls for the auto pickup on full inventory function FullInventory: - # If items should be collected to the backpack if the players inventory is full + # If items should be collected to the backpack if the players inventory is full. + # This is also the default if 'IsToggleAllowed' is enabled. CollectItems: false # Interval in seconds how often items around the player should be collected, increase it if it lags the server CheckInterval: 1 # Radius in which items get collected, in meter/blocks, allow decimals CollectRadius: 1.5 + # If this feature may be toggled. + IsToggleAllowed: false # Database settings diff --git a/Minepacks/resources/lang/en.yml b/Minepacks/resources/lang/en.yml index 59eabf9..c15bf77 100644 --- a/Minepacks/resources/lang/en.yml +++ b/Minepacks/resources/lang/en.yml @@ -60,6 +60,10 @@ Language: Cleared: "Inventory cleared." ClearedOther: "{DisplayName}'s&r inventory has been cleared." ClearedOtherTarget: "Your inventory has been cleared by {DisplayName}&r." + Pickup: + NotEnabled: "&cNot allowed. &7This server doesn't allow you to change the toggle item collection." + ToggleOn: "&7Automatic item collection has been toggled &aON&7." + ToggleOff: "&7Automatic item collection has been toggled &cOFF&7." Commands: HelpFormat: "[\"\",{\"text\":\"/{MainCommand} {SubCommand} {Parameters}\",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"/{MainCommand} {SubCommand}\"}},{\"text\":\" - \",\"color\":\"white\"},{\"text\":\"{Description}\",\"color\":\"aqua\"}]" PlayerNameVariable: "player_name" @@ -78,6 +82,7 @@ Language: RestoreList: "Lists all available backups." Help: "Shows all available commands and their description." Migrate: "Migrates the used database from one type to another." + Pickup: "Toggle the state of the automatic pickup when the inventory is full." Command: Backpack: @@ -110,6 +115,9 @@ Command: - clear - inventoryclear - clean + Pickup: + - pickup + - toggle # Will be shown in the console during startup LanguageName: "english" diff --git a/Minepacks/resources/plugin.yml b/Minepacks/resources/plugin.yml index 7c8da48..c08811b 100644 --- a/Minepacks/resources/plugin.yml +++ b/Minepacks/resources/plugin.yml @@ -84,6 +84,9 @@ permissions: backpack.fullpickup: description: Allows the player to automatically pick up items when their inventory is full (function needs to be enabled in the config) defaut: true + backpack.fullpickup.toggle: + description: Allows the player to toggle the automatic pickup feature. + default: true backpack.clean.other: description: Allows the player to clean other players backpacks. default: op diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Command/CommandManager.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Command/CommandManager.java index 8d35311..8bb5b22 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Command/CommandManager.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Command/CommandManager.java @@ -81,6 +81,7 @@ public CommandManager(@NotNull Minepacks plugin) registerSubCommand(new RestoreCommand(plugin)); registerSubCommand(new MigrateCommand(plugin)); registerSubCommand(new VersionCommand(plugin)); + registerSubCommand(new PickupCommand(plugin)); registerSubCommand(new DebugCommand(plugin)); registerSubCommand(new HelpCommand(plugin, commands, this)); } diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Command/PickupCommand.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Command/PickupCommand.java new file mode 100644 index 0000000..7775eea --- /dev/null +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Command/PickupCommand.java @@ -0,0 +1,55 @@ +package at.pcgamingfreaks.Minepacks.Bukkit.Command; + +import at.pcgamingfreaks.Bukkit.Message.Message; +import at.pcgamingfreaks.Minepacks.Bukkit.API.MinepacksCommand; +import at.pcgamingfreaks.Minepacks.Bukkit.ItemsCollector; +import at.pcgamingfreaks.Minepacks.Bukkit.Minepacks; +import at.pcgamingfreaks.Minepacks.Bukkit.Permissions; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +public class PickupCommand extends MinepacksCommand { + + private final Minepacks plugin; + private final Message featureNotEnabled; + + private final Message toggleOn; + private final Message toggleOff; + + public PickupCommand(Minepacks plugin) { + super(plugin, "pickup", plugin.getLanguage().getTranslated("Commands.Description.Pickup"), Permissions.PICKUP_TOGGLE, true, plugin.getLanguage().getCommandAliases("Pickup")); + + this.plugin = plugin; + featureNotEnabled = plugin.getLanguage().getMessage("Ingame.Pickup.NotEnabled"); + toggleOn = plugin.getLanguage().getMessage("Ingame.Pickup.ToggleOn"); + toggleOff = plugin.getLanguage().getMessage("Ingame.Pickup.ToggleOff"); + } + + @Override + public void execute(@NotNull CommandSender sender, @NotNull String s, @NotNull String s1, @NotNull String[] args) { + Player player = (Player) sender; + ItemsCollector collector = plugin.getItemsCollector(); + + if (collector == null || !collector.isToggleable()) { + featureNotEnabled.send(player); + return; + } + + boolean isEnabled = collector.toggleState(player.getUniqueId()); + + if (isEnabled) { + toggleOn.send(player); + return; + } + + toggleOff.send(player); + } + + @Override + public List tabComplete(@NotNull CommandSender commandSender, @NotNull String s, @NotNull String s1, @NotNull String[] strings) { + return null; + } +} diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Config.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Config.java index d4a84bf..4929845 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Config.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Database/Config.java @@ -317,6 +317,14 @@ public double getFullInvRadius() { return getConfigE().getDouble("FullInventory.CollectRadius", 1.5); // in blocks } + + public boolean isToggleAllowed() { + return getConfigE().getBoolean("FullInventory.IsToggleAllowed", false); + } + + public boolean isEnabledOnJoin() { + return getFullInvCollect(); + } //endregion //region Shulkerboxes diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/ItemsCollector.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/ItemsCollector.java index 643ba33..04e49eb 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/ItemsCollector.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/ItemsCollector.java @@ -28,8 +28,7 @@ import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitTask; -import java.util.List; -import java.util.Map; +import java.util.*; public class ItemsCollector extends BukkitRunnable { @@ -38,10 +37,30 @@ public class ItemsCollector extends BukkitRunnable private final BukkitTask task; private final ItemFilter itemFilter; + /** + * Is the feature enabled? + */ + private final boolean isToggleable; + + /** + * Default on join? + */ + private final boolean enabledOnJoin; + + /** + * List of players that toggled the feature. + */ + private final Set toggleList; + public ItemsCollector(Minepacks plugin) { this.plugin = plugin; this.radius = plugin.getConfiguration().getFullInvRadius(); + + this.isToggleable = plugin.getConfiguration().isToggleAllowed(); + this.enabledOnJoin = plugin.getConfiguration().isEnabledOnJoin(); + this.toggleList = new HashSet<>(); + task = runTaskTimer(plugin, plugin.getConfiguration().getFullInvCheckInterval(), plugin.getConfiguration().getFullInvCheckInterval()); itemFilter = plugin.getItemFilter(); } @@ -52,37 +71,42 @@ public void run() for(Player player : Bukkit.getServer().getOnlinePlayers()) { if(plugin.isDisabled(player) != WorldBlacklistMode.None) return; - if(player.getInventory().firstEmpty() == -1 && player.hasPermission(Permissions.USE) && player.hasPermission(Permissions.FULL_PICKUP)) + + // Check toggle + if (!isToggleable || !isPickupEnabled(player.getUniqueId())) return; + + // No permission ot use the backpack. + if (!player.hasPermission(Permissions.USE)) return; + + // If a player has either of these permissions, pickup is allowed. + if (!player.hasPermission(Permissions.FULL_PICKUP) && !player.hasPermission(Permissions.PICKUP_TOGGLE)); + + // Inventory is full + if (player.getInventory().firstEmpty() != -1) return; + + // Only check loaded backpacks (loading them would take too much time for a repeating task, the backpack will be loaded async soon enough) + Backpack backpack = (Backpack) plugin.getBackpackCachedOnly(player); + if(backpack == null) { - // Only check loaded backpacks (loading them would take too much time for a repeating task, the backpack will be loaded async soon enough) - Backpack backpack = (Backpack) plugin.getBackpackCachedOnly(player); - if(backpack == null) - { - continue; - } - List entities = player.getNearbyEntities(radius, radius, radius); - for(Entity entity : entities) - { - if(entity instanceof Item) - { - Item item = (Item) entity; - if(!item.isDead() && item.getPickupDelay() <= 0) - { - Map leftover = player.getInventory().addItem(item.getItemStack()); - if(!leftover.isEmpty()) - { - ItemStack itemStack = leftover.get(0); - if(itemStack == null || itemStack.getAmount() == 0 || (itemFilter != null && itemFilter.isItemBlocked(itemStack))) continue; - leftover = backpack.addItems(itemStack); - } - if(!leftover.isEmpty()) - { - item.setItemStack(leftover.get(0)); - } - else - { - item.remove(); - } + continue; + } + List entities = player.getNearbyEntities(radius, radius, radius); + for(Entity entity : entities) + { + if(entity instanceof Item) { + Item item = (Item) entity; + if (!item.isDead() && item.getPickupDelay() <= 0) { + Map leftover = player.getInventory().addItem(item.getItemStack()); + if (!leftover.isEmpty()) { + ItemStack itemStack = leftover.get(0); + if (itemStack == null || itemStack.getAmount() == 0 || (itemFilter != null && itemFilter.isItemBlocked(itemStack))) + continue; + leftover = backpack.addItems(itemStack); + } + if (!leftover.isEmpty()) { + item.setItemStack(leftover.get(0)); + } else { + item.remove(); } } } @@ -94,4 +118,34 @@ public void close() { task.cancel(); } + + /** + * Toggles the automatic collection for the player. + * @param uuid The players UUID + * @return The new state. True = collection enabled. + */ + public boolean toggleState(UUID uuid) { + boolean removed = toggleList.remove(uuid); + if (!removed) { + toggleList.add(uuid); + } + return isPickupEnabled(uuid); + } + + /** + * The item pickup state for a certain player. + * @param uuid The player uuid + * @return true if enabled + */ + public boolean isPickupEnabled(UUID uuid) { + return enabledOnJoin ^ toggleList.contains(uuid); + } + + /** + * If this feature is enabled or not. + * @return + */ + public boolean isToggleable() { + return isToggleable; + } } \ No newline at end of file diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java index b846d00..7ced67d 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Minepacks.java @@ -216,7 +216,7 @@ private void load() else shortcut = null; if(config.isWorldWhitelistMode()) pluginManager.registerEvents(new WorldBlacklistUpdater(this), this); //endregion - if(config.getFullInvCollect()) collector = new ItemsCollector(this); + if(config.getFullInvCollect() || config.isToggleAllowed()) collector = new ItemsCollector(this); worldBlacklist = config.getWorldBlacklist(); worldBlacklistMode = (worldBlacklist.size() == 0) ? WorldBlacklistMode.None : config.getWorldBlockMode(); @@ -402,6 +402,10 @@ public boolean isBackpackItem(final @Nullable ItemStack itemStack) return shortcut.isItemShortcut(itemStack); } + public ItemsCollector getItemsCollector() { + return collector; + } + @Override public @NotNull Version getVersion() { diff --git a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Permissions.java b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Permissions.java index e3a2f0d..587009b 100644 --- a/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Permissions.java +++ b/Minepacks/src/at/pcgamingfreaks/Minepacks/Bukkit/Permissions.java @@ -25,6 +25,7 @@ public class Permissions public static final String CLEAN = BASE + "clean"; public static final String CLEAN_OTHER = BASE + "clean.other"; public static final String FULL_PICKUP = BASE + "fullpickup"; + public static final String PICKUP_TOGGLE = BASE + "fullpickup.toggle"; public static final String OTHERS = BASE + "others"; public static final String OTHERS_EDIT = BASE + "others.edit"; public static final String KEEP_ON_DEATH = BASE + "keepOnDeath";