From 9b1896464bae8ad10f303b27e0ab59f18284f01b Mon Sep 17 00:00:00 2001 From: MCZME <13183052+mczme@user.noreply.gitee.com> Date: Sat, 31 Aug 2024 16:17:12 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=EF=BC=9A=E6=BB=A1=E8=B6=B3?= =?UTF-8?q?=20=E6=95=88=E6=9E=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mczme/lingshi/common/block/FoodBlock.java | 12 +++- .../data/recipe/CookingPotRecipeDataGen.java | 2 +- .../common/effect/GratificationEffect.java | 52 ++++++++++++++++++ .../common/event/IndependentEvent.java | 10 ++++ .../lingshi/common/registry/ModEffects.java | 20 +++++++ .../lingshi/common/registry/ModFluids.java | 3 +- .../lingshi/common/registry/ModItems.java | 32 +++++++---- src/main/java/mczme/lingshi/lingshi.java | 5 ++ .../assets/lingshi/textures/item/cabbage.png | Bin 478 -> 476 bytes 9 files changed, 123 insertions(+), 13 deletions(-) create mode 100644 src/main/java/mczme/lingshi/common/effect/GratificationEffect.java create mode 100644 src/main/java/mczme/lingshi/common/event/IndependentEvent.java create mode 100644 src/main/java/mczme/lingshi/common/registry/ModEffects.java diff --git a/src/main/java/mczme/lingshi/common/block/FoodBlock.java b/src/main/java/mczme/lingshi/common/block/FoodBlock.java index a349f19..78a346a 100644 --- a/src/main/java/mczme/lingshi/common/block/FoodBlock.java +++ b/src/main/java/mczme/lingshi/common/block/FoodBlock.java @@ -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; @@ -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; } } diff --git a/src/main/java/mczme/lingshi/common/data/recipe/CookingPotRecipeDataGen.java b/src/main/java/mczme/lingshi/common/data/recipe/CookingPotRecipeDataGen.java index 74773c4..8bdf970 100644 --- a/src/main/java/mczme/lingshi/common/data/recipe/CookingPotRecipeDataGen.java +++ b/src/main/java/mczme/lingshi/common/data/recipe/CookingPotRecipeDataGen.java @@ -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")); diff --git a/src/main/java/mczme/lingshi/common/effect/GratificationEffect.java b/src/main/java/mczme/lingshi/common/effect/GratificationEffect.java new file mode 100644 index 0000000..cc3c4d6 --- /dev/null +++ b/src/main/java/mczme/lingshi/common/effect/GratificationEffect.java @@ -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()); + } + } + } + } + } + +} diff --git a/src/main/java/mczme/lingshi/common/event/IndependentEvent.java b/src/main/java/mczme/lingshi/common/event/IndependentEvent.java new file mode 100644 index 0000000..30063c0 --- /dev/null +++ b/src/main/java/mczme/lingshi/common/event/IndependentEvent.java @@ -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()); + } +} diff --git a/src/main/java/mczme/lingshi/common/registry/ModEffects.java b/src/main/java/mczme/lingshi/common/registry/ModEffects.java new file mode 100644 index 0000000..6a82334 --- /dev/null +++ b/src/main/java/mczme/lingshi/common/registry/ModEffects.java @@ -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 MOD_EFFECTS = DeferredRegister.create(Registries.MOB_EFFECT, lingshi.MODID); + + public static final DeferredHolder GRATIFICATION_EFFECT = MOD_EFFECTS.register("gratification_effect", () -> new GratificationEffect(MobEffectCategory.BENEFICIAL, 0xffffff)); + + public static void register(IEventBus eventBus){ + MOD_EFFECTS.register(eventBus); + } +} diff --git a/src/main/java/mczme/lingshi/common/registry/ModFluids.java b/src/main/java/mczme/lingshi/common/registry/ModFluids.java index 4e76908..8ea6c76 100644 --- a/src/main/java/mczme/lingshi/common/registry/ModFluids.java +++ b/src/main/java/mczme/lingshi/common/registry/ModFluids.java @@ -37,7 +37,8 @@ public class ModFluids { public static final Supplier OIL_SOURCE = FLUIDS.register("oil_source", ()-> new OilFluid.Source(ModFluids.properties)); public static final Supplier 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); diff --git a/src/main/java/mczme/lingshi/common/registry/ModItems.java b/src/main/java/mczme/lingshi/common/registry/ModItems.java index 1969db1..9621a49 100644 --- a/src/main/java/mczme/lingshi/common/registry/ModItems.java +++ b/src/main/java/mczme/lingshi/common/registry/ModItems.java @@ -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; @@ -22,8 +23,10 @@ public class ModItems { public static final Supplier RICE_SEEDLING = registerWithCreateTab("rice_seedling", () -> new BlockItem(ModBlocks.RICE_SEEDLING.get(), new Item.Properties())); public static final Supplier RICE_OF_EAR = registerWithCreateTab("rice_of_ear", () -> new Item(new Item.Properties())); public static final Supplier CABBAGE_SEED = registerWithCreateTab("cabbage_seed", () -> new BlockItem(ModBlocks.CABBAGE.get(), new Item.Properties())); - public static final Supplier CABBAGE = registerWithCreateTab("cabbage", () -> new Item(new Item.Properties().food(new FoodProperties.Builder().nutrition(3).saturationModifier(2.4f).build()))); - public static final Supplier CABBAGE_LEAF = registerWithCreateTab("cabbage_leaf", () -> new Item( new Item.Properties().food(new FoodProperties.Builder().nutrition(1).saturationModifier(0.8f).build()))); + public static final Supplier CABBAGE = registerWithCreateTab("cabbage", () -> new Item(new Item.Properties().food(new FoodProperties.Builder() + .nutrition(3).saturationModifier(2.4f).build()))); + public static final Supplier CABBAGE_LEAF = registerWithCreateTab("cabbage_leaf", () -> new Item( new Item.Properties().food(new FoodProperties.Builder() + .nutrition(1).saturationModifier(0.8f).build()))); public static final Supplier TEA_TREE = registerWithCreateTab("tea_tree", () -> new BlockItem(ModBlocks.TEA_TREE.get(), new Item.Properties())); public static final Supplier TEA_LEAF = registerWithCreateTab("tea_leaf", () -> new Item(new Item.Properties())); @@ -34,21 +37,30 @@ public class ModItems { public static final Supplier CHOPPING_BOARD = registerWithCreateTab("chopping_board", () -> new BlockItem(ModBlocks.CHOPPING_BOARD.get(), new Item.Properties())); public static final Supplier SPATULA = registerWithCreateTab("spatula", () -> new SpatulaItem(new Item.Properties().stacksTo(1))); - public static final Supplier IRON_KNIFE = registerWithCreateTab("iron_knife", () -> new KnifeItem(Tiers.IRON, new Item.Properties().attributes(SwordItem.createAttributes(Tiers.IRON, 3, -2.4F)))); + public static final Supplier IRON_KNIFE = registerWithCreateTab("iron_knife", () -> new KnifeItem(Tiers.IRON, new Item.Properties() + .attributes(SwordItem.createAttributes(Tiers.IRON, 3, -2.4F)))); public static final Supplier STOVE = registerWithCreateTab("stove", () -> new BlockItem(ModBlocks.STOVE.get(), new Item.Properties())); - public static final Supplier OIL_BUCKET = registerWithCreateTab("oil_bucket", () -> new BucketItem(ModFluids.OIL_SOURCE.get(), new Item.Properties().craftRemainder(Items.BUCKET).stacksTo(1))); + public static final Supplier OIL_BUCKET = registerWithCreateTab("oil_bucket", () -> new BucketItem(ModFluids.OIL_SOURCE.get(), new Item.Properties() + .craftRemainder(Items.BUCKET).stacksTo(1))); //食物 public static final Supplier STRANGE_FOOD = registerWithCreateTab("strange_food", () -> new Item(new Item.Properties())); - public static final Supplier FRIED_EGG = registerWithCreateTab("fried_egg", () -> new Item(new Item.Properties().food(new FoodProperties.Builder().nutrition(4).saturationModifier(3.2f).build()))); - public static final Supplier 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 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 FRIED_EGG = registerWithCreateTab("fried_egg", () -> new Item(new Item.Properties().food(new FoodProperties.Builder() + .nutrition(4).saturationModifier(3.2f).build()))); + public static final Supplier 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 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 FLOUR = registerWithCreateTab("flour", () -> new Item(new Item.Properties())); public static final Supplier DOUGH = registerWithCreateTab("dough", () -> new Item(new Item.Properties())); - public static final Supplier NOODLES = registerWithCreateTab("noodles", () -> new Item(new Item.Properties().food(new FoodProperties.Builder().nutrition(2).saturationModifier(1f).build()))); - public static final Supplier PIG_FEET = registerWithCreateTab("pig_feet", () -> new Item(new Item.Properties().food(new FoodProperties.Builder().nutrition(5).saturationModifier(3f).build()))); + public static final Supplier NOODLES = registerWithCreateTab("noodles", () -> new Item(new Item.Properties().food(new FoodProperties.Builder() + .nutrition(2).saturationModifier(1f).build()))); + public static final Supplier PIG_FEET = registerWithCreateTab("pig_feet", () -> new Item(new Item.Properties().food(new FoodProperties.Builder() + .nutrition(5).saturationModifier(3f).build()))); //可放置食物 - public static final Supplier 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 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 registerWithCreateTab(String item_name, Supplier itemSupplier) { Supplier item = ITEMS.register(item_name, itemSupplier); diff --git a/src/main/java/mczme/lingshi/lingshi.java b/src/main/java/mczme/lingshi/lingshi.java index 44df3ec..4fa525a 100644 --- a/src/main/java/mczme/lingshi/lingshi.java +++ b/src/main/java/mczme/lingshi/lingshi.java @@ -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; @@ -29,6 +30,10 @@ public lingshi(IEventBus modEventBus, ModContainer modContainer) ModSerializer.register(modEventBus); ModMenuTypes.register(modEventBus); ModFluids.register(modEventBus); + ModEffects.register(modEventBus); + + IndependentEvent.register(); + } diff --git a/src/main/resources/assets/lingshi/textures/item/cabbage.png b/src/main/resources/assets/lingshi/textures/item/cabbage.png index 6653ba2fde4bb5842db927867830a00239641c1c..9993a2d260fa805a7ea1895a0c286fb1906a913b 100644 GIT binary patch delta 450 zcmV;z0X_cS1Kb0UB!8nxL_t(Ijn$GrNE=Zc$3GYElDw-`1A;eb7ZnFVL9ukPQ2bMa zSZF~j*g=GXf{3G|gM&DfA||+WDTofz21UC#IEleQi_W?zXeo^d=5lwrJ_pIOF&4qi z-}rm)^WlBp?+1^*wEHfMB@7uZ^8n~9S^=QF^^M~GEBF_jXn#HiAQ&+B|2deMZx7!|&UoL@Yr?rVAGjLl5F*U5-QoQxBs7 zPSyg>0nqP)Q>Qj7i~`V5^A^)M;R)dQCSXw==w|zJr~%*qHp?^@z zi;%0G^B}(Q<6P0Os}{^v25N%RA?NoTZLbuk2|l3$uwk;Q|3b sbgD!GIGGGwlyd3+AsO@UUHJ#s6-+mdE{{)Q1ONa407*qoM6N<$f?oa6EdT%j delta 452 zcmV;#0XzQO1KtCWB!8tzL_t(Ijn$GrNE=Zc$3J&oV%|0W2^73RT~r)|W{?&aoupbs zp+mJ`2azm75giJSMI1_@P{gPRT?(Ru*r3!d4xNg@p%sOKLtQGUhzSMna=9GOLGm<- zBDnQC{@(k1`2F7d!G9m^t_!1yfDBc806Z;Q1E90LPjR;ju786Qt$zUsmInaXN+y-0 zL=cVX5@305fjZf%>eAQna#>EqX8?YDPcz*4UNef}@i%=$EJ01Cn=BpyBZ!yFav&0z zMhFdXa~6;Wz_|DkEV47Pmek%*#Yi{X19w-njsOxQt%Tkh%o`0000l*+UK