mirror of
https://github.com/GeorgH93/Minepacks.git
synced 2024-11-14 10:45:23 +01:00
Fix issues with auto pickup (#237)
This commit is contained in:
parent
8102e11759
commit
26815d6aee
@ -219,4 +219,4 @@ Misc:
|
||||
UseBungeeCord: false
|
||||
|
||||
# Config file version. Don't touch it!
|
||||
Version: 32
|
||||
Version: 33
|
@ -61,7 +61,6 @@ Language:
|
||||
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:
|
||||
@ -123,4 +122,4 @@ LanguageName: "english"
|
||||
Author: "GeorgH93"
|
||||
|
||||
# Language file version. Don't touch it!
|
||||
Version: 19
|
||||
Version: 20
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) 2020 GeorgH93
|
||||
* Copyright (C) 2022 GeorgH93
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
@ -18,9 +18,9 @@
|
||||
package at.pcgamingfreaks.Minepacks.Bukkit.Command;
|
||||
|
||||
import at.pcgamingfreaks.Bukkit.Command.CommandExecutorWithSubCommandsGeneric;
|
||||
import at.pcgamingfreaks.Bukkit.Command.RegisterablePluginCommand;
|
||||
import at.pcgamingfreaks.Bukkit.MCVersion;
|
||||
import at.pcgamingfreaks.Bukkit.Message.Message;
|
||||
import at.pcgamingfreaks.Bukkit.Command.RegisterablePluginCommand;
|
||||
import at.pcgamingfreaks.Command.HelpData;
|
||||
import at.pcgamingfreaks.ConsoleColor;
|
||||
import at.pcgamingfreaks.Minepacks.Bukkit.API.MinepacksCommand;
|
||||
@ -81,7 +81,7 @@ public CommandManager(@NotNull Minepacks plugin)
|
||||
registerSubCommand(new RestoreCommand(plugin));
|
||||
registerSubCommand(new MigrateCommand(plugin));
|
||||
registerSubCommand(new VersionCommand(plugin));
|
||||
registerSubCommand(new PickupCommand(plugin));
|
||||
if (plugin.getConfiguration().getFullInvCollect() && plugin.getConfiguration().isToggleAllowed()) registerSubCommand(new PickupCommand(plugin));
|
||||
registerSubCommand(new DebugCommand(plugin));
|
||||
registerSubCommand(new HelpCommand(plugin, commands, this));
|
||||
}
|
||||
@ -129,7 +129,6 @@ public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Comman
|
||||
{
|
||||
case Message: plugin.messageWorldDisabled.send(sender); break;
|
||||
case MissingPermission: plugin.messageNoPermission.send(sender); break;
|
||||
case NoPlugin: return null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
@ -1,3 +1,20 @@
|
||||
/*
|
||||
* Copyright (C) 2022 GeorgH93
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package at.pcgamingfreaks.Minepacks.Bukkit.Command;
|
||||
|
||||
import at.pcgamingfreaks.Bukkit.Message.Message;
|
||||
@ -5,51 +22,54 @@
|
||||
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 {
|
||||
|
||||
public class PickupCommand extends MinepacksCommand
|
||||
{
|
||||
private final Minepacks plugin;
|
||||
private final Message featureNotEnabled;
|
||||
private final Message toggleOn, toggleOff;
|
||||
|
||||
private final Message toggleOn;
|
||||
private final Message toggleOff;
|
||||
|
||||
public PickupCommand(Minepacks plugin) {
|
||||
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");
|
||||
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) {
|
||||
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;
|
||||
}
|
||||
if (collector == null) return;
|
||||
|
||||
boolean isEnabled = collector.toggleState(player.getUniqueId());
|
||||
|
||||
if (isEnabled) {
|
||||
if (collector.toggleState(player.getUniqueId()))
|
||||
{
|
||||
toggleOn.send(player);
|
||||
return;
|
||||
}
|
||||
|
||||
toggleOff.send(player);
|
||||
else
|
||||
{
|
||||
toggleOff.send(player);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> tabComplete(@NotNull CommandSender commandSender, @NotNull String s, @NotNull String s1, @NotNull String[] strings) {
|
||||
public List<String> tabComplete(@NotNull CommandSender commandSender, @NotNull String s, @NotNull String s1, @NotNull String[] strings)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canUse(@NotNull CommandSender sender)
|
||||
{
|
||||
return canUse(sender) && sender.hasPermission(Permissions.FULL_PICKUP) && sender.hasPermission(Permissions.USE);
|
||||
}
|
||||
}
|
||||
|
@ -40,7 +40,7 @@
|
||||
|
||||
public class Config extends Configuration implements DatabaseConnectionConfiguration, ILanguageConfiguration
|
||||
{
|
||||
private static final Version CONFIG_VERSION = new Version(32), PRE_V2_VERSION = new Version(20);
|
||||
private static final Version CONFIG_VERSION = new Version(33), PRE_V2_VERSION = new Version(20);
|
||||
|
||||
public Config(Minepacks plugin)
|
||||
{
|
||||
|
@ -30,7 +30,7 @@
|
||||
|
||||
public class Language extends at.pcgamingfreaks.Bukkit.Config.Language
|
||||
{
|
||||
private static final Version LANG_VERSION = new Version(19);
|
||||
private static final Version LANG_VERSION = new Version(20);
|
||||
|
||||
public Language(Minepacks plugin)
|
||||
{
|
||||
|
@ -65,47 +65,56 @@ public ItemsCollector(Minepacks plugin)
|
||||
itemFilter = plugin.getItemFilter();
|
||||
}
|
||||
|
||||
private boolean canUseAutoPickup(Player player)
|
||||
{
|
||||
// Check if player can use in world
|
||||
if(plugin.isDisabled(player) != WorldBlacklistMode.None) return false;
|
||||
|
||||
// Check if toggle is enabled AND player has been disabled, return.
|
||||
if (isToggleable && !isPickupEnabled(player.getUniqueId())) return false;
|
||||
|
||||
// Check permission
|
||||
return (player.hasPermission(Permissions.USE) && player.hasPermission(Permissions.FULL_PICKUP));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
for(Player player : Bukkit.getServer().getOnlinePlayers())
|
||||
{
|
||||
if(plugin.isDisabled(player) != WorldBlacklistMode.None) return;
|
||||
|
||||
// If toggle is enabled AND player has been disabled, return.
|
||||
if (isToggleable && !isPickupEnabled(player.getUniqueId())) return;
|
||||
|
||||
// No permission ot use the backpack.
|
||||
if (!player.hasPermission(Permissions.USE)) return;
|
||||
|
||||
// Check if auto pickup is allowed
|
||||
if (!player.hasPermission(Permissions.FULL_PICKUP)) return;
|
||||
if (!canUseAutoPickup(player)) continue;
|
||||
|
||||
// Inventory is full
|
||||
if (player.getInventory().firstEmpty() != -1) return;
|
||||
if (player.getInventory().firstEmpty() != -1) continue;
|
||||
|
||||
// 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;
|
||||
}
|
||||
if (backpack == null) continue;
|
||||
|
||||
List<Entity> entities = player.getNearbyEntities(radius, radius, radius);
|
||||
for(Entity entity : entities)
|
||||
{
|
||||
if(entity instanceof Item) {
|
||||
if(entity instanceof Item)
|
||||
{
|
||||
Item item = (Item) entity;
|
||||
if (!item.isDead() && item.getPickupDelay() <= 0) {
|
||||
if (!item.isDead() && item.getPickupDelay() <= 0)
|
||||
{
|
||||
Map<Integer, ItemStack> leftover = player.getInventory().addItem(item.getItemStack());
|
||||
if (!leftover.isEmpty()) {
|
||||
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()) {
|
||||
if (!leftover.isEmpty())
|
||||
{
|
||||
item.setItemStack(leftover.get(0));
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
item.remove();
|
||||
}
|
||||
}
|
||||
@ -124,9 +133,11 @@ public void close()
|
||||
* @param uuid The players UUID
|
||||
* @return The new state. True = collection enabled.
|
||||
*/
|
||||
public boolean toggleState(UUID uuid) {
|
||||
public boolean toggleState(UUID uuid)
|
||||
{
|
||||
boolean removed = toggleList.remove(uuid);
|
||||
if (!removed) {
|
||||
if (!removed)
|
||||
{
|
||||
toggleList.add(uuid);
|
||||
}
|
||||
return isPickupEnabled(uuid);
|
||||
@ -137,15 +148,8 @@ public boolean toggleState(UUID uuid) {
|
||||
* @param uuid The player uuid
|
||||
* @return true if enabled
|
||||
*/
|
||||
public boolean isPickupEnabled(UUID uuid) {
|
||||
public boolean isPickupEnabled(UUID uuid)
|
||||
{
|
||||
return enabledOnJoin ^ toggleList.contains(uuid);
|
||||
}
|
||||
|
||||
/**
|
||||
* If this feature is enabled or not.
|
||||
* @return
|
||||
*/
|
||||
public boolean isToggleable() {
|
||||
return isToggleable;
|
||||
}
|
||||
}
|
@ -216,7 +216,7 @@ private void load()
|
||||
else shortcut = null;
|
||||
if(config.isWorldWhitelistMode()) pluginManager.registerEvents(new WorldBlacklistUpdater(this), this);
|
||||
//endregion
|
||||
if(config.getFullInvCollect() || config.isToggleAllowed()) collector = new ItemsCollector(this);
|
||||
if(config.getFullInvCollect()) collector = new ItemsCollector(this);
|
||||
worldBlacklist = config.getWorldBlacklist();
|
||||
worldBlacklistMode = (worldBlacklist.size() == 0) ? WorldBlacklistMode.None : config.getWorldBlockMode();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user