Skip to content

Commit

Permalink
Fixed tooltips and placeholder
Browse files Browse the repository at this point in the history
+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.
  • Loading branch information
CleverNucleus committed Sep 11, 2021
1 parent 8ce58e1 commit b613ef1
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -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<EntityAttributeModifier.Operation, Set<EntityAttributeModifier>> operationToModifiers;

@Override
public Collection<EntityAttributeModifier> modifiersByOperation(Operation operationIn) {
return (Collection<EntityAttributeModifier>)this.operationToModifiers.getOrDefault(operationIn, Collections.emptySet());
}
}
Original file line number Diff line number Diff line change
@@ -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<UUID> 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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.clevernucleus.playerex.util;

import java.util.Collection;

import net.minecraft.entity.attribute.EntityAttributeModifier;

public interface EntityAttributeInstanceAccess {

Collection<EntityAttributeModifier> modifiersByOperation(EntityAttributeModifier.Operation operationIn);
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -33,6 +33,16 @@ private static PlaceholderHandler specificLevelPlacement(final int place) {

List<ServerPlayerEntity> 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];

Expand Down Expand Up @@ -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());
}
}
3 changes: 2 additions & 1 deletion src/main/resources/playerex.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"mixins": [
"LivingEntityMixin",
"PlayerEntityMixin",
"ProjectileEntityMixin"
"ProjectileEntityMixin",
"EntityAttributeInstanceMixin"
],
"client": [
"client.HandledScreenMixin",
Expand Down

0 comments on commit b613ef1

Please sign in to comment.