Skip to content

Commit

Permalink
Fixed Bugs and Added Events
Browse files Browse the repository at this point in the history
+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
CleverNucleus committed Oct 22, 2021
1 parent 5a91ed8 commit a46e185
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 6 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ dependencies {
modImplementation "io.github.onyxstudios.Cardinal-Components-API:cardinal-components-level:${project.cardinal_components_version}"
include "io.github.onyxstudios.Cardinal-Components-API:cardinal-components-level:${project.cardinal_components_version}"

modImplementation "curse.maven:dataattributes-514734:3462727"
modImplementation "curse.maven:dataattributes-514734:3484768"

modImplementation("me.shedaniel.cloth:cloth-config-fabric:${project.cloth_config_version}") {
exclude(group: "net.fabricmc.fabric-api")
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.11.6

# Mod Properties
mod_version = 3.0.5
mod_version = 3.0.6
maven_group = com.github.clevernucleus
archives_base_name = playerex

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
public final class PlayerEx implements ModInitializer {
public static final ModifierJsonLoader MANAGER = new ModifierJsonLoader();
/** Manual; ugh, I know. */
public static final String VERSION = "3.0.5";
public static final String VERSION = "3.0.6";
public static final ConfigCache CONFIG = new ConfigCache();
public static final SoundEvent LEVEL_UP_SOUND = new SoundEvent(new Identifier(ExAPI.MODID, "level_up"));
public static final SoundEvent SP_SPEND_SOUND = new SoundEvent(new Identifier(ExAPI.MODID, "sp_spend"));
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ public void drawBackground(MatrixStack matrices, float delta, int mouseX, int mo
} else {
EntityAttribute attribute = API.getAttribute(key).get();
IAttribute instance = (IAttribute)attribute;
double value = player.getAttributeValue(attribute);
double value = this.modifierData.get(attribute);

if(this.canRefund()) {
button.active = value >= 1.0D;
Expand Down
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;
}
}
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@
"fabric": ">=0.40.1",
"minecraft": "1.17.x",
"java": ">=16",
"dataattributes": ">=1.0.4"
"dataattributes": ">=1.0.6"
}
}
3 changes: 2 additions & 1 deletion src/main/resources/playerex.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"LivingEntityMixin",
"PlayerEntityMixin",
"ProjectileEntityMixin",
"EntityAttributeInstanceMixin"
"EntityAttributeInstanceMixin",
"ItemStackMixin"
],
"client": [
"client.HandledScreenMixin",
Expand Down

0 comments on commit a46e185

Please sign in to comment.