Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/java/dev/badtz/prefixes/PrefixApplier.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ public static boolean applyRandom(ItemStack stack, net.minecraft.util.RandomSour
return true;
}

public static boolean applyRandomIfNeeded(ItemStack stack,
net.minecraft.util.RandomSource random) {
if (stack.isEmpty() || stack.has(Prefixes.PREFIX) || !isPrefixable(stack)) {
return false;
}

return applyRandom(stack, random);
}

public static Optional<PrefixManager.PrefixDefinition> getPrefix(ItemStack stack) {
Identifier prefixId = stack.get(Prefixes.PREFIX);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.badtz.prefixes.mixin;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Invoker;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;

@Mixin(AbstractContainerMenu.class)
public interface AbstractContainerMenuAccessor {
@Invoker("moveItemStackTo")
boolean prefixes$moveItemStackTo(ItemStack stack, int startIndex, int endIndex,
boolean reverseDirection);
}
25 changes: 25 additions & 0 deletions src/main/java/dev/badtz/prefixes/mixin/CraftingMenuMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package dev.badtz.prefixes.mixin;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import dev.badtz.prefixes.PrefixApplier;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.CraftingMenu;
import net.minecraft.world.item.ItemStack;

@Mixin(CraftingMenu.class)
public class CraftingMenuMixin {
@Redirect(method = "quickMoveStack", at = @At(value = "INVOKE",
target = "Lnet/minecraft/world/inventory/CraftingMenu;moveItemStackTo(Lnet/minecraft/world/item/ItemStack;IIZ)Z",
ordinal = 0))
private boolean prefixes$applyRandomPrefixOnShiftCraft(CraftingMenu menu, ItemStack stack,
int startIndex, int endIndex, boolean reverseDirection, Player player, int slotIndex) {
if (slotIndex == 0 && !player.level().isClientSide()) {
PrefixApplier.applyRandomIfNeeded(stack, player.getRandom());
}

return ((AbstractContainerMenuAccessor) menu).prefixes$moveItemStackTo(stack, startIndex,
endIndex, reverseDirection);
}
}
13 changes: 9 additions & 4 deletions src/main/java/dev/badtz/prefixes/mixin/ResultSlotMixin.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
package dev.badtz.prefixes.mixin;

import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import dev.badtz.prefixes.PrefixApplier;
import dev.badtz.prefixes.Prefixes;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.ResultSlot;
import net.minecraft.world.item.ItemStack;

@Mixin(ResultSlot.class)
public class ResultSlotMixin {
@Shadow
@Final
private Player player;

@Inject(method = "onTake", at = @At("HEAD"))
private void prefixes$applyRandomPrefixOnCraft(Player player, ItemStack carried,
private void prefixes$applyRandomPrefixOnCraft(Player player, ItemStack stack,
CallbackInfo ci) {
if (!player.level().isClientSide() && !carried.isEmpty() && !carried.has(Prefixes.PREFIX)) {
PrefixApplier.applyRandom(carried, player.getRandom());
if (!this.player.level().isClientSide()) {
PrefixApplier.applyRandomIfNeeded(stack, this.player.getRandom());
}
}
}
4 changes: 3 additions & 1 deletion src/main/resources/prefixes.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
"package": "dev.badtz.prefixes.mixin",
"compatibilityLevel": "JAVA_25",
"mixins": [
"CraftingMenuMixin",
"ResultSlotMixin",
"MerchantResultSlotMixin",
"CrafterBlockMixin",
"PlayerMixin"
"PlayerMixin",
"AbstractContainerMenuAccessor"
],
"client": [],
"injectors": {
Expand Down