-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Textplaceholders and fixed deps
+Added TextplaceholderAPI as a jar-in-jar dependency. +Added placeholders: see github wiki page. *Fixed build.gradle dependencies *Fixed tooltip on config about game restart. *Incremented version to 3.0.2.
- Loading branch information
1 parent
287b7ca
commit f3e139a
Showing
7 changed files
with
121 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/com/github/clevernucleus/playerex/util/PlayerLevelPair.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.github.clevernucleus.playerex.util; | ||
|
||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
|
||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.util.Pair; | ||
import net.minecraft.util.math.MathHelper; | ||
|
||
public final class PlayerLevelPair extends Pair<PlayerEntity, Integer> { | ||
public PlayerLevelPair(PlayerEntity player, int level) { | ||
super(player, level); | ||
} | ||
|
||
public int cast() { | ||
return this.getRight(); | ||
} | ||
|
||
public static PlayerLevelPair sort(PlayerLevelPair[] pairs, final int place) { | ||
Arrays.sort(pairs, Comparator.comparing(PlayerLevelPair::cast)); | ||
|
||
int index = MathHelper.clamp(place, 1, pairs.length); | ||
|
||
return pairs[pairs.length - index]; | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/com/github/clevernucleus/playerex/util/StoredPlaceholder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.github.clevernucleus.playerex.util; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import com.github.clevernucleus.playerex.api.ExAPI; | ||
|
||
import eu.pb4.placeholders.PlaceholderHandler; | ||
import eu.pb4.placeholders.PlaceholderResult; | ||
import net.minecraft.entity.attribute.AttributeContainer; | ||
import net.minecraft.entity.attribute.EntityAttribute; | ||
import net.minecraft.entity.player.PlayerEntity; | ||
import net.minecraft.server.MinecraftServer; | ||
import net.minecraft.server.PlayerManager; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
import net.minecraft.util.Identifier; | ||
|
||
public final class StoredPlaceholder { | ||
public static final Map<Identifier, PlaceholderHandler> STORE = new HashMap<Identifier, PlaceholderHandler>(); | ||
|
||
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) { | ||
return ctx -> { | ||
MinecraftServer server = ctx.getServer(); | ||
PlayerManager manager = server.getPlayerManager(); | ||
EntityAttribute attribute = ExAPI.LEVEL.get(); | ||
|
||
if(attribute == null) return PlaceholderResult.value(""); | ||
|
||
List<ServerPlayerEntity> players = manager.getPlayerList(); | ||
int size = players.size(); | ||
|
||
PlayerLevelPair[] pairs = new PlayerLevelPair[size]; | ||
|
||
if(place > size) return PlaceholderResult.value(""); | ||
|
||
for(int i = 0; i < size; i++) { | ||
ServerPlayerEntity player = players.get(i); | ||
AttributeContainer container = player.getAttributes(); | ||
|
||
if(!container.hasAttribute(attribute)) continue; | ||
|
||
int level = Math.round((float)container.getValue(attribute)); | ||
pairs[i] = new PlayerLevelPair(player, level); | ||
} | ||
|
||
PlayerEntity result = PlayerLevelPair.sort(pairs, place).getLeft(); | ||
|
||
return PlaceholderResult.value(result.getName()); | ||
}; | ||
} | ||
|
||
static { | ||
register("level", ctx -> { | ||
PlayerEntity player = ctx.getPlayer(); | ||
|
||
if(player == null) return PlaceholderResult.value("n/a"); | ||
|
||
AttributeContainer container = player.getAttributes(); | ||
EntityAttribute attribute = ExAPI.LEVEL.get(); | ||
|
||
if(attribute == null || !container.hasAttribute(attribute)) return PlaceholderResult.value("n/a"); | ||
|
||
int level = Math.round((float)container.getValue(attribute)); | ||
|
||
return PlaceholderResult.value(String.valueOf(level)); | ||
}); | ||
|
||
for(int i = 1; i < 11; i++) { | ||
register("level_top_" + i, specificLevelPlacement(i)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters