Skip to content

Commit

Permalink
添加:烹饪HUD界面
Browse files Browse the repository at this point in the history
  • Loading branch information
MCZME committed Aug 19, 2024
1 parent 55ffc57 commit ebf92f5
Show file tree
Hide file tree
Showing 24 changed files with 371 additions and 94 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ base {
// Mojang ships Java 21 to end users starting in 1.20.5, so mods should target Java 21.
java.toolchain.languageVersion = JavaLanguageVersion.of(21)

//minecraft.accessTransformers.file rootProject.file('src/main/resources/META-INF/accesstransformer.cfg')
minecraft.accessTransformers.file rootProject.file('src/main/resources/META-INF/accesstransformer.cfg')
//minecraft.accessTransformers.entry public net.minecraft.client.Minecraft textureManager # textureManager

// Default run configurations.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// 1.21 2024-08-16T23:38:40.1170951 Recipes
// 1.21 2024-08-19T18:23:04.1006546 Recipes
30317ddefa8c34feeae2fb9b47b40dae9a2c1335 data/lingshi/advancement/recipes/skillet/rice_apple.json
60b5c5b8536660e2fc3758499991b48002d68196 data/lingshi/advancement/recipes/skillet/test01.json
24367c601743c0d2f9df76c4a1e59f6650f0de4a data/lingshi/advancement/recipes/skillet/test02.json
3e41620c4ec5b19f87046dad7fe5c7ed6ede21c2 data/lingshi/recipe/chopping_board/rice.json
1268fc69304435f6b1c56650c7e2f8fcc9fe7e6e data/lingshi/recipe/skillet/rice_apple.json
a25f37985d35ec9c738209ef29f82c466a34f01c data/lingshi/recipe/skillet/rice_apple.json
c80602ecff2ea09399aff222a6d36c7b28d0164f data/lingshi/recipe/skillet/test01.json
89bc0e33a11e930f7b69c4d726d92b4f9446b3d3 data/lingshi/recipe/skillet/test02.json
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// 1.21 2024-08-18T01:50:29.4609975 Data Maps
15e4de66fa16cad8d36f7b96c6aa67d7cb0b0f1a data/lingshi/data_maps/item/cooking_food_item.json
// 1.21 2024-08-19T23:11:55.0223064 Data Maps
7efa3dffa87b902d4fac709bea335215c17fdf2a data/lingshi/data_maps/item/cooking_food_item.json
7 changes: 7 additions & 0 deletions src/main/java/mczme/lingshi/client/event/Registry.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import mczme.lingshi.client.BlockEntityRenderer.ChoppingBoardBER;
import mczme.lingshi.client.BlockEntityRenderer.SkilletBER;
import mczme.lingshi.client.recipebook.CookingFoodRecipeLabel;
import mczme.lingshi.client.screen.CookingHud;
import mczme.lingshi.client.screen.SkilletScreen;
import mczme.lingshi.common.recipe.SkilletRecipe;
import mczme.lingshi.common.registry.BlockEntityTypes;
Expand All @@ -24,6 +25,7 @@
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.fml.event.lifecycle.FMLClientSetupEvent;
import net.neoforged.neoforge.client.event.EntityRenderersEvent;
import net.neoforged.neoforge.client.event.RegisterGuiLayersEvent;
import net.neoforged.neoforge.client.event.RegisterMenuScreensEvent;
import net.neoforged.neoforge.client.event.RegisterRecipeBookCategoriesEvent;
import net.neoforged.neoforge.client.extensions.common.IClientFluidTypeExtensions;
Expand Down Expand Up @@ -118,4 +120,9 @@ public void modifyFogRender(Camera camera, FogRenderer.FogMode mode, float rende
}, ModFluids.MOD_FLUID_TYPE.get());
}

//HUD
@SubscribeEvent
public static void registerGuiLayersEvent(RegisterGuiLayersEvent event) {
event.registerAboveAll(ResourceLocation.fromNamespaceAndPath(lingshi.MODID,"cooking_hud"), CookingHud.getInstance());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package mczme.lingshi.client.menu;

import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.items.ItemStackHandler;
import org.jetbrains.annotations.NotNull;

public class CookingItemStackHandler extends ItemStackHandler {

public CookingItemStackHandler(int size){
super(size);
}

@Override
protected int getStackLimit(int slot, @NotNull ItemStack stack) {
return 1;
}
}
2 changes: 1 addition & 1 deletion src/main/java/mczme/lingshi/client/menu/SkilletMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class SkilletMenu extends RecipeBookMenu<SkilletRecipeInput, SkilletRecip

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

public SkilletMenu(int pContainerId, Inventory pPlayerInventory, FriendlyByteBuf pContext) {
this(pContainerId, pPlayerInventory, (SkilletBlockEntity) pPlayerInventory.player.level().getBlockEntity(pContext.readBlockPos()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public void setupGhostRecipe(RecipeHolder<?> pRecipe, List<Slot> pSlots) {
ItemStack resultitem = pRecipe.value().getResultItem(this.minecraft.level.registryAccess());
this.ghostRecipe.setRecipe(pRecipe);
this.ghostRecipe.addIngredient(Ingredient.of(resultitem), pSlots.get(6).x, pSlots.get(6).y);
if(skilletRecipe.getContainer() != ItemStack.EMPTY){
this.ghostRecipe.addIngredient(Ingredient.of(skilletRecipe.getContainer()), pSlots.get(5).x, pSlots.get(5).y);
if(skilletRecipe.getContainer().container() != ItemStack.EMPTY){
this.ghostRecipe.addIngredient(Ingredient.of(skilletRecipe.getContainer().container()), pSlots.get(5).x, pSlots.get(5).y);
}

NonNullList<Ingredient> nonnulllist = pRecipe.value().getIngredients();
Expand Down
92 changes: 92 additions & 0 deletions src/main/java/mczme/lingshi/client/screen/CookingHud.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package mczme.lingshi.client.screen;

import mczme.lingshi.common.block.entity.SkilletBlockEntity;
import mczme.lingshi.common.datamap.DataMapTypes;
import mczme.lingshi.common.datamap.ingredient.CookingFoodData;
import mczme.lingshi.common.network.CookingDataClientPayloadHandler;
import mczme.lingshi.common.registry.BlockEntityTypes;
import mczme.lingshi.lingshi;
import net.minecraft.client.DeltaTracker;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.LayeredDraw;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;
import net.neoforged.neoforge.items.ItemStackHandler;
import org.jetbrains.annotations.NotNull;

public class CookingHud implements LayeredDraw.Layer {

public static final CookingHud hud = new CookingHud();

public static final ResourceLocation HUD_Sprite = ResourceLocation.fromNamespaceAndPath(lingshi.MODID, "textures/gui/cooking_hud.png");
private ItemStackHandler itemStackHandler;
private int[] cookingTime;
private ItemStack result;
private int MAX_SLOT;
private Player player;
private BlockPos blockPos;

private final int X = 28;
private final int Y = 140;

@Override
public void render(@NotNull GuiGraphics pGuiGraphics, @NotNull DeltaTracker pDeltaTracker) {
if (Minecraft.getInstance().level == null) {
return;
}
this.player = Minecraft.getInstance().player;
if (player != null && getHitResult(BlockEntityTypes.SKILLET_BLOCKENTITY.get())) {
getData();
for (int i = 0; i < MAX_SLOT; i++) {
if (!itemStackHandler.getStackInSlot(i).isEmpty()) {
pGuiGraphics.renderItem(itemStackHandler.getStackInSlot(i), X-18, Y + i * 18);
CookingFoodData cookingFoodData = itemStackHandler.getStackInSlot(i).getItemHolder().getData(DataMapTypes.COOKING_FOOD_ITEM);
if (cookingFoodData != null) {
drawItemProgress(pGuiGraphics, cookingFoodData, i);
}
}
}
}

}

private void getData() {
SkilletBlockEntity blockEntity = null;
if (Minecraft.getInstance().level != null) {
blockEntity = (SkilletBlockEntity) Minecraft.getInstance().level.getBlockEntity(blockPos);
}
if (blockEntity != null) {
this.itemStackHandler = blockEntity.getItemStacks();
this.cookingTime = blockEntity.getCookingTime();
this.result = CookingDataClientPayloadHandler.getResult();
this.MAX_SLOT = blockEntity.getMAX();
}
}

private boolean getHitResult(BlockEntityType pType) {
Vec3 start = player.getEyePosition();
Vec3 end = player.getLookAngle().normalize().scale(3).add(start);
BlockHitResult blockHitResult = Minecraft.getInstance().level.clip(new ClipContext(start, end, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, player));
this.blockPos = blockHitResult.getBlockPos();
return Minecraft.getInstance().level.getBlockEntity(blockPos) != null && Minecraft.getInstance().level.getBlockEntity(blockPos).getType() == pType;
}

private void drawItemProgress(GuiGraphics pGuiGraphics, CookingFoodData cookingFoodData, int i) {
int pHeight = 8;
pGuiGraphics.blit(HUD_Sprite, X, Y + i * 18 + 7, cookingFoodData.cookedTime() * 2, pHeight, 0, 0, cookingFoodData.cookedTime() * 2 + 2, pHeight, 64, 64);
pGuiGraphics.blit(HUD_Sprite, X + cookingFoodData.cookedTime() * 2, Y+ i * 18 + 7, (cookingFoodData.burntTime() - cookingFoodData.cookedTime()) * 2, pHeight, 2, 8, (cookingFoodData.burntTime() - cookingFoodData.cookedTime()) * 2, pHeight, 64, 64);
pGuiGraphics.blit(HUD_Sprite, X + cookingFoodData.burntTime() * 2, Y + i * 18 + 7, 62 - cookingFoodData.burntTime() * 2, pHeight, cookingFoodData.burntTime() * 2 + 2, 24, 62 - cookingFoodData.burntTime() * 2, pHeight, 64, 64);
pGuiGraphics.blit(HUD_Sprite, X+1 + cookingTime[i] / 10, Y + i * 18 + 9, 4, 4, 0, 32, 4, 4, 64, 64);
}

public static CookingHud getInstance() {
return hud;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package mczme.lingshi.common.block.entity;

import mczme.lingshi.client.menu.CookingItemStackHandler;
import mczme.lingshi.client.menu.SkilletMenu;
import mczme.lingshi.common.block.entity.baseblockentity.ICanBeHeated;
import mczme.lingshi.common.datamap.DataMapTypes;
Expand All @@ -12,6 +13,8 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.HolderLookup;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
Expand Down Expand Up @@ -39,13 +42,14 @@ public class SkilletBlockEntity extends BlockEntity implements MenuProvider, ICa
private final int MAX_SLOT = 5;

private FluidStack fluidStacks = FluidStack.EMPTY;
private ItemStackHandler itemStackHandler = new ItemStackHandler(MAX_SLOT + 2);
private final CookingItemStackHandler itemStackHandler = new CookingItemStackHandler(MAX_SLOT + 2);

private final int[] cookingTime = new int[5];


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

}

public boolean isFull() {
Expand Down Expand Up @@ -81,7 +85,7 @@ public FluidStack getFluid() {
return fluidStacks;
}

public ItemStackHandler getItemStacks() {
public CookingItemStackHandler getItemStacks() {
return itemStackHandler;
}

Expand All @@ -95,36 +99,40 @@ public void setItem(ItemStack item) {
}

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

public void setFluid(FluidStack fluid) {
fluidStacks = fluid;
}

public int size(){
public int size() {
int size = 0;
for (int i = 0; i < MAX_SLOT; i++) {
if(!itemStackHandler.getStackInSlot(i).isEmpty()){
if (!itemStackHandler.getStackInSlot(i).isEmpty()) {
size++;
}
}
return size;
}

public void clear(){
public void clear() {
for (int i = 0; i < MAX_SLOT; i++) {
itemStackHandler.setStackInSlot(i, ItemStack.EMPTY);
}
}

public void consume(){
public void consume() {
for (int i = 0; i < MAX_SLOT; i++) {
int count = itemStackHandler.getStackInSlot(i).getCount();
itemStackHandler.getStackInSlot(i).setCount(count - 1);
}
}

public int[] getCookingTime() {
return cookingTime;
}

public int getMAX() {
return MAX_SLOT;
}
Expand All @@ -141,6 +149,7 @@ public CompoundTag getUpdateTag(HolderLookup.Provider pRegistries) {
if (!fluidStacks.isEmpty()) {
tag.put("fluid", fluidStacks.save(pRegistries));
}
saveCookingTime(tag);
return tag;
}

Expand All @@ -150,6 +159,9 @@ protected void loadAdditional(CompoundTag pTag, HolderLookup.Provider pRegistrie
if (pTag.get("fluid") != null) {
fluidStacks = FluidStack.parse(pRegistries, pTag.getCompound("fluid")).orElse(FluidStack.EMPTY);
}
if (pTag.get("cookingTime") != null && pTag.getInt("Size") != 0) {
loadCookingTime(pTag.getList("cookingTime", Tag.TAG_COMPOUND),pTag.getInt("Size"));
}
}

@Override
Expand All @@ -159,6 +171,24 @@ protected void saveAdditional(CompoundTag pTag, HolderLookup.Provider pRegistrie
if (!this.fluidStacks.isEmpty()) {
pTag.put("fluid", fluidStacks.save(pRegistries));
}
saveCookingTime(pTag);
}

private void saveCookingTime(CompoundTag nbt){
ListTag nbtTagList = new ListTag();
for (int i = 0; i < MAX_SLOT; i++) {
CompoundTag itemTag = new CompoundTag();
itemTag.putInt("index", cookingTime[i]);
nbtTagList.add(itemTag);
}
nbt.put("cookingTime", nbtTagList);
nbt.putInt("Size", MAX_SLOT);
}

private void loadCookingTime(ListTag pTag,int size){
for (int i = 0; i < size; i++) {
cookingTime[i]= pTag.getCompound(i).getInt("index");
}
}

@Override
Expand All @@ -181,46 +211,46 @@ public static <T extends BlockEntity> void serverTick(Level pLevel, BlockPos pPo

if (heat_flag) {
if (!flag) {
int cookedTime=0,burntTime=0;
int cookedTime = 0, burntTime = 0;
for (int i = 0; i < MAX_SLOT; i++) {
ItemStack itemStack = itemStackHandler.getStackInSlot(i);
if (itemStack.isEmpty()) {
continue;
}
CookingFoodData cookingFoodData = itemStack.getItemHolder().getData(DataMapTypes.COOKING_FOOD_ITEM);
if (cookingFoodData != null) {
cookedTime=cookingFoodData.cookedTime();
burntTime=cookingFoodData.burntTime();
cookedTime = cookingFoodData.cookedTime();
burntTime = cookingFoodData.burntTime();
blockEntity.cookingTime[i]++;
}
}
SkilletRecipeInput input = new SkilletRecipeInput(itemStackHandler,blockEntity.getFluid());
SkilletRecipeInput input = new SkilletRecipeInput(itemStackHandler, blockEntity.getFluid());
Optional<RecipeHolder<SkilletRecipe>> optional = pLevel.getRecipeManager().getRecipeFor(
ModRecipes.SKILLET_RECIPE.get(),
input,
pLevel
);
ItemStack result = optional.map(RecipeHolder::value)
.map(e->e.assemble(input,pLevel.registryAccess()))
.map(e -> e.assemble(input, pLevel.registryAccess()))
.orElse(ItemStack.EMPTY);
if (!result.isEmpty()) {
int size=0;
int size = 0;
for (int i = 0; i < MAX_SLOT; i++) {
if(blockEntity.cookingTime[i]>cookedTime*20){
if (blockEntity.cookingTime[i] > cookedTime * 20) {
size++;
}
}
if (size==blockEntity.size()){
Containers.dropItemStack(pLevel, pPos.getX(), pPos.getY()+0.2, pPos.getZ(), result);
Arrays.fill(blockEntity.cookingTime,0);
if (size == blockEntity.size()) {
Containers.dropItemStack(pLevel, pPos.getX(), pPos.getY() + 0.2, pPos.getZ(), result);
Arrays.fill(blockEntity.cookingTime, 0);
blockEntity.consume();
pLevel.sendBlockUpdated(pPos, blockState, blockState, Block.UPDATE_CLIENTS);
}
}else {
Arrays.fill(blockEntity.cookingTime,0);
}
} else {
Arrays.fill(blockEntity.cookingTime, 0);
}
pLevel.sendBlockUpdated(pPos, blockState, blockState, Block.UPDATE_CLIENTS);
}
blockEntity.setChanged();
}
blockEntity.setChanged();
}
}
Loading

0 comments on commit ebf92f5

Please sign in to comment.