Skip to content

Commit

Permalink
Fixed mending in trinket slots. Close #109
Browse files Browse the repository at this point in the history
Fixed mending in trinket slots. Close #109
  • Loading branch information
jaredlll08 committed Dec 9, 2022
1 parent 114fed4 commit 039930e
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 7 deletions.
Expand Up @@ -9,11 +9,10 @@
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.ExperienceOrb;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.entity.EntityTypeTest;
import net.minecraft.world.phys.AABB;
Expand All @@ -23,16 +22,22 @@
import org.spongepowered.asm.mixin.Unique;
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 org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BiFunction;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Mixin(ExperienceOrb.class)
@Mixin(value = ExperienceOrb.class, priority = 1001)
public abstract class MixinExperienceOrb extends Entity implements IClumpedOrb {

@Shadow
Expand Down Expand Up @@ -62,6 +67,9 @@ private static boolean canMerge(ExperienceOrb experienceOrb, int id, int value)
@Unique
public Map<Integer, Integer> clumps$clumpedMap;

@Unique
public Map.Entry<EquipmentSlot, ItemStack> clumps$currentEntry;

public MixinExperienceOrb(EntityType<?> entityType, Level level) {

super(entityType, level);
Expand Down Expand Up @@ -112,14 +120,22 @@ public void playerTouch(Player player, CallbackInfo ci) {
}
}

@ModifyVariable(index = 3, method = "repairPlayerItems", at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/world/item/enchantment/EnchantmentHelper;getRandomItemWith(Lnet/minecraft/world/item/enchantment/Enchantment;Lnet/minecraft/world/entity/LivingEntity;Ljava/util/function/Predicate;)Ljava/util/Map$Entry;"))
public Map.Entry<EquipmentSlot, ItemStack> clumps$captureCurrentEntry(Map.Entry<EquipmentSlot, ItemStack> entry) {

clumps$currentEntry = entry;
return entry;
}

@Inject(method = "repairPlayerItems", at = @At(value = "HEAD"), cancellable = true)
@Inject(method = "repairPlayerItems", cancellable = true, at = @At(value = "INVOKE_ASSIGN", target = "Lnet/minecraft/world/item/enchantment/EnchantmentHelper;getRandomItemWith(Lnet/minecraft/world/item/enchantment/Enchantment;Lnet/minecraft/world/entity/LivingEntity;Ljava/util/function/Predicate;)Ljava/util/Map$Entry;"))
public void clumps$repairPlayerItems(Player player, int actualValue, CallbackInfoReturnable<Integer> cir) {

cir.setReturnValue(Optional.ofNullable(EnchantmentHelper.getRandomItemWith(Enchantments.MENDING, player, ItemStack::isDamaged))
cir.setReturnValue(Optional.ofNullable(clumps$currentEntry)
.map(Map.Entry::getValue)
.map(foundItem -> {
int toRepair = Math.min(this.xpToDurability(actualValue), foundItem.getDamageValue());
BiFunction<ItemStack, Integer, Float> repairRatio = Services.PLATFORM.getRepairRatio((itemStack, integer) -> (float) this.xpToDurability(integer));
int toRepair = Math.min(repairRatio.apply(foundItem, actualValue)
.intValue(), foundItem.getDamageValue());
foundItem.setDamageValue(foundItem.getDamageValue() - toRepair);
int used = actualValue - this.durabilityToXp(toRepair);
return used > 0 ? this.repairPlayerItems(player, used) : 0;
Expand Down
@@ -0,0 +1,14 @@
package com.blamejared.clumps.platform;

import net.minecraft.world.item.ItemStack;

import java.util.function.BiFunction;

public interface IPlatformHelper {

default BiFunction<ItemStack, Integer, Float> getRepairRatio(BiFunction<ItemStack, Integer, Float> defaultRepairRatio) {

return defaultRepairRatio;
}

}
Expand Up @@ -7,6 +7,8 @@
public class Services {

public static final IEventHelper EVENT = load(IEventHelper.class);
public static final IPlatformHelper PLATFORM = load(IPlatformHelper.class);


public static <T> T load(Class<T> clazz) {

Expand Down
@@ -0,0 +1,6 @@
package com.blamejared.clumps.platform;


public class FabricPlatformHelper implements IPlatformHelper {

}
@@ -0,0 +1 @@
com.blamejared.clumps.platform.FabricPlatformHelper
@@ -0,0 +1,15 @@
package com.blamejared.clumps.platform;

import net.minecraft.world.item.ItemStack;

import java.util.function.BiFunction;

public class ForgePlatformHelper implements IPlatformHelper {

@Override
public BiFunction<ItemStack, Integer, Float> getRepairRatio(BiFunction<ItemStack, Integer, Float> defaultRepairRatio) {

return (itemStack, integer) -> itemStack.getXpRepairRatio();
}

}
@@ -0,0 +1 @@
com.blamejared.clumps.platform.ForgePlatformHelper

0 comments on commit 039930e

Please sign in to comment.