Skip to content

Commit

Permalink
fix: Link trinkets to player, so it can be used in powers
Browse files Browse the repository at this point in the history
  • Loading branch information
shap-po committed Aug 29, 2024
1 parent ab3a422 commit ad6500a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.shap_po.shappoli.mixin.integration.trinkets;

import com.github.shap_po.shappoli.integration.trinkets.power.ActionOnTrinketUpdatePower;
import com.github.shap_po.shappoli.util.InventoryUtil;
import dev.emi.trinkets.api.SlotType;
import dev.emi.trinkets.api.TrinketInventory;
import dev.emi.trinkets.api.TrinketsApi;
Expand Down Expand Up @@ -46,6 +47,8 @@ private void tick(CallbackInfo info) {
ItemStack newStack = inventory.getStack(index);
ItemStack newStackCopy = newStack.copy(); // old stack is removed when unequipped

InventoryUtil.setHolder(newStack, entity); // link the stack to the player, so it can be used in powers

if (!ItemStack.areEqual(newStack, oldStack)) {
// Call unequip powers on old trinket
PowerHolderComponent.withPower(entity, ActionOnTrinketUpdatePower.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.github.shap_po.shappoli.power.ActionOnEventReceivePower;
import com.github.shap_po.shappoli.power.factory.action.meta.SendEventAction;
import io.github.apace100.apoli.access.EntityLinkedItemStack;
import com.github.shap_po.shappoli.util.InventoryUtil;
import io.github.apace100.apoli.power.factory.action.ActionFactory;
import io.github.apace100.apoli.registry.ApoliRegistries;
import net.minecraft.inventory.StackReference;
Expand All @@ -14,7 +14,7 @@ public class ItemActions {
public static void register() {
register(
SendEventAction.getFactory(
worldAndStack -> ((EntityLinkedItemStack) (Object) worldAndStack.getRight().get()).apoli$getEntity(),
worldAndStack -> InventoryUtil.getHolder(worldAndStack.getRight().get()),
ActionOnEventReceivePower::receiveItemEvent
)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.shap_po.shappoli.power.factory.condition.item;

import com.github.shap_po.shappoli.Shappoli;
import io.github.apace100.apoli.access.EntityLinkedItemStack;
import com.github.shap_po.shappoli.util.InventoryUtil;
import io.github.apace100.apoli.data.ApoliDataTypes;
import io.github.apace100.apoli.power.factory.condition.ConditionFactory;
import io.github.apace100.apoli.power.factory.condition.ItemConditions;
Expand All @@ -15,7 +15,7 @@

public class HolderCondition {
public static boolean condition(SerializableData.Instance data, Pair<World, ItemStack> worldAndStack) {
Entity holder = ((EntityLinkedItemStack) (Object) worldAndStack.getRight()).apoli$getEntity();
Entity holder = InventoryUtil.getHolder(worldAndStack.getRight());
Predicate<Entity> entityCondition = data.get("condition");
return holder != null && entityCondition != null && entityCondition.test(holder);
}
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/github/shap_po/shappoli/util/InventoryUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.github.shap_po.shappoli.util;

import io.github.apace100.apoli.access.EntityLinkedItemStack;
import net.minecraft.entity.Entity;
import net.minecraft.item.ItemStack;

@SuppressWarnings({"UnusedReturnValue"})
public class InventoryUtil {
@SuppressWarnings({"ConstantConditions"})
public static Entity getHolder(ItemStack stack) {
return ((EntityLinkedItemStack) (Object) stack).apoli$getEntity();
}

@SuppressWarnings({"ConstantConditions"})
public static ItemStack setHolder(ItemStack stack, Entity holder) {
((EntityLinkedItemStack) (Object) stack).apoli$setEntity(holder);
return stack;
}
}

0 comments on commit ad6500a

Please sign in to comment.