mirror of
https://github.com/songoda/UltimateStacker.git
synced 2024-12-24 17:37:37 +01:00
Fix item stacking issues, add annotations
This commit is contained in:
parent
b9b76b8d28
commit
86a19a520b
@ -14,21 +14,22 @@ public interface StackedItemManager {
|
||||
|
||||
/**
|
||||
* Get the StackedItem for the given Item
|
||||
* Creates a new StackedItem if it does not exist
|
||||
* @param item The Item to get the stack for
|
||||
* @return The StackedItem for the given Item or null if not stacked
|
||||
* @return The StackedItem for the given Item
|
||||
*/
|
||||
@Nullable StackedItem getStackedItem(Item item);
|
||||
|
||||
/**
|
||||
* Get the StackedItem for the given Item
|
||||
* @param item The Item to get the stack for
|
||||
* @param create If true, will create a new stack if one does not exist
|
||||
* @return The StackedItem for the given Item or null if not stacked
|
||||
*/
|
||||
@NotNull StackedItem getStackedItem(Item item, boolean create);
|
||||
@NotNull StackedItem getStackedItem(Item item);
|
||||
|
||||
/**
|
||||
* Create a new StackedItem for the given item
|
||||
* @param item The item to create the stack for
|
||||
* @param amount The amount of items in the stack
|
||||
* @return The StackedItem for the given Item
|
||||
*/
|
||||
@NotNull StackedItem createStack(Item item, int amount);
|
||||
|
||||
/**
|
||||
* Create a new StackedItem for the given ItemStack
|
||||
* @param item The ItemStack to create the stack for
|
||||
* @param amount The amount of items in the stack
|
||||
* @param location The location to spawn the stack
|
||||
@ -37,12 +38,12 @@ public interface StackedItemManager {
|
||||
@Nullable StackedItem createStack(ItemStack item, Location location, int amount);
|
||||
|
||||
/**
|
||||
* Create a new StackedItem for the given item
|
||||
* @param item The item to create the stack for
|
||||
* @param amount The amount of items in the stack
|
||||
* @return The StackedItem for the given Item or null if it could not be created
|
||||
* Update the stack for the given item
|
||||
* @param item The Item to update
|
||||
* @param newAmount The new amount of the stack
|
||||
* @return The StackedItem for the given Item
|
||||
*/
|
||||
@Nullable StackedItem createStack(Item item, int amount);
|
||||
@NotNull StackedItem updateStack(Item item, int newAmount);
|
||||
|
||||
/**
|
||||
* Create a new StackedItem for the given item in the main thread
|
||||
@ -57,9 +58,17 @@ public interface StackedItemManager {
|
||||
* Create a new StackedItem for the given item in the main thread
|
||||
* @param item The item to create the stack for
|
||||
* @param amount The amount of items in the stack
|
||||
* @return The StackedItem for the given Item or null if it could not be created
|
||||
* @return The StackedItem for the given Item
|
||||
*/
|
||||
@Nullable Future<StackedItem> createStackSync(Item item, int amount);
|
||||
@NotNull Future<StackedItem> createStackSync(Item item, int amount);
|
||||
|
||||
/**
|
||||
* Update the stack for the given item in the main thread
|
||||
* @param item The Item to update
|
||||
* @param newAmount The new amount of the stack
|
||||
* @return The StackedItem for the given Item
|
||||
*/
|
||||
@NotNull Future<StackedItem> updateStackSync(Item item, int newAmount);
|
||||
|
||||
/**
|
||||
* Get the actual amount of the given item
|
||||
@ -83,7 +92,7 @@ public interface StackedItemManager {
|
||||
* @param ignoreRestrictions ignore ignoreRestrictions such as max stack size, or blacklist
|
||||
* @return The merged item or null if they merge was unsuccessful
|
||||
*/
|
||||
StackedItem merge(Item from, Item to, boolean ignoreRestrictions);
|
||||
@Nullable StackedItem merge(Item from, Item to, boolean ignoreRestrictions);
|
||||
|
||||
/**
|
||||
* Merge two items together if they are the same type
|
||||
@ -93,7 +102,7 @@ public interface StackedItemManager {
|
||||
* @param callback callback to be called when the merge is successful see {@link ItemMergeCallback#accept(Item, Item, StackedItem)}
|
||||
* @return The merged item or null if they merge was unsuccessful
|
||||
*/
|
||||
StackedItem merge(Item from, Item to, boolean ignoreRestrictions, ItemMergeCallback<Item, Item, StackedItem> callback);
|
||||
@Nullable StackedItem merge(Item from, Item to, boolean ignoreRestrictions, ItemMergeCallback<Item, Item, StackedItem> callback);
|
||||
|
||||
/**
|
||||
* Check to see if this material is not permitted to stack
|
||||
|
@ -65,7 +65,7 @@ public class SpawnerListeners implements Listener {
|
||||
|
||||
SpawnerStack spawnerStack = spawnerStackManager.getSpawner(location);
|
||||
|
||||
int amountToSpawn = Settings.STACK_ENTITIES.getBoolean() ? spawnerStack.calculateSpawnCount(entity.getType()) : 1;
|
||||
int amountToSpawn = Settings.STACK_ENTITIES.getBoolean() ? spawnerStack.calculateSpawnCount(entity.getType(), false) : 1;
|
||||
if (amountToSpawn <= 0) return;
|
||||
entity.remove();
|
||||
|
||||
|
@ -8,20 +8,20 @@ import com.craftaro.ultimatestacker.utils.Methods;
|
||||
import org.bukkit.entity.Arrow;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.entity.EntityPickupItemEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public class ItemCurrentListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
||||
public void onPickup(EntityPickupItemEvent event) {
|
||||
if (!Settings.STACK_ITEMS.getBoolean() || event.getItem() instanceof Arrow) return;
|
||||
// Amount here is not the total amount of item (32 if more than 32) but the amount of item the player can retrieve
|
||||
// ie there is x64 diamonds blocks (so 32), the player pick 8 items so the amount is 8 and not 32
|
||||
|
||||
StackedItem stackedItem = UltimateStackerApi.getStackedItemManager().getStackedItem(event.getItem());
|
||||
if (stackedItem == null) return;
|
||||
ItemStack stack = stackedItem.getItem().getItemStack();
|
||||
int amount = stackedItem.getAmount();
|
||||
|
||||
|
@ -7,15 +7,14 @@ import com.craftaro.ultimatestacker.utils.Methods;
|
||||
import org.bukkit.entity.Arrow;
|
||||
import org.bukkit.entity.Item;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerPickupItemEvent;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
public class ItemLegacyListener implements Listener {
|
||||
|
||||
//TODO Do we need this?
|
||||
|
||||
@EventHandler
|
||||
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
|
||||
public void onPickup(PlayerPickupItemEvent event) {
|
||||
if (!Settings.STACK_ITEMS.getBoolean() || event.getItem() instanceof Arrow) return;
|
||||
// Amount here is not the total amount of item (32 if more than 32) but the amount of item the player can retrieve
|
||||
|
@ -88,6 +88,16 @@ public class EntityStackImpl implements EntityStack {
|
||||
this.amount -= amount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location getLocation() {
|
||||
return hostEntity.getLocation();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return hostEntity.isValid();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUuid() {
|
||||
return hostEntity.getUniqueId();
|
||||
|
@ -5,6 +5,7 @@ import com.craftaro.ultimatestacker.UltimateStacker;
|
||||
import com.craftaro.ultimatestacker.api.stack.item.StackedItem;
|
||||
import com.craftaro.ultimatestacker.settings.Settings;
|
||||
import com.craftaro.ultimatestacker.utils.Methods;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.entity.Item;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
@ -16,6 +17,9 @@ public class StackedItemImpl implements StackedItem {
|
||||
|
||||
public StackedItemImpl(Item item) {
|
||||
this.item = item;
|
||||
if (!item.hasMetadata("US_AMT")) {
|
||||
item.setMetadata("US_AMT", new FixedMetadataValue(UltimateStacker.getInstance(), item.getItemStack().getAmount()));
|
||||
}
|
||||
}
|
||||
|
||||
public StackedItemImpl(Item item, int amount) {
|
||||
@ -25,12 +29,10 @@ public class StackedItemImpl implements StackedItem {
|
||||
|
||||
@Override
|
||||
public int getAmount() {
|
||||
ItemStack itemStack = item.getItemStack();
|
||||
int amount = itemStack.getAmount();
|
||||
if (item.hasMetadata("US_AMT")) {
|
||||
return item.getMetadata("US_AMT").get(0).asInt();
|
||||
} else {
|
||||
return amount;
|
||||
return item.getItemStack().getAmount();
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,6 +56,16 @@ public class StackedItemImpl implements StackedItem {
|
||||
updateItemAmount(item, getAmount() - amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location getLocation() {
|
||||
return item.getLocation();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return item.isValid();
|
||||
}
|
||||
|
||||
private void updateItemAmount(Item item, int newAmount) {
|
||||
updateItemAmount(item, item.getItemStack(), newAmount);
|
||||
}
|
||||
@ -84,8 +96,7 @@ public class StackedItemImpl implements StackedItem {
|
||||
|
||||
private void updateItemMeta(Item item, ItemStack itemStack, int newAmount) {
|
||||
Material material = itemStack.getType();
|
||||
if (material == Material.AIR)
|
||||
return;
|
||||
if (material == Material.AIR) return;
|
||||
|
||||
String name = TextUtils.convertToInvisibleString("IS") + Methods.compileItemName(itemStack, newAmount);
|
||||
|
||||
@ -101,8 +112,9 @@ public class StackedItemImpl implements StackedItem {
|
||||
if ((blacklisted && !Settings.ITEM_HOLOGRAM_BLACKLIST.getBoolean())
|
||||
|| !UltimateStacker.getInstance().getItemFile().getBoolean("Items." + material + ".Has Hologram")
|
||||
|| !Settings.ITEM_HOLOGRAMS.getBoolean()
|
||||
|| newAmount < Settings.ITEM_MIN_HOLOGRAM_SIZE.getInt())
|
||||
|| newAmount < Settings.ITEM_MIN_HOLOGRAM_SIZE.getInt()) {
|
||||
return;
|
||||
}
|
||||
|
||||
item.setCustomName(name);
|
||||
item.setCustomNameVisible(true);
|
||||
@ -120,4 +132,12 @@ public class StackedItemImpl implements StackedItem {
|
||||
item.setCustomNameVisible(true);
|
||||
return item;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "StackedItemImpl{" +
|
||||
"ItemStack=" + item.getItemStack() +
|
||||
", us_amount=" + getAmount() +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
@ -22,16 +22,8 @@ import java.util.concurrent.atomic.AtomicReference;
|
||||
public class StackedItemManagerImpl implements StackedItemManager {
|
||||
|
||||
@Override
|
||||
public @Nullable StackedItem getStackedItem(Item item) {
|
||||
if (item.hasMetadata("US_AMT")) {
|
||||
return new StackedItemImpl(item);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull StackedItem getStackedItem(Item item, boolean create) {
|
||||
return null;
|
||||
public @NotNull StackedItem getStackedItem(Item item) {
|
||||
return new StackedItemImpl(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -39,23 +31,21 @@ public class StackedItemManagerImpl implements StackedItemManager {
|
||||
if (item.getType() == Material.AIR) return null;
|
||||
World world = location.getWorld();
|
||||
if (world == null) return null;
|
||||
AtomicReference<StackedItem> stack = new AtomicReference<>(null);
|
||||
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_17)) {
|
||||
world.dropItem(location, item, dropped -> {
|
||||
if (dropped.getItemStack().getType() == Material.AIR) return;
|
||||
stack.set(new StackedItemImpl(dropped, amount));
|
||||
});
|
||||
} else {
|
||||
Item dropped = world.dropItem(location, item);
|
||||
if (dropped.getItemStack().getType() == Material.AIR) return null;
|
||||
stack.set(new StackedItemImpl(dropped, amount));
|
||||
}
|
||||
return stack.get();
|
||||
Item dropped = world.dropItem(location, item);
|
||||
if (dropped.getItemStack().getType() == Material.AIR) return null;
|
||||
return new StackedItemImpl(dropped, amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable StackedItem createStack(Item item, int amount) {
|
||||
return null;
|
||||
public @NotNull StackedItem createStack(Item item, int amount) {
|
||||
return new StackedItemImpl(item, amount);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull StackedItem updateStack(Item item, int newAmount) {
|
||||
StackedItem stackedItem = getStackedItem(item);
|
||||
stackedItem.setAmount(newAmount);
|
||||
return stackedItem;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -64,10 +54,15 @@ public class StackedItemManagerImpl implements StackedItemManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Future<StackedItem> createStackSync(Item item, int amount) {
|
||||
public @NotNull Future<StackedItem> createStackSync(Item item, int amount) {
|
||||
return Bukkit.getScheduler().callSyncMethod(UltimateStacker.getInstance(), () -> createStack(item, amount));
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Future<StackedItem> updateStackSync(Item item, int newAmount) {
|
||||
return Bukkit.getScheduler().callSyncMethod(UltimateStacker.getInstance(), () -> updateStack(item, newAmount));
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getActualItemAmount(Item item) {
|
||||
if (isStackedItem(item)) {
|
||||
@ -86,12 +81,12 @@ public class StackedItemManagerImpl implements StackedItemManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public StackedItem merge(Item from, Item to, boolean ignoreRestrictions) {
|
||||
public @Nullable StackedItem merge(Item from, Item to, boolean ignoreRestrictions) {
|
||||
return merge(from, to, ignoreRestrictions, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public StackedItem merge(Item from, Item to, boolean ignoreRestrictions, ItemMergeCallback<Item, Item, StackedItem> callback) {
|
||||
public @Nullable StackedItem merge(Item from, Item to, boolean ignoreRestrictions, ItemMergeCallback<Item, Item, StackedItem> callback) {
|
||||
|
||||
if (!ignoreRestrictions) {
|
||||
if (!Settings.STACK_ITEMS.getBoolean()) return null;
|
||||
@ -111,11 +106,18 @@ public class StackedItemManagerImpl implements StackedItemManager {
|
||||
|
||||
int maxSize = UltimateStacker.getInstance().getItemFile().getInt("Items." + fromItemStack.getType().name() + ".Max Stack Size");
|
||||
|
||||
if (maxSize <= 0) {
|
||||
maxSize = maxItemStackSize;
|
||||
} else {
|
||||
maxSize = Math.min(maxSize, maxItemStackSize);
|
||||
}
|
||||
|
||||
int fromAmount = getActualItemAmount(from);
|
||||
int toAmount = getActualItemAmount(to);
|
||||
|
||||
if (fromAmount + toAmount > maxSize) {
|
||||
if (callback != null) callback.accept(from, to, null);
|
||||
|
||||
//merge was unsuccessful
|
||||
return null;
|
||||
} else {
|
||||
|
@ -6,6 +6,8 @@ import com.craftaro.ultimatestacker.api.stack.spawner.SpawnerStack;
|
||||
import com.craftaro.ultimatestacker.api.stack.spawner.SpawnerStackManager;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.block.Block;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
@ -31,7 +33,7 @@ public class SpawnerStackManagerImpl implements SpawnerStackManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public SpawnerStack removeSpawner(Location location) {
|
||||
public @Nullable SpawnerStack removeSpawner(Location location) {
|
||||
return registeredSpawners.remove(roundLocation(location));
|
||||
}
|
||||
|
||||
@ -45,6 +47,11 @@ public class SpawnerStackManagerImpl implements SpawnerStackManager {
|
||||
return this.registeredSpawners.get(location);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSpawner(Block block) {
|
||||
return isSpawner(block.getLocation());
|
||||
}
|
||||
|
||||
@Override
|
||||
public SpawnerStack getSpawner(Block block) {
|
||||
return this.getSpawner(block.getLocation());
|
||||
@ -56,12 +63,12 @@ public class SpawnerStackManagerImpl implements SpawnerStackManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<SpawnerStack> getStacks() {
|
||||
public @NotNull Collection<SpawnerStack> getStacks() {
|
||||
return Collections.unmodifiableCollection(this.registeredSpawners.values());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Data> getStacksData() {
|
||||
public @NotNull Collection<Data> getStacksData() {
|
||||
return Collections.unmodifiableCollection(this.registeredSpawners.values());
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user