fix lock and optimize client-side sync for stored item
This commit is contained in:
parent
22fdd89e4c
commit
75e31a5d4f
2 changed files with 23 additions and 19 deletions
|
@ -28,12 +28,15 @@ import net.minecraft.block.BlockState;
|
||||||
import net.minecraft.entity.player.PlayerEntity;
|
import net.minecraft.entity.player.PlayerEntity;
|
||||||
import net.minecraft.item.Item;
|
import net.minecraft.item.Item;
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
|
import net.minecraft.item.Items;
|
||||||
import net.minecraft.nbt.CompoundTag;
|
import net.minecraft.nbt.CompoundTag;
|
||||||
import net.minecraft.text.LiteralText;
|
import net.minecraft.text.LiteralText;
|
||||||
import net.minecraft.text.Text;
|
import net.minecraft.text.Text;
|
||||||
import net.minecraft.text.TranslatableText;
|
import net.minecraft.text.TranslatableText;
|
||||||
import net.minecraft.util.Formatting;
|
import net.minecraft.util.Formatting;
|
||||||
|
import net.minecraft.util.Identifier;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
|
import net.minecraft.util.registry.Registry;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import reborncore.api.IListInfoProvider;
|
import reborncore.api.IListInfoProvider;
|
||||||
import reborncore.api.IToolDrop;
|
import reborncore.api.IToolDrop;
|
||||||
|
@ -50,19 +53,18 @@ import techreborn.init.TRContent;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity
|
public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implements InventoryProvider, IToolDrop, IListInfoProvider, BuiltScreenHandlerProvider {
|
||||||
implements InventoryProvider, IToolDrop, IListInfoProvider, BuiltScreenHandlerProvider {
|
|
||||||
|
|
||||||
|
|
||||||
// Inventory constants
|
// Inventory constants
|
||||||
private static final int INPUT_SLOT = 0;
|
private static final int INPUT_SLOT = 0;
|
||||||
private static final int OUTPUT_SLOT = 1;
|
private static final int OUTPUT_SLOT = 1;
|
||||||
// Client sync variable, how much stored
|
|
||||||
|
// Client sync variables for GUI, what and how much stored
|
||||||
|
public Item storedItem = Items.AIR;
|
||||||
public int storedAmount = 0;
|
public int storedAmount = 0;
|
||||||
|
|
||||||
protected RebornInventory<StorageUnitBaseBlockEntity> inventory;
|
protected RebornInventory<StorageUnitBaseBlockEntity> inventory;
|
||||||
private int maxCapacity;
|
private int maxCapacity;
|
||||||
private boolean shouldUpdate = true;
|
|
||||||
private Item prevItem = null;
|
|
||||||
|
|
||||||
private ItemStack storeItemStack;
|
private ItemStack storeItemStack;
|
||||||
|
|
||||||
|
@ -113,13 +115,12 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update for stored item for clients if out of date
|
if(inventory.hasChanged()){
|
||||||
if(world.getTime() % 20 == 0) {
|
if(storedItem != getStoredStack().getItem()) {
|
||||||
Item storedItem = this.getStoredStack().getItem();
|
storedItem = getStoredStack().getItem();
|
||||||
if (!isLocked() && prevItem != storedItem) {
|
|
||||||
prevItem = storedItem;
|
|
||||||
syncWithAll();
|
syncWithAll();
|
||||||
}
|
}
|
||||||
|
inventory.resetChanged();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -166,7 +167,7 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity
|
||||||
// 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()) {
|
||||||
return getStoredStack();
|
return inventory.getStack(OUTPUT_SLOT);
|
||||||
} else {
|
} else {
|
||||||
// Render the locked stack even if the unit is empty
|
// Render the locked stack even if the unit is empty
|
||||||
return lockedItemStack;
|
return lockedItemStack;
|
||||||
|
@ -188,7 +189,7 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity
|
||||||
|
|
||||||
boolean isSameStack = isSameType(inputStack);
|
boolean isSameStack = isSameType(inputStack);
|
||||||
|
|
||||||
if (storeItemStack == ItemStack.EMPTY && (isSameStack || getCurrentCapacity() == 0)) {
|
if (storeItemStack == ItemStack.EMPTY && (isSameStack || (getCurrentCapacity() == 0 && !isLocked()))) {
|
||||||
// Check if storage is empty, NOT including the output slot
|
// Check if storage is empty, NOT including the output slot
|
||||||
storeItemStack = inputStack.copy();
|
storeItemStack = inputStack.copy();
|
||||||
|
|
||||||
|
@ -443,12 +444,12 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity
|
||||||
this.storedAmount = storedAmount;
|
this.storedAmount = storedAmount;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@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()
|
||||||
.blockEntity(this).slot(0, 100, 53)
|
.blockEntity(this)
|
||||||
.outputSlot(1, 140, 53)
|
.slot(INPUT_SLOT, 100, 53)
|
||||||
|
.outputSlot(OUTPUT_SLOT, 140, 53)
|
||||||
.sync(this::isLockedInt, this::setLockedInt)
|
.sync(this::isLockedInt, this::setLockedInt)
|
||||||
.sync(this::getStoredAmount, this::setStoredAmount)
|
.sync(this::getStoredAmount, this::setStoredAmount)
|
||||||
.addInventory().create(this, syncID);
|
.addInventory().create(this, syncID);
|
||||||
|
|
|
@ -62,16 +62,19 @@ public class GuiStorageUnit extends GuiBase<BuiltScreenHandler> {
|
||||||
builder.drawText(matrixStack, this, new TranslatableText("gui.techreborn.unit.in"), 100, 43, 4210752);
|
builder.drawText(matrixStack, this, new TranslatableText("gui.techreborn.unit.in"), 100, 43, 4210752);
|
||||||
builder.drawText(matrixStack, this, new TranslatableText("gui.techreborn.unit.out"), 140, 43, 4210752);
|
builder.drawText(matrixStack, this, new TranslatableText("gui.techreborn.unit.out"), 140, 43, 4210752);
|
||||||
|
|
||||||
if (storageEntity.storedAmount == 0 && !storageEntity.isLocked()) {
|
|
||||||
|
int storedAmount = storageEntity.storedAmount;
|
||||||
|
|
||||||
|
if (storedAmount == 0 && !storageEntity.isLocked()) {
|
||||||
textRenderer.draw(matrixStack, new TranslatableText("techreborn.tooltip.unit.empty"), 10, 20, 4210752);
|
textRenderer.draw(matrixStack, new TranslatableText("techreborn.tooltip.unit.empty"), 10, 20, 4210752);
|
||||||
} else {
|
} else {
|
||||||
textRenderer.draw(matrixStack, new TranslatableText("gui.techreborn.storage.store"), 10, 20, 4210752);
|
textRenderer.draw(matrixStack, new TranslatableText("gui.techreborn.storage.store"), 10, 20, 4210752);
|
||||||
textRenderer.draw(matrixStack, storageEntity.getDisplayedStack().getName(), 10, 30, 4210752);
|
textRenderer.draw(matrixStack, storageEntity.getDisplayedStack().getName(), 10, 30, 4210752);
|
||||||
|
|
||||||
textRenderer.draw(matrixStack, new TranslatableText("gui.techreborn.storage.amount"), 10, 50, 4210752);
|
textRenderer.draw(matrixStack, new TranslatableText("gui.techreborn.storage.amount"), 10, 50, 4210752);
|
||||||
textRenderer.draw(matrixStack, String.valueOf(storageEntity.storedAmount), 10, 60, 4210752);
|
textRenderer.draw(matrixStack, String.valueOf(storedAmount), 10, 60, 4210752);
|
||||||
|
|
||||||
String percentFilled = String.valueOf((int) ((double) storageEntity.storedAmount / (double) storageEntity.getMaxCapacity() * 100));
|
String percentFilled = String.valueOf((int) ((double) storedAmount / (double) storageEntity.getMaxCapacity() * 100));
|
||||||
|
|
||||||
textRenderer.draw(matrixStack, new TranslatableText("gui.techreborn.unit.used").append(percentFilled + "%"), 10, 70, 4210752);
|
textRenderer.draw(matrixStack, new TranslatableText("gui.techreborn.unit.used").append(percentFilled + "%"), 10, 70, 4210752);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue