From b1ad7a8ac6abafb64ccd165bb820f0f294a75693 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Wed, 8 Sep 2021 20:44:16 +0700 Subject: [PATCH] Generify mattery drive --- .../mc/otm/capability/drive/DrivePool.java | 1 + .../capability/drive/IItemMatteryDrive.java | 22 ++ .../otm/capability/drive/IMatteryDrive.java | 33 +-- .../capability/drive/ItemMatteryDrive.java | 125 +++++++++ .../mc/otm/capability/drive/MatteryDrive.java | 262 ++++++------------ .../item/ItemPortableCondensationDrive.java | 15 +- .../mc/otm/matter/MatterRegistry.java | 10 +- .../mc/otm/menu/DriveViewerMenu.java | 24 +- .../mc/otm/storage/IStorageStack.java | 4 +- .../mc/otm/storage/StorageObjectRegistry.java | 6 +- 10 files changed, 262 insertions(+), 240 deletions(-) create mode 100644 src/main/java/ru/dbotthepony/mc/otm/capability/drive/IItemMatteryDrive.java create mode 100644 src/main/java/ru/dbotthepony/mc/otm/capability/drive/ItemMatteryDrive.java diff --git a/src/main/java/ru/dbotthepony/mc/otm/capability/drive/DrivePool.java b/src/main/java/ru/dbotthepony/mc/otm/capability/drive/DrivePool.java index 75856edb0..5b010b3bb 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/capability/drive/DrivePool.java +++ b/src/main/java/ru/dbotthepony/mc/otm/capability/drive/DrivePool.java @@ -161,6 +161,7 @@ public class DrivePool { base_directory = new File(server.storageSource.getWorldDir().toFile(), DATA_PATH); base_directory.mkdirs(); + stopping = false; thread = new Thread(null, () -> DrivePool.thread(server), "Overdrive That Matters DrivePool IO"); thread.start(); } diff --git a/src/main/java/ru/dbotthepony/mc/otm/capability/drive/IItemMatteryDrive.java b/src/main/java/ru/dbotthepony/mc/otm/capability/drive/IItemMatteryDrive.java new file mode 100644 index 000000000..8e641f195 --- /dev/null +++ b/src/main/java/ru/dbotthepony/mc/otm/capability/drive/IItemMatteryDrive.java @@ -0,0 +1,22 @@ +package ru.dbotthepony.mc.otm.capability.drive; + +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import ru.dbotthepony.mc.otm.storage.IStorageTuple; +import ru.dbotthepony.mc.otm.storage.ItemStackWrapper; + +import java.util.List; + +public interface IItemMatteryDrive extends IMatteryDrive { + /** + * @param item + * @return all items belonging to passed class + */ + List> findItems(Item item); + + /** + * @param stack + * @return all items that match this itemstack + */ + List> findItems(ItemStack stack); +} diff --git a/src/main/java/ru/dbotthepony/mc/otm/capability/drive/IMatteryDrive.java b/src/main/java/ru/dbotthepony/mc/otm/capability/drive/IMatteryDrive.java index bb8a5c830..2ff22fa6b 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/capability/drive/IMatteryDrive.java +++ b/src/main/java/ru/dbotthepony/mc/otm/capability/drive/IMatteryDrive.java @@ -7,44 +7,19 @@ import net.minecraft.world.item.ItemStack; import ru.dbotthepony.mc.otm.storage.*; import javax.annotation.ParametersAreNonnullByDefault; +import java.math.BigDecimal; import java.util.List; import java.util.UUID; @ParametersAreNonnullByDefault @MethodsReturnNonnullByDefault -public interface IMatteryDrive extends IStorageComponent { - record StoredStack(ItemStack stack, UUID id) {}; - - List getItems(); - +public interface IMatteryDrive extends IStorageComponent { boolean isDirty(); void markDirty(); void markClean(); - /** - * @param item - * @return all items belonging to passed class - */ - List findItems(Item item); - - /** - * @param stack - * @return all items that match this itemstack - */ - List findItems(ItemStack stack); - - ItemStack getItem(UUID id); - - int getStoredCount(); - int getCapacity(); - - ItemStack insertItem(ItemStack item, boolean simulate); - - default ItemStack extractItem(UUID id, int amount, boolean simulate) { - return extractItem(id, amount, true, simulate); - } - - ItemStack extractItem(UUID id, int amount, boolean obey_stack_size, boolean simulate); + BigDecimal getStoredCount(); + BigDecimal getCapacity(); // not extending INBTSerializable to avoid serializing it as forgecaps CompoundTag serializeNBT(); diff --git a/src/main/java/ru/dbotthepony/mc/otm/capability/drive/ItemMatteryDrive.java b/src/main/java/ru/dbotthepony/mc/otm/capability/drive/ItemMatteryDrive.java new file mode 100644 index 000000000..4f586ef76 --- /dev/null +++ b/src/main/java/ru/dbotthepony/mc/otm/capability/drive/ItemMatteryDrive.java @@ -0,0 +1,125 @@ +package ru.dbotthepony.mc.otm.capability.drive; + +import net.minecraft.MethodsReturnNonnullByDefault; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; +import net.minecraftforge.registries.RegistryManager; +import ru.dbotthepony.mc.otm.storage.IStorageTuple; +import ru.dbotthepony.mc.otm.storage.ItemStackWrapper; +import ru.dbotthepony.mc.otm.storage.StorageObjectRegistry; +import ru.dbotthepony.mc.otm.storage.StorageObjectTuple; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +@MethodsReturnNonnullByDefault +@ParametersAreNonnullByDefault +public class ItemMatteryDrive extends MatteryDrive implements IItemMatteryDrive { + private static StorageObjectTuple identity; + + public ItemMatteryDrive(BigDecimal capacity, int max_different_stacks) { + super(capacity, max_different_stacks); + } + + public ItemMatteryDrive(BigDecimal capacity) { + super(capacity); + } + + @Override + public StorageObjectTuple identity() { + if (identity == null) + identity = StorageObjectRegistry.getOrError(ItemStackWrapper.class); + + return identity; + } + + public ItemStack insertObject(ItemStack item, boolean simulate) { + return insertObject(new ItemStackWrapper(item), simulate).stack(); + } + + @Override + protected CompoundTag serializeStack(IStorageTuple item) { + final var tag = new CompoundTag(); + final var location = Objects.requireNonNull(item.object().stack().getItem().getRegistryName(), "Missing registry name for stored Item").toString(); + + tag.putString("item", location); + tag.putInt("count", item.object().stack().getCount()); + + CompoundTag item_tag; + + if ((item_tag = item.object().stack().getTag()) != null) { + tag.put("data", item_tag); + } + + return tag; + } + + @Nullable + @Override + protected ItemStackWrapper deserializeStack(CompoundTag tag) { + final var item = RegistryManager.ACTIVE.getRegistry(Item.class).getValue(new ResourceLocation(tag.getString("item"))); + + if (item != null && item != Items.AIR) { + final var count = tag.getInt("count"); + final var data = tag.get("data"); + + final var itemstack = new ItemStack(item, count); + + if (data != null) { + itemstack.setTag((CompoundTag) data); + } + + return new ItemStackWrapper(itemstack); + } + + return null; + } + + + @Override + public List> findItems(Item item) { + var list = items.get(item); + + if (list != null) + return List.copyOf(list); + + return List.of(); + } + + @Override + public List> findItems(ItemStack stack) { + var list = items.get(stack.getItem()); + + if (list == null) + return List.of(); + + var amount = 0; + + for (var _stack : list) { + if (ItemStack.tagMatches(_stack.object().stack(), stack)) { + amount++; + } + } + + var build_list = new ArrayList>(amount); + var i = 0; + + for (var _stack : list) { + if (ItemStack.tagMatches(_stack.object().stack(), stack)) { + build_list.set(i, _stack); + i++; + } + } + + return build_list; + } +} diff --git a/src/main/java/ru/dbotthepony/mc/otm/capability/drive/MatteryDrive.java b/src/main/java/ru/dbotthepony/mc/otm/capability/drive/MatteryDrive.java index a4bdca794..83693f767 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/capability/drive/MatteryDrive.java +++ b/src/main/java/ru/dbotthepony/mc/otm/capability/drive/MatteryDrive.java @@ -4,59 +4,37 @@ import net.minecraft.MethodsReturnNonnullByDefault; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; import net.minecraft.nbt.Tag; -import net.minecraft.resources.ResourceLocation; -import net.minecraft.world.item.Item; -import net.minecraft.world.item.ItemStack; -import net.minecraft.world.item.Items; -import net.minecraftforge.registries.RegistryManager; -import ru.dbotthepony.mc.otm.storage.IStorageListener; -import ru.dbotthepony.mc.otm.storage.IStorageTuple; -import ru.dbotthepony.mc.otm.storage.ItemStackWrapper; -import ru.dbotthepony.mc.otm.storage.StorageTuple; +import ru.dbotthepony.mc.otm.storage.*; import javax.annotation.Nonnull; +import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; import java.math.BigDecimal; import java.util.*; @MethodsReturnNonnullByDefault @ParametersAreNonnullByDefault -public class MatteryDrive implements IMatteryDrive { - protected final HashMap> items = new HashMap<>(); - protected final HashMap items_by_id = new HashMap<>(); +abstract public class MatteryDrive implements IMatteryDrive { + protected final HashMap>> items = new HashMap<>(); + protected final HashMap> items_by_id = new HashMap<>(); + + abstract public StorageObjectTuple identity(); protected boolean dirty = false; protected int different_stacks = 0; - protected int stored = 0; + protected BigDecimal stored = BigDecimal.ZERO; protected int max_different_stacks; - protected int capacity; + protected BigDecimal capacity; - public MatteryDrive(int capacity, int max_different_stacks) { + public MatteryDrive(BigDecimal capacity, int max_different_stacks) { this.capacity = capacity; this.max_different_stacks = max_different_stacks; } - public MatteryDrive(int capacity) { + public MatteryDrive(BigDecimal capacity) { this(capacity, 0xFFFF); } - @Override - public List getItems() { - int amount = 0; - - for (var list : items.values()) { - amount += list.size(); - } - - var output = new ArrayList(amount); - - for (var list : items.values()) { - output.addAll(list); - } - - return output; - } - @Override public boolean isDirty() { return dirty; @@ -73,86 +51,40 @@ public class MatteryDrive implements IMatteryDrive { } @Override - public int getStoredCount() { + public BigDecimal getStoredCount() { return stored; } @Override - public int getCapacity() { + public BigDecimal getCapacity() { return capacity; } - @Override - @Nonnull - public List findItems(@Nonnull Item item) { - var list = items.get(item); - - if (list != null) - return List.copyOf(list); - - return List.of(); - } - - @Override - @Nonnull - public List findItems(@Nonnull ItemStack stack) { - var list = items.get(stack.getItem()); - - if (list == null) - return List.of(); - - var amount = 0; - - for (var _stack : list) { - if (ItemStack.tagMatches(_stack.stack(), stack)) { - amount++; - } - } - - var build_list = new ArrayList(amount); - var i = 0; - - for (var _stack : list) { - if (ItemStack.tagMatches(_stack.stack(), stack)) { - build_list.set(i, _stack); - i++; - } - } - - return build_list; - } - @Nonnull @Override - public ItemStack getItem(UUID id) { - var stack = items_by_id.get(id); - return stack != null ? stack.stack() : ItemStack.EMPTY; - } + @SuppressWarnings("unchecked") + public T insertObject(T item, boolean simulate) { + BigDecimal max_insert = getCapacity().subtract(getStoredCount()).min(item.getCount()); - @Nonnull - @Override - public ItemStack insertItem(@Nonnull ItemStack item, boolean simulate) { - int max_insert = Math.min(getCapacity() - getStoredCount(), item.getCount()); - - if (max_insert <= 0) + if (max_insert.compareTo(BigDecimal.ZERO) <= 0) return item; - var listing = items.computeIfAbsent(item.getItem(), (key) -> new ArrayList<>()); + final var listing = items.computeIfAbsent(item.partitionKey(), (key) -> new ArrayList<>()); for (var state : listing) { - if (ItemStack.tagMatches(state.stack(), item)) { + if (state.object().sameItem(item)) { if (!simulate) { - state.stack().grow(max_insert); - stored += max_insert; + state.object().grow(max_insert); + stored = stored.add(max_insert); for (var listener : listeners2) { - listener.changeObject(state.id(), new BigDecimal(state.stack().getCount())); + listener.changeObject(state.id(), state.object().getCount()); } markDirty(); } - var copy_item = item.copy(); + final var copy_item = (T) item.copy(); copy_item.shrink(max_insert); return copy_item; } @@ -164,54 +96,49 @@ public class MatteryDrive implements IMatteryDrive { if (!simulate) { different_stacks++; - stored += max_insert; + stored = stored.add(max_insert); - var copy = item.copy(); + final var copy = (T) item.copy(); copy.setCount(max_insert); - var state = new StoredStack(copy, UUID.randomUUID()); + final var state = new StorageTuple<>(UUID.randomUUID(), copy); listing.add(state); items_by_id.put(state.id(), state); for (var listener : listeners2) { - listener.addObject(new ItemStackWrapper(state.stack()), state.id(), this); + listener.addObject(state.object(), state.id(), this); } markDirty(); } - var copy_item = item.copy(); + final var copy_item = (T) item.copy(); copy_item.shrink(max_insert); return copy_item; } - @Override - public ItemStackWrapper insertObject(ItemStackWrapper obj, boolean simulate) { - var get = insertItem(obj.stack(), simulate); - return get.isEmpty() ? ItemStackWrapper.EMPTY : new ItemStackWrapper(get); - } - @Nonnull @Override - public ItemStack extractItem(UUID id, int amount, boolean obey_stack_size, boolean simulate) { + @SuppressWarnings("unchecked") + public T extractObject(UUID id, BigDecimal amount, boolean simulate) { var get = items_by_id.get(id); if (get == null) - return ItemStack.EMPTY; + return identity().empty(); - var extract = Math.min(amount, get.stack().getCount()); + if (amount.compareTo(BigDecimal.ZERO) <= 0) + amount = get.object().getMaxStackSize().orElse(get.object().getCount()); - if (obey_stack_size) - extract = Math.min(extract, get.stack().getItem().getItemStackLimit(get.stack())); + amount = amount.min(get.object().getCount()); - if (extract <= 0) - return ItemStack.EMPTY; + if (amount.compareTo(BigDecimal.ZERO) <= 0) + return identity().empty(); - var copy = get.stack().copy(); - copy.setCount(extract); + final var copy = (T) get.object().copy(); + copy.setCount(amount); if (!simulate) { - if (extract == get.stack().getCount()) { - var listing = items.get(get.stack().getItem()); + if (amount.compareTo(get.object().getCount()) == 0) { + var listing = items.get(get.object().partitionKey()); listing.remove(get); different_stacks--; @@ -220,16 +147,16 @@ public class MatteryDrive implements IMatteryDrive { } if (listing.size() == 0) { - items.remove(get.stack().getItem()); + items.remove(get.object().partitionKey()); } } - stored -= extract; - get.stack().shrink(extract); + stored = stored.subtract(amount); + get.object().shrink(amount); - if (get.stack().getCount() != 0) { + if (get.object().getCount().compareTo(BigDecimal.ZERO) != 0) { for (var listener : listeners2) { - listener.changeObject(get.id(), new BigDecimal(get.stack().getCount())); + listener.changeObject(get.id(), get.object().getCount()); } } @@ -239,132 +166,99 @@ public class MatteryDrive implements IMatteryDrive { return copy; } + @Nullable + abstract protected CompoundTag serializeStack(IStorageTuple item); + @Override public CompoundTag serializeNBT() { final var compound = new CompoundTag(); - compound.putInt("capacity", capacity); + compound.putString("capacity", capacity.toString()); compound.putInt("max_different_stacks", max_different_stacks); var list_items = new ListTag(); compound.put("items", list_items); for (var entry : items.entrySet()) { - var item = Objects.requireNonNull(entry.getKey().getRegistryName(), "Missing registry name for stored Item").toString(); - var entry_compound = new CompoundTag(); - - entry_compound.putString("id", item); - var stack_list = new ListTag(); - entry_compound.put("list", stack_list); - for (var stack : entry.getValue()) { - var stack_nbt = new CompoundTag(); - stack_list.add(stack_nbt); + CompoundTag entry_compound; - stack_nbt.putInt("count", stack.stack().getCount()); - stack_nbt.putLongArray("id", new long[] { stack.id().getMostSignificantBits(), stack.id().getLeastSignificantBits() }); - - if (stack.stack().getTag() != null) { - stack_nbt.put("data", stack.stack().getTag()); + if ((entry_compound = serializeStack(stack)) != null) { + entry_compound.putLongArray("id", new long[] { stack.id().getMostSignificantBits(), stack.id().getLeastSignificantBits() }); + list_items.add(entry_compound); } } - - list_items.add(entry_compound); } return compound; } + @Nullable + abstract protected T deserializeStack(CompoundTag tag); + @Override public void deserializeNBT(CompoundTag nbt) { items.clear(); items_by_id.clear(); - stored = 0; + stored = BigDecimal.ZERO; different_stacks = 0; - capacity = nbt.getInt("capacity"); + capacity = new BigDecimal(nbt.getString("capacity")); max_different_stacks = nbt.getInt("max_different_stacks"); - var list_items = nbt.getList("items", Tag.TAG_COMPOUND); - var registry = RegistryManager.ACTIVE.getRegistry(Item.class); - - for (var _entry : list_items) { + for (var _entry : nbt.getList("items", Tag.TAG_COMPOUND)) { if (_entry instanceof CompoundTag entry) { - var item = registry.getValue(new ResourceLocation(entry.getString("id"))); + final var stack = deserializeStack(entry); - if (item != null && item != Items.AIR) { - var map_list = items.computeIfAbsent(item, (key) -> new ArrayList<>()); + if (stack != null) { + stored = stored.add(stack.getCount()); + different_stacks++; + final var id = entry.getLongArray("id"); - var stack_list = entry.getList("list", Tag.TAG_COMPOUND); - - for (var _stack : stack_list) { - if (_stack instanceof CompoundTag stack) { - var count = stack.getInt("count"); - var id = stack.getLongArray("id"); - var data = stack.get("data"); - - var itemstack = new ItemStack(item, count); - - if (data != null) { - itemstack.setTag((CompoundTag) data); - } - - stored += count; - different_stacks += 1; - var state = new StoredStack(itemstack, id.length == 2 ? new UUID(id[0], id[1]) : UUID.randomUUID()); - map_list.add(state); - items_by_id.put(state.id(), state); - } - } + final var tuple = new StorageTuple<>(id.length == 2 ? new UUID(id[0], id[1]) : UUID.randomUUID(), stack); + items.computeIfAbsent(stack.partitionKey(), (key) -> new ArrayList<>()).add(tuple); + items_by_id.put(tuple.id(), tuple); } } } } @Override - public Class storageIdentity() { - return ItemStackWrapper.class; + public Class storageIdentity() { + return identity().identity(); } @Override - public ItemStackWrapper getStoredObject(UUID id) { - var get = getItem(id); - return get.isEmpty() ? ItemStackWrapper.EMPTY : new ItemStackWrapper(get); + public T getStoredObject(UUID id) { + var get = items_by_id.get(id); + return get == null ? identity().empty() : get.object(); } @Override - public ItemStackWrapper extractObject(UUID id, BigDecimal amount, boolean simulate) { - var get = extractItem(id, amount.intValue(), simulate); - return get.isEmpty() ? ItemStackWrapper.EMPTY : new ItemStackWrapper(get); - } - - @Override - public List> getStorageObjects() { + public List> getStorageObjects() { int amount = 0; for (var listing : items.values()) amount += listing.size(); - var list = new ArrayList>(amount); + var list = new ArrayList>(amount); for (var listing : items.values()) { - for (var stack : listing) { - list.add(new StorageTuple<>(stack.id(), new ItemStackWrapper(stack.stack()))); - } + list.addAll(listing); } return list; } - protected final HashSet> listeners2 = new HashSet<>(); + protected final HashSet> listeners2 = new HashSet<>(); @Override - public boolean addListener(IStorageListener listener) { + public boolean addListener(IStorageListener listener) { return listeners2.add(listener); } @Override - public boolean removeListener(IStorageListener listener) { + public boolean removeListener(IStorageListener listener) { return listeners2.remove(listener); } } diff --git a/src/main/java/ru/dbotthepony/mc/otm/item/ItemPortableCondensationDrive.java b/src/main/java/ru/dbotthepony/mc/otm/item/ItemPortableCondensationDrive.java index 934b43f5e..477da6657 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/item/ItemPortableCondensationDrive.java +++ b/src/main/java/ru/dbotthepony/mc/otm/item/ItemPortableCondensationDrive.java @@ -35,11 +35,14 @@ import ru.dbotthepony.mc.otm.OverdriveThatMatters; import ru.dbotthepony.mc.otm.capability.MatteryCapability; import ru.dbotthepony.mc.otm.capability.drive.DrivePool; import ru.dbotthepony.mc.otm.capability.drive.IMatteryDrive; +import ru.dbotthepony.mc.otm.capability.drive.ItemMatteryDrive; import ru.dbotthepony.mc.otm.capability.drive.MatteryDrive; import ru.dbotthepony.mc.otm.menu.DriveViewerMenu; +import ru.dbotthepony.mc.otm.storage.ItemStackWrapper; import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -47,11 +50,11 @@ import java.util.UUID; import java.util.function.Supplier; public class ItemPortableCondensationDrive extends Item { - public final int capacity; + public final BigDecimal capacity; public ItemPortableCondensationDrive(int capacity) { super(new Properties().stacksTo(1).tab(OverdriveThatMatters.CREATIVE_TAB)); - this.capacity = capacity; + this.capacity = new BigDecimal(capacity); } @SubscribeEvent @@ -72,12 +75,12 @@ public class ItemPortableCondensationDrive extends Item { var _cap = stack.getCapability(MatteryCapability.DRIVE).resolve(); if (_cap.isPresent()) { - var cap = _cap.get(); + var cap = (ItemMatteryDrive) _cap.get(); var filter = drive.getFilterSettings(stack); if (filter.matches(event.getItem().getItem())) { var copy = event.getItem().getItem().copy(); - var remaining = cap.insertItem(event.getItem().getItem(), false); + var remaining = cap.insertObject(event.getItem().getItem(), false); if (remaining.getCount() == event.getItem().getItem().getCount()) { continue; @@ -117,10 +120,10 @@ public class ItemPortableCondensationDrive extends Item { private final ItemStack stack; private final LazyOptional resolver = LazyOptional.of(() -> { return DrivePool.get(uuid, (tag) -> { - var drive = new MatteryDrive(capacity); + var drive = new ItemMatteryDrive(capacity); drive.deserializeNBT(tag); return drive; - }, () -> new MatteryDrive(capacity)); + }, () -> new ItemMatteryDrive(capacity)); }); DriveCapability(ItemStack stack) { diff --git a/src/main/java/ru/dbotthepony/mc/otm/matter/MatterRegistry.java b/src/main/java/ru/dbotthepony/mc/otm/matter/MatterRegistry.java index 2d4317f00..48dde883b 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/matter/MatterRegistry.java +++ b/src/main/java/ru/dbotthepony/mc/otm/matter/MatterRegistry.java @@ -20,9 +20,11 @@ import net.minecraftforge.fmlserverevents.FMLServerStartedEvent; import net.minecraftforge.fmlserverevents.FMLServerStartingEvent; import ru.dbotthepony.mc.otm.OverdriveThatMatters; import ru.dbotthepony.mc.otm.capability.MatteryCapability; +import ru.dbotthepony.mc.otm.capability.drive.IMatteryDrive; import ru.dbotthepony.mc.otm.menu.FormattingHelper; import ru.dbotthepony.mc.otm.network.MatterRegistryPacket; import ru.dbotthepony.mc.otm.network.MatteryNetworking; +import ru.dbotthepony.mc.otm.storage.ItemStackWrapper; import javax.annotation.Nullable; import java.math.BigDecimal; @@ -50,7 +52,7 @@ public class MatterRegistry { } public static boolean canDecompose(ItemStack stack) { - if (stack.getCapability(MatteryCapability.DRIVE).isPresent() && stack.getCapability(MatteryCapability.DRIVE).resolve().get().getStoredCount() > 0) + if (stack.getCapability(MatteryCapability.DRIVE).isPresent() && stack.getCapability(MatteryCapability.DRIVE).resolve().get().getStoredCount().compareTo(BigDecimal.ZERO) > 0) return false; if (stack.getCapability(MatteryCapability.MATTER).isPresent() && stack.getCapability(MatteryCapability.MATTER).resolve().get().getStoredMatter().compareTo(BigDecimal.ZERO) > 0) @@ -74,9 +76,9 @@ public class MatterRegistry { var cap1 = stack.getCapability(MatteryCapability.DRIVE).resolve(); var cap2 = stack.getCapability(MatteryCapability.MATTER).resolve(); - if (cap1.isPresent()) { - for (var stored : cap1.get().getItems()) { - matter = matter.add(getMatterValue(stored.stack(), level + 1)); + if (cap1.isPresent() && cap1.get().storageIdentity() == ItemStackWrapper.class) { + for (var stored : ((IMatteryDrive) cap1.get()).getStorageObjects()) { + matter = matter.add(getMatterValue(stored.object().stack(), level + 1)); } } diff --git a/src/main/java/ru/dbotthepony/mc/otm/menu/DriveViewerMenu.java b/src/main/java/ru/dbotthepony/mc/otm/menu/DriveViewerMenu.java index dacd5bd3c..95f8d5e80 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/menu/DriveViewerMenu.java +++ b/src/main/java/ru/dbotthepony/mc/otm/menu/DriveViewerMenu.java @@ -22,6 +22,7 @@ import ru.dbotthepony.mc.otm.menu.data.NetworkedItemView; import ru.dbotthepony.mc.otm.menu.slot.MatterySlot; import ru.dbotthepony.mc.otm.network.MatteryNetworking; import ru.dbotthepony.mc.otm.network.SetCarriedPacket; +import ru.dbotthepony.mc.otm.storage.ItemStackWrapper; import javax.annotation.Nullable; import java.util.function.Supplier; @@ -29,7 +30,7 @@ import java.util.function.Supplier; public class DriveViewerMenu extends PoweredMatteryMenu implements INetworkedItemViewSupplier { public final NetworkedItemView view; public MatterySlot drive_slot; - public IMatteryDrive last_drive; + public IMatteryDrive last_drive; @Override public NetworkedItemView getNetworkedItemView() { @@ -78,6 +79,7 @@ public class DriveViewerMenu extends PoweredMatteryMenu implements INetworkedIte } @Override + @SuppressWarnings("unchecked") public void broadcastChanges() { super.broadcastChanges(); @@ -89,10 +91,10 @@ public class DriveViewerMenu extends PoweredMatteryMenu implements INetworkedIte } else { var get_cap = get.getCapability(MatteryCapability.DRIVE).resolve(); - if (get_cap.isEmpty()) { + if (get_cap.isEmpty() || get_cap.get().storageIdentity() != ItemStackWrapper.class) { last_drive = null; } else { - last_drive = get_cap.get(); + last_drive = (IMatteryDrive) get_cap.get(); } } @@ -139,9 +141,9 @@ public class DriveViewerMenu extends PoweredMatteryMenu implements INetworkedIte return ItemStack.EMPTY; if (amount == item.getCount()) { - var remaining = last_drive.insertItem(item, false); + var remaining = last_drive.insertObject(new ItemStackWrapper(item), false); - if (remaining.getCount() == item.getCount()) + if (remaining.getCount().intValue() == item.getCount()) return ItemStack.EMPTY; if (remaining.isEmpty()) { @@ -152,8 +154,8 @@ public class DriveViewerMenu extends PoweredMatteryMenu implements INetworkedIte } var copy = item.copy(); - tile.getIOItemCount(item.getCount() - remaining.getCount(), false); - item.shrink(remaining.getCount()); + tile.getIOItemCount(item.getCount() - remaining.getCount().intValue(), false); + item.shrink(remaining.getCount().intValue()); slot.setChanged(); return copy; @@ -162,14 +164,14 @@ public class DriveViewerMenu extends PoweredMatteryMenu implements INetworkedIte var copy_insert = item.copy(); copy_insert.setCount(amount); - var remaining = last_drive.insertItem(copy_insert, false); + var remaining = last_drive.insertObject(new ItemStackWrapper(copy_insert), false); - if (remaining.getCount() == copy_insert.getCount()) + if (remaining.getCount().intValue() == copy_insert.getCount()) return ItemStack.EMPTY; var copy = item.copy(); - tile.getIOItemCount(amount - remaining.getCount(), false); - item.shrink(amount - remaining.getCount()); + tile.getIOItemCount(amount - remaining.getCount().intValue(), false); + item.shrink(amount - remaining.getCount().intValue()); slot.setChanged(); return copy; diff --git a/src/main/java/ru/dbotthepony/mc/otm/storage/IStorageStack.java b/src/main/java/ru/dbotthepony/mc/otm/storage/IStorageStack.java index 1ca8144c8..6c918a025 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/storage/IStorageStack.java +++ b/src/main/java/ru/dbotthepony/mc/otm/storage/IStorageStack.java @@ -35,8 +35,8 @@ public interface IStorageStack { * @return Identity utilized to partition view table (if it has any). * Defaults to no partitioning; meaning performance will degrade much quicker than it should. * Good object is an object that will influence on sameItem() return result, making it return true. - * It is weakly considered that if !this.itemIdentity().equals(other.itemIdentity()); - * then this.sameItem(other) will always return false. + * It is strictly considered that if !this.itemIdentity().equals(other.itemIdentity()); + * then this.sameItem(other) will never return true. * * If implemented, and storage is also partitioned properly, then performance will be flat equally to view table's performance. * diff --git a/src/main/java/ru/dbotthepony/mc/otm/storage/StorageObjectRegistry.java b/src/main/java/ru/dbotthepony/mc/otm/storage/StorageObjectRegistry.java index 523c5a109..80d947771 100644 --- a/src/main/java/ru/dbotthepony/mc/otm/storage/StorageObjectRegistry.java +++ b/src/main/java/ru/dbotthepony/mc/otm/storage/StorageObjectRegistry.java @@ -13,12 +13,10 @@ import java.util.Objects; public class StorageObjectRegistry { private static final HashMap, StorageObjectTuple> REGISTRY = new HashMap<>(); - public static boolean register(Class identity, T empty) { + public static StorageObjectTuple register(Class identity, T empty) { final var tuple = new StorageObjectTuple<>(identity, empty); - final boolean removed = REGISTRY.remove(identity) != null; REGISTRY.put(identity, tuple); - - return removed; + return tuple; } @SuppressWarnings("unchecked")