Skip to content

Commit

Permalink
Port fabric-item-api-v1
Browse files Browse the repository at this point in the history
  • Loading branch information
Su5eD committed Jun 23, 2024
1 parent c21168c commit 9b12244
Show file tree
Hide file tree
Showing 22 changed files with 150 additions and 457 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.fabricmc.fabric.impl.client.item;

import net.fabricmc.fabric.api.client.item.v1.ItemTooltipCallback;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.event.entity.player.ItemTooltipEvent;

@EventBusSubscriber
public class ClientItemEventHooks {

@SubscribeEvent
public static void onItemTooltip(ItemTooltipEvent event) {
ItemTooltipCallback.EVENT.invoker().getTooltip(event.getItemStack(), event.getContext(), event.getFlags(), event.getToolTip());
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.fabricmc.fabric.mixin.item.client;

import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import net.fabricmc.fabric.api.item.v1.FabricItem;
import net.fabricmc.fabric.impl.item.RecursivityHelper;
import net.minecraft.client.Minecraft;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.neoforged.neoforge.common.extensions.IItemExtension;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

@Mixin(IItemExtension.class)
public interface IItemExtensionClientMixin {

@ModifyReturnValue(method = "shouldCauseReequipAnimation", at = @At("RETURN"))
default boolean shouldCauseReequipAnimation(boolean result, ItemStack oldStack, ItemStack newStack, boolean slotChanged) {
if (result) {
Player player = Minecraft.getInstance().player;
InteractionHand hand = oldStack == player.getMainHandItem() ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND;
return RecursivityHelper.nonRecursiveApiCall(() -> ((FabricItem) this).allowComponentsUpdateAnimation(player, hand, oldStack, newStack));
}
return false;
}

@Inject(method = "shouldCauseBlockBreakReset", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/ItemStack;isDamageableItem()Z"), cancellable = true)
default void shouldCauseBlockBreakReset(ItemStack oldStack, ItemStack newStack, CallbackInfoReturnable<Boolean> cir) {
if (!ItemStack.isSameItemSameComponents(newStack, oldStack) && oldStack.getItem().allowContinuingBlockBreaking(Minecraft.getInstance().player, oldStack, newStack)) {
cir.setReturnValue(false);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
"package": "net.fabricmc.fabric.mixin.item.client",
"compatibilityLevel": "JAVA_17",
"client": [
"ClientPlayerInteractionManagerMixin",
"HeldItemRendererMixin",
"ItemStackMixin"
"IItemExtensionClientMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
package net.fabricmc.fabric.api.item.v1;

import net.fabricmc.fabric.impl.item.FabricItemInternals;
import net.fabricmc.fabric.impl.item.RecursivityHelper;
import net.minecraft.core.Holder;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.Enchantment;
import net.neoforged.neoforge.common.extensions.IItemExtension;

/**
* General-purpose Fabric-provided extensions for {@link Item} subclasses.
Expand All @@ -45,7 +47,7 @@ public interface FabricItem {
* @return true to run the vanilla animation, false to cancel it.
*/
default boolean allowComponentsUpdateAnimation(Player player, InteractionHand hand, ItemStack oldStack, ItemStack newStack) {
return true;
return !RecursivityHelper.allowForgeCall() || ((IItemExtension) this).shouldCauseReequipAnimation(oldStack, newStack, false);
}

/**
Expand Down Expand Up @@ -91,7 +93,7 @@ default boolean allowContinuingBlockBreaking(Player player, ItemStack oldStack,
* @return the leftover item stack
*/
default ItemStack getRecipeRemainder(ItemStack stack) {
return ((Item) this).hasCraftingRemainingItem() ? ((Item) this).getCraftingRemainingItem().getDefaultInstance() : ItemStack.EMPTY;
return RecursivityHelper.allowForgeCall() ? ((IItemExtension) this).getCraftingRemainingItem(stack) : ItemStack.EMPTY;
}

/**
Expand All @@ -112,7 +114,7 @@ default ItemStack getRecipeRemainder(ItemStack stack) {
*/
default boolean canBeEnchantedWith(ItemStack stack, Holder<Enchantment> enchantment, EnchantingContext context) {
return context == EnchantingContext.PRIMARY
? enchantment.value().isPrimaryItem(stack)
? stack.isPrimaryItemFor(enchantment)
: enchantment.value().canEnchant(stack);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.Enchantment;
import net.neoforged.neoforge.common.extensions.IItemStackExtension;

/*
* Fabric-provided extensions for {@link ItemStack}.
* This interface is automatically implemented on all item stacks via Mixin and interface injection.
*/
public interface FabricItemStack {
public interface FabricItemStack extends IItemStackExtension {
/**
* Return a leftover item for use in recipes.
*
Expand All @@ -37,7 +38,7 @@ public interface FabricItemStack {
* @return the leftover item
*/
default ItemStack getRecipeRemainder() {
return ((ItemStack) this).getItem().getRecipeRemainder((ItemStack) this);
return IItemStackExtension.super.getCraftingRemainingItem();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@
import net.minecraft.core.component.DataComponentMap;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.item.Item;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.common.EventBusSubscriber;
import net.neoforged.neoforge.event.ModifyDefaultComponentsEvent;
import org.sinytra.fabric.item_api.generated.GeneratedEntryPoint;

@EventBusSubscriber(bus = EventBusSubscriber.Bus.MOD, modid = GeneratedEntryPoint.MOD_ID)
public class DefaultItemComponentImpl {
public static void modifyItemComponents() {

@SubscribeEvent
public static void modifyItemComponents(ModifyDefaultComponentsEvent event) {
DefaultItemComponentEvents.MODIFY.invoker().modify(ModifyContextImpl.INSTANCE);
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package net.fabricmc.fabric.impl.item;

import java.util.function.Supplier;

public final class RecursivityHelper {
public static final ThreadLocal<Boolean> FORGE_CALL = ThreadLocal.withInitial(() -> false);

public static <T> T nonRecursiveApiCall(Supplier<T> supplier) {
FORGE_CALL.set(true);
T result = supplier.get();
FORGE_CALL.set(false);
return result;
}

public static boolean allowForgeCall() {
return !FORGE_CALL.get();
}

private RecursivityHelper() {
}
}
Loading

0 comments on commit 9b12244

Please sign in to comment.