Skip to content

Commit

Permalink
Two-way item api compat
Browse files Browse the repository at this point in the history
Closes #7
  • Loading branch information
Su5eD committed Aug 2, 2023
1 parent bad229b commit dc14f3a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;

import net.fabricmc.fabric.impl.item.FabricItemInternals;

import net.minecraft.entity.LivingEntity;

import net.minecraftforge.common.extensions.IForgeItem;
Expand Down Expand Up @@ -60,7 +62,7 @@ public interface FabricItem extends IForgeItem {
* @return true to run the vanilla animation, false to cancel it.
*/
default boolean allowNbtUpdateAnimation(PlayerEntity player, Hand hand, ItemStack oldStack, ItemStack newStack) {
return true;
return !FabricItemInternals.allowForgeCall() || shouldCauseReequipAnimation(oldStack, newStack, false);
}

/**
Expand All @@ -74,7 +76,7 @@ default boolean allowNbtUpdateAnimation(PlayerEntity player, Hand hand, ItemStac
* @return true to allow continuing block breaking, false to reset the progress.
*/
default boolean allowContinuingBlockBreaking(PlayerEntity player, ItemStack oldStack, ItemStack newStack) {
return false;
return FabricItemInternals.allowForgeCall() && shouldCauseBlockBreakReset(oldStack, newStack);
}

/**
Expand All @@ -88,6 +90,9 @@ default boolean allowContinuingBlockBreaking(PlayerEntity player, ItemStack oldS
* @return the attribute modifiers
*/
default Multimap<EntityAttribute, EntityAttributeModifier> getAttributeModifiers(ItemStack stack, EquipmentSlot slot) {
if (FabricItemInternals.allowForgeCall()) {
return getAttributeModifiers(slot, stack);
}
return HashMultimap.create();
}

Expand All @@ -100,7 +105,7 @@ default Multimap<EntityAttribute, EntityAttributeModifier> getAttributeModifiers
* @return true if drops can be harvested
*/
default boolean isSuitableFor(ItemStack stack, BlockState state) {
return false;
return FabricItemInternals.allowForgeCall() && isCorrectToolForDrops(stack, state);
}

/**
Expand Down Expand Up @@ -133,14 +138,17 @@ default boolean isSuitableFor(ItemStack stack, BlockState state) {
*/
@ApiStatus.OverrideOnly
default ItemStack getRecipeRemainder(ItemStack stack) {
if (FabricItemInternals.allowForgeCall()) {
return getCraftingRemainingItem(stack);
}
return ItemStack.EMPTY;
}

// FFAPI: Forge default implementation

@Override
default ItemStack getCraftingRemainingItem(ItemStack stack) {
ItemStack fabricRemainder = getRecipeRemainder(stack);
ItemStack fabricRemainder = FabricItemInternals.nonRecursiveApiCall(() -> getRecipeRemainder(stack));
if (!fabricRemainder.isEmpty()) {
return fabricRemainder;
}
Expand All @@ -149,36 +157,36 @@ default ItemStack getCraftingRemainingItem(ItemStack stack) {

@Override
default boolean hasCraftingRemainingItem(ItemStack stack) {
return !getRecipeRemainder(stack).isEmpty() || IForgeItem.super.hasCraftingRemainingItem(stack);
return !FabricItemInternals.nonRecursiveApiCall(() -> getRecipeRemainder(stack)).isEmpty() || IForgeItem.super.hasCraftingRemainingItem(stack);
}

@Override
default Multimap<EntityAttribute, EntityAttributeModifier> getAttributeModifiers(EquipmentSlot slot, ItemStack stack) {
// Fetch forge attribute modifiers first
Multimap<EntityAttribute, EntityAttributeModifier> modifiers = HashMultimap.create(IForgeItem.super.getAttributeModifiers(slot, stack));
// Add all fabric attribute modifiers
modifiers.putAll(getAttributeModifiers(stack, slot));
modifiers.putAll(FabricItemInternals.nonRecursiveApiCall(() -> getAttributeModifiers(stack, slot)));
return modifiers;
}

@Override
default boolean isCorrectToolForDrops(ItemStack stack, BlockState state) {
return isSuitableFor(stack, state) || IForgeItem.super.isCorrectToolForDrops(stack, state);
return FabricItemInternals.nonRecursiveApiCall(() -> isSuitableFor(stack, state)) || IForgeItem.super.isCorrectToolForDrops(stack, state);
}

@Override
default boolean shouldCauseReequipAnimation(ItemStack oldStack, ItemStack newStack, boolean slotChanged) {
if (IForgeItem.super.shouldCauseReequipAnimation(oldStack, newStack, slotChanged)) {
PlayerEntity player = ItemApiClientEventHooks.getClientPlayerSafely();
Hand hand = oldStack == player.getMainHandStack() ? Hand.MAIN_HAND : Hand.OFF_HAND;
return allowNbtUpdateAnimation(player, hand, oldStack, newStack);
return FabricItemInternals.nonRecursiveApiCall(() -> allowNbtUpdateAnimation(player, hand, oldStack, newStack));
}
return false;
}

@Override
default boolean shouldCauseBlockBreakReset(ItemStack oldStack, ItemStack newStack) {
return IForgeItem.super.shouldCauseBlockBreakReset(oldStack, newStack) && !allowContinuingBlockBreaking(ItemApiClientEventHooks.getClientPlayerSafely(), oldStack, newStack);
return IForgeItem.super.shouldCauseBlockBreakReset(oldStack, newStack) && FabricItemInternals.nonRecursiveApiCall(() -> !allowContinuingBlockBreaking(ItemApiClientEventHooks.getClientPlayerSafely(), oldStack, newStack));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package net.fabricmc.fabric.impl.item;

import java.util.WeakHashMap;
import java.util.function.Supplier;

import org.jetbrains.annotations.Nullable;

Expand All @@ -26,6 +27,7 @@
import net.fabricmc.fabric.api.item.v1.EquipmentSlotProvider;

public final class FabricItemInternals {
public static final ThreadLocal<Boolean> FORGE_CALL = ThreadLocal.withInitial(() -> false);
private static final WeakHashMap<Item.Settings, ExtraData> extraData = new WeakHashMap<>();

private FabricItemInternals() {
Expand All @@ -44,6 +46,17 @@ public static void onBuild(Item.Settings settings, Item item) {
}
}

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();
}

public static final class ExtraData {
private @Nullable EquipmentSlotProvider equipmentSlotProvider;
private @Nullable CustomDamageHandler customDamageHandler;
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ fabric-client-tags-api-v1-version=1.1.1
loom.platform=forge
forge_version=1.20.1-47.1.3
pack_format=15
forgified_version=1.7.0
forgified_version=1.7.1
forge_fabric_loader_version=2.2.1+0.14.21+1.20.1

0 comments on commit dc14f3a

Please sign in to comment.