Remove ItemStore API until a stable solution is found

This commit is contained in:
TheMode 2021-04-11 17:01:05 +02:00
parent 2fadf1da94
commit 792dd9787f
6 changed files with 7 additions and 133 deletions

View File

@ -265,7 +265,7 @@ public abstract class ItemMetaBuilder {
return dest;
}
public interface Provider<T> {
public interface Provider<T extends ItemMetaBuilder> {
}
}

View File

@ -10,7 +10,6 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.function.IntUnaryOperator;
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);
private final UUID uuid = UUID.randomUUID();
private final StackingRule stackingRule = new VanillaStackingRule(64);
private final Material material;
private final int amount;
private final ItemMeta meta;
private final ItemStore store;
protected ItemStack(@NotNull Material material, int amount,
@NotNull ItemMeta meta,
@NotNull ItemStore store) {
@NotNull ItemMeta meta) {
this.material = material;
this.amount = amount;
this.meta = meta;
this.store = store;
}
@Contract(value = "_ -> new", pure = true)
@ -61,11 +55,6 @@ public class ItemStack implements HoverEventSource<HoverEvent.ShowItem> {
return of(material, 1);
}
@Contract(pure = true)
public @NotNull UUID getUuid() {
return uuid;
}
@Contract(pure = true)
public @NotNull Material getMaterial() {
return material;
@ -103,16 +92,6 @@ public class ItemStack implements HoverEventSource<HoverEvent.ShowItem> {
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)
public @Nullable Component getDisplayName() {
return meta.getDisplayName();
@ -153,11 +132,6 @@ public class ItemStack implements HoverEventSource<HoverEvent.ShowItem> {
return meta;
}
@Contract(pure = true)
public @NotNull ItemStore getStore() {
return store;
}
@Contract(pure = true)
public boolean isAir() {
return material.equals(Material.AIR);
@ -175,7 +149,6 @@ public class ItemStack implements HoverEventSource<HoverEvent.ShowItem> {
if (o == null || getClass() != o.getClass()) return false;
ItemStack itemStack = (ItemStack) o;
if (uuid.equals(itemStack.uuid)) return true;
if (amount != itemStack.amount) 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)
protected @NotNull ItemStackBuilder builder() {
return new ItemStackBuilder(material, meta.builder(), store.builder())
return new ItemStackBuilder(material, meta.builder())
.amount(amount);
}

View File

@ -19,13 +19,11 @@ public class ItemStackBuilder {
private final Material material;
private int amount;
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.amount = 1;
this.metaBuilder = metaBuilder;
this.storeBuilder = storeBuilder;
}
private static final Map<Material, Supplier<ItemMetaBuilder>> MATERIAL_SUPPLIER_MAP = new ConcurrentHashMap<>();
@ -54,8 +52,7 @@ public class ItemStackBuilder {
protected ItemStackBuilder(@NotNull Material material) {
this(material,
MATERIAL_SUPPLIER_MAP.getOrDefault(material, DefaultMeta::new).get(),
new ItemStoreBuilder());
MATERIAL_SUPPLIER_MAP.getOrDefault(material, DefaultMeta::new).get());
}
@Contract(value = "_ -> this")
@ -100,21 +97,9 @@ public class ItemStackBuilder {
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)
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 {

View File

@ -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));
}
}

View File

@ -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;
}
}
}

View File

@ -76,9 +76,6 @@ public class PlayerInit {
builder.lodestonePosition(new Position(0, 0, 0));
builder.set(ItemTag.Integer("int"), 25);
})
.store(store -> {
store.set("key", 5, Integer::sum);
})
.build();
itemStack = itemStack.with(itemBuilder -> itemBuilder
@ -207,6 +204,7 @@ public class PlayerInit {
globalEventHandler.addEventCallback(PlayerLoginEvent.class, event -> {
final Player player = event.getPlayer();
player.sendMessage("test");
event.setSpawningInstance(instanceContainer);
int x = Math.abs(ThreadLocalRandom.current().nextInt()) % 1000 - 250;
@ -233,9 +231,6 @@ public class PlayerInit {
.meta(itemMetaBuilder ->
itemMetaBuilder.canPlaceOn(Set.of(Block.STONE))
.canDestroy(Set.of(Block.DIAMOND_ORE)))
.store(store -> {
store.set("key", 5, Integer::sum);
})
.build();
System.out.println(itemStack.getMeta().toSNBT());
@ -265,7 +260,6 @@ public class PlayerInit {
globalEventHandler.addEventCallback(PlayerUseItemEvent.class, useEvent -> {
final Player player = useEvent.getPlayer();
System.out.println("test: " + player.getItemInMainHand().getStore().get("key2"));
player.sendMessage("Using item in air: " + useEvent.getItemStack().getMaterial());
});