Skip to content

Commit

Permalink
check passed
Browse files Browse the repository at this point in the history
  • Loading branch information
Phoupraw committed Oct 21, 2023
1 parent 4d6a44a commit 3e2388d
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
Expand All @@ -34,17 +33,19 @@

@Mixin(WolfEntity.class)
class WolfEntityMixin {
@Unique
private ItemStack fabric_interactMob_itemStack = ItemStack.EMPTY;
@SuppressWarnings("MissingUnique")
private static final ThreadLocal<ItemStack> fabric_interactMob_itemStack = new ThreadLocal<>();

@Inject(method = "interactMob", at = @At("HEAD"))
private void storeCopy(PlayerEntity player, Hand hand, CallbackInfoReturnable<ActionResult> cir) {
fabric_interactMob_itemStack = player.getStackInHand(hand).copy();
fabric_interactMob_itemStack.set(player.getStackInHand(hand).copy());
}

@Redirect(method = "interactMob", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/Item;getFoodComponent()Lnet/minecraft/item/FoodComponent;"))
private @Nullable FoodComponent getStackAwareFoodComponent(Item instance, PlayerEntity player, Hand hand) {
return fabric_interactMob_itemStack.getFoodComponent();
FoodComponent fc = fabric_interactMob_itemStack.get().getFoodComponent();
fabric_interactMob_itemStack.remove();
return fc;
}

@Redirect(method = "isBreedingItem", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/Item;getFoodComponent()Lnet/minecraft/item/FoodComponent;"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

package net.fabricmc.fabric.test.item;

import org.jetbrains.annotations.Nullable;

import net.minecraft.item.FoodComponent;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.PickaxeItem;
Expand Down Expand Up @@ -79,13 +76,5 @@ public ItemStack getRecipeRemainder(ItemStack stack) {

return ItemStack.EMPTY;
}

@Override
public @Nullable FoodComponent getFoodComponent(ItemStack stack) {
return new FoodComponent.Builder()
.hunger(20 - 20 * stack.getDamage() / stack.getMaxDamage())
.saturationModifier(0.5f)
.build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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;

import org.jetbrains.annotations.Nullable;

import net.minecraft.item.FoodComponent;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.util.Identifier;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;

public final class FoodGameInitializer implements ModInitializer {
public static final Item DAMAGE = Registry.register(Registries.ITEM, new Identifier("fabric-item-api-v1-testmod", "damage_food"), new DamageFood(new FabricItemSettings().maxDamage(20)));
public static final Item NAME = Registry.register(Registries.ITEM, new Identifier("fabric-item-api-v1-testmod", "name_food"), new NameFood(new FabricItemSettings()));

@Override
public void onInitialize() {
}

public static class DamageFood extends Item {
public DamageFood(Settings settings) {
super(settings);
}

@Override
public @Nullable FoodComponent getFoodComponent(ItemStack stack) {
return new FoodComponent.Builder()
.hunger(20 - 20 * stack.getDamage() / stack.getMaxDamage())
.saturationModifier(0.5f)
.build();
}
}

public static class NameFood extends Item {
public NameFood(Settings settings) {
super(settings);
}

@Override
public @Nullable FoodComponent getFoodComponent(ItemStack stack) {
return Registries.ITEM.get(new Identifier(stack.getName().getString())).getFoodComponent(stack);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,31 @@

import java.util.Objects;

import org.jetbrains.annotations.Nullable;

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;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.test.GameTest;
import net.minecraft.test.TestContext;
import net.minecraft.text.Text;
import net.minecraft.util.Hand;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Vec3d;

import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.gametest.v1.FabricGameTest;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;

public final class FoodGameTest implements FabricGameTest, ModInitializer {
public static final Item DAMAGE = Registry.register(Registries.ITEM, new Identifier("fabric-item-api-v1-testmod", "damage_food"), new DamageFood(new FabricItemSettings().maxDamage(20)));
public static final Item NAME = Registry.register(Registries.ITEM, new Identifier("fabric-item-api-v1-testmod", "name_food"), new NameFood(new FabricItemSettings()));
import net.fabricmc.fabric.test.item.FoodGameInitializer;

public final class FoodGameTest implements FabricGameTest {
@GameTest(templateName = EMPTY_STRUCTURE)
public void damageFoodTest(TestContext context) {
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();
ItemStack foodStack = FoodGameInitializer.DAMAGE.getDefaultStack();
foodStack.setDamage(damage);
player.eatFood(player.getWorld(), foodStack.copy());
FoodComponent fc = Objects.requireNonNull(foodStack.getFoodComponent());
Expand All @@ -72,7 +62,7 @@ public void nameFoodTest(TestContext context) {
PlayerEntity player = context.createMockSurvivalPlayer();
HungerManager hungerManager = player.getHungerManager();
hungerManager.add(-hungerManager.getFoodLevel(), 0.5f);
ItemStack foodStack = NAME.getDefaultStack();
ItemStack foodStack = FoodGameInitializer.NAME.getDefaultStack();
foodStack.setCustomName(Text.literal("enchanted_golden_apple"));
player.eatFood(player.getWorld(), foodStack.copy());
FoodComponent fc = FoodComponents.ENCHANTED_GOLDEN_APPLE;
Expand All @@ -92,60 +82,12 @@ public void nameMeatTest(TestContext context) {
wolf.setTamed(true);
wolf.setOwner(player);
wolf.setHealth(1f);
ItemStack meat = NAME.getDefaultStack();
ItemStack meat = FoodGameInitializer.NAME.getDefaultStack();
meat.setCustomName(Text.of("mutton"));
player.setStackInHand(Hand.MAIN_HAND, meat);
player.interact(wolf, Hand.MAIN_HAND);
float wolfHealth = wolf.getHealth();
context.assertTrue(wolfHealth > 1, "actual %f, expect > 0".formatted(wolfHealth));
context.complete();
}

//@GameTest(templateName = EMPTY_STRUCTURE)
public void giveItems(TestContext context) {
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);
ItemStack mutton = NAME.getDefaultStack();
mutton.setCustomName(Text.of("mutton"));
player.getInventory().offerOrDrop(mutton);
context.complete();
}

@Override
public void onInitialize() {
}

public static class DamageFood extends Item {
public DamageFood(Settings settings) {
super(settings);
}

@Override
public @Nullable FoodComponent getFoodComponent(ItemStack stack) {
return new FoodComponent.Builder()
.hunger(20 - 20 * stack.getDamage() / stack.getMaxDamage())
.saturationModifier(0.5f)
.build();
}
}

public static class NameFood extends Item {
public NameFood(Settings settings) {
super(settings);
}

@Override
public @Nullable FoodComponent getFoodComponent(ItemStack stack) {
return Registries.ITEM.get(new Identifier(stack.getName().getString())).getFoodComponent(stack);
}
}
}
3 changes: 2 additions & 1 deletion fabric-item-api-v1/src/testmod/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"net.fabricmc.fabric.test.item.FabricItemSettingsTests",
"net.fabricmc.fabric.test.item.ItemUpdateAnimationTest",
"net.fabricmc.fabric.test.item.ModifyItemAttributeModifiersCallbackTest",
"net.fabricmc.fabric.test.item.ArmorKnockbackResistanceTest"
"net.fabricmc.fabric.test.item.ArmorKnockbackResistanceTest",
"net.fabricmc.fabric.test.item.FoodGameInitializer"
],
"client": [
"net.fabricmc.fabric.test.item.client.TooltipTests"
Expand Down

0 comments on commit 3e2388d

Please sign in to comment.