Skip to content

Commit

Permalink
check and test both passed
Browse files Browse the repository at this point in the history
  • Loading branch information
Phoupraw committed Oct 21, 2023
1 parent 261c821 commit 4d6a44a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@
import org.spongepowered.asm.mixin.injection.Redirect;

import net.minecraft.data.server.recipe.CookingRecipeJsonBuilder;
import net.minecraft.item.BlockItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemUsageContext;

@Mixin(CookingRecipeJsonBuilder.class)
class CookingRecipeJsonBuilderMixin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class HungerManagerMixin {
return stack.getFoodComponent();
}

@Redirect(method = "eat", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/Item;isFood()Z",ordinal = 0))
@Redirect(method = "eat", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/Item;isFood()Z"))
private boolean isStackAwareFood(Item instance, Item item, ItemStack stack) {
return stack.isFood();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,4 @@ public boolean hookIsSuitableFor(Item item, BlockState state) {
public void isStackAwareFood(CallbackInfoReturnable<Boolean> cir) {
cir.setReturnValue(this.getFoodComponent() != null);
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.test.item.gametest;

import java.util.Objects;
Expand All @@ -7,6 +23,7 @@
import net.minecraft.entity.EntityType;
import net.minecraft.entity.passive.WolfEntity;
import net.minecraft.entity.player.HungerManager;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.FoodComponent;
import net.minecraft.item.FoodComponents;
import net.minecraft.item.Item;
Expand All @@ -30,8 +47,9 @@ public final class FoodGameTest implements FabricGameTest, ModInitializer {

@GameTest(templateName = EMPTY_STRUCTURE)
public void damageFoodTest(TestContext context) {
var player = context.createMockSurvivalPlayer();
PlayerEntity player = context.createMockSurvivalPlayer();
HungerManager hungerManager = player.getHungerManager();

for (int damage : new int[]{0, 1, 10, 19}) {
hungerManager.add(-hungerManager.getFoodLevel(), 0.5f);
ItemStack foodStack = DAMAGE.getDefaultStack();
Expand All @@ -45,12 +63,13 @@ public void damageFoodTest(TestContext context) {
float satExpect = Math.min(foodExpect, fc.getHunger() * fc.getSaturationModifier() * 2);
context.assertTrue(satActual == satExpect, "damage=%d, sat actual %f, expect %f".formatted(damage, satActual, satExpect));
}

context.complete();
}

@GameTest(templateName = EMPTY_STRUCTURE)
public void nameFoodTest(TestContext context) {
var player = context.createMockSurvivalPlayer();
PlayerEntity player = context.createMockSurvivalPlayer();
HungerManager hungerManager = player.getHungerManager();
hungerManager.add(-hungerManager.getFoodLevel(), 0.5f);
ItemStack foodStack = NAME.getDefaultStack();
Expand All @@ -68,7 +87,7 @@ public void nameFoodTest(TestContext context) {

@GameTest(templateName = EMPTY_STRUCTURE)
public void nameMeatTest(TestContext context) {
var player = context.createMockSurvivalPlayer();
PlayerEntity player = context.createMockSurvivalPlayer();
WolfEntity wolf = context.spawnEntity(EntityType.WOLF, context.getRelative(Vec3d.ZERO));
wolf.setTamed(true);
wolf.setOwner(player);
Expand All @@ -84,12 +103,14 @@ public void nameMeatTest(TestContext context) {

//@GameTest(templateName = EMPTY_STRUCTURE)
public void giveItems(TestContext context) {
var player = context.getWorld().getPlayers().get(0);
PlayerEntity player = context.getWorld().getPlayers().get(0);

for (int damage : new int[]{0, 1, 10, 19}) {
ItemStack foodStack = DAMAGE.getDefaultStack();
foodStack.setDamage(damage);
player.getInventory().offerOrDrop(foodStack);
}

ItemStack apple = NAME.getDefaultStack();
apple.setCustomName(Text.literal("enchanted_golden_apple"));
player.getInventory().offerOrDrop(apple);
Expand All @@ -101,11 +122,9 @@ public void giveItems(TestContext context) {

@Override
public void onInitialize() {

}

public static class DamageFood extends Item {

public DamageFood(Settings settings) {
super(settings);
}
Expand All @@ -120,7 +139,6 @@ public DamageFood(Settings settings) {
}

public static class NameFood extends Item {

public NameFood(Settings settings) {
super(settings);
}
Expand Down

0 comments on commit 4d6a44a

Please sign in to comment.