mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-24 09:01:54 +01:00
Added InventoryModifier#replaceItemStack
This commit is contained in:
parent
d1c10243a8
commit
f52e10c2b2
@ -29,6 +29,7 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
import java.util.concurrent.CopyOnWriteArraySet;
|
import java.util.concurrent.CopyOnWriteArraySet;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
import java.util.function.UnaryOperator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents an inventory which can be viewed by a collection of {@link Player}.
|
* Represents an inventory which can be viewed by a collection of {@link Player}.
|
||||||
@ -159,7 +160,13 @@ public class Inventory implements InventoryModifier, InventoryClickHandler, View
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clear() {
|
public synchronized void replaceItemStack(int slot, @NotNull UnaryOperator<@NotNull ItemStack> operator) {
|
||||||
|
// Make the method synchronized
|
||||||
|
InventoryModifier.super.replaceItemStack(slot, operator);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void clear() {
|
||||||
// Clear the item array
|
// Clear the item array
|
||||||
Arrays.fill(itemStacks, ItemStack.AIR);
|
Arrays.fill(itemStacks, ItemStack.AIR);
|
||||||
// Send the cleared inventory to viewers
|
// Send the cleared inventory to viewers
|
||||||
|
@ -9,6 +9,7 @@ import net.minestom.server.utils.validate.Check;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.function.UnaryOperator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents an inventory where items can be modified/retrieved.
|
* Represents an inventory where items can be modified/retrieved.
|
||||||
@ -75,6 +76,11 @@ public interface InventoryModifier {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default void replaceItemStack(int slot, @NotNull UnaryOperator<@NotNull ItemStack> operator) {
|
||||||
|
var currentItem = getItemStack(slot);
|
||||||
|
setItemStack(slot, operator.apply(currentItem));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clears the inventory and send relevant update to the viewer(s).
|
* Clears the inventory and send relevant update to the viewer(s).
|
||||||
*/
|
*/
|
||||||
|
@ -23,6 +23,7 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
import java.util.concurrent.CopyOnWriteArrayList;
|
||||||
|
import java.util.function.UnaryOperator;
|
||||||
|
|
||||||
import static net.minestom.server.utils.inventory.PlayerInventoryUtils.*;
|
import static net.minestom.server.utils.inventory.PlayerInventoryUtils.*;
|
||||||
|
|
||||||
@ -73,7 +74,7 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setItemStack(int slot, @NotNull ItemStack itemStack) {
|
public synchronized void setItemStack(int slot, @NotNull ItemStack itemStack) {
|
||||||
PlayerSetItemStackEvent setItemStackEvent = new PlayerSetItemStackEvent(player, slot, itemStack);
|
PlayerSetItemStackEvent setItemStackEvent = new PlayerSetItemStackEvent(player, slot, itemStack);
|
||||||
player.callEvent(PlayerSetItemStackEvent.class, setItemStackEvent);
|
player.callEvent(PlayerSetItemStackEvent.class, setItemStackEvent);
|
||||||
if (setItemStackEvent.isCancelled())
|
if (setItemStackEvent.isCancelled())
|
||||||
@ -107,7 +108,13 @@ public class PlayerInventory implements InventoryModifier, InventoryClickHandler
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void clear() {
|
public synchronized void replaceItemStack(int slot, @NotNull UnaryOperator<@NotNull ItemStack> operator) {
|
||||||
|
// Make the method synchronized
|
||||||
|
InventoryModifier.super.replaceItemStack(slot, operator);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public synchronized void clear() {
|
||||||
// Clear the item array
|
// Clear the item array
|
||||||
Arrays.fill(itemStacks, ItemStack.AIR);
|
Arrays.fill(itemStacks, ItemStack.AIR);
|
||||||
|
|
||||||
|
@ -225,7 +225,8 @@ public class PlayerInit {
|
|||||||
|
|
||||||
PlayerInventory inventory = player.getInventory();
|
PlayerInventory inventory = player.getInventory();
|
||||||
ItemStack itemStack = ItemStack.of(Material.STONE, 64);
|
ItemStack itemStack = ItemStack.of(Material.STONE, 64);
|
||||||
//inventory.addItemStack(itemStack);
|
inventory.addItemStack(itemStack);
|
||||||
|
inventory.replaceItemStack(0, i -> i.withAmount(32));
|
||||||
|
|
||||||
{
|
{
|
||||||
ItemStack item = ItemStack.builder(Material.DIAMOND_CHESTPLATE)
|
ItemStack item = ItemStack.builder(Material.DIAMOND_CHESTPLATE)
|
||||||
|
Loading…
Reference in New Issue
Block a user