2015-09-12 18:17:41 +02:00
|
|
|
/*
|
2023-01-11 19:15:39 +01:00
|
|
|
* Copyright (C) 2023 GeorgH93
|
2015-09-12 18:17:41 +02:00
|
|
|
*
|
2016-12-19 22:06:08 +01:00
|
|
|
* 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.
|
2015-09-12 18:17:41 +02:00
|
|
|
*
|
2016-12-19 22:06:08 +01:00
|
|
|
* 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.
|
2015-09-12 18:17:41 +02:00
|
|
|
*
|
2016-12-19 22:06:08 +01:00
|
|
|
* You should have received a copy of the GNU General Public License
|
2023-06-15 20:07:38 +02:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2015-09-12 18:17:41 +02:00
|
|
|
*/
|
|
|
|
|
2016-12-19 22:06:08 +01:00
|
|
|
package at.pcgamingfreaks.Minepacks.Bukkit;
|
2015-09-12 18:17:41 +02:00
|
|
|
|
2020-12-07 19:56:38 +01:00
|
|
|
import at.pcgamingfreaks.Minepacks.Bukkit.API.WorldBlacklistMode;
|
2019-10-16 22:42:57 +02:00
|
|
|
import at.pcgamingfreaks.Minepacks.Bukkit.Listener.ItemFilter;
|
2015-09-12 18:17:41 +02:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.entity.Entity;
|
|
|
|
import org.bukkit.entity.Item;
|
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import org.bukkit.scheduler.BukkitRunnable;
|
2019-07-14 19:44:05 +02:00
|
|
|
import org.bukkit.scheduler.BukkitTask;
|
2015-09-12 18:17:41 +02:00
|
|
|
|
2022-11-30 21:14:21 +01:00
|
|
|
import java.util.*;
|
2015-09-12 18:17:41 +02:00
|
|
|
|
|
|
|
public class ItemsCollector extends BukkitRunnable
|
|
|
|
{
|
2016-11-23 21:06:45 +01:00
|
|
|
private final Minepacks plugin;
|
2016-11-06 18:09:23 +01:00
|
|
|
private final double radius;
|
2019-07-14 19:44:05 +02:00
|
|
|
private final BukkitTask task;
|
2019-10-16 22:42:57 +02:00
|
|
|
private final ItemFilter itemFilter;
|
2015-09-12 18:17:41 +02:00
|
|
|
|
2022-11-30 21:14:21 +01:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2016-11-23 21:06:45 +01:00
|
|
|
public ItemsCollector(Minepacks plugin)
|
2015-09-12 18:17:41 +02:00
|
|
|
{
|
2016-11-06 18:09:23 +01:00
|
|
|
this.plugin = plugin;
|
2018-06-07 00:30:48 +02:00
|
|
|
this.radius = plugin.getConfiguration().getFullInvRadius();
|
2022-11-30 21:14:21 +01:00
|
|
|
|
2023-01-11 19:15:39 +01:00
|
|
|
this.isToggleable = plugin.getConfiguration().isFullInvToggleAllowed();
|
|
|
|
this.enabledOnJoin = plugin.getConfiguration().isFullInvEnabledOnJoin();
|
2022-11-30 21:14:21 +01:00
|
|
|
this.toggleList = new HashSet<>();
|
|
|
|
|
2019-07-14 19:44:05 +02:00
|
|
|
task = runTaskTimer(plugin, plugin.getConfiguration().getFullInvCheckInterval(), plugin.getConfiguration().getFullInvCheckInterval());
|
2019-10-16 22:42:57 +02:00
|
|
|
itemFilter = plugin.getItemFilter();
|
2015-09-12 18:17:41 +02:00
|
|
|
}
|
|
|
|
|
2022-12-09 17:35:52 +01:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2015-09-12 18:17:41 +02:00
|
|
|
@Override
|
|
|
|
public void run()
|
|
|
|
{
|
|
|
|
for(Player player : Bukkit.getServer().getOnlinePlayers())
|
|
|
|
{
|
2022-12-09 17:35:52 +01:00
|
|
|
if (!canUseAutoPickup(player)) continue;
|
2022-11-30 21:14:21 +01:00
|
|
|
|
|
|
|
// Inventory is full
|
2022-12-09 17:35:52 +01:00
|
|
|
if (player.getInventory().firstEmpty() != -1) continue;
|
2022-11-30 21:14:21 +01:00
|
|
|
|
|
|
|
// 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);
|
2022-12-09 17:35:52 +01:00
|
|
|
if (backpack == null) continue;
|
|
|
|
|
2022-11-30 21:14:21 +01:00
|
|
|
List<Entity> entities = player.getNearbyEntities(radius, radius, radius);
|
|
|
|
for(Entity entity : entities)
|
|
|
|
{
|
2022-12-09 17:35:52 +01:00
|
|
|
if(entity instanceof Item)
|
|
|
|
{
|
2022-11-30 21:14:21 +01:00
|
|
|
Item item = (Item) entity;
|
2022-12-09 17:35:52 +01:00
|
|
|
if (!item.isDead() && item.getPickupDelay() <= 0)
|
|
|
|
{
|
2022-11-30 21:14:21 +01:00
|
|
|
Map<Integer, ItemStack> leftover = player.getInventory().addItem(item.getItemStack());
|
2022-12-09 17:35:52 +01:00
|
|
|
if (!leftover.isEmpty())
|
|
|
|
{
|
2022-11-30 21:14:21 +01:00
|
|
|
ItemStack itemStack = leftover.get(0);
|
|
|
|
if (itemStack == null || itemStack.getAmount() == 0 || (itemFilter != null && itemFilter.isItemBlocked(itemStack)))
|
2022-12-09 17:35:52 +01:00
|
|
|
{
|
2022-11-30 21:14:21 +01:00
|
|
|
continue;
|
2022-12-09 17:35:52 +01:00
|
|
|
}
|
2022-11-30 21:14:21 +01:00
|
|
|
leftover = backpack.addItems(itemStack);
|
|
|
|
}
|
2022-12-09 17:35:52 +01:00
|
|
|
if (!leftover.isEmpty())
|
|
|
|
{
|
2022-11-30 21:14:21 +01:00
|
|
|
item.setItemStack(leftover.get(0));
|
2022-12-09 17:35:52 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-11-30 21:14:21 +01:00
|
|
|
item.remove();
|
2015-09-12 18:17:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-14 19:44:05 +02:00
|
|
|
|
|
|
|
public void close()
|
|
|
|
{
|
|
|
|
task.cancel();
|
|
|
|
}
|
2022-11-30 21:14:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Toggles the automatic collection for the player.
|
|
|
|
* @param uuid The players UUID
|
|
|
|
* @return The new state. True = collection enabled.
|
|
|
|
*/
|
2022-12-09 17:35:52 +01:00
|
|
|
public boolean toggleState(UUID uuid)
|
|
|
|
{
|
2022-11-30 21:14:21 +01:00
|
|
|
boolean removed = toggleList.remove(uuid);
|
2022-12-09 17:35:52 +01:00
|
|
|
if (!removed)
|
|
|
|
{
|
2022-11-30 21:14:21 +01:00
|
|
|
toggleList.add(uuid);
|
|
|
|
}
|
|
|
|
return isPickupEnabled(uuid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The item pickup state for a certain player.
|
|
|
|
* @param uuid The player uuid
|
|
|
|
* @return true if enabled
|
|
|
|
*/
|
2022-12-09 17:35:52 +01:00
|
|
|
public boolean isPickupEnabled(UUID uuid)
|
|
|
|
{
|
2022-11-30 21:14:21 +01:00
|
|
|
return enabledOnJoin ^ toggleList.contains(uuid);
|
|
|
|
}
|
2015-09-12 18:17:41 +02:00
|
|
|
}
|