Skip to content

Commit

Permalink
enable automatic item use stat tracking via OwoItemSettings#trackUsag…
Browse files Browse the repository at this point in the history
…eStat
  • Loading branch information
gliscowo committed Aug 20, 2023
1 parent 1d2711c commit f705f91
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/main/java/io/wispforest/owo/itemgroup/OwoItemSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,6 +20,7 @@ public class OwoItemSettings extends FabricItemSettings {
private OwoItemGroup group = null;
private int tab = 0;
private BiConsumer<Item, ItemGroup.Entries> stackGenerator = OwoItemGroup.DEFAULT_STACK_GENERATOR;
private boolean trackUsageStat = false;

public OwoItemSettings group(ItemGroupReference ref) {
this.group = ref.group();
Expand Down Expand Up @@ -58,6 +62,20 @@ public BiConsumer<Item, ItemGroup.Entries> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ActionResult> cir) {
if (!((OwoItemExtensions) stack.getItem()).owo$shouldTrackUsageStat() || !cir.getReturnValue().shouldIncrementStat()) {
return;
}

player.incrementStat(Stats.USED.getOrCreateStat(stack.getItem()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ public class ItemMixin implements OwoItemExtensions {
@Unique
private BiConsumer<Item, ItemGroup.Entries> owo$stackGenerator;

@Unique
private boolean owo$trackUsageStat = false;

@Inject(method = "<init>", 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();
}
}

Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
3 changes: 2 additions & 1 deletion src/main/resources/owo.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"ScreenHandlerMixin",
"ServerLoginNetworkHandlerAccessor",
"ServerPlayerEntityMixin",
"ServerPlayerInteractionManagerMixin",
"ServerPlayNetworkHandlerAccessor",
"SimpleRegistryMixin",
"TagGroupLoaderMixin",
Expand All @@ -36,8 +37,8 @@
],
"client": [
"ClientLoginNetworkHandlerAccessor",
"MinecraftClientMixin",
"DrawContextMixin",
"MinecraftClientMixin",
"itemgroup.AbstractInventoryScreenMixin",
"itemgroup.CreativeInventoryScreenAccessor",
"itemgroup.CreativeInventoryScreenMixin",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down

0 comments on commit f705f91

Please sign in to comment.