9
0
mirror of https://github.com/Xiao-MoMi/Custom-Nameplates.git synced 2025-12-19 15:09:23 +00:00
This commit is contained in:
XiaoMoMi
2024-11-10 18:29:25 +08:00
parent 018dc18eee
commit e3d81e7c0c
31 changed files with 178 additions and 79 deletions

View File

@@ -23,6 +23,8 @@ dependencies {
compileOnly("io.netty:netty-all:4.1.113.Final")
// GSON
compileOnly("com.google.code.gson:gson:${rootProject.properties["gson_version"]}")
// Fast util
compileOnly("it.unimi.dsi:fastutil:${rootProject.properties["fastutil_version"]}")
}
java {

View File

@@ -18,6 +18,11 @@
package net.momirealms.customnameplates.api;
import io.netty.channel.Channel;
import it.unimi.dsi.fastutil.ints.Int2ObjectLinkedOpenHashMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenCustomHashMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import net.momirealms.customnameplates.api.feature.Feature;
import net.momirealms.customnameplates.api.feature.TimeStampData;
import net.momirealms.customnameplates.api.feature.tag.TeamView;
@@ -51,11 +56,11 @@ public abstract class AbstractCNPlayer implements CNPlayer {
private final TeamView teamView = new TeamView();
private final Map<Integer, TimeStampData<String>> cachedValues = new ConcurrentHashMap<>();
private final Map<Integer, WeakHashMap<CNPlayer, TimeStampData<String>>> cachedRelationalValues = new ConcurrentHashMap<>();
private final Int2ObjectOpenHashMap<TimeStampData<String>> cachedValues = new Int2ObjectOpenHashMap<>(64);
private final Int2ObjectOpenHashMap<WeakHashMap<CNPlayer, TimeStampData<String>>> cachedRelationalValues = new Int2ObjectOpenHashMap<>(64);
private final Map<Requirement, TimeStampData<Boolean>> cachedRequirements = new ConcurrentHashMap<>();
private final Map<Requirement, WeakHashMap<CNPlayer, TimeStampData<Boolean>>> cachedRelationalRequirements = new ConcurrentHashMap<>();
private final Int2ObjectOpenHashMap<TimeStampData<Boolean>> cachedRequirements = new Int2ObjectOpenHashMap<>(128);
private final Int2ObjectOpenHashMap<WeakHashMap<CNPlayer, TimeStampData<Boolean>>> cachedRelationalRequirements = new Int2ObjectOpenHashMap<>(128);
private final Set<Feature> activeFeatures = new CopyOnWriteArraySet<>();
private final Map<Placeholder, Set<Feature>> placeholder2Features = new ConcurrentHashMap<>();
@@ -71,7 +76,7 @@ public abstract class AbstractCNPlayer implements CNPlayer {
@Override
public List<Placeholder> activePlaceholdersToRefresh() {
Placeholder[] activePlaceholders = activePlaceholders();
List<Placeholder> placeholderWithChildren = new ArrayList<>();
List<Placeholder> placeholderWithChildren = new ObjectArrayList<>();
for (Placeholder placeholder : activePlaceholders) {
childrenFirstList(placeholder, placeholderWithChildren);
}
@@ -79,9 +84,9 @@ public abstract class AbstractCNPlayer implements CNPlayer {
}
@Override
public void forceUpdatePlaceholders(Set<Placeholder> placeholders, Set<CNPlayer> others) {
public void forceUpdatePlaceholders(Set<Placeholder> placeholders, Collection<CNPlayer> others) {
if (placeholders.isEmpty()) return;
List<Placeholder> placeholderWithChildren = new ArrayList<>();
List<Placeholder> placeholderWithChildren = new ObjectArrayList<>();
for (Placeholder placeholder : placeholders) {
childrenFirstList(placeholder, placeholderWithChildren);
}
@@ -224,7 +229,7 @@ public abstract class AbstractCNPlayer implements CNPlayer {
Set<Placeholder> allPlaceholders = feature.allPlaceholders();
feature2Placeholders.put(feature, allPlaceholders);
for (Placeholder placeholder : allPlaceholders) {
Set<Feature> featureSet = placeholder2Features.computeIfAbsent(placeholder, k -> new HashSet<>());
Set<Feature> featureSet = placeholder2Features.computeIfAbsent(placeholder, k -> new ObjectOpenHashSet<>());
featureSet.add(feature);
}
}
@@ -330,7 +335,7 @@ public abstract class AbstractCNPlayer implements CNPlayer {
@Override
public Placeholder[] activePlaceholders() {
HashSet<Placeholder> placeholders = new HashSet<>();
Set<Placeholder> placeholders = new ObjectOpenHashSet<>();
for (Feature feature : activeFeatures) {
placeholders.addAll(feature.activePlaceholders());
}
@@ -341,7 +346,7 @@ public abstract class AbstractCNPlayer implements CNPlayer {
public boolean isMet(Requirement[] requirements) {
int currentTicks = MainTask.getTicks();
for (Requirement requirement : requirements) {
TimeStampData<Boolean> data = cachedRequirements.get(requirement);
TimeStampData<Boolean> data = cachedRequirements.get(requirement.countId());
if (data != null) {
if (data.ticks() + requirement.refreshInterval() > currentTicks) {
if (!data.data()) {
@@ -358,7 +363,7 @@ public abstract class AbstractCNPlayer implements CNPlayer {
} else {
boolean satisfied = requirement.isSatisfied(this, this);
data = new TimeStampData<>(satisfied, currentTicks, true);
cachedRequirements.put(requirement, data);
cachedRequirements.put(requirement.countId(), data);
if (!satisfied) {
return false;
}
@@ -371,7 +376,7 @@ public abstract class AbstractCNPlayer implements CNPlayer {
public boolean isMet(CNPlayer another, Requirement[] requirements) {
int currentTicks = MainTask.getTicks();
for (Requirement requirement : requirements) {
WeakHashMap<CNPlayer, TimeStampData<Boolean>> innerMap = cachedRelationalRequirements.computeIfAbsent(requirement, k -> new WeakHashMap<>());
WeakHashMap<CNPlayer, TimeStampData<Boolean>> innerMap = cachedRelationalRequirements.computeIfAbsent(requirement.countId(), k -> new WeakHashMap<>());
TimeStampData<Boolean> data = innerMap.get(another);
if (data != null) {
if (data.ticks() + requirement.refreshInterval() > currentTicks) {
@@ -421,8 +426,8 @@ public abstract class AbstractCNPlayer implements CNPlayer {
}
@Override
public Set<CNPlayer> nearbyPlayers() {
return new HashSet<>(trackers.keySet());
public Collection<CNPlayer> nearbyPlayers() {
return new ObjectArrayList<>(trackers.keySet());
}
@Override
@@ -446,7 +451,7 @@ public abstract class AbstractCNPlayer implements CNPlayer {
@Override
public Set<Integer> getTrackedPassengerIds(CNPlayer another) {
return Optional.ofNullable(trackers.get(another)).map(Tracker::getPassengerIDs).orElse(new HashSet<>());
return Optional.ofNullable(trackers.get(another)).map(Tracker::getPassengerIDs).orElse(new ObjectOpenHashSet<>());
}
@Override

View File

@@ -28,6 +28,7 @@ import net.momirealms.customnameplates.api.util.Vector3;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.UUID;
@@ -190,7 +191,7 @@ public interface CNPlayer {
* @param placeholders the placeholders to update
* @param another the players related to the placeholders
*/
void forceUpdatePlaceholders(Set<Placeholder> placeholders, Set<CNPlayer> another);
void forceUpdatePlaceholders(Set<Placeholder> placeholders, Collection<CNPlayer> another);
/**
* Retrieves the cached data for a given placeholder.
@@ -317,7 +318,7 @@ public interface CNPlayer {
*
* @return the set of nearby players
*/
Set<CNPlayer> nearbyPlayers();
Collection<CNPlayer> nearbyPlayers();
/**
* Adds passenger entities to the tracker for another player.

View File

@@ -17,6 +17,8 @@
package net.momirealms.customnameplates.api;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import net.momirealms.customnameplates.api.feature.actionbar.ActionBarManager;
import net.momirealms.customnameplates.api.feature.advance.AdvanceManager;
import net.momirealms.customnameplates.api.feature.background.BackgroundManager;
@@ -38,10 +40,7 @@ import net.momirealms.customnameplates.common.locale.TranslationManager;
import net.momirealms.customnameplates.common.plugin.NameplatesPlugin;
import net.momirealms.customnameplates.common.plugin.scheduler.SchedulerTask;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.UUID;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.function.Supplier;
@@ -69,7 +68,7 @@ public abstract class CustomNameplates implements NameplatesPlugin {
protected MainTask mainTask;
protected SchedulerTask scheduledMainTask;
protected ConcurrentHashMap<UUID, CNPlayer> onlinePlayerMap = new ConcurrentHashMap<>();
protected HashMap<Integer, CNPlayer> entityIDFastLookup = new HashMap<>();
protected Int2ObjectOpenHashMap<CNPlayer> entityIDFastLookup = new Int2ObjectOpenHashMap<>();
protected AdvanceManager advanceManager;
protected BackgroundManager backgroundManager;
protected EventManager eventManager;
@@ -288,7 +287,7 @@ public abstract class CustomNameplates implements NameplatesPlugin {
* @return a collection of {@link CNPlayer} instances
*/
public Collection<CNPlayer> getOnlinePlayers() {
return new HashSet<>(onlinePlayerMap.values());
return new ObjectArrayList<>(onlinePlayerMap.values());
}
/**

View File

@@ -17,6 +17,8 @@
package net.momirealms.customnameplates.api;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
@@ -29,7 +31,7 @@ public class MainTask implements Runnable {
private static final Map<Integer, Integer> TIME_1 = new ConcurrentHashMap<>(2048);
private static final Map<Integer, Integer> TIME_2 = new ConcurrentHashMap<>(2048);
private static final Set<Integer> requestedSharedPlaceholders = Collections.synchronizedSet(new HashSet<>());
private static final Set<Integer> requestedSharedPlaceholders = Collections.synchronizedSet(new ObjectOpenHashSet<>());
private int timer;
private final CustomNameplates plugin;

View File

@@ -17,6 +17,8 @@
package net.momirealms.customnameplates.api.feature;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import net.momirealms.customnameplates.api.CNPlayer;
import net.momirealms.customnameplates.api.CustomNameplates;
import net.momirealms.customnameplates.api.placeholder.*;
@@ -27,8 +29,8 @@ import java.util.function.Function;
public class PreParsedDynamicText {
private final String text;
private final List<Function<CNPlayer, Function<CNPlayer, String>>> textFunctions = new ArrayList<>();
private final Set<Placeholder> set = new HashSet<>();
private final List<Function<CNPlayer, Function<CNPlayer, String>>> textFunctions = new ObjectArrayList<>();
private final Set<Placeholder> set = new ObjectOpenHashSet<>();
private boolean init = false;
public PreParsedDynamicText(String text) {
@@ -86,12 +88,12 @@ public class PreParsedDynamicText {
String remaining = original0.substring(lastIndex);
textFunctions.add((owner) -> (viewer) -> remaining);
}
// To optimize the tree height, call new HashSet twice here
set.addAll(new HashSet<>(placeholders));
// To optimize the tree height
set.addAll(new ObjectArrayList<>(placeholders));
}
public DynamicText fastCreate(CNPlayer player) {
List<Function<CNPlayer, String>> functions = new ArrayList<>();
List<Function<CNPlayer, String>> functions = new ObjectArrayList<>();
for (Function<CNPlayer, Function<CNPlayer, String>> textFunction : textFunctions) {
functions.add(textFunction.apply(player));
}

View File

@@ -18,6 +18,7 @@
package net.momirealms.customnameplates.api.feature.advance;
import com.google.gson.JsonObject;
import it.unimi.dsi.fastutil.ints.Int2FloatOpenHashMap;
import java.util.HashMap;
import java.util.List;
@@ -28,11 +29,11 @@ import static java.util.Objects.requireNonNull;
public class CharacterFontAdvanceDataImpl implements CharacterFontAdvanceData {
private final HashMap<Integer, Float> data;
private final Map<Integer, Float> data;
private final String id;
private final Function<Map<String, Object>, List<JsonObject>> fontProviderFunction;
public CharacterFontAdvanceDataImpl(String id, HashMap<Integer, Float> data, Function<Map<String, Object>, List<JsonObject>> fontProviderFunction) {
public CharacterFontAdvanceDataImpl(String id, Map<Integer, Float> data, Function<Map<String, Object>, List<JsonObject>> fontProviderFunction) {
this.data = data;
this.id = requireNonNull(id);
this.fontProviderFunction = requireNonNull(fontProviderFunction);
@@ -65,7 +66,7 @@ public class CharacterFontAdvanceDataImpl implements CharacterFontAdvanceData {
public static class BuilderImpl implements Builder {
private final HashMap<Integer, Float> data = new HashMap<>();
private final Int2FloatOpenHashMap data = new Int2FloatOpenHashMap();
private String id;
private Function<Map<String, Object>, List<JsonObject>> fontProviderFunction = (stringObjectMap -> null);

View File

@@ -17,18 +17,18 @@
package net.momirealms.customnameplates.api.feature.advance;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import it.unimi.dsi.fastutil.ints.Int2FloatOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import java.util.*;
import static java.util.Objects.requireNonNull;
public class ConfigurableFontAdvanceDataImpl implements ConfigurableFontAdvanceData {
private final float defaultAdvance;
private final HashMap<Integer, Float> data = new HashMap<>();
private final List<CharacterFontAdvanceData> parents = new ArrayList<>();
private final Map<Integer, Float> data = new Int2FloatOpenHashMap();
private final List<CharacterFontAdvanceData> parents = new ObjectArrayList<>();
private final String id;
public ConfigurableFontAdvanceDataImpl(String id, float defaultAdvance, HashMap<Integer, Float> customData, List<CharacterFontAdvanceData> parentFonts) {

View File

@@ -17,16 +17,15 @@
package net.momirealms.customnameplates.api.feature.tag;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.*;
public class TeamView {
private final HashMap<String, Set<String>> teamMembers = new HashMap<>();
private final Map<String, Set<String>> teamMembers = new Object2ObjectOpenHashMap<>();
@Nullable
public Set<String> getTeamMembers(String team) {
@@ -34,7 +33,7 @@ public class TeamView {
}
public void addTeamMembers(String team, Collection<String> members) {
teamMembers.computeIfAbsent(team, k -> new HashSet<>(members));
teamMembers.computeIfAbsent(team, k -> new ObjectOpenHashSet<>(members));
}
public void removeTeamMembers(String team, Collection<String> members) {

View File

@@ -17,6 +17,7 @@
package net.momirealms.customnameplates.api.network;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import net.momirealms.customnameplates.api.CNPlayer;
import java.util.HashSet;
@@ -80,7 +81,7 @@ public class Tracker {
}
public Set<Integer> getPassengerIDs() {
return new HashSet<>(passengerIDs);
return new ObjectOpenHashSet<>(passengerIDs);
}
public boolean isEmpty() {

View File

@@ -17,6 +17,7 @@
package net.momirealms.customnameplates.api.placeholder;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import net.momirealms.customnameplates.api.CustomNameplates;
import java.util.HashSet;
@@ -29,8 +30,8 @@ public abstract class AbstractPlaceholder implements Placeholder {
protected int countId = PlaceholderCounter.getAndIncrease();
protected int refreshInterval;
protected PlaceholderManager manager;
protected Set<Placeholder> children = new HashSet<>();
protected Set<Placeholder> parents = new HashSet<>();
protected Set<Placeholder> children = new ObjectOpenHashSet<>();
protected Set<Placeholder> parents = new ObjectOpenHashSet<>();
protected AbstractPlaceholder(PlaceholderManager manager, String id, int refreshInterval) {
if (refreshInterval == 0) {

View File

@@ -18,6 +18,7 @@
package net.momirealms.customnameplates.api.requirement;
import net.momirealms.customnameplates.api.CNPlayer;
import net.momirealms.customnameplates.api.CustomNameplates;
/**
* A requirement that is always satisfied, representing an "empty" or default requirement.
@@ -57,6 +58,11 @@ public class EmptyRequirement implements Requirement {
return "empty";
}
@Override
public int countId() {
return CustomNameplates.getInstance().getRequirementManager().countId(this);
}
/**
* Returns -1, indicating that this requirement does not have a refresh interval.
*

View File

@@ -40,6 +40,13 @@ public interface Requirement {
*/
String type();
/**
* Returns the int id for faster lookup
*
* @return count id
*/
int countId();
/**
* Returns the refresh interval of the requirement.
*

View File

@@ -87,4 +87,12 @@ public interface RequirementManager extends Reloadable {
*/
@NotNull
Requirement parseSimpleRequirement(@NotNull String type, @NotNull Object value);
/**
* Gets the count id for the requirement for faster lookup
*
* @param requirement requirement
* @return the id
*/
int countId(Requirement requirement);
}