diff --git a/fabric-item-api-v1/src/main/java/net/fabricmc/fabric/mixin/item/WolfEntityMixin.java b/fabric-item-api-v1/src/main/java/net/fabricmc/fabric/mixin/item/WolfEntityMixin.java index 8555d40665..1d39d52e60 100644 --- a/fabric-item-api-v1/src/main/java/net/fabricmc/fabric/mixin/item/WolfEntityMixin.java +++ b/fabric-item-api-v1/src/main/java/net/fabricmc/fabric/mixin/item/WolfEntityMixin.java @@ -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; @@ -34,17 +33,19 @@ @Mixin(WolfEntity.class) class WolfEntityMixin { - @Unique - private ItemStack fabric_interactMob_itemStack = ItemStack.EMPTY; + @SuppressWarnings("MissingUnique") + private static final ThreadLocal fabric_interactMob_itemStack = new ThreadLocal<>(); @Inject(method = "interactMob", at = @At("HEAD")) private void storeCopy(PlayerEntity player, Hand hand, CallbackInfoReturnable 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;")) diff --git a/fabric-item-api-v1/src/testmod/java/net/fabricmc/fabric/test/item/CustomDamageTest.java b/fabric-item-api-v1/src/testmod/java/net/fabricmc/fabric/test/item/CustomDamageTest.java index ce26f4695d..fcd6768ee9 100644 --- a/fabric-item-api-v1/src/testmod/java/net/fabricmc/fabric/test/item/CustomDamageTest.java +++ b/fabric-item-api-v1/src/testmod/java/net/fabricmc/fabric/test/item/CustomDamageTest.java @@ -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; @@ -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(); - } } } diff --git a/fabric-item-api-v1/src/testmod/java/net/fabricmc/fabric/test/item/FoodGameInitializer.java b/fabric-item-api-v1/src/testmod/java/net/fabricmc/fabric/test/item/FoodGameInitializer.java new file mode 100644 index 0000000000..4d58c04dec --- /dev/null +++ b/fabric-item-api-v1/src/testmod/java/net/fabricmc/fabric/test/item/FoodGameInitializer.java @@ -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); + } + } +} diff --git a/fabric-item-api-v1/src/testmod/java/net/fabricmc/fabric/test/item/gametest/FoodGameTest.java b/fabric-item-api-v1/src/testmod/java/net/fabricmc/fabric/test/item/gametest/FoodGameTest.java index 592ea88973..1eb04718a9 100644 --- a/fabric-item-api-v1/src/testmod/java/net/fabricmc/fabric/test/item/gametest/FoodGameTest.java +++ b/fabric-item-api-v1/src/testmod/java/net/fabricmc/fabric/test/item/gametest/FoodGameTest.java @@ -18,33 +18,23 @@ 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(); @@ -52,7 +42,7 @@ public void damageFoodTest(TestContext context) { 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()); @@ -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; @@ -92,7 +82,7 @@ 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); @@ -100,52 +90,4 @@ public void nameMeatTest(TestContext context) { 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); - } - } } diff --git a/fabric-item-api-v1/src/testmod/resources/fabric.mod.json b/fabric-item-api-v1/src/testmod/resources/fabric.mod.json index 63b22fd4df..627b046066 100644 --- a/fabric-item-api-v1/src/testmod/resources/fabric.mod.json +++ b/fabric-item-api-v1/src/testmod/resources/fabric.mod.json @@ -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"