From b613ef16ff26c12c7bf37cb286884dbc7ad076c5 Mon Sep 17 00:00:00 2001 From: CleverNucleus Date: Sat, 11 Sep 2021 17:38:38 +0100 Subject: [PATCH] Fixed tooltips and placeholder +Added Fix for item tooltip attack damage/speed; -Removed the text placeholders %playerex:level_top_1% to 10. +Added the text placeholder: %playerex:level_top/x% where x is the position on the server's level leaderboard. I.e. x = 1 would mean the highest level, then x = 2 would be the next highest level etc. --- .../mixin/EntityAttributeInstanceMixin.java | 29 ++++++++++ .../playerex/mixin/client/ItemStackMixin.java | 55 ++++++++++++++----- .../util/EntityAttributeInstanceAccess.java | 10 ++++ .../playerex/util/ItemFieldAccess.java | 19 +++++++ .../playerex/util/StoredPlaceholder.java | 16 ++++-- src/main/resources/playerex.mixins.json | 3 +- 6 files changed, 112 insertions(+), 20 deletions(-) create mode 100644 src/main/java/com/github/clevernucleus/playerex/mixin/EntityAttributeInstanceMixin.java create mode 100644 src/main/java/com/github/clevernucleus/playerex/util/EntityAttributeInstanceAccess.java create mode 100644 src/main/java/com/github/clevernucleus/playerex/util/ItemFieldAccess.java diff --git a/src/main/java/com/github/clevernucleus/playerex/mixin/EntityAttributeInstanceMixin.java b/src/main/java/com/github/clevernucleus/playerex/mixin/EntityAttributeInstanceMixin.java new file mode 100644 index 00000000..de8c2cda --- /dev/null +++ b/src/main/java/com/github/clevernucleus/playerex/mixin/EntityAttributeInstanceMixin.java @@ -0,0 +1,29 @@ +package com.github.clevernucleus.playerex.mixin; + +import java.util.Collection; +import java.util.Collections; +import java.util.Map; +import java.util.Set; + +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; + +import com.github.clevernucleus.playerex.util.EntityAttributeInstanceAccess; + +import net.minecraft.entity.attribute.EntityAttributeInstance; +import net.minecraft.entity.attribute.EntityAttributeModifier; +import net.minecraft.entity.attribute.EntityAttributeModifier.Operation; + +@Mixin(EntityAttributeInstance.class) +abstract class EntityAttributeInstanceMixin implements EntityAttributeInstanceAccess { + + @Final + @Shadow + private Map> operationToModifiers; + + @Override + public Collection modifiersByOperation(Operation operationIn) { + return (Collection)this.operationToModifiers.getOrDefault(operationIn, Collections.emptySet()); + } +} diff --git a/src/main/java/com/github/clevernucleus/playerex/mixin/client/ItemStackMixin.java b/src/main/java/com/github/clevernucleus/playerex/mixin/client/ItemStackMixin.java index 1eb7e618..d9bc346a 100644 --- a/src/main/java/com/github/clevernucleus/playerex/mixin/client/ItemStackMixin.java +++ b/src/main/java/com/github/clevernucleus/playerex/mixin/client/ItemStackMixin.java @@ -1,38 +1,63 @@ package com.github.clevernucleus.playerex.mixin.client; +import java.util.UUID; +import java.util.function.Supplier; + import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.ModifyVariable; import com.github.clevernucleus.playerex.api.ExAPI; -import com.github.clevernucleus.playerex.api.ModifierData; +import com.github.clevernucleus.playerex.util.EntityAttributeInstanceAccess; +import com.github.clevernucleus.playerex.util.ItemFieldAccess; +import net.minecraft.entity.attribute.EntityAttribute; +import net.minecraft.entity.attribute.EntityAttributeInstance; +import net.minecraft.entity.attribute.EntityAttributeModifier; import net.minecraft.entity.attribute.EntityAttributes; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; @Mixin(ItemStack.class) abstract class ItemStackMixin { + private double modifyGeneralisedValue(double dIn, PlayerEntity player, EntityAttribute attributeIn, Supplier uuidIn) { + if(player == null) return dIn; + + EntityAttributeInstance instance = player.getAttributeInstance(attributeIn); + EntityAttributeInstanceAccess access = (EntityAttributeInstanceAccess)instance; + + double d = dIn; + + for(EntityAttributeModifier modifier : access.modifiersByOperation(EntityAttributeModifier.Operation.ADDITION)) { + if(modifier.getId().equals(uuidIn.get())) continue; + + d += modifier.getValue(); + } + + double e = d; + + for(EntityAttributeModifier modifier : access.modifiersByOperation(EntityAttributeModifier.Operation.MULTIPLY_BASE)) { + e += d * modifier.getValue(); + } + + for(EntityAttributeModifier modifier : access.modifiersByOperation(EntityAttributeModifier.Operation.MULTIPLY_TOTAL)) { + e *= 1.0D + modifier.getValue(); + } + + return attributeIn.clamp(e); + } @ModifyVariable(method = "getTooltip", at = @At(value = "STORE", ordinal = 1), ordinal = 0) private double modifyAttackDamage(double d, PlayerEntity player) { - if(player == null) return d; - - ModifierData data = ExAPI.DATA.get(player); - double value = data.get(EntityAttributes.GENERIC_ATTACK_DAMAGE); + if(!ExAPI.CONFIG.get().enableTooltipFix()) return d; - System.out.println("v: " + value); - // TODO got sleepy will finish later - return d + value + 10; + return modifyGeneralisedValue(d, player, EntityAttributes.GENERIC_ATTACK_DAMAGE, ItemFieldAccess::attackDamageModifierId); } - /* + @ModifyVariable(method = "getTooltip", at = @At(value = "STORE", ordinal = 3), ordinal = 0) private double modifyAttackSpeed(double d, PlayerEntity player) { - if(player == null) return d; - - ModifierData data = ExAPI.DATA.get(player); - double value = data.get(EntityAttributes.GENERIC_ATTACK_SPEED); + if(!ExAPI.CONFIG.get().enableTooltipFix()) return d; - return d + value; - }*/ + return modifyGeneralisedValue(d, player, EntityAttributes.GENERIC_ATTACK_SPEED, ItemFieldAccess::attackSpeedModifierId); + } } diff --git a/src/main/java/com/github/clevernucleus/playerex/util/EntityAttributeInstanceAccess.java b/src/main/java/com/github/clevernucleus/playerex/util/EntityAttributeInstanceAccess.java new file mode 100644 index 00000000..4bd0c754 --- /dev/null +++ b/src/main/java/com/github/clevernucleus/playerex/util/EntityAttributeInstanceAccess.java @@ -0,0 +1,10 @@ +package com.github.clevernucleus.playerex.util; + +import java.util.Collection; + +import net.minecraft.entity.attribute.EntityAttributeModifier; + +public interface EntityAttributeInstanceAccess { + + Collection modifiersByOperation(EntityAttributeModifier.Operation operationIn); +} diff --git a/src/main/java/com/github/clevernucleus/playerex/util/ItemFieldAccess.java b/src/main/java/com/github/clevernucleus/playerex/util/ItemFieldAccess.java new file mode 100644 index 00000000..d4fddc22 --- /dev/null +++ b/src/main/java/com/github/clevernucleus/playerex/util/ItemFieldAccess.java @@ -0,0 +1,19 @@ +package com.github.clevernucleus.playerex.util; + +import java.util.UUID; + +import net.minecraft.item.Item; + +public final class ItemFieldAccess extends Item { + private ItemFieldAccess() { + super(new Item.Settings()); + } + + public static UUID attackDamageModifierId() { + return ATTACK_DAMAGE_MODIFIER_ID; + } + + public static UUID attackSpeedModifierId() { + return ATTACK_SPEED_MODIFIER_ID; + } +} diff --git a/src/main/java/com/github/clevernucleus/playerex/util/StoredPlaceholder.java b/src/main/java/com/github/clevernucleus/playerex/util/StoredPlaceholder.java index e7a08dcc..3057dbb7 100644 --- a/src/main/java/com/github/clevernucleus/playerex/util/StoredPlaceholder.java +++ b/src/main/java/com/github/clevernucleus/playerex/util/StoredPlaceholder.java @@ -23,7 +23,7 @@ private static void register(final String key, final PlaceholderHandler handler) STORE.put(new Identifier(ExAPI.MODID, key), handler); } - private static PlaceholderHandler specificLevelPlacement(final int place) { + private static PlaceholderHandler specificLevelPlacement() { return ctx -> { MinecraftServer server = ctx.getServer(); PlayerManager manager = server.getPlayerManager(); @@ -33,6 +33,16 @@ private static PlaceholderHandler specificLevelPlacement(final int place) { List players = manager.getPlayerList(); int size = players.size(); + int place = 1; + + if(ctx.hasArgument()) { + try { + int i = Integer.parseInt(ctx.getArgument()); + place = Math.max(1, i); + } catch(NumberFormatException e) { + return PlaceholderResult.value(""); + } + } PlayerLevelPair[] pairs = new PlayerLevelPair[size]; @@ -70,8 +80,6 @@ private static PlaceholderHandler specificLevelPlacement(final int place) { return PlaceholderResult.value(String.valueOf(level)); }); - for(int i = 1; i < 11; i++) { - register("level_top_" + i, specificLevelPlacement(i)); - } + register("level_top", specificLevelPlacement()); } } diff --git a/src/main/resources/playerex.mixins.json b/src/main/resources/playerex.mixins.json index 1ba76ec2..8fbea57f 100644 --- a/src/main/resources/playerex.mixins.json +++ b/src/main/resources/playerex.mixins.json @@ -6,7 +6,8 @@ "mixins": [ "LivingEntityMixin", "PlayerEntityMixin", - "ProjectileEntityMixin" + "ProjectileEntityMixin", + "EntityAttributeInstanceMixin" ], "client": [ "client.HandledScreenMixin",