-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+Fixed display bug that potentially allows free skill points. +Added two events: ItemStack#Constructor and ItemStack#getAttributeModifiers which allows more freedom to use NBT-based dynamic attributes.
- Loading branch information
1 parent
5a91ed8
commit a46e185
Showing
8 changed files
with
108 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/main/java/com/github/clevernucleus/playerex/api/event/ItemStackEvents.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.github.clevernucleus.playerex.api.event; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
|
||
import com.google.common.collect.Multimap; | ||
|
||
import net.fabricmc.fabric.api.event.Event; | ||
import net.fabricmc.fabric.api.event.EventFactory; | ||
import net.minecraft.entity.EquipmentSlot; | ||
import net.minecraft.entity.attribute.EntityAttribute; | ||
import net.minecraft.entity.attribute.EntityAttributeModifier; | ||
import net.minecraft.item.Item; | ||
import net.minecraft.item.ItemStack; | ||
|
||
/** | ||
* Provides a hook into the ItemStack constructor and ItemStack#getAttributeModifiers. | ||
* | ||
* @author CleverNucleus | ||
* | ||
*/ | ||
public final class ItemStackEvents { | ||
|
||
/** | ||
* Fired at the end of the itemstack constructor. The item may be null. | ||
*/ | ||
public static final Event<ItemStackEvents.Constructor> ITEMSTACK_CONSTRUCTED = EventFactory.createArrayBacked(ItemStackEvents.Constructor.class, listeners -> (item, itemStack, count) -> { | ||
for(Constructor listener : listeners) { | ||
listener.onItemStackConstructor(item, itemStack, count); | ||
} | ||
}); | ||
|
||
/** | ||
* Fired on {@link ItemStack#getAttributeModifiers(EquipmentSlot)}. Allows for editing the modifiers multimap with ItemStack (i.e. NBT) arguments. | ||
*/ | ||
public static final Event<ItemStackEvents.GetAttributeModifiers> ITEMSTACK_MODIFIERS = EventFactory.createArrayBacked(ItemStackEvents.GetAttributeModifiers.class, listeners -> (itemStack, modifiers, slot) -> { | ||
for(GetAttributeModifiers listener : listeners) { | ||
listener.getAttributeModifiers(itemStack, modifiers, slot); | ||
} | ||
}); | ||
|
||
@FunctionalInterface | ||
public interface Constructor { | ||
|
||
/** | ||
* | ||
* @param item | ||
* @param itemStack | ||
* @param count | ||
*/ | ||
void onItemStackConstructor(@Nullable final Item item, final ItemStack itemStack, final int count); | ||
} | ||
|
||
@FunctionalInterface | ||
public interface GetAttributeModifiers { | ||
|
||
/** | ||
* | ||
* @param itemStack | ||
* @param modifiers | ||
* @param slot | ||
*/ | ||
void getAttributeModifiers(final ItemStack itemStack, final Multimap<EntityAttribute, EntityAttributeModifier> modifiers, final EquipmentSlot slot); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/java/com/github/clevernucleus/playerex/mixin/ItemStackMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.github.clevernucleus.playerex.mixin; | ||
|
||
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.ModifyVariable; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
import com.github.clevernucleus.playerex.api.event.ItemStackEvents; | ||
import com.google.common.collect.Multimap; | ||
|
||
import net.minecraft.entity.EquipmentSlot; | ||
import net.minecraft.entity.attribute.EntityAttribute; | ||
import net.minecraft.entity.attribute.EntityAttributeModifier; | ||
import net.minecraft.item.Item; | ||
import net.minecraft.item.ItemConvertible; | ||
import net.minecraft.item.ItemStack; | ||
|
||
@Mixin(ItemStack.class) | ||
abstract class ItemStackMixin { | ||
|
||
@Inject(method = "<init>(Lnet/minecraft/item/ItemConvertible;I)V", at = @At("TAIL")) | ||
private void constructorCall(ItemConvertible item, int count, CallbackInfo info) { | ||
Item itemIn = item == null ? null : item.asItem(); | ||
ItemStack stack = (ItemStack)(Object)this; | ||
|
||
ItemStackEvents.ITEMSTACK_CONSTRUCTED.invoker().onItemStackConstructor(itemIn, stack, count); | ||
} | ||
|
||
@ModifyVariable(method = "getAttributeModifiers", at = @At(value = "STORE", ordinal = 1), ordinal = 0) | ||
private Multimap<EntityAttribute, EntityAttributeModifier> modifyItemStackModifiers(Multimap<EntityAttribute, EntityAttributeModifier> original, EquipmentSlot slot) { | ||
ItemStack stack = (ItemStack)(Object)this; | ||
ItemStackEvents.ITEMSTACK_MODIFIERS.invoker().getAttributeModifiers(stack, original, slot); | ||
|
||
return original; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters