Skip to content

Commit

Permalink
调整:使用ItemStackHandler来处理容器存储
Browse files Browse the repository at this point in the history
  • Loading branch information
MCZME committed Aug 17, 2024
1 parent d601007 commit 0bc2bbe
Show file tree
Hide file tree
Showing 14 changed files with 120 additions and 144 deletions.
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.21 2024-08-14T01:55:49.7601336 Data Maps
a8bc32918970acec484472a12e6492804a8783f1 data/lingshi/data_maps/item/cooking_food.json
// 1.21 2024-08-17T18:08:32.4751382 Data Maps
93d8561309e6cfabdc62f0e31ff69d3f67e79b50 data/lingshi/data_maps/item/cooking_food.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"values": {
"lingshi:rice": {
"complete_time": 25.0,
"cooked_time": 15.0
"complete_time": 25,
"cooked_time": 15
},
"minecraft:apple": {
"complete_time": 20.0,
"cooked_time": 5.0
"complete_time": 20,
"cooked_time": 5
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraft.core.Direction;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.items.ItemStackHandler;

import java.util.List;

public class SkilletBER implements BlockEntityRenderer<SkilletBlockEntity> {

Expand All @@ -22,18 +21,18 @@ public SkilletBER(BlockEntityRendererProvider.Context pContext){

@Override
public void render(SkilletBlockEntity pBlockEntity, float pPartialTick, PoseStack pPoseStack, MultiBufferSource pBufferSource, int pPackedLight, int pPackedOverlay) {
List<ItemStack> itemStack = pBlockEntity.getItemStacks();
ItemStackHandler itemStack = pBlockEntity.getItemStacks();
Direction direction = pBlockEntity.getBlockState().getValue(ChoppingBoardBlock.FACING);

if(!itemStack.isEmpty()){
for (int i = 0; i <itemStack.size(); i++) {
if(itemStack.get(i).isEmpty()) continue;
if(!pBlockEntity.isEmpty()){
for (int i = 0; i <pBlockEntity.getMAX(); i++) {
if(itemStack.getStackInSlot(i).isEmpty()) continue;
pPoseStack.pushPose();
pPoseStack.translate(0.5,0.2+i/10.0,0.5);
pPoseStack.mulPose(Axis.YP.rotationDegrees(-direction.toYRot()));
pPoseStack.mulPose(Axis.XP.rotationDegrees(90.0F));
pPoseStack.scale(0.5F, 0.5F, 0.5F);
Minecraft.getInstance().getItemRenderer().renderStatic(itemStack.get(i), ItemDisplayContext.FIXED, pPackedLight, pPackedOverlay, pPoseStack, pBufferSource, pBlockEntity.getLevel(), (int) pBlockEntity.getBlockPos().asLong());
Minecraft.getInstance().getItemRenderer().renderStatic(itemStack.getStackInSlot(i), ItemDisplayContext.FIXED, pPackedLight, pPackedOverlay, pPoseStack, pBufferSource, pBlockEntity.getLevel(), (int) pBlockEntity.getBlockPos().asLong());
pPoseStack.popPose();
}
}
Expand Down
41 changes: 12 additions & 29 deletions src/main/java/mczme/lingshi/client/menu/SkilletMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
import java.util.List;

import static mczme.lingshi.client.recipebook.ModRecipeBookType.SKILLET;
import static mczme.lingshi.common.utility.ListUtility.copy;

public class SkilletMenu extends RecipeBookMenu<SkilletRecipeInput, SkilletRecipe> {

SkilletBlockEntity blockEntity;
public final ItemStackHandler inventory;
public SkilletBlockEntity blockEntity;
protected final Level level;
private final ItemStackHandler itemStackHandler;

public SkilletMenu(int pContainerId, Inventory pPlayerInventory, FriendlyByteBuf pContext) {
this(pContainerId, pPlayerInventory, (SkilletBlockEntity) pPlayerInventory.player.level().getBlockEntity(pContext.readBlockPos()));
Expand All @@ -36,26 +35,22 @@ public SkilletMenu(int pContainerId, Inventory pPlayerInventory,
super(ModMenuTypes.SKILLET_MENU.get(), pContainerId);
checkContainerSize(pPlayerInventory, 1);
this.blockEntity = blockEntity;
this.inventory = blockEntity.getInventory();
this.level = pPlayerInventory.player.level();
this.addWorkSlot(copy(blockEntity.getItemStacks()));
this.itemStackHandler = blockEntity.getItemStacks();
this.addWorkSlot(itemStackHandler);
layoutPlayerInventorySlots(pPlayerInventory);
}

private void addWorkSlot(List<ItemStack> itemStacks) {
if (itemStacks.size() < blockEntity.getMAX()) {
for (int i = itemStacks.size(); i < blockEntity.getMAX(); i++)
itemStacks.add(ItemStack.EMPTY);
}
private void addWorkSlot(ItemStackHandler itemStacks) {
int[] X = {42, 60, 33, 51, 69, 93, 127};
int[] Y = {29, 29, 47, 47, 47, 29, 47};
for (int i = 0; i < blockEntity.getMAX(); i++) {
SlotItemHandler slot = new SlotItemHandler(inventory, i, X[i], Y[i]);
slot.set(itemStacks.get(i));
SlotItemHandler slot = new SlotItemHandler(itemStackHandler, i, X[i], Y[i]);
slot.set(itemStacks.getStackInSlot(i));
this.addSlot(slot);
}
this.addSlot(new SlotItemHandler(inventory, 5, X[5], Y[5]));
this.addSlot(new SlotItemHandler(inventory, 6, X[6], Y[6]));
this.addSlot(new SlotItemHandler(itemStackHandler, 5, X[5], Y[5]));
this.addSlot(new SlotItemHandler(itemStackHandler, 6, X[6], Y[6]));
}

private void layoutPlayerInventorySlots(Inventory playerInventory) {
Expand All @@ -71,18 +66,6 @@ private void layoutPlayerInventorySlots(Inventory playerInventory) {
}
}

@Override
public void removed(Player pPlayer) {
blockEntity.getItemStacks().clear();
for (int i = 0; i < blockEntity.getMAX(); i++) {
if (!this.getSlot(i).getItem().isEmpty()) {
blockEntity.setItem(this.getSlot(i).getItem());
}
}
blockEntity.setChanged();
super.removed(pPlayer);
}

@Override
public ItemStack quickMoveStack(Player pPlayer, int pIndex) {
ItemStack quickMovedStack = ItemStack.EMPTY;
Expand Down Expand Up @@ -124,8 +107,8 @@ public boolean stillValid(Player pPlayer) {

@Override
public void fillCraftSlotsStackedContents(StackedContents pItemHelper) {
for (ItemStack itemstack : blockEntity.getItemStacks()) {
pItemHelper.accountSimpleStack(itemstack);
for (int i = 0; i < blockEntity.getMAX(); i++) {
pItemHelper.accountSimpleStack(itemStackHandler.getStackInSlot(i));
}
}

Expand All @@ -141,7 +124,7 @@ public void clearCraftingContent() {

@Override
public boolean recipeMatches(RecipeHolder<SkilletRecipe> pRecipe) {
return pRecipe.value().matches(new SkilletRecipeInput(getInputSlotItem(),blockEntity.getFluid(),null), this.level);
return pRecipe.value().matches(new SkilletRecipeInput(getInputSlotItem(),blockEntity.getFluid()), this.level);
}

@Override
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/mczme/lingshi/client/screen/SkilletScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import mczme.lingshi.client.menu.SkilletMenu;
import mczme.lingshi.client.recipebook.SkilletRecipeBookComponent;
import mczme.lingshi.common.block.entity.SkilletBlockEntity;
import mczme.lingshi.lingshi;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.ImageButton;
Expand Down Expand Up @@ -63,7 +64,11 @@ public void render(GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, float pPa

@Override
protected void renderBg(GuiGraphics pGuiGraphics, float pPartialTick, int pMouseX, int pMouseY) {
SkilletBlockEntity blockEntity = this.menu.blockEntity;
pGuiGraphics.blit(BACKGROUND_TEXTURE, this.leftPos, this.topPos, 0, 0, this.imageWidth, this.imageHeight);
if(blockEntity.isHeated(blockEntity.getLevel(),blockEntity.getBlockPos())){
pGuiGraphics.blit(BACKGROUND_TEXTURE, this.leftPos+93, this.topPos+64, 0, 166, 14, 14);
}
}

@Override
Expand Down
14 changes: 8 additions & 6 deletions src/main/java/mczme/lingshi/common/block/SkilletBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties;
Expand Down Expand Up @@ -69,7 +71,7 @@ protected InteractionResult useWithoutItem(BlockState pState, Level pLevel, Bloc
if(!pLevel.isClientSide()){
pPlayer.openMenu(blockEntity,pPos);
}
}else if(!blockEntity.getItemStacks().isEmpty()) {
}else if(!blockEntity.isEmpty()) {
Containers.dropItemStack(pLevel, pPos.getX(), pPos.getY()+0.2, pPos.getZ(), blockEntity.dropItem());
blockEntity.setChanged();
}
Expand All @@ -83,11 +85,11 @@ public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) {
return new SkilletBlockEntity(pPos, pState);
}

// @Nullable
// @Override
// public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type) {
// return type == BlockEntityTypes.SKILLET_BLOCKENTITY ? SkilletBlockEntity::tick : null;
// }
@Nullable
@Override
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level pLevel, BlockState pState, BlockEntityType<T> pType) {
return !pLevel.isClientSide ? SkilletBlockEntity::serverTick : null;
}

@Override
protected VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package mczme.lingshi.common.block.entity;

import mczme.lingshi.client.menu.SkilletMenu;
import mczme.lingshi.common.block.entity.baseblockentity.ICanBeHeated;
import mczme.lingshi.common.registry.BlockEntityTypes;
import mczme.lingshi.lingshi;
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
Expand All @@ -23,44 +23,66 @@
import net.neoforged.neoforge.items.ItemStackHandler;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
public class SkilletBlockEntity extends BlockEntity implements MenuProvider, ICanBeHeated {

public class SkilletBlockEntity extends BlockEntity implements MenuProvider {
private final int MAX_SLOT = 5;

private List<ItemStack> itemStacks = new ArrayList<>();
private FluidStack fluidStacks = FluidStack.EMPTY;
private ItemStackHandler itemStackHandler = new ItemStackHandler(MAX_SLOT+2);

private final int MAX_SLOT = 5;

private final ItemStackHandler inventory = new ItemStackHandler(MAX_SLOT + 2);

public SkilletBlockEntity(BlockPos pPos, BlockState pBlockState) {
super(BlockEntityTypes.SKILLET_BLOCKENTITY.get(), pPos, pBlockState);
}

public boolean isFull() {
return itemStacks.size() >= MAX_SLOT;
for (int i = 0; i < MAX_SLOT; i++) {
if(itemStackHandler.getStackInSlot(i).isEmpty()){
return false;
}
}
return true;
}

public ItemStack getItem(int slot) {
return itemStacks.get(slot);
public boolean isEmpty(){
for (int i = 0; i < MAX_SLOT; i++) {
if(!itemStackHandler.getStackInSlot(i).isEmpty()){
return false;
}
}
return fluidStacks.isEmpty();
}

public ItemStack dropItem() {
return itemStacks.removeLast();
for (int i = MAX_SLOT-1; i >=0; i--) {
if(!itemStackHandler.getStackInSlot(i).isEmpty()){
ItemStack stack = itemStackHandler.getStackInSlot(i).copy();
itemStackHandler.setStackInSlot(i,ItemStack.EMPTY);
return stack;
}
}
return ItemStack.EMPTY;
}

public FluidStack getFluid() {
return fluidStacks;
}

public List<ItemStack> getItemStacks() {
return itemStacks;
public ItemStackHandler getItemStacks() {
return itemStackHandler;
}

public void setItem(ItemStack item) {
itemStacks.add(item);
for (int i = 0; i < MAX_SLOT; i++) {
if(itemStackHandler.getStackInSlot(i).isEmpty()){
itemStackHandler.setStackInSlot(i,item);
break;
}
}
}

public void setItem(ItemStack item,int slot) {
itemStackHandler.setStackInSlot(slot,item);
}

public void setFluid(FluidStack fluid) {
Expand All @@ -71,10 +93,6 @@ public int getMAX() {
return MAX_SLOT;
}

public ItemStackHandler getInventory() {
return inventory;
}

@Override
public Packet<ClientGamePacketListener> getUpdatePacket() {
return ClientboundBlockEntityDataPacket.create(this);
Expand All @@ -83,18 +101,7 @@ public Packet<ClientGamePacketListener> getUpdatePacket() {
@Override
public CompoundTag getUpdateTag(HolderLookup.Provider pRegistries) {
CompoundTag tag = new CompoundTag();
ListTag listtag = new ListTag();
if (!itemStacks.isEmpty()) {
for (int i = 0; i < itemStacks.size(); i++) {
ItemStack itemstack = itemStacks.get(i);
if (!itemstack.isEmpty()) {
CompoundTag compoundtag = new CompoundTag();
compoundtag.putByte("Slot", (byte) i);
listtag.add(itemstack.save(pRegistries, compoundtag));
}
}
}
tag.put("skillet", listtag);
tag.put("items",itemStackHandler.serializeNBT(pRegistries));
if (!fluidStacks.isEmpty()) {
tag.put("fluid", fluidStacks.save(pRegistries));
}
Expand All @@ -103,14 +110,7 @@ public CompoundTag getUpdateTag(HolderLookup.Provider pRegistries) {

@Override
protected void loadAdditional(CompoundTag pTag, HolderLookup.Provider pRegistries) {
ListTag listtag = (ListTag) pTag.get("skillet");
if (listtag != null) {
for (int i = 0; i < listtag.size(); i++) {
CompoundTag compoundtag = listtag.getCompound(i);
int j = compoundtag.getByte("Slot") & 255;
itemStacks.add(j, ItemStack.parse(pRegistries, compoundtag).orElse(ItemStack.EMPTY));
}
}
itemStackHandler.deserializeNBT(pRegistries, pTag.getCompound("items"));
if (pTag.get("fluid") != null) {
fluidStacks = FluidStack.parse(pRegistries, pTag.getCompound("fluid")).orElse(FluidStack.EMPTY);
}
Expand All @@ -119,39 +119,10 @@ protected void loadAdditional(CompoundTag pTag, HolderLookup.Provider pRegistrie
@Override
protected void saveAdditional(CompoundTag pTag, HolderLookup.Provider pRegistries) {
super.saveAdditional(pTag, pRegistries);
ListTag listtag = new ListTag();
if (!this.itemStacks.isEmpty()) {
for (int i = 0; i < itemStacks.size(); i++) {
ItemStack itemstack = itemStacks.get(i);
if (!itemstack.isEmpty()) {
CompoundTag compoundtag = new CompoundTag();
compoundtag.putByte("Slot", (byte) i);
listtag.add(itemstack.save(pRegistries, compoundtag));
}
}
}
pTag.put("items",itemStackHandler.serializeNBT(pRegistries));
if (!this.fluidStacks.isEmpty()) {
pTag.put("fluid", fluidStacks.save(pRegistries));
}
pTag.put("skillet", listtag);
}

private void loadItem(CompoundTag pTag, HolderLookup.Provider pLevelRegistry) {
ListTag listtag = (ListTag) pTag.get("item");
if (listtag != null) {
for (int i = 0; i < listtag.size(); i++) {
CompoundTag compoundtag = listtag.getCompound(i);
int j = compoundtag.getByte("Slot") & 255;
itemStacks.add(j, ItemStack.parse(pLevelRegistry, compoundtag).orElse(ItemStack.EMPTY));
}
} else if (this.level != null && this.level.isClientSide()) {
for (int i = 0; i < pTag.size(); i++) {
itemStacks.add(i, ItemStack.parse(pLevelRegistry, pTag.getCompound("Item" + i)).orElse(ItemStack.EMPTY));
}
} else {
itemStacks = new ArrayList<>();
setChanged();
}
}

@Override
Expand All @@ -165,7 +136,12 @@ public AbstractContainerMenu createMenu(int pContainerId, Inventory pPlayerInven
return new SkilletMenu(pContainerId, pPlayerInventory, this);
}

public static void tick(Level level, BlockPos pos, BlockState state, SkilletBlockEntity blockEntity) {
public static <T extends BlockEntity> void serverTick(Level pLevel, BlockPos pPos, BlockState blockState, T t) {
SkilletBlockEntity blockEntity = (SkilletBlockEntity) t;
boolean flag = blockEntity.isEmpty();
boolean heat_flag = blockEntity.isHeated(pLevel,pPos);
int MAX_SLOT = blockEntity.MAX_SLOT;

blockEntity.setChanged();
}
}
Loading

0 comments on commit 0bc2bbe

Please sign in to comment.