Creative storage unit will void excessive input items. Some work on #2205
This commit is contained in:
parent
efad3e6411
commit
92bdb4112f
1 changed files with 137 additions and 147 deletions
|
@ -88,34 +88,30 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TileMachineBase
|
public boolean isLocked() {
|
||||||
@Override
|
return lockedItemStack != ItemStack.EMPTY;
|
||||||
public void tick() {
|
}
|
||||||
super.tick();
|
|
||||||
if (world == null || world.isClient) {
|
public void setLocked(boolean value) {
|
||||||
|
if (isLocked() == value) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If there is an item in the input AND stored is less than max capacity
|
// Only set lockedItem in response to user input
|
||||||
if (!inventory.getStack(INPUT_SLOT).isEmpty() && !isFull()) {
|
ItemStack stack = getStoredStack().copy();
|
||||||
inventory.setStack(INPUT_SLOT, processInput(inventory.getStack(INPUT_SLOT)));
|
stack.setCount(1);
|
||||||
}
|
lockedItemStack = value ? stack : ItemStack.EMPTY;
|
||||||
|
|
||||||
// Fill output slot with goodies when stored has items and output count is less than max stack size
|
|
||||||
if (storeItemStack.getCount() > 0 && inventory.getStack(OUTPUT_SLOT).getCount() < getStoredStack().getMaxCount()) {
|
|
||||||
populateOutput();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type == TRContent.StorageUnit.CREATIVE) {
|
|
||||||
if (!isFull() && !isEmpty()) {
|
|
||||||
fillToCapacity();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inventory.hasChanged()) {
|
|
||||||
syncWithAll();
|
syncWithAll();
|
||||||
inventory.resetChanged();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean canModifyLocking() {
|
||||||
|
// Can always be unlocked
|
||||||
|
if (isLocked()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Can only lock if there is an item to lock
|
||||||
|
return !isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void populateOutput() {
|
private void populateOutput() {
|
||||||
|
@ -154,10 +150,6 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement
|
||||||
return storeItemStack.isEmpty() ? inventory.getStack(OUTPUT_SLOT) : storeItemStack;
|
return storeItemStack.isEmpty() ? inventory.getStack(OUTPUT_SLOT) : storeItemStack;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setStoredStack(ItemStack itemStack) {
|
|
||||||
storeItemStack = itemStack;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns the ItemStack to be displayed to the player via UI / model
|
// Returns the ItemStack to be displayed to the player via UI / model
|
||||||
public ItemStack getDisplayedStack() {
|
public ItemStack getDisplayedStack() {
|
||||||
if (!isLocked()) {
|
if (!isLocked()) {
|
||||||
|
@ -239,15 +231,6 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement
|
||||||
return getCurrentCapacity() == maxCapacity;
|
return getCurrentCapacity() == maxCapacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isEmpty() {
|
|
||||||
return getCurrentCapacity() == 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean canInsert(int index, ItemStack stack, @Nullable Direction direction) {
|
|
||||||
return super.canInsert(index, stack, direction) && (this.isEmpty() && !isLocked() || isSameType(stack));
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getCurrentCapacity() {
|
public int getCurrentCapacity() {
|
||||||
return storeItemStack.getCount() + inventory.getStack(OUTPUT_SLOT).getCount();
|
return storeItemStack.getCount() + inventory.getStack(OUTPUT_SLOT).getCount();
|
||||||
}
|
}
|
||||||
|
@ -256,12 +239,46 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement
|
||||||
return maxCapacity;
|
return maxCapacity;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Other stuff
|
// MachineBaseBlockEntity
|
||||||
|
@Override
|
||||||
|
public void tick() {
|
||||||
|
super.tick();
|
||||||
|
if (world == null || world.isClient) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// If there is an item in the input AND stored is less than max capacity
|
||||||
|
if (!inventory.getStack(INPUT_SLOT).isEmpty() && !isFull()) {
|
||||||
|
inventory.setStack(INPUT_SLOT, processInput(inventory.getStack(INPUT_SLOT)));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fill output slot with goodies when stored has items and output count is less than max stack size
|
||||||
|
if (storeItemStack.getCount() > 0 && inventory.getStack(OUTPUT_SLOT).getCount() < getStoredStack().getMaxCount()) {
|
||||||
|
populateOutput();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type == TRContent.StorageUnit.CREATIVE) {
|
||||||
|
if (!isFull() && !isEmpty()) {
|
||||||
|
fillToCapacity();
|
||||||
|
}
|
||||||
|
// void input items for creative storage (#2205)
|
||||||
|
inventory.setStack(INPUT_SLOT, ItemStack.EMPTY);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (inventory.hasChanged()) {
|
||||||
|
syncWithAll();
|
||||||
|
inventory.resetChanged();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean canBeUpgraded() {
|
public boolean isEmpty() {
|
||||||
return false;
|
return getCurrentCapacity() == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean canInsert(int index, ItemStack stack, @Nullable Direction direction) {
|
||||||
|
return super.canInsert(index, stack, direction) && (this.isEmpty() && !isLocked() || isSameType(stack));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -325,57 +342,6 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement
|
||||||
return tagCompound;
|
return tagCompound;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ItemHandlerProvider
|
|
||||||
@Override
|
|
||||||
public RebornInventory<StorageUnitBaseBlockEntity> getInventory() {
|
|
||||||
return inventory;
|
|
||||||
}
|
|
||||||
|
|
||||||
// IToolDrop
|
|
||||||
@Override
|
|
||||||
public ItemStack getToolDrop(PlayerEntity entityPlayer) {
|
|
||||||
return getDropWithNBT();
|
|
||||||
}
|
|
||||||
|
|
||||||
public ItemStack getDropWithNBT() {
|
|
||||||
ItemStack dropStack = new ItemStack(getBlockType(), 1);
|
|
||||||
final CompoundTag blockEntity = new CompoundTag();
|
|
||||||
|
|
||||||
this.toTag(blockEntity);
|
|
||||||
dropStack.setTag(new CompoundTag());
|
|
||||||
dropStack.getTag().put("blockEntity", blockEntity);
|
|
||||||
|
|
||||||
return dropStack;
|
|
||||||
}
|
|
||||||
|
|
||||||
// IListInfoProvider
|
|
||||||
@Override
|
|
||||||
public void addInfo(final List<Text> info, final boolean isReal, boolean hasData) {
|
|
||||||
if (isReal || hasData) {
|
|
||||||
if (!this.isEmpty()) {
|
|
||||||
info.add(
|
|
||||||
new LiteralText(String.valueOf(this.getCurrentCapacity()))
|
|
||||||
.append(new TranslatableText("techreborn.tooltip.unit.divider"))
|
|
||||||
.append(this.getStoredStack().getName())
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
info.add(new TranslatableText("techreborn.tooltip.unit.empty"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
info.add(
|
|
||||||
new TranslatableText("techreborn.tooltip.unit.capacity")
|
|
||||||
.formatted(Formatting.GRAY)
|
|
||||||
.append(
|
|
||||||
new LiteralText(String.valueOf(this.getMaxCapacity()))
|
|
||||||
.formatted(Formatting.GOLD)
|
|
||||||
.append(" items (")
|
|
||||||
.append(String.valueOf(this.getMaxCapacity() / 64))
|
|
||||||
.append(")")
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onBreak(World world, PlayerEntity playerEntity, BlockPos blockPos, BlockState blockState) {
|
public void onBreak(World world, PlayerEntity playerEntity, BlockPos blockPos, BlockState blockState) {
|
||||||
super.onBreak(world, playerEntity, blockPos, blockState);
|
super.onBreak(world, playerEntity, blockPos, blockState);
|
||||||
|
@ -411,60 +377,72 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement
|
||||||
// Inventory gets dropped automatically
|
// Inventory gets dropped automatically
|
||||||
}
|
}
|
||||||
|
|
||||||
// The int methods are only for ContainerBuilder.sync()
|
@Override
|
||||||
private int isLockedInt() {
|
public boolean isValid(int slot, ItemStack stack) {
|
||||||
return isLocked() ? 1 : 0;
|
if (slot == INPUT_SLOT && isLocked()) {
|
||||||
}
|
return ItemUtils.isItemEqual(lockedItemStack, stack, true, true);
|
||||||
|
|
||||||
private void setLockedInt(int lockedInt) {
|
|
||||||
setLocked(lockedInt == 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean isLocked() {
|
|
||||||
return lockedItemStack != ItemStack.EMPTY;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setLocked(boolean value) {
|
|
||||||
if (isLocked() == value) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Only set lockedItem in response to user input
|
|
||||||
ItemStack stack = getStoredStack().copy();
|
|
||||||
stack.setCount(1);
|
|
||||||
lockedItemStack = value ? stack : ItemStack.EMPTY;
|
|
||||||
syncWithAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean canModifyLocking() {
|
|
||||||
// Can always be unlocked
|
|
||||||
if (isLocked()) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Can only lock if there is an item to lock
|
|
||||||
return !isEmpty();
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getStoredAmount() {
|
|
||||||
return this.getCurrentCapacity();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setStoredAmount(int storedAmount) {
|
|
||||||
this.storedAmount = storedAmount;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setStoredStackFromNBT(CompoundTag tag) {
|
|
||||||
storeItemStack = ItemStack.fromTag(tag);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public CompoundTag getStoredStackNBT() {
|
if (slot == INPUT_SLOT && !(isEmpty() || isSameType(stack))) {
|
||||||
CompoundTag tag = new CompoundTag();
|
return false;
|
||||||
getStoredStack().toTag(tag);
|
}
|
||||||
return tag;
|
return super.isValid(slot, stack);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean canBeUpgraded() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// InventoryProvider
|
||||||
|
@Override
|
||||||
|
public RebornInventory<StorageUnitBaseBlockEntity> getInventory() {
|
||||||
|
return inventory;
|
||||||
|
}
|
||||||
|
|
||||||
|
// IToolDrop
|
||||||
|
@Override
|
||||||
|
public ItemStack getToolDrop(PlayerEntity entityPlayer) {
|
||||||
|
ItemStack dropStack = new ItemStack(getBlockType(), 1);
|
||||||
|
final CompoundTag blockEntity = new CompoundTag();
|
||||||
|
|
||||||
|
this.toTag(blockEntity);
|
||||||
|
dropStack.setTag(new CompoundTag());
|
||||||
|
dropStack.getOrCreateTag().put("blockEntity", blockEntity);
|
||||||
|
|
||||||
|
return dropStack;
|
||||||
|
}
|
||||||
|
|
||||||
|
// IListInfoProvider
|
||||||
|
@Override
|
||||||
|
public void addInfo(final List<Text> info, final boolean isReal, boolean hasData) {
|
||||||
|
if (isReal || hasData) {
|
||||||
|
if (!this.isEmpty()) {
|
||||||
|
info.add(
|
||||||
|
new LiteralText(String.valueOf(this.getCurrentCapacity()))
|
||||||
|
.append(new TranslatableText("techreborn.tooltip.unit.divider"))
|
||||||
|
.append(this.getStoredStack().getName())
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
info.add(new TranslatableText("techreborn.tooltip.unit.empty"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
info.add(
|
||||||
|
new TranslatableText("techreborn.tooltip.unit.capacity")
|
||||||
|
.formatted(Formatting.GRAY)
|
||||||
|
.append(
|
||||||
|
new LiteralText(String.valueOf(this.getMaxCapacity()))
|
||||||
|
.formatted(Formatting.GOLD)
|
||||||
|
.append(" items (")
|
||||||
|
.append(String.valueOf(this.getMaxCapacity() / 64))
|
||||||
|
.append(")")
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// BuiltScreenHandlerProvider
|
||||||
@Override
|
@Override
|
||||||
public BuiltScreenHandler createScreenHandler(int syncID, final PlayerEntity playerEntity) {
|
public BuiltScreenHandler createScreenHandler(int syncID, final PlayerEntity playerEntity) {
|
||||||
return new ScreenHandlerBuilder("chest").player(playerEntity.inventory).inventory().hotbar().addInventory()
|
return new ScreenHandlerBuilder("chest").player(playerEntity.inventory).inventory().hotbar().addInventory()
|
||||||
|
@ -479,18 +457,30 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement
|
||||||
// Note that inventory is synced, and it gets the stack from that
|
// Note that inventory is synced, and it gets the stack from that
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
// The int methods are only for ContainerBuilder.sync()
|
||||||
public boolean isValid(int slot, ItemStack stack) {
|
private int isLockedInt() {
|
||||||
if (slot == INPUT_SLOT && isLocked()) {
|
return isLocked() ? 1 : 0;
|
||||||
return ItemUtils.isItemEqual(lockedItemStack, stack, true, true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void setLockedInt(int lockedInt) {
|
||||||
if (slot == INPUT_SLOT && !(isEmpty() || isSameType(stack))) {
|
setLocked(lockedInt == 1);
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return super.isValid(slot, stack);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getStoredAmount() {
|
||||||
|
return this.getCurrentCapacity();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStoredAmount(int storedAmount) {
|
||||||
|
this.storedAmount = storedAmount;
|
||||||
|
}
|
||||||
|
|
||||||
|
public CompoundTag getStoredStackNBT() {
|
||||||
|
CompoundTag tag = new CompoundTag();
|
||||||
|
getStoredStack().toTag(tag);
|
||||||
|
return tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setStoredStackFromNBT(CompoundTag tag) {
|
||||||
|
storeItemStack = ItemStack.fromTag(tag);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue