Prevented the extraction the storage upgrades if the size goes over limit

This commit is contained in:
Buuz135 2021-12-22 18:28:46 +01:00
parent ddff5a8f3a
commit 8f6a4b8f13
7 changed files with 73 additions and 6 deletions

View File

@ -92,6 +92,11 @@ public class CompactingDrawerTile extends ControllableDrawerTile<CompactingDrawe
return lazyStorage;
}
@Override
public int getBaseSize(int slot) {
return handler.getSlotLimitBase(slot);
}
@Nonnull
@Override
public <U> LazyOptional<U> getCapability(@Nonnull Capability<U> cap, @Nullable Direction side) {

View File

@ -47,10 +47,31 @@ public abstract class ControllableDrawerTile<T extends ControllableDrawerTile<T>
public ControllableDrawerTile(BasicTileBlock<T> base, BlockPos pos, BlockState state) {
super(base, pos, state);
this.addInventory((InventoryComponent<T>) (this.storageUpgrades = new InventoryComponent<ControllableDrawerTile<T>>("storage_upgrades", 10, 70, getStorageSlotAmount())
this.addInventory((InventoryComponent<T>) (this.storageUpgrades = new InventoryComponent<ControllableDrawerTile<T>>("storage_upgrades", 10, 70, getStorageSlotAmount()) {
@NotNull
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate) {
ItemStack stack = this.getStackInSlot(slot);
if (stack.getItem() instanceof StorageUpgradeItem){
int mult = 1;
for (int i = 0; i < storageUpgrades.getSlots(); i++) {
if (storageUpgrades.getStackInSlot(i).getItem() instanceof StorageUpgradeItem){
if (i == slot) continue;
if (mult == 1) mult = ((StorageUpgradeItem) storageUpgrades.getStackInSlot(i).getItem()).getStorageMultiplier();
else mult *= ((StorageUpgradeItem) storageUpgrades.getStackInSlot(i).getItem()).getStorageMultiplier();
}
}
for (int i = 0; i < getStorage().getSlots(); i++) {
if (getBaseSize(i) * mult < getStorage().getStackInSlot(i).getCount()){
return ItemStack.EMPTY;
}
}
}
return super.extractItem(slot, amount, simulate);
}
}
.setInputFilter((stack, integer) -> stack.getItem() instanceof UpgradeItem && ((UpgradeItem) stack.getItem()).getType() == UpgradeItem.Type.STORAGE)
.setSlotLimit(1)
)
.setSlotLimit(1))
);
this.addInventory((InventoryComponent<T>) (this.utilityUpgrades = new InventoryComponent<ControllableDrawerTile<T>>("utility_upgrades", 114, 70, 3)
.setInputFilter((stack, integer) -> stack.getItem() instanceof UpgradeItem && ((UpgradeItem) stack.getItem()).getType() == UpgradeItem.Type.UTILITY)
@ -137,11 +158,12 @@ public abstract class ControllableDrawerTile<T extends ControllableDrawerTile<T>
}
}
public abstract IItemHandler getStorage();
public abstract LazyOptional<IItemHandler> getOptional();
public abstract int getBaseSize(int lost);
@Override
public void invalidateCaps() {
super.invalidateCaps();

View File

@ -71,6 +71,11 @@ public class DrawerControllerTile extends ControllableDrawerTile<DrawerControlle
return lazyStorage;
}
@Override
public int getBaseSize(int lost) {
return 1;
}
@NotNull
@Override
public DrawerControllerTile getSelf() {

View File

@ -92,4 +92,9 @@ public class DrawerTile extends ControllableDrawerTile<DrawerTile> {
public LazyOptional<IItemHandler> getOptional() {
return lazyStorage;
}
@Override
public int getBaseSize(int lost) {
return type.getSlotAmount();
}
}

View File

@ -2,6 +2,7 @@ package com.buuz135.functionalstorage.client;
import com.buuz135.functionalstorage.FunctionalStorage;
import com.buuz135.functionalstorage.block.tile.DrawerTile;
import com.buuz135.functionalstorage.util.NumberUtils;
import com.mojang.blaze3d.platform.Lighting;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Matrix3f;
@ -143,7 +144,7 @@ public class DrawerRenderer implements BlockEntityRenderer<DrawerTile> {
matrixStack.mulPose(Vector3f.YP.rotationDegrees(-180));
matrixStack.translate(0,0, 0.23f*2);
}
renderText(matrixStack, bufferIn, combinedOverlayIn, new TextComponent(ChatFormatting.WHITE + "" + amount), Direction.NORTH, scale);
renderText(matrixStack, bufferIn, combinedOverlayIn, new TextComponent(ChatFormatting.WHITE + "" + NumberUtils.getFormatedBigNumber(amount)), Direction.NORTH, scale);
}

View File

@ -18,7 +18,7 @@ public abstract class CompactingInventoryHandler implements IItemHandler, INBTSe
public static String STACK = "Stack";
public static String AMOUNT = "Amount";
private final int TOTAL_AMOUNT = 512 * 9 * 9;
public static final int TOTAL_AMOUNT = 512 * 9 * 9;
private int amount;
private List<CompactingUtil.Result> resultList;
@ -116,6 +116,10 @@ public abstract class CompactingInventoryHandler implements IItemHandler, INBTSe
return (int) Math.min(Integer.MAX_VALUE, Math.floor((TOTAL_AMOUNT * getMultiplier()) / this.resultList.get(slot).getNeeded()));
}
public int getSlotLimitBase(int slot){
return (int) Math.min(Integer.MAX_VALUE, Math.floor(TOTAL_AMOUNT / this.resultList.get(slot).getNeeded()));
}
@Override
public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
if (slot < 3){

View File

@ -0,0 +1,25 @@
package com.buuz135.functionalstorage.util;
import java.text.DecimalFormat;
public class NumberUtils {
private static DecimalFormat formatterWithUnits = new DecimalFormat("####0.#");
public static String getFormatedBigNumber(int number) {
if (number >= 1000000000) { //BILLION
float numb = number / 1000000000F;
return formatterWithUnits.format(numb) + "B";
} else if (number >= 1000000) { //MILLION
float numb = number / 1000000F;
if (number > 100000000) numb = Math.round(numb);
return formatterWithUnits.format(numb) + "M";
} else if (number >= 1000) { //THOUSANDS
float numb = number / 1000F;
if (number > 100000) numb = Math.round(numb);
return formatterWithUnits.format(numb) + "K";
}
return String.valueOf(number);
}
}