mirror of
https://github.com/Minestom/Minestom.git
synced 2025-03-09 13:19:05 +01:00
Remove ItemStore API until a stable solution is found
This commit is contained in:
parent
2fadf1da94
commit
792dd9787f
@ -265,7 +265,7 @@ public abstract class ItemMetaBuilder {
|
|||||||
return dest;
|
return dest;
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface Provider<T> {
|
public interface Provider<T extends ItemMetaBuilder> {
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,6 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.IntUnaryOperator;
|
import java.util.function.IntUnaryOperator;
|
||||||
import java.util.function.UnaryOperator;
|
import java.util.function.UnaryOperator;
|
||||||
@ -28,22 +27,17 @@ public class ItemStack implements HoverEventSource<HoverEvent.ShowItem> {
|
|||||||
*/
|
*/
|
||||||
public static final @NotNull ItemStack AIR = ItemStack.of(Material.AIR);
|
public static final @NotNull ItemStack AIR = ItemStack.of(Material.AIR);
|
||||||
|
|
||||||
private final UUID uuid = UUID.randomUUID();
|
|
||||||
private final StackingRule stackingRule = new VanillaStackingRule(64);
|
private final StackingRule stackingRule = new VanillaStackingRule(64);
|
||||||
|
|
||||||
private final Material material;
|
private final Material material;
|
||||||
private final int amount;
|
private final int amount;
|
||||||
private final ItemMeta meta;
|
private final ItemMeta meta;
|
||||||
|
|
||||||
private final ItemStore store;
|
|
||||||
|
|
||||||
protected ItemStack(@NotNull Material material, int amount,
|
protected ItemStack(@NotNull Material material, int amount,
|
||||||
@NotNull ItemMeta meta,
|
@NotNull ItemMeta meta) {
|
||||||
@NotNull ItemStore store) {
|
|
||||||
this.material = material;
|
this.material = material;
|
||||||
this.amount = amount;
|
this.amount = amount;
|
||||||
this.meta = meta;
|
this.meta = meta;
|
||||||
this.store = store;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Contract(value = "_ -> new", pure = true)
|
@Contract(value = "_ -> new", pure = true)
|
||||||
@ -61,11 +55,6 @@ public class ItemStack implements HoverEventSource<HoverEvent.ShowItem> {
|
|||||||
return of(material, 1);
|
return of(material, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Contract(pure = true)
|
|
||||||
public @NotNull UUID getUuid() {
|
|
||||||
return uuid;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Contract(pure = true)
|
@Contract(pure = true)
|
||||||
public @NotNull Material getMaterial() {
|
public @NotNull Material getMaterial() {
|
||||||
return material;
|
return material;
|
||||||
@ -103,16 +92,6 @@ public class ItemStack implements HoverEventSource<HoverEvent.ShowItem> {
|
|||||||
return builder().meta(metaOperator).build();
|
return builder().meta(metaOperator).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Contract(value = "_, -> new", pure = true)
|
|
||||||
public @NotNull ItemStack withStore(@NotNull ItemStore store) {
|
|
||||||
return builder().store(store).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Contract(value = "_ -> new", pure = true)
|
|
||||||
public @NotNull ItemStack withStore(@NotNull Consumer<@NotNull ItemStoreBuilder> storeConsumer) {
|
|
||||||
return builder().store(storeConsumer).build();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Contract(pure = true)
|
@Contract(pure = true)
|
||||||
public @Nullable Component getDisplayName() {
|
public @Nullable Component getDisplayName() {
|
||||||
return meta.getDisplayName();
|
return meta.getDisplayName();
|
||||||
@ -153,11 +132,6 @@ public class ItemStack implements HoverEventSource<HoverEvent.ShowItem> {
|
|||||||
return meta;
|
return meta;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Contract(pure = true)
|
|
||||||
public @NotNull ItemStore getStore() {
|
|
||||||
return store;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Contract(pure = true)
|
@Contract(pure = true)
|
||||||
public boolean isAir() {
|
public boolean isAir() {
|
||||||
return material.equals(Material.AIR);
|
return material.equals(Material.AIR);
|
||||||
@ -175,7 +149,6 @@ public class ItemStack implements HoverEventSource<HoverEvent.ShowItem> {
|
|||||||
if (o == null || getClass() != o.getClass()) return false;
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
|
||||||
ItemStack itemStack = (ItemStack) o;
|
ItemStack itemStack = (ItemStack) o;
|
||||||
if (uuid.equals(itemStack.uuid)) return true;
|
|
||||||
|
|
||||||
if (amount != itemStack.amount) return false;
|
if (amount != itemStack.amount) return false;
|
||||||
if (!stackingRule.equals(itemStack.stackingRule)) return false;
|
if (!stackingRule.equals(itemStack.stackingRule)) return false;
|
||||||
@ -194,7 +167,7 @@ public class ItemStack implements HoverEventSource<HoverEvent.ShowItem> {
|
|||||||
|
|
||||||
@Contract(value = "-> new", pure = true)
|
@Contract(value = "-> new", pure = true)
|
||||||
protected @NotNull ItemStackBuilder builder() {
|
protected @NotNull ItemStackBuilder builder() {
|
||||||
return new ItemStackBuilder(material, meta.builder(), store.builder())
|
return new ItemStackBuilder(material, meta.builder())
|
||||||
.amount(amount);
|
.amount(amount);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,13 +19,11 @@ public class ItemStackBuilder {
|
|||||||
private final Material material;
|
private final Material material;
|
||||||
private int amount;
|
private int amount;
|
||||||
protected ItemMetaBuilder metaBuilder;
|
protected ItemMetaBuilder metaBuilder;
|
||||||
protected ItemStoreBuilder storeBuilder;
|
|
||||||
|
|
||||||
protected ItemStackBuilder(@NotNull Material material, @NotNull ItemMetaBuilder metaBuilder, @NotNull ItemStoreBuilder storeBuilder) {
|
protected ItemStackBuilder(@NotNull Material material, @NotNull ItemMetaBuilder metaBuilder) {
|
||||||
this.material = material;
|
this.material = material;
|
||||||
this.amount = 1;
|
this.amount = 1;
|
||||||
this.metaBuilder = metaBuilder;
|
this.metaBuilder = metaBuilder;
|
||||||
this.storeBuilder = storeBuilder;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final Map<Material, Supplier<ItemMetaBuilder>> MATERIAL_SUPPLIER_MAP = new ConcurrentHashMap<>();
|
private static final Map<Material, Supplier<ItemMetaBuilder>> MATERIAL_SUPPLIER_MAP = new ConcurrentHashMap<>();
|
||||||
@ -54,8 +52,7 @@ public class ItemStackBuilder {
|
|||||||
|
|
||||||
protected ItemStackBuilder(@NotNull Material material) {
|
protected ItemStackBuilder(@NotNull Material material) {
|
||||||
this(material,
|
this(material,
|
||||||
MATERIAL_SUPPLIER_MAP.getOrDefault(material, DefaultMeta::new).get(),
|
MATERIAL_SUPPLIER_MAP.getOrDefault(material, DefaultMeta::new).get());
|
||||||
new ItemStoreBuilder());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Contract(value = "_ -> this")
|
@Contract(value = "_ -> this")
|
||||||
@ -100,21 +97,9 @@ public class ItemStackBuilder {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Contract(value = "_ -> this")
|
|
||||||
public @NotNull ItemStackBuilder store(@NotNull ItemStore store) {
|
|
||||||
this.storeBuilder = store.builder();
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Contract(value = "_ -> this")
|
|
||||||
public @NotNull ItemStackBuilder store(@NotNull Consumer<@NotNull ItemStoreBuilder> consumer) {
|
|
||||||
consumer.accept(storeBuilder);
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Contract(value = "-> new", pure = true)
|
@Contract(value = "-> new", pure = true)
|
||||||
public @NotNull ItemStack build() {
|
public @NotNull ItemStack build() {
|
||||||
return new ItemStack(material, amount, metaBuilder.build(), storeBuilder.build());
|
return new ItemStack(material, amount, metaBuilder.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final class DefaultMeta extends ItemMetaBuilder {
|
private static final class DefaultMeta extends ItemMetaBuilder {
|
||||||
|
@ -1,30 +0,0 @@
|
|||||||
package net.minestom.server.item;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.Contract;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
|
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
|
|
||||||
public class ItemStore {
|
|
||||||
|
|
||||||
private final Map<String, ItemStoreBuilder.Entry<?>> entryMap;
|
|
||||||
|
|
||||||
protected ItemStore(@NotNull Map<String, ItemStoreBuilder.Entry<?>> entryMap) {
|
|
||||||
this.entryMap = Collections.unmodifiableMap(entryMap);
|
|
||||||
}
|
|
||||||
|
|
||||||
public <T> T get(@NotNull String key) {
|
|
||||||
if (entryMap.containsKey(key)) {
|
|
||||||
return (T) entryMap.get(key).value;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Contract(value = "-> new", pure = true)
|
|
||||||
protected @NotNull ItemStoreBuilder builder() {
|
|
||||||
return new ItemStoreBuilder(new ConcurrentHashMap<>(entryMap));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,48 +0,0 @@
|
|||||||
package net.minestom.server.item;
|
|
||||||
|
|
||||||
import org.jetbrains.annotations.Contract;
|
|
||||||
import org.jetbrains.annotations.NotNull;
|
|
||||||
import org.jetbrains.annotations.Nullable;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
|
|
||||||
public class ItemStoreBuilder {
|
|
||||||
|
|
||||||
private final Map<String, Entry<?>> entryMap;
|
|
||||||
|
|
||||||
protected ItemStoreBuilder(@NotNull Map<String, Entry<?>> entryMap) {
|
|
||||||
this.entryMap = entryMap;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected ItemStoreBuilder() {
|
|
||||||
this(new ConcurrentHashMap<>());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Contract(value = "_, _, _ -> this")
|
|
||||||
public <T> @NotNull ItemStoreBuilder set(@NotNull String key, T value, MergingRule<T> mergingRule) {
|
|
||||||
this.entryMap.put(key, new Entry<>(value, mergingRule));
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Contract(value = "-> new", pure = true)
|
|
||||||
public @NotNull ItemStore build() {
|
|
||||||
return new ItemStore(new HashMap<>(entryMap));
|
|
||||||
}
|
|
||||||
|
|
||||||
public interface MergingRule<T> {
|
|
||||||
@Nullable T apply(@NotNull T value1, @NotNull T value2);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected static class Entry<T> {
|
|
||||||
protected final T value;
|
|
||||||
protected final MergingRule<T> mergingRule;
|
|
||||||
|
|
||||||
private Entry(@NotNull T value, @NotNull MergingRule<T> mergingRule) {
|
|
||||||
this.value = value;
|
|
||||||
this.mergingRule = mergingRule;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -76,9 +76,6 @@ public class PlayerInit {
|
|||||||
builder.lodestonePosition(new Position(0, 0, 0));
|
builder.lodestonePosition(new Position(0, 0, 0));
|
||||||
builder.set(ItemTag.Integer("int"), 25);
|
builder.set(ItemTag.Integer("int"), 25);
|
||||||
})
|
})
|
||||||
.store(store -> {
|
|
||||||
store.set("key", 5, Integer::sum);
|
|
||||||
})
|
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
itemStack = itemStack.with(itemBuilder -> itemBuilder
|
itemStack = itemStack.with(itemBuilder -> itemBuilder
|
||||||
@ -207,6 +204,7 @@ public class PlayerInit {
|
|||||||
|
|
||||||
globalEventHandler.addEventCallback(PlayerLoginEvent.class, event -> {
|
globalEventHandler.addEventCallback(PlayerLoginEvent.class, event -> {
|
||||||
final Player player = event.getPlayer();
|
final Player player = event.getPlayer();
|
||||||
|
player.sendMessage("test");
|
||||||
|
|
||||||
event.setSpawningInstance(instanceContainer);
|
event.setSpawningInstance(instanceContainer);
|
||||||
int x = Math.abs(ThreadLocalRandom.current().nextInt()) % 1000 - 250;
|
int x = Math.abs(ThreadLocalRandom.current().nextInt()) % 1000 - 250;
|
||||||
@ -233,9 +231,6 @@ public class PlayerInit {
|
|||||||
.meta(itemMetaBuilder ->
|
.meta(itemMetaBuilder ->
|
||||||
itemMetaBuilder.canPlaceOn(Set.of(Block.STONE))
|
itemMetaBuilder.canPlaceOn(Set.of(Block.STONE))
|
||||||
.canDestroy(Set.of(Block.DIAMOND_ORE)))
|
.canDestroy(Set.of(Block.DIAMOND_ORE)))
|
||||||
.store(store -> {
|
|
||||||
store.set("key", 5, Integer::sum);
|
|
||||||
})
|
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
System.out.println(itemStack.getMeta().toSNBT());
|
System.out.println(itemStack.getMeta().toSNBT());
|
||||||
@ -265,7 +260,6 @@ public class PlayerInit {
|
|||||||
|
|
||||||
globalEventHandler.addEventCallback(PlayerUseItemEvent.class, useEvent -> {
|
globalEventHandler.addEventCallback(PlayerUseItemEvent.class, useEvent -> {
|
||||||
final Player player = useEvent.getPlayer();
|
final Player player = useEvent.getPlayer();
|
||||||
System.out.println("test: " + player.getItemInMainHand().getStore().get("key2"));
|
|
||||||
player.sendMessage("Using item in air: " + useEvent.getItemStack().getMaterial());
|
player.sendMessage("Using item in air: " + useEvent.getItemStack().getMaterial());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user