diff --git a/src/main/java/io/wispforest/owo/itemgroup/OwoItemSettings.java b/src/main/java/io/wispforest/owo/itemgroup/OwoItemSettings.java index 95b4c95a..6b7e49f1 100644 --- a/src/main/java/io/wispforest/owo/itemgroup/OwoItemSettings.java +++ b/src/main/java/io/wispforest/owo/itemgroup/OwoItemSettings.java @@ -3,10 +3,13 @@ import net.fabricmc.fabric.api.item.v1.CustomDamageHandler; import net.fabricmc.fabric.api.item.v1.EquipmentSlotProvider; import net.fabricmc.fabric.api.item.v1.FabricItemSettings; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.FoodComponent; import net.minecraft.item.Item; import net.minecraft.item.ItemGroup; +import net.minecraft.util.Hand; import net.minecraft.util.Rarity; +import net.minecraft.world.World; import org.jetbrains.annotations.Nullable; import java.util.function.BiConsumer; @@ -17,6 +20,7 @@ public class OwoItemSettings extends FabricItemSettings { private OwoItemGroup group = null; private int tab = 0; private BiConsumer stackGenerator = OwoItemGroup.DEFAULT_STACK_GENERATOR; + private boolean trackUsageStat = false; public OwoItemSettings group(ItemGroupReference ref) { this.group = ref.group(); @@ -58,6 +62,20 @@ public BiConsumer stackGenerator() { return this.stackGenerator; } + /** + * Automatically increment {@link net.minecraft.stat.Stats#USED} + * for this item every time {@link Item#use(World, PlayerEntity, Hand)} + * returns an accepted result + */ + public OwoItemSettings trackUsageStat() { + this.trackUsageStat = true; + return this; + } + + public boolean shouldTrackUsageStat() { + return this.trackUsageStat; + } + @Override public OwoItemSettings equipmentSlot(EquipmentSlotProvider equipmentSlotProvider) { return (OwoItemSettings) super.equipmentSlot(equipmentSlotProvider); diff --git a/src/main/java/io/wispforest/owo/mixin/ServerPlayerInteractionManagerMixin.java b/src/main/java/io/wispforest/owo/mixin/ServerPlayerInteractionManagerMixin.java new file mode 100644 index 00000000..35cc06f8 --- /dev/null +++ b/src/main/java/io/wispforest/owo/mixin/ServerPlayerInteractionManagerMixin.java @@ -0,0 +1,28 @@ +package io.wispforest.owo.mixin; + +import io.wispforest.owo.util.pond.OwoItemExtensions; +import net.minecraft.item.ItemStack; +import net.minecraft.server.network.ServerPlayerEntity; +import net.minecraft.server.network.ServerPlayerInteractionManager; +import net.minecraft.stat.Stats; +import net.minecraft.util.ActionResult; +import net.minecraft.util.Hand; +import net.minecraft.world.World; +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(ServerPlayerInteractionManager.class) +public class ServerPlayerInteractionManagerMixin { + + @Inject(method = "interactItem", at = @At("RETURN")) + private void incrementUseState(ServerPlayerEntity player, World world, ItemStack stack, Hand hand, CallbackInfoReturnable cir) { + if (!((OwoItemExtensions) stack.getItem()).owo$shouldTrackUsageStat() || !cir.getReturnValue().shouldIncrementStat()) { + return; + } + + player.incrementStat(Stats.USED.getOrCreateStat(stack.getItem())); + } + +} diff --git a/src/main/java/io/wispforest/owo/mixin/itemgroup/ItemMixin.java b/src/main/java/io/wispforest/owo/mixin/itemgroup/ItemMixin.java index 59f000f5..e22072b2 100644 --- a/src/main/java/io/wispforest/owo/mixin/itemgroup/ItemMixin.java +++ b/src/main/java/io/wispforest/owo/mixin/itemgroup/ItemMixin.java @@ -26,12 +26,16 @@ public class ItemMixin implements OwoItemExtensions { @Unique private BiConsumer owo$stackGenerator; + @Unique + private boolean owo$trackUsageStat = false; + @Inject(method = "", at = @At("TAIL")) private void grabTab(Item.Settings settings, CallbackInfo ci) { if (settings instanceof OwoItemSettings owoSettings) { this.owo$tab = owoSettings.tab(); this.owo$stackGenerator = owoSettings.stackGenerator(); this.owo$group = owoSettings.group(); + this.owo$trackUsageStat = owoSettings.shouldTrackUsageStat(); } } @@ -54,4 +58,9 @@ private void grabTab(Item.Settings settings, CallbackInfo ci) { public @Nullable ItemGroup owo$group() { return this.owo$group; } + + @Override + public boolean owo$shouldTrackUsageStat() { + return this.owo$trackUsageStat; + } } diff --git a/src/main/java/io/wispforest/owo/util/pond/OwoItemExtensions.java b/src/main/java/io/wispforest/owo/util/pond/OwoItemExtensions.java index d7ec9a76..b511d3df 100644 --- a/src/main/java/io/wispforest/owo/util/pond/OwoItemExtensions.java +++ b/src/main/java/io/wispforest/owo/util/pond/OwoItemExtensions.java @@ -34,4 +34,9 @@ public interface OwoItemExtensions { */ @Nullable ItemGroup owo$group(); + /** + * @return {@code true} if this item should automatically + * have its usage stat incremented + */ + boolean owo$shouldTrackUsageStat(); } diff --git a/src/main/resources/owo.mixins.json b/src/main/resources/owo.mixins.json index 95bf6fdf..6d39c4ad 100644 --- a/src/main/resources/owo.mixins.json +++ b/src/main/resources/owo.mixins.json @@ -12,6 +12,7 @@ "ScreenHandlerMixin", "ServerLoginNetworkHandlerAccessor", "ServerPlayerEntityMixin", + "ServerPlayerInteractionManagerMixin", "ServerPlayNetworkHandlerAccessor", "SimpleRegistryMixin", "TagGroupLoaderMixin", @@ -36,8 +37,8 @@ ], "client": [ "ClientLoginNetworkHandlerAccessor", - "MinecraftClientMixin", "DrawContextMixin", + "MinecraftClientMixin", "itemgroup.AbstractInventoryScreenMixin", "itemgroup.CreativeInventoryScreenAccessor", "itemgroup.CreativeInventoryScreenMixin", diff --git a/src/testmod/java/io/wispforest/uwu/items/UwuTestStickItem.java b/src/testmod/java/io/wispforest/uwu/items/UwuTestStickItem.java index 8c596a28..fdbd39cc 100644 --- a/src/testmod/java/io/wispforest/uwu/items/UwuTestStickItem.java +++ b/src/testmod/java/io/wispforest/uwu/items/UwuTestStickItem.java @@ -30,6 +30,7 @@ public class UwuTestStickItem extends Item { public UwuTestStickItem() { super(new OwoItemSettings().group(Uwu.SIX_TAB_GROUP).tab(3).maxCount(1) + .trackUsageStat() .stackGenerator(OwoItemGroup.DEFAULT_STACK_GENERATOR.andThen((item, stacks) -> { final var stack = new ItemStack(item); stack.setCustomName(Text.literal("the stick of the test").styled(style -> style.withItalic(false)));