Storage unit text display, QOL withdraw by attacking, misc sync fixes

This commit is contained in:
Justin Vitale 2020-07-09 04:21:25 +10:00
parent 75e31a5d4f
commit 61345c0fa4
3 changed files with 118 additions and 10 deletions

View file

@ -56,11 +56,10 @@ import java.util.List;
public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implements InventoryProvider, IToolDrop, IListInfoProvider, BuiltScreenHandlerProvider {
// Inventory constants
private static final int INPUT_SLOT = 0;
private static final int OUTPUT_SLOT = 1;
public static final int INPUT_SLOT = 0;
public static final int OUTPUT_SLOT = 1;
// Client sync variables for GUI, what and how much stored
public Item storedItem = Items.AIR;
public int storedAmount = 0;
protected RebornInventory<StorageUnitBaseBlockEntity> inventory;
@ -116,10 +115,7 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement
}
if(inventory.hasChanged()){
if(storedItem != getStoredStack().getItem()) {
storedItem = getStoredStack().getItem();
syncWithAll();
}
syncWithAll();
inventory.resetChanged();
}
}
@ -167,7 +163,7 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement
// Returns the ItemStack to be displayed to the player via UI / model
public ItemStack getDisplayedStack() {
if (!isLocked()) {
return inventory.getStack(OUTPUT_SLOT);
return getStoredStack();
} else {
// Render the locked stack even if the unit is empty
return lockedItemStack;
@ -218,6 +214,7 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement
}
}
inventory.setChanged();
return inputStack;
}
@ -285,6 +282,11 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement
storeItemStack.setCount(Math.min(tagCompound.getInt("storedQuantity"), this.maxCapacity));
}
// Renderer only
if (tagCompound.contains("totalStoredAmount")) {
storedAmount = tagCompound.getInt("totalStoredAmount");
}
if (tagCompound.contains("lockedItem")) {
lockedItemStack = ItemStack.fromTag(tagCompound.getCompound("lockedItem"));
}
@ -309,6 +311,9 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement
tagCompound.putInt("storedQuantity", 0);
}
// Renderer only
tagCompound.putInt("totalStoredAmount", getCurrentCapacity());
if (isLocked()) {
tagCompound.put("lockedItem", lockedItemStack.toTag(new CompoundTag()));
}
@ -444,6 +449,16 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement
this.storedAmount = storedAmount;
}
public void setStoredStack(String itemStringID) {
storeItemStack = new ItemStack(Registry.ITEM.get(new Identifier(itemStringID)));
}
public String convertItemStringID() {
return Registry.ITEM.getId(getStoredStack().getItem()).toString();
}
@Override
public BuiltScreenHandler createScreenHandler(int syncID, final PlayerEntity playerEntity) {
return new ScreenHandlerBuilder("chest").player(playerEntity.inventory).inventory().hotbar().addInventory()
@ -451,6 +466,7 @@ public class StorageUnitBaseBlockEntity extends MachineBaseBlockEntity implement
.slot(INPUT_SLOT, 100, 53)
.outputSlot(OUTPUT_SLOT, 140, 53)
.sync(this::isLockedInt, this::setLockedInt)
.sync(this::convertItemStringID, this::setStoredStack)
.sync(this::getStoredAmount, this::setStoredAmount)
.addInventory().create(this, syncID);

View file

@ -27,7 +27,9 @@ package techreborn.blocks.storage.item;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ToolItem;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
@ -35,7 +37,10 @@ import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import reborncore.api.blockentity.IMachineGuiHandler;
import reborncore.api.items.InventoryBase;
import reborncore.common.blocks.BlockMachineBase;
import reborncore.common.util.RebornInventory;
import reborncore.common.util.WorldUtils;
import techreborn.blockentity.storage.item.StorageUnitBaseBlockEntity;
import techreborn.client.GuiType;
import techreborn.init.TRContent;
@ -62,13 +67,14 @@ public class StorageUnitBlock extends BlockMachineBase {
final StorageUnitBaseBlockEntity storageEntity = (StorageUnitBaseBlockEntity) worldIn.getBlockEntity(pos);
ItemStack stackInHand = playerIn.getStackInHand(Hand.MAIN_HAND);
Item itemInHand = stackInHand.getItem();
if (storageEntity != null && storageEntity.isSameType(stackInHand)) {
if (storageEntity != null && (storageEntity.isSameType(stackInHand) || (!storageEntity.isLocked() && storageEntity.isEmpty() && !(itemInHand instanceof ToolItem)))) {
// Add item which is the same type (in users inventory) into storage
for (int i = 0; i < playerIn.inventory.size() && !storageEntity.isFull(); i++) {
ItemStack curStack = playerIn.inventory.getStack(i);
if (storageEntity.isSameType(curStack)) {
if (curStack.getItem() == itemInHand) {
playerIn.inventory.setStack(i, storageEntity.processInput(curStack));
}
}
@ -78,6 +84,35 @@ public class StorageUnitBlock extends BlockMachineBase {
return super.onUse(state, worldIn, pos, playerIn, hand, hitResult);
}
@Override
public void onBlockBreakStart(BlockState state, World world, BlockPos pos, PlayerEntity player) {
super.onBlockBreakStart(state, world, pos, player);
if(world.isClient) return;
final StorageUnitBaseBlockEntity storageEntity = (StorageUnitBaseBlockEntity) world.getBlockEntity(pos);
ItemStack stackInHand = player.getStackInHand(Hand.MAIN_HAND);
if (storageEntity != null && (stackInHand.isEmpty() || storageEntity.isSameType(stackInHand)) && !storageEntity.isEmpty()) {
RebornInventory<StorageUnitBaseBlockEntity> inventory = storageEntity.getInventory();
ItemStack out = inventory.getStack(StorageUnitBaseBlockEntity.OUTPUT_SLOT);
// Drop stack if sneaking
if(player.isSneaking()){
WorldUtils.dropItem(new ItemStack(out.getItem()), world, player.getBlockPos());
out.decrement(1);
}else {
WorldUtils.dropItem(out, world, player.getBlockPos());
out.setCount(0);
}
inventory.setChanged();
}
}
@Override
public IMachineGuiHandler getGui() {
return GuiType.STORAGE_UNIT;

View file

@ -24,19 +24,34 @@
package techreborn.client.render.entitys;
import net.minecraft.block.BlockState;
import net.minecraft.block.SignBlock;
import net.minecraft.block.WallSignBlock;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.render.OverlayTexture;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.WorldRenderer;
import net.minecraft.client.render.block.entity.BlockEntityRenderDispatcher;
import net.minecraft.client.render.block.entity.BlockEntityRenderer;
import net.minecraft.client.render.block.entity.SignBlockEntityRenderer;
import net.minecraft.client.render.model.json.ModelTransformation;
import net.minecraft.client.texture.NativeImage;
import net.minecraft.client.util.SpriteIdentifier;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.client.util.math.Vector3f;
import net.minecraft.item.ItemStack;
import net.minecraft.text.StringRenderable;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.math.Direction;
import techreborn.blockentity.storage.item.StorageUnitBaseBlockEntity;
import java.util.List;
import static net.minecraft.client.render.block.entity.SignBlockEntityRenderer.getModelTexture;
/**
* Created by drcrazy on 07-Jan-20 for TechReborn-1.15.
*/
@ -54,6 +69,8 @@ public class StorageUnitRenderer extends BlockEntityRenderer<StorageUnitBaseBloc
if (stack.isEmpty()) {
return;
}
// Item rendering
matrices.push();
Direction direction = storage.getFacing();
matrices.multiply(Vector3f.POSITIVE_Y.getDegreesQuaternion((direction.getHorizontal() - 2) * 90F));
@ -73,5 +90,45 @@ public class StorageUnitRenderer extends BlockEntityRenderer<StorageUnitBaseBloc
int lightAbove = WorldRenderer.getLightmapCoordinates(storage.getWorld(), storage.getPos().offset(storage.getFacing()));
MinecraftClient.getInstance().getItemRenderer().renderItem(stack, ModelTransformation.Mode.FIXED, lightAbove, OverlayTexture.DEFAULT_UV, matrices, vertexConsumers);
matrices.pop();
// Text rendering
matrices.push();
TextRenderer textRenderer = this.dispatcher.getTextRenderer();
switch (storage.getFacing()){
case NORTH:
matrices.multiply(Vector3f.POSITIVE_Y.getDegreesQuaternion(storage.getFacing().getOpposite().asRotation()));
matrices.translate(0.5f,0.5,-0.001f);
break;
case SOUTH:
matrices.multiply(Vector3f.POSITIVE_Y.getDegreesQuaternion(storage.getFacing().getOpposite().asRotation()));
matrices.translate(-0.5f,0.5,-1.001f);
break;
case EAST:
matrices.multiply(Vector3f.POSITIVE_Y.getDegreesQuaternion(storage.getFacing().asRotation()));
matrices.translate(0.5f,0.5,-1.001f);
break;
case WEST:
matrices.multiply(Vector3f.POSITIVE_Y.getDegreesQuaternion(storage.getFacing().asRotation()));
matrices.translate(-0.5f,0.5,-0.001f);
break;
}
matrices.scale(-0.01f, -0.01F, -0.01f);
float xPosition;
// Render item count
String count = String.valueOf(storage.storedAmount);
xPosition = (float)(-textRenderer.getWidth(count) / 2);
textRenderer.draw(count, xPosition, -4f + 40, 0, false, matrices.peek().getModel(), vertexConsumers, false, 0, light);
// Render name
String item = stack.getName().asTruncatedString(18);
xPosition = (float)(-textRenderer.getWidth(item) / 2);
textRenderer.draw(item, xPosition, -4f - 40, 0, false, matrices.peek().getModel(), vertexConsumers, false, 0, light);
matrices.pop();
}
}