Initial pickup collection toggle command

This commit is contained in:
MartenM 2022-11-30 21:14:21 +01:00
parent 82c42449a8
commit e2e5c84c08
9 changed files with 171 additions and 34 deletions

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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));
}

View File

@ -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<String> tabComplete(@NotNull CommandSender commandSender, @NotNull String s, @NotNull String s1, @NotNull String[] strings) {
return null;
}
}

View File

@ -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

View File

@ -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<UUID> 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<Entity> 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<Integer, ItemStack> 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<Entity> 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<Integer, ItemStack> 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;
}
}

View File

@ -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()
{

View File

@ -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";