Merge branch 'master' of https://gitlab.com/DBotThePony/overdrive-that-matters
This commit is contained in:
commit
3a61be004b
@ -7,11 +7,13 @@ import net.minecraft.network.chat.TranslatableComponent;
|
||||
import net.minecraft.world.entity.player.Inventory;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.inventory.AbstractContainerMenu;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.entity.BlockEntityType;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import ru.dbotthepony.mc.otm.OverdriveThatMatters;
|
||||
import ru.dbotthepony.mc.otm.Registry;
|
||||
import ru.dbotthepony.mc.otm.block.BlockDriveViewer;
|
||||
import ru.dbotthepony.mc.otm.block.entity.worker.WorkerState;
|
||||
@ -30,37 +32,29 @@ public class BlockEntityDriveViewer extends BlockEntityMatteryPowered {
|
||||
return energy.getBatteryLevel().compareTo(MTE_PER_OPERATION) >= 0;
|
||||
}
|
||||
|
||||
private void updateState(Level level) {
|
||||
if (isRemoved())
|
||||
return;
|
||||
|
||||
var state = getBlockState();
|
||||
|
||||
if (drive_slot.getItem(0).getCapability(MatteryCapability.DRIVE).isPresent() && canIOItems()) {
|
||||
state = state.setValue(WorkerState.SEMI_WORKER_STATE, WorkerState.WORKING);
|
||||
} else {
|
||||
state = state.setValue(WorkerState.SEMI_WORKER_STATE, WorkerState.IDLE);
|
||||
}
|
||||
|
||||
if (state != getBlockState()) {
|
||||
level.setBlock(getBlockPos(), state, Block.UPDATE_CLIENTS);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setChanged() {
|
||||
super.setChanged();
|
||||
|
||||
if (level != null) {
|
||||
var state = getBlockState();
|
||||
|
||||
if (drive_slot.getItem(0).getCapability(MatteryCapability.DRIVE).isPresent()) {
|
||||
if (!getBlockState().getValue(BlockDriveViewer.DRIVE_PRESENT)) {
|
||||
state = state.setValue(BlockDriveViewer.DRIVE_PRESENT, true);
|
||||
}
|
||||
} else {
|
||||
if (getBlockState().getValue(BlockDriveViewer.DRIVE_PRESENT)) {
|
||||
state = state.setValue(BlockDriveViewer.DRIVE_PRESENT, false);
|
||||
}
|
||||
}
|
||||
|
||||
if (drive_slot.getItem(0).getCapability(MatteryCapability.DRIVE).isPresent() && canIOItems()) {
|
||||
if (getBlockState().getValue(WorkerState.SEMI_WORKER_STATE) != WorkerState.WORKING) {
|
||||
state = state.setValue(WorkerState.SEMI_WORKER_STATE, WorkerState.WORKING);
|
||||
}
|
||||
} else {
|
||||
if (getBlockState().getValue(WorkerState.SEMI_WORKER_STATE) != WorkerState.IDLE) {
|
||||
state = state.setValue(WorkerState.SEMI_WORKER_STATE, WorkerState.IDLE);
|
||||
}
|
||||
}
|
||||
|
||||
if (state != getBlockState()) {
|
||||
level.setBlock(getBlockPos(), state, Block.UPDATE_CLIENTS);
|
||||
}
|
||||
}
|
||||
if (level != null)
|
||||
OverdriveThatMatters.tickOnce(level, this::updateState);
|
||||
}
|
||||
|
||||
public int getIOItemCount(int desired, boolean simulate) {
|
||||
@ -85,7 +79,29 @@ public class BlockEntityDriveViewer extends BlockEntityMatteryPowered {
|
||||
|
||||
private static final TranslatableComponent NAME = new TranslatableComponent("block.overdrive_that_matters.drive_viewer");
|
||||
|
||||
public final MatteryContainer drive_slot = new MatteryContainer(this::setChanged, 1);
|
||||
public final MatteryContainer drive_slot = new MatteryContainer(this::setChanged, 1) {
|
||||
@Override
|
||||
public void setChanged(int slot, ItemStack new_state, ItemStack old_state) {
|
||||
super.setChanged(slot, new_state, old_state);
|
||||
|
||||
if (level != null)
|
||||
OverdriveThatMatters.tickOnce(level, (level) -> {
|
||||
if (!isRemoved()) {
|
||||
var state = getBlockState();
|
||||
|
||||
if (new_state.getCapability(MatteryCapability.DRIVE).isPresent()) {
|
||||
state = state.setValue(BlockDriveViewer.DRIVE_PRESENT, true);
|
||||
} else {
|
||||
state = state.setValue(BlockDriveViewer.DRIVE_PRESENT, false);
|
||||
}
|
||||
|
||||
if (state != getBlockState()) {
|
||||
level.setBlock(getBlockPos(), state, Block.UPDATE_CLIENTS);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@Override
|
||||
protected Component getDefaultDisplayName() {
|
||||
|
@ -63,55 +63,55 @@ abstract public class AbstractMatteryDrive<T extends IStorageStack> implements I
|
||||
@Nonnull
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public T insertObject(T item, boolean simulate) {
|
||||
BigDecimal max_insert = getCapacity().subtract(getStoredCount()).min(item.getCount());
|
||||
public T insertStack(T stack, boolean simulate) {
|
||||
BigDecimal max_insert = getCapacity().subtract(getStoredCount()).min(stack.getCount());
|
||||
|
||||
if (max_insert.compareTo(BigDecimal.ZERO) <= 0)
|
||||
return item;
|
||||
return stack;
|
||||
|
||||
final var listing = items.computeIfAbsent(item.partitionKey(), (key) -> new ArrayList<>());
|
||||
final var listing = items.computeIfAbsent(stack.partitionKey(), (key) -> new ArrayList<>());
|
||||
|
||||
for (var state : listing) {
|
||||
if (state.object().sameItem(item)) {
|
||||
if (state.stack().sameItem(stack)) {
|
||||
if (!simulate) {
|
||||
state.object().grow(max_insert);
|
||||
state.stack().grow(max_insert);
|
||||
stored = stored.add(max_insert);
|
||||
|
||||
for (var listener : listeners2) {
|
||||
listener.changeObject(state.id(), state.object().getCount());
|
||||
listener.changeObject(state.id(), state.stack().getCount());
|
||||
}
|
||||
|
||||
markDirty();
|
||||
}
|
||||
|
||||
final var copy_item = (T) item.copy();
|
||||
final var copy_item = (T) stack.copy();
|
||||
copy_item.shrink(max_insert);
|
||||
return copy_item;
|
||||
}
|
||||
}
|
||||
|
||||
if (different_stacks >= max_different_stacks) {
|
||||
return item;
|
||||
return stack;
|
||||
}
|
||||
|
||||
if (!simulate) {
|
||||
different_stacks++;
|
||||
stored = stored.add(max_insert);
|
||||
|
||||
final var copy = (T) item.copy();
|
||||
final var copy = (T) stack.copy();
|
||||
copy.setCount(max_insert);
|
||||
final var state = new StorageTuple<>(UUID.randomUUID(), copy);
|
||||
listing.add(state);
|
||||
items_by_id.put(state.id(), state);
|
||||
|
||||
for (var listener : listeners2) {
|
||||
listener.addObject(state.object(), state.id(), this);
|
||||
listener.addObject(state.stack(), state.id(), this);
|
||||
}
|
||||
|
||||
markDirty();
|
||||
}
|
||||
|
||||
final var copy_item = (T) item.copy();
|
||||
final var copy_item = (T) stack.copy();
|
||||
copy_item.shrink(max_insert);
|
||||
return copy_item;
|
||||
}
|
||||
@ -119,26 +119,26 @@ abstract public class AbstractMatteryDrive<T extends IStorageStack> implements I
|
||||
@Nonnull
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public T extractObject(UUID id, BigDecimal amount, boolean simulate) {
|
||||
public T extractStack(UUID id, BigDecimal amount, boolean simulate) {
|
||||
var get = items_by_id.get(id);
|
||||
|
||||
if (get == null)
|
||||
return identity().empty();
|
||||
|
||||
if (amount.compareTo(BigDecimal.ZERO) <= 0)
|
||||
amount = get.object().getMaxStackSize().orElse(get.object().getCount());
|
||||
amount = get.stack().getMaxStackSize().orElse(get.stack().getCount());
|
||||
|
||||
amount = amount.min(get.object().getCount());
|
||||
amount = amount.min(get.stack().getCount());
|
||||
|
||||
if (amount.compareTo(BigDecimal.ZERO) <= 0)
|
||||
return identity().empty();
|
||||
|
||||
final var copy = (T) get.object().copy();
|
||||
final var copy = (T) get.stack().copy();
|
||||
copy.setCount(amount);
|
||||
|
||||
if (!simulate) {
|
||||
if (amount.compareTo(get.object().getCount()) == 0) {
|
||||
var listing = items.get(get.object().partitionKey());
|
||||
if (amount.compareTo(get.stack().getCount()) == 0) {
|
||||
var listing = items.get(get.stack().partitionKey());
|
||||
listing.remove(get);
|
||||
different_stacks--;
|
||||
|
||||
@ -147,16 +147,16 @@ abstract public class AbstractMatteryDrive<T extends IStorageStack> implements I
|
||||
}
|
||||
|
||||
if (listing.size() == 0) {
|
||||
items.remove(get.object().partitionKey());
|
||||
items.remove(get.stack().partitionKey());
|
||||
}
|
||||
}
|
||||
|
||||
stored = stored.subtract(amount);
|
||||
get.object().shrink(amount);
|
||||
get.stack().shrink(amount);
|
||||
|
||||
if (get.object().getCount().compareTo(BigDecimal.ZERO) != 0) {
|
||||
if (get.stack().getCount().compareTo(BigDecimal.ZERO) != 0) {
|
||||
for (var listener : listeners2) {
|
||||
listener.changeObject(get.id(), get.object().getCount());
|
||||
listener.changeObject(get.id(), get.stack().getCount());
|
||||
}
|
||||
}
|
||||
|
||||
@ -229,13 +229,13 @@ abstract public class AbstractMatteryDrive<T extends IStorageStack> implements I
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getStoredObject(UUID id) {
|
||||
public T getStack(UUID id) {
|
||||
var get = items_by_id.get(id);
|
||||
return get == null ? identity().empty() : get.object();
|
||||
return get == null ? identity().empty() : get.stack();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<IStorageTuple<T>> getStorageObjects() {
|
||||
public List<IStorageTuple<T>> getStacks() {
|
||||
int amount = 0;
|
||||
|
||||
for (var listing : items.values())
|
||||
|
@ -41,20 +41,20 @@ public class ItemMatteryDrive extends AbstractMatteryDrive<ItemStackWrapper> imp
|
||||
}
|
||||
|
||||
public ItemStack insertObject(ItemStack item, boolean simulate) {
|
||||
return insertObject(new ItemStackWrapper(item), simulate).stack();
|
||||
return insertStack(new ItemStackWrapper(item), simulate).stack();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected CompoundTag serializeStack(IStorageTuple<ItemStackWrapper> item) {
|
||||
final var tag = new CompoundTag();
|
||||
final var location = Objects.requireNonNull(item.object().stack().getItem().getRegistryName(), "Missing registry name for stored Item").toString();
|
||||
final var location = Objects.requireNonNull(item.stack().stack().getItem().getRegistryName(), "Missing registry name for stored Item").toString();
|
||||
|
||||
tag.putString("item", location);
|
||||
tag.putInt("count", item.object().stack().getCount());
|
||||
tag.putInt("count", item.stack().stack().getCount());
|
||||
|
||||
CompoundTag item_tag;
|
||||
|
||||
if ((item_tag = item.object().stack().getTag()) != null) {
|
||||
if ((item_tag = item.stack().stack().getTag()) != null) {
|
||||
tag.put("data", item_tag);
|
||||
}
|
||||
|
||||
@ -103,7 +103,7 @@ public class ItemMatteryDrive extends AbstractMatteryDrive<ItemStackWrapper> imp
|
||||
var amount = 0;
|
||||
|
||||
for (var _stack : list) {
|
||||
if (ItemStack.tagMatches(_stack.object().stack(), stack)) {
|
||||
if (ItemStack.tagMatches(_stack.stack().stack(), stack)) {
|
||||
amount++;
|
||||
}
|
||||
}
|
||||
@ -112,7 +112,7 @@ public class ItemMatteryDrive extends AbstractMatteryDrive<ItemStackWrapper> imp
|
||||
var i = 0;
|
||||
|
||||
for (var _stack : list) {
|
||||
if (ItemStack.tagMatches(_stack.object().stack(), stack)) {
|
||||
if (ItemStack.tagMatches(_stack.stack().stack(), stack)) {
|
||||
build_list.set(i, _stack);
|
||||
i++;
|
||||
}
|
||||
|
@ -284,7 +284,7 @@ public class MatteryContainer implements Container, Iterable<ItemStack> {
|
||||
|
||||
@Override
|
||||
public ItemStack getItem(int slot) {
|
||||
return slots[slot];
|
||||
return slots[slot].isEmpty() ? ItemStack.EMPTY : slots[slot];
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -15,9 +15,7 @@ import net.minecraftforge.event.entity.player.ItemTooltipEvent;
|
||||
import net.minecraftforge.event.entity.player.PlayerEvent;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fmllegacy.network.PacketDistributor;
|
||||
import net.minecraftforge.fmlserverevents.FMLServerAboutToStartEvent;
|
||||
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;
|
||||
@ -29,7 +27,6 @@ import ru.dbotthepony.mc.otm.storage.ItemStackWrapper;
|
||||
import javax.annotation.Nullable;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.*;
|
||||
|
||||
public class MatterRegistry {
|
||||
@ -77,8 +74,8 @@ public class MatterRegistry {
|
||||
var cap2 = stack.getCapability(MatteryCapability.MATTER).resolve();
|
||||
|
||||
if (cap1.isPresent() && cap1.get().storageIdentity() == ItemStackWrapper.class) {
|
||||
for (var stored : ((IMatteryDrive<ItemStackWrapper>) cap1.get()).getStorageObjects()) {
|
||||
matter = matter.add(getMatterValue(stored.object().stack(), level + 1));
|
||||
for (var stored : ((IMatteryDrive<ItemStackWrapper>) cap1.get()).getStacks()) {
|
||||
matter = matter.add(getMatterValue(stored.stack().stack(), level + 1));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,13 +1,9 @@
|
||||
package ru.dbotthepony.mc.otm.menu;
|
||||
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.network.FriendlyByteBuf;
|
||||
import net.minecraft.world.SimpleContainer;
|
||||
import net.minecraft.world.entity.player.Inventory;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.inventory.ClickAction;
|
||||
import net.minecraft.world.inventory.ClickType;
|
||||
import net.minecraft.world.item.Item;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraftforge.energy.CapabilityEnergy;
|
||||
import net.minecraftforge.fmllegacy.network.NetworkEvent;
|
||||
@ -17,11 +13,8 @@ import ru.dbotthepony.mc.otm.capability.MatteryCapability;
|
||||
import ru.dbotthepony.mc.otm.capability.drive.IMatteryDrive;
|
||||
import ru.dbotthepony.mc.otm.item.ItemPortableCondensationDrive;
|
||||
import ru.dbotthepony.mc.otm.menu.data.INetworkedItemViewSupplier;
|
||||
import ru.dbotthepony.mc.otm.menu.data.InteractPacket;
|
||||
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;
|
||||
@ -141,7 +134,7 @@ public class DriveViewerMenu extends PoweredMatteryMenu implements INetworkedIte
|
||||
return ItemStack.EMPTY;
|
||||
|
||||
if (amount == item.getCount()) {
|
||||
var remaining = last_drive.insertObject(new ItemStackWrapper(item), false);
|
||||
var remaining = last_drive.insertStack(new ItemStackWrapper(item), false);
|
||||
|
||||
if (remaining.getCount().intValue() == item.getCount())
|
||||
return ItemStack.EMPTY;
|
||||
@ -164,7 +157,7 @@ public class DriveViewerMenu extends PoweredMatteryMenu implements INetworkedIte
|
||||
var copy_insert = item.copy();
|
||||
copy_insert.setCount(amount);
|
||||
|
||||
var remaining = last_drive.insertObject(new ItemStackWrapper(copy_insert), false);
|
||||
var remaining = last_drive.insertStack(new ItemStackWrapper(copy_insert), false);
|
||||
|
||||
if (remaining.getCount().intValue() == copy_insert.getCount())
|
||||
return ItemStack.EMPTY;
|
||||
|
@ -217,13 +217,13 @@ public class NetworkedItemView implements IStorageListener<ItemStackWrapper> {
|
||||
}
|
||||
|
||||
int amount = calculateIOAmount(action == ClickAction.PRIMARY ? get_state.stack().getMaxStackSize() : Math.max(1, get_state.stack().getMaxStackSize() / 2));
|
||||
var extracted = provider.extractObject(get_state.id_upstream(), new BigDecimal(amount), true);
|
||||
var extracted = provider.extractStack(get_state.id_upstream(), new BigDecimal(amount), true);
|
||||
|
||||
if (!extracted.isEmpty()) {
|
||||
var move = menu.quickMoveToInventory(extracted.stack(), false);
|
||||
|
||||
if (move.remaining().getCount() != extracted.stack().getCount()) {
|
||||
provider.extractObject(get_state.id_upstream(), new BigDecimal(extracted.stack().getCount() - move.remaining().getCount()), false);
|
||||
provider.extractStack(get_state.id_upstream(), new BigDecimal(extracted.stack().getCount() - move.remaining().getCount()), false);
|
||||
doneIOAmount(extracted.stack().getCount() - move.remaining().getCount());
|
||||
}
|
||||
}
|
||||
@ -238,7 +238,7 @@ public class NetworkedItemView implements IStorageListener<ItemStackWrapper> {
|
||||
int amount = calculateIOAmount(carried.getCount());
|
||||
|
||||
if (amount == carried.getCount()) {
|
||||
var leftover = provider.insertObject(new ItemStackWrapper(menu.getCarried()), false);
|
||||
var leftover = provider.insertStack(new ItemStackWrapper(menu.getCarried()), false);
|
||||
menu.setCarried(leftover.stack());
|
||||
doneIOAmount(amount - leftover.stack().getCount());
|
||||
|
||||
@ -248,7 +248,7 @@ public class NetworkedItemView implements IStorageListener<ItemStackWrapper> {
|
||||
var copy = carried.copy();
|
||||
copy.setCount(amount);
|
||||
|
||||
var leftover = provider.insertObject(new ItemStackWrapper(copy), false);
|
||||
var leftover = provider.insertStack(new ItemStackWrapper(copy), false);
|
||||
doneIOAmount(amount - leftover.stack().getCount());
|
||||
|
||||
leftover.setCount(carried.getCount() - amount + leftover.getCountInt());
|
||||
@ -261,7 +261,7 @@ public class NetworkedItemView implements IStorageListener<ItemStackWrapper> {
|
||||
var copy = menu.getCarried().copy();
|
||||
copy.setCount(1);
|
||||
|
||||
if (calculateIOAmount(1) == 1 && provider.insertObject(new ItemStackWrapper(copy), false).isEmpty()) {
|
||||
if (calculateIOAmount(1) == 1 && provider.insertStack(new ItemStackWrapper(copy), false).isEmpty()) {
|
||||
menu.getCarried().shrink(1);
|
||||
doneIOAmount(1);
|
||||
MatteryNetworking.send((ServerPlayer) ply, new SetCarriedPacket(menu.getCarried()));
|
||||
@ -277,7 +277,7 @@ public class NetworkedItemView implements IStorageListener<ItemStackWrapper> {
|
||||
|
||||
int amount = calculateIOAmount(action == ClickAction.PRIMARY ? get_state.stack().getMaxStackSize() : Math.max(1, get_state.stack().getMaxStackSize() / 2));
|
||||
|
||||
var extracted = provider.extractObject(get_state.id_upstream(), new BigDecimal(amount), false);
|
||||
var extracted = provider.extractStack(get_state.id_upstream(), new BigDecimal(amount), false);
|
||||
|
||||
doneIOAmount(extracted.getCountInt());
|
||||
menu.setCarried(extracted.stack());
|
||||
|
@ -7,5 +7,5 @@ import javax.annotation.ParametersAreNonnullByDefault;
|
||||
@MethodsReturnNonnullByDefault
|
||||
@ParametersAreNonnullByDefault
|
||||
public interface IStorageConsumer<T extends IStorageStack> extends IStorageIdentity<T> {
|
||||
T insertObject(T obj, boolean simulate);
|
||||
T insertStack(T stack, boolean simulate);
|
||||
}
|
||||
|
@ -4,5 +4,5 @@ import java.util.UUID;
|
||||
|
||||
public interface IStorageTuple<T extends IStorageStack> {
|
||||
UUID id();
|
||||
T object();
|
||||
T stack();
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ public interface IStorageView<T extends IStorageStack> extends IStorageTrigger<T
|
||||
* @param id identifier of object
|
||||
* @return stored object (not a copy). Do not edit it.
|
||||
*/
|
||||
T getStoredObject(UUID id);
|
||||
T getStack(UUID id);
|
||||
|
||||
/**
|
||||
* @param id identifier of object to extract
|
||||
@ -23,7 +23,7 @@ public interface IStorageView<T extends IStorageStack> extends IStorageTrigger<T
|
||||
* @param simulate whenever to simulate the action or not
|
||||
* @return copy of object, with amount of units actually extracted
|
||||
*/
|
||||
T extractObject(UUID id, BigDecimal amount, boolean simulate);
|
||||
T extractStack(UUID id, BigDecimal amount, boolean simulate);
|
||||
|
||||
/**
|
||||
* Designed for views, for extraction with less computation overhead caused by
|
||||
@ -34,16 +34,16 @@ public interface IStorageView<T extends IStorageStack> extends IStorageTrigger<T
|
||||
* @param simulate whenever to simulate the action or not
|
||||
* @return amount extracted
|
||||
*/
|
||||
default BigDecimal extractObjectCount(UUID id, BigDecimal amount, boolean simulate) {
|
||||
return extractObject(id, amount, simulate).getCount();
|
||||
default BigDecimal extractStackCount(UUID id, BigDecimal amount, boolean simulate) {
|
||||
return extractStack(id, amount, simulate).getCount();
|
||||
}
|
||||
|
||||
List<IStorageTuple<T>> getStorageObjects();
|
||||
List<IStorageTuple<T>> getStacks();
|
||||
|
||||
default boolean addListenerAuto(IStorageListener<T> listener) {
|
||||
if (addListener(listener)) {
|
||||
for (var stack : getStorageObjects()) {
|
||||
listener.addObject(stack.object(), stack.id(), this);
|
||||
for (var stack : getStacks()) {
|
||||
listener.addObject(stack.stack(), stack.id(), this);
|
||||
}
|
||||
|
||||
return true;
|
||||
@ -54,7 +54,7 @@ public interface IStorageView<T extends IStorageStack> extends IStorageTrigger<T
|
||||
|
||||
default boolean removeListenerAuto(IStorageListener<T> listener) {
|
||||
if (removeListener(listener)) {
|
||||
for (var stack : getStorageObjects()) {
|
||||
for (var stack : getStacks()) {
|
||||
listener.removeObject(stack.id());
|
||||
}
|
||||
|
||||
|
@ -20,7 +20,7 @@ public class StorageGrid {
|
||||
public StorageGrid() {}
|
||||
|
||||
public <T extends IStorageStack> T insertObject(Class<T> type, T object, boolean simulate) {
|
||||
return getVirtualComponent(type).insertObject(object, simulate);
|
||||
return getVirtualComponent(type).insertStack(object, simulate);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -2,5 +2,5 @@ package ru.dbotthepony.mc.otm.storage;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public record StorageTuple<T extends IStorageStack>(UUID id, T object) implements IStorageTuple<T> {
|
||||
public record StorageTuple<T extends IStorageStack>(UUID id, T stack) implements IStorageTuple<T> {
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ import java.util.function.Supplier;
|
||||
@MethodsReturnNonnullByDefault
|
||||
@ParametersAreNonnullByDefault
|
||||
public class VirtualComponent<T extends IStorageStack> implements IStorageComponent<T>, IStorageListener<T> {
|
||||
public record LocalTuple<T extends IStorageStack>(T object, UUID id, List<RemoteTuple<T>> tuples) implements IStorageTuple<T> {
|
||||
public record LocalTuple<T extends IStorageStack>(T stack, UUID id, List<RemoteTuple<T>> tuples) implements IStorageTuple<T> {
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return obj instanceof LocalTuple tuple && tuple.id.equals(id) || obj instanceof UUID id && this.id.equals(id);
|
||||
@ -27,11 +27,11 @@ public class VirtualComponent<T extends IStorageStack> implements IStorageCompon
|
||||
|
||||
public record RemoteTuple<T extends IStorageStack>(T object, UUID remote_id, IStorageView<T> provider, LocalTuple<T> local) {
|
||||
public T extract(BigDecimal amount, boolean simulate) {
|
||||
return provider.extractObject(remote_id, amount, simulate);
|
||||
return provider.extractStack(remote_id, amount, simulate);
|
||||
}
|
||||
|
||||
public BigDecimal extractCount(BigDecimal amount, boolean simulate) {
|
||||
return provider.extractObjectCount(remote_id, amount, simulate);
|
||||
return provider.extractStackCount(remote_id, amount, simulate);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -98,11 +98,11 @@ public class VirtualComponent<T extends IStorageStack> implements IStorageCompon
|
||||
}
|
||||
|
||||
@Override
|
||||
public T insertObject(T object, boolean simulate) {
|
||||
var leftover = object;
|
||||
public T insertStack(T stack, boolean simulate) {
|
||||
var leftover = stack;
|
||||
|
||||
for (var consumer : consumers) {
|
||||
leftover = consumer.insertObject(leftover, simulate);
|
||||
leftover = consumer.insertStack(leftover, simulate);
|
||||
|
||||
if (leftover.isEmpty()) {
|
||||
return leftover;
|
||||
@ -128,28 +128,28 @@ public class VirtualComponent<T extends IStorageStack> implements IStorageCompon
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getStoredObject(UUID id) {
|
||||
public T getStack(UUID id) {
|
||||
final var tuple = indexed_by_local_uuid.get(id);
|
||||
return tuple != null ? tuple.object : identity.empty();
|
||||
return tuple != null ? tuple.stack : identity.empty();
|
||||
}
|
||||
|
||||
public static final BigDecimal MINUS_ONE = new BigDecimal(-1);
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public T extractObject(UUID id, BigDecimal amount, boolean simulate) {
|
||||
public T extractStack(UUID id, BigDecimal amount, boolean simulate) {
|
||||
var tuple = indexed_by_local_uuid.get(id);
|
||||
|
||||
if (tuple == null || amount.compareTo(BigDecimal.ZERO) == 0)
|
||||
return identity.empty();
|
||||
|
||||
if (amount.compareTo(MINUS_ONE) <= 0)
|
||||
amount = tuple.object.getMaxStackSize().orElse(tuple.object.getCount());
|
||||
amount = tuple.stack.getMaxStackSize().orElse(tuple.stack.getCount());
|
||||
|
||||
BigDecimal extract = tuple.object.getCount().min(amount);
|
||||
BigDecimal extract = tuple.stack.getCount().min(amount);
|
||||
BigDecimal extracted = BigDecimal.ZERO;
|
||||
|
||||
final var copy = (T) tuple.object.copy();
|
||||
final var copy = (T) tuple.stack.copy();
|
||||
|
||||
for (var remote_tuple : tuple.tuples) {
|
||||
extracted = extracted.add(remote_tuple.extractCount(extract.subtract(extracted), simulate));
|
||||
@ -167,7 +167,7 @@ public class VirtualComponent<T extends IStorageStack> implements IStorageCompon
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<IStorageTuple<T>> getStorageObjects() {
|
||||
public List<IStorageTuple<T>> getStacks() {
|
||||
int capacity = 0;
|
||||
|
||||
for (var list : partitions.values())
|
||||
@ -192,8 +192,8 @@ public class VirtualComponent<T extends IStorageStack> implements IStorageCompon
|
||||
LocalTuple<T> local_tuple = null;
|
||||
|
||||
for (var item : items) {
|
||||
if (item.object.sameItem(stack)) {
|
||||
item.object.grow(stack.getCount());
|
||||
if (item.stack.sameItem(stack)) {
|
||||
item.stack.grow(stack.getCount());
|
||||
local_tuple = item;
|
||||
break;
|
||||
}
|
||||
@ -213,11 +213,11 @@ public class VirtualComponent<T extends IStorageStack> implements IStorageCompon
|
||||
|
||||
if (added) {
|
||||
for (var listener : listeners) {
|
||||
listener.addObject(local_tuple.object, local_tuple.id, this);
|
||||
listener.addObject(local_tuple.stack, local_tuple.id, this);
|
||||
}
|
||||
} else {
|
||||
for (var listener : listeners) {
|
||||
listener.changeObject(local_tuple.id, local_tuple.object.getCount());
|
||||
listener.changeObject(local_tuple.id, local_tuple.stack.getCount());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -233,10 +233,10 @@ public class VirtualComponent<T extends IStorageStack> implements IStorageCompon
|
||||
|
||||
final var diff = new_count.subtract(tuple.object.getCount());
|
||||
tuple.object.setCount(new_count);
|
||||
tuple.local.object.grow(diff);
|
||||
tuple.local.stack.grow(diff);
|
||||
|
||||
for (var listener : listeners) {
|
||||
listener.changeObject(tuple.local.id, tuple.local.object.getCount());
|
||||
listener.changeObject(tuple.local.id, tuple.local.stack.getCount());
|
||||
}
|
||||
}
|
||||
|
||||
@ -247,13 +247,13 @@ public class VirtualComponent<T extends IStorageStack> implements IStorageCompon
|
||||
if (tuple == null)
|
||||
throw new IllegalStateException("No such tuple with id " + id);
|
||||
|
||||
final var item = tuple.local.object.partitionKey();
|
||||
tuple.local.object.shrink(tuple.object.getCount());
|
||||
final var item = tuple.local.stack.partitionKey();
|
||||
tuple.local.stack.shrink(tuple.object.getCount());
|
||||
tuple.local.tuples.remove(tuple);
|
||||
|
||||
indexed_by_remote_uuid.remove(id);
|
||||
|
||||
final boolean a = tuple.local.object.getCount().compareTo(BigDecimal.ZERO) <= 0;
|
||||
final boolean a = tuple.local.stack.getCount().compareTo(BigDecimal.ZERO) <= 0;
|
||||
final boolean b = tuple.local.tuples.size() == 0;
|
||||
|
||||
if (a || b) {
|
||||
|
Loading…
Reference in New Issue
Block a user