Skip to content

Commit

Permalink
添加:满足 效果
Browse files Browse the repository at this point in the history
  • Loading branch information
MCZME committed Aug 31, 2024
1 parent d5481df commit 9b18964
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 13 deletions.
12 changes: 11 additions & 1 deletion src/main/java/mczme/lingshi/common/block/FoodBlock.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package mczme.lingshi.common.block;

import mczme.lingshi.common.registry.ModEffects;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.ItemInteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.context.BlockPlaceContext;
import net.minecraft.world.level.BlockGetter;
Expand Down Expand Up @@ -43,7 +46,14 @@ public ItemInteractionResult useItemOn(ItemStack pStack, BlockState pState, Leve
}else {
if(pPlayer.canEat(false)) {
pLevel.setBlock(pPos, pState.setValue(AMOUNT, 0), Block.UPDATE_CLIENTS);
pPlayer.eat(pLevel, new ItemStack(pState.getBlock().asItem()));
if(pPlayer.hasEffect(ModEffects.GRATIFICATION_EFFECT)){
ItemStack itemStack = new ItemStack(pState.getBlock().asItem());
FoodProperties foodData =itemStack.get(DataComponents.FOOD);
pPlayer.eat(pLevel, itemStack, new FoodProperties.Builder().nutrition(foodData.nutrition()).saturationModifier(foodData.saturation()*2).build());
}else {
pPlayer.eat(pLevel, new ItemStack(pState.getBlock().asItem()));
}

return ItemInteractionResult.SUCCESS;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected void buildRecipes(RecipeOutput output) {
.unlockedBy("has_rice", has(ModItems.RICE.get())).group("rice")
.setContainer(new CookingPotRecipe.CookingPotContainer(new ItemStack(Items.BOWL),0))
.save(output,create("cooked_rice_2"));
build(List.of(Ingredient.of(ModItems.RICE.get()),Ingredient.of(ModItems.RICE.get()),Ingredient.of(ModItems.CABBAGE_LEAF.get()),Ingredient.of(ModItems.PIG_FEET.get())),new FluidStack(Fluids.WATER,1000),new ItemStack(ModItems.PORK_FEET_RICE.get(),2),CookingFoodRecipeLabel.BOIL)
build(List.of(Ingredient.of(ModItems.RICE.get()),Ingredient.of(ModItems.RICE.get()),Ingredient.of(ModItems.CABBAGE_LEAF.get()),Ingredient.of(ModItems.PIG_FEET.get())),new FluidStack(Fluids.WATER,1000),new ItemStack(ModItems.PORK_FEET_RICE.get(),2),CookingFoodRecipeLabel.STEW)
.unlockedBy("has_pig_feet", has(ModItems.PIG_FEET.get()))
.setContainer(new CookingPotRecipe.CookingPotContainer(new ItemStack(Items.BOWL),5))
.save(output,create("pork_feet_rice"));
Expand Down
52 changes: 52 additions & 0 deletions src/main/java/mczme/lingshi/common/effect/GratificationEffect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package mczme.lingshi.common.effect;

import mczme.lingshi.common.registry.ModEffects;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.food.FoodData;
import net.minecraft.world.item.ItemStack;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.neoforge.event.entity.living.LivingEntityUseItemEvent;

public class GratificationEffect extends MobEffect {
public GratificationEffect(MobEffectCategory category, int color) {
super(category, color);
}

@Override
public boolean applyEffectTick(LivingEntity pLivingEntity, int pAmplifier) {
if(!pLivingEntity.level().isClientSide() && pLivingEntity instanceof Player player) {
FoodData foodData = player.getFoodData();
if(foodData.getFoodLevel() < 18.0F) {
float Exhaustion = Math.min(player.getFoodData().getExhaustionLevel(), 4.0F);
if (player.getFoodData().getExhaustionLevel() > 0.0F) {
player.causeFoodExhaustion(-Exhaustion);
}
}
}
return true;
}

@Override
public boolean shouldApplyEffectTickThisTick(int pDuration, int pAmplifier) {
return true;
}

public static class Event{
@SubscribeEvent
public void onEvent(LivingEntityUseItemEvent.Finish event) {
if(event.getEntity() instanceof Player player && !event.getEntity().level().isClientSide()){
if(player.hasEffect(ModEffects.GRATIFICATION_EFFECT)){
ItemStack itemStack = event.getItem();
if(itemStack.get(DataComponents.FOOD) != null) {
player.getFoodData().eat(0,itemStack.get(DataComponents.FOOD).saturation());
}
}
}
}
}

}
10 changes: 10 additions & 0 deletions src/main/java/mczme/lingshi/common/event/IndependentEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package mczme.lingshi.common.event;

import mczme.lingshi.common.effect.GratificationEffect;
import net.neoforged.neoforge.common.NeoForge;

public class IndependentEvent {
public static void register(){
NeoForge.EVENT_BUS.register(new GratificationEffect.Event());
}
}
20 changes: 20 additions & 0 deletions src/main/java/mczme/lingshi/common/registry/ModEffects.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package mczme.lingshi.common.registry;

import mczme.lingshi.common.effect.GratificationEffect;
import mczme.lingshi.lingshi;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectCategory;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.neoforge.registries.DeferredHolder;
import net.neoforged.neoforge.registries.DeferredRegister;

public class ModEffects {
public static final DeferredRegister<MobEffect> MOD_EFFECTS = DeferredRegister.create(Registries.MOB_EFFECT, lingshi.MODID);

public static final DeferredHolder<MobEffect,GratificationEffect> GRATIFICATION_EFFECT = MOD_EFFECTS.register("gratification_effect", () -> new GratificationEffect(MobEffectCategory.BENEFICIAL, 0xffffff));

public static void register(IEventBus eventBus){
MOD_EFFECTS.register(eventBus);
}
}
3 changes: 2 additions & 1 deletion src/main/java/mczme/lingshi/common/registry/ModFluids.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public class ModFluids {

public static final Supplier<FlowingFluid> OIL_SOURCE = FLUIDS.register("oil_source", ()-> new OilFluid.Source(ModFluids.properties));
public static final Supplier<FlowingFluid> OIL_FLOWING = FLUIDS.register("oil_flowing",()-> new OilFluid.Flowing(ModFluids.properties));
public static final BaseFlowingFluid.Properties properties = new BaseFlowingFluid.Properties(MOD_FLUID_TYPE,ModFluids.OIL_SOURCE,ModFluids.OIL_FLOWING).bucket(ModItems.OIL_BUCKET).slopeFindDistance(2).levelDecreasePerBlock(2).block(ModBlocks.OIL_LIQUID_BLOCK);
public static final BaseFlowingFluid.Properties properties = new BaseFlowingFluid.Properties(MOD_FLUID_TYPE,ModFluids.OIL_SOURCE,ModFluids.OIL_FLOWING)
.bucket(ModItems.OIL_BUCKET).slopeFindDistance(2).levelDecreasePerBlock(2).block(ModBlocks.OIL_LIQUID_BLOCK);

public static void register(IEventBus modEventBus) {
FLUID_TYPES.register(modEventBus);
Expand Down
32 changes: 22 additions & 10 deletions src/main/java/mczme/lingshi/common/registry/ModItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import mczme.lingshi.common.item.PotLid;
import mczme.lingshi.common.item.SpatulaItem;
import mczme.lingshi.lingshi;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.item.*;
import net.neoforged.bus.api.IEventBus;
Expand All @@ -22,8 +23,10 @@ public class ModItems {
public static final Supplier<Item> RICE_SEEDLING = registerWithCreateTab("rice_seedling", () -> new BlockItem(ModBlocks.RICE_SEEDLING.get(), new Item.Properties()));
public static final Supplier<Item> RICE_OF_EAR = registerWithCreateTab("rice_of_ear", () -> new Item(new Item.Properties()));
public static final Supplier<Item> CABBAGE_SEED = registerWithCreateTab("cabbage_seed", () -> new BlockItem(ModBlocks.CABBAGE.get(), new Item.Properties()));
public static final Supplier<Item> CABBAGE = registerWithCreateTab("cabbage", () -> new Item(new Item.Properties().food(new FoodProperties.Builder().nutrition(3).saturationModifier(2.4f).build())));
public static final Supplier<Item> CABBAGE_LEAF = registerWithCreateTab("cabbage_leaf", () -> new Item( new Item.Properties().food(new FoodProperties.Builder().nutrition(1).saturationModifier(0.8f).build())));
public static final Supplier<Item> CABBAGE = registerWithCreateTab("cabbage", () -> new Item(new Item.Properties().food(new FoodProperties.Builder()
.nutrition(3).saturationModifier(2.4f).build())));
public static final Supplier<Item> CABBAGE_LEAF = registerWithCreateTab("cabbage_leaf", () -> new Item( new Item.Properties().food(new FoodProperties.Builder()
.nutrition(1).saturationModifier(0.8f).build())));

public static final Supplier<Item> TEA_TREE = registerWithCreateTab("tea_tree", () -> new BlockItem(ModBlocks.TEA_TREE.get(), new Item.Properties()));
public static final Supplier<Item> TEA_LEAF = registerWithCreateTab("tea_leaf", () -> new Item(new Item.Properties()));
Expand All @@ -34,21 +37,30 @@ public class ModItems {
public static final Supplier<Item> CHOPPING_BOARD = registerWithCreateTab("chopping_board", () -> new BlockItem(ModBlocks.CHOPPING_BOARD.get(), new Item.Properties()));

public static final Supplier<Item> SPATULA = registerWithCreateTab("spatula", () -> new SpatulaItem(new Item.Properties().stacksTo(1)));
public static final Supplier<Item> IRON_KNIFE = registerWithCreateTab("iron_knife", () -> new KnifeItem(Tiers.IRON, new Item.Properties().attributes(SwordItem.createAttributes(Tiers.IRON, 3, -2.4F))));
public static final Supplier<Item> IRON_KNIFE = registerWithCreateTab("iron_knife", () -> new KnifeItem(Tiers.IRON, new Item.Properties()
.attributes(SwordItem.createAttributes(Tiers.IRON, 3, -2.4F))));

public static final Supplier<Item> STOVE = registerWithCreateTab("stove", () -> new BlockItem(ModBlocks.STOVE.get(), new Item.Properties()));
public static final Supplier<Item> OIL_BUCKET = registerWithCreateTab("oil_bucket", () -> new BucketItem(ModFluids.OIL_SOURCE.get(), new Item.Properties().craftRemainder(Items.BUCKET).stacksTo(1)));
public static final Supplier<Item> OIL_BUCKET = registerWithCreateTab("oil_bucket", () -> new BucketItem(ModFluids.OIL_SOURCE.get(), new Item.Properties()
.craftRemainder(Items.BUCKET).stacksTo(1)));
//食物
public static final Supplier<Item> STRANGE_FOOD = registerWithCreateTab("strange_food", () -> new Item(new Item.Properties()));
public static final Supplier<Item> FRIED_EGG = registerWithCreateTab("fried_egg", () -> new Item(new Item.Properties().food(new FoodProperties.Builder().nutrition(4).saturationModifier(3.2f).build())));
public static final Supplier<Item> COOKED_RICE = registerWithCreateTab("cooked_rice", () -> new Item(new Item.Properties().food(new FoodProperties.Builder().nutrition(4).usingConvertsTo(Items.BOWL).saturationModifier(4f).build())));
public static final Supplier<Item> EGG_FRIED_RICE = registerWithCreateTab("egg_fried_rice", () -> new Item(new Item.Properties().food(new FoodProperties.Builder().nutrition(8).usingConvertsTo(Items.BOWL).saturationModifier(7.2f).build())));
public static final Supplier<Item> FRIED_EGG = registerWithCreateTab("fried_egg", () -> new Item(new Item.Properties().food(new FoodProperties.Builder()
.nutrition(4).saturationModifier(3.2f).build())));
public static final Supplier<Item> COOKED_RICE = registerWithCreateTab("cooked_rice", () -> new Item(new Item.Properties().food(new FoodProperties.Builder()
.nutrition(4).usingConvertsTo(Items.BOWL).saturationModifier(4f).build())));
public static final Supplier<Item> EGG_FRIED_RICE = registerWithCreateTab("egg_fried_rice", () -> new Item(new Item.Properties().food(new FoodProperties.Builder()
.nutrition(8).usingConvertsTo(Items.BOWL).saturationModifier(7.2f).build())));
public static final Supplier<Item> FLOUR = registerWithCreateTab("flour", () -> new Item(new Item.Properties()));
public static final Supplier<Item> DOUGH = registerWithCreateTab("dough", () -> new Item(new Item.Properties()));
public static final Supplier<Item> NOODLES = registerWithCreateTab("noodles", () -> new Item(new Item.Properties().food(new FoodProperties.Builder().nutrition(2).saturationModifier(1f).build())));
public static final Supplier<Item> PIG_FEET = registerWithCreateTab("pig_feet", () -> new Item(new Item.Properties().food(new FoodProperties.Builder().nutrition(5).saturationModifier(3f).build())));
public static final Supplier<Item> NOODLES = registerWithCreateTab("noodles", () -> new Item(new Item.Properties().food(new FoodProperties.Builder()
.nutrition(2).saturationModifier(1f).build())));
public static final Supplier<Item> PIG_FEET = registerWithCreateTab("pig_feet", () -> new Item(new Item.Properties().food(new FoodProperties.Builder()
.nutrition(5).saturationModifier(3f).build())));
//可放置食物
public static final Supplier<Item> PORK_FEET_RICE = registerWithCreateTab("pork_feet_rice",()-> new FoodBlockItem(ModBlocks.PORK_FEET_RICE.get(), new Item.Properties().food(new FoodProperties.Builder().nutrition(12).saturationModifier(9.4f).build())));
public static final Supplier<Item> PORK_FEET_RICE = registerWithCreateTab("pork_feet_rice",()-> new FoodBlockItem(ModBlocks.PORK_FEET_RICE.get(), new Item.Properties()
.food(new FoodProperties.Builder().usingConvertsTo(Items.BOWL).nutrition(12).saturationModifier(9.4f)
.effect(()-> new MobEffectInstance(ModEffects.GRATIFICATION_EFFECT,5*60*20),1.0F).build())));

private static Supplier<Item> registerWithCreateTab(String item_name, Supplier<Item> itemSupplier) {
Supplier<Item> item = ITEMS.register(item_name, itemSupplier);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/mczme/lingshi/lingshi.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mczme.lingshi;

import mczme.lingshi.common.createtab.CreateTabs;
import mczme.lingshi.common.event.IndependentEvent;
import mczme.lingshi.common.registry.*;
import org.slf4j.Logger;

Expand Down Expand Up @@ -29,6 +30,10 @@ public lingshi(IEventBus modEventBus, ModContainer modContainer)
ModSerializer.register(modEventBus);
ModMenuTypes.register(modEventBus);
ModFluids.register(modEventBus);
ModEffects.register(modEventBus);

IndependentEvent.register();

}


Expand Down
Binary file modified src/main/resources/assets/lingshi/textures/item/cabbage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9b18964

Please sign in to comment.