Skip to content

Commit

Permalink
fix: fix PropertyHandler not setting values
Browse files Browse the repository at this point in the history
misc: 一些调整
  • Loading branch information
MATRIX-feather committed Oct 4, 2024
1 parent b56edbc commit 9f87d9a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package xiamomc.morph.misc.disguiseProperty;

import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xiamomc.morph.MorphPlugin;
import xiamomc.morph.misc.disguiseProperty.values.AbstractProperties;

import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -15,12 +17,19 @@
public class PropertyHandler
{
private final Map<SingleProperty<?>, Object> propertyMap = new ConcurrentHashMap<>();
private final List<SingleProperty<?>> validProperties = new ObjectArrayList<>();

private final Random random = ThreadLocalRandom.current();

@Nullable
private AbstractProperties properties;

public void setProperties(AbstractProperties properties)
{
reset();

this.properties = properties;
validProperties.addAll(properties.getValues());
properties.getValues().forEach(this::addProperty);
}

Expand All @@ -36,6 +45,8 @@ private void addProperty(SingleProperty<?> property)

public void reset()
{
this.validProperties.clear();
this.properties = null;
propertyMap.clear();
}

Expand All @@ -49,9 +60,9 @@ private void writeGeneric(SingleProperty<?> property, Object value)

public <X> void set(SingleProperty<X> property, X value)
{
if (!propertyMap.containsKey(property))
if (!validProperties.contains(property))
{
MorphPlugin.getInstance().getSLF4JLogger().warn("The given property '%s' doesn't exist.".formatted(property));
MorphPlugin.getInstance().getSLF4JLogger().warn("The given property '%s' doesn't exist in '%s'".formatted(property.id(), this.properties));
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ protected void registerSingle(SingleProperty<?> value)

public List<SingleProperty<?>> getValues()
{
return values;
return new ObjectArrayList<>(values);
}
}
70 changes: 28 additions & 42 deletions src/main/java/xiamomc/morph/misc/skins/PlayerSkinProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

public class PlayerSkinProvider extends MorphPluginObject
Expand All @@ -38,40 +37,22 @@ public static PlayerSkinProvider getInstance()
return instance;
}

private CompletableFuture<Optional<GameProfile>> getProfileAsyncV2(String name)
{
var executor = Util.PROFILE_EXECUTOR;

return CompletableFuture.supplyAsync(() -> this.fetchProfileV2(name), executor)
.whenCompleteAsync((optional, throwable) -> {}, executor);
}

private final SkinCache skinCache = new SkinCache();

public static boolean isValidUsername(String name) {
if (name != null && !name.isEmpty() && name.length() <= 16) {
int i = 0;

for(int len = name.length(); i < len; ++i) {
char c = name.charAt(i);
if ((c < 'a' || c > 'z') && (c < 'A' || c > 'Z') && (c < '0' || c > '9') && c != '_' && c != '.') {
return false;
}
}
private CompletableFuture<Optional<GameProfile>> fetchPlayerInfoAsync(String name)
{
var executor = Util.PROFILE_EXECUTOR;

return true;
} else {
return false;
}
return CompletableFuture.supplyAsync(() -> this.fetchPlayerInfo(name), executor);
}

/**
* 根据给定的名称搜索对应的Profile(不包含皮肤)
* @apiNote 此方法返回的GameProfile不包含皮肤,若要获取于此对应的皮肤,请使用 {@link PlayerSkinProvider#fetchSkinFromProfile(GameProfile)}
* @apiNote 此方法返回的GameProfile不包含皮肤,若要获取于此对应的皮肤,请使用 {@link PlayerSkinProvider#fetchSkin(GameProfile)}
* @param name
* @return
*/
private Optional<GameProfile> fetchProfileV2(String name)
private Optional<GameProfile> fetchPlayerInfo(String name)
{
var profileRef = new AtomicReference<GameProfile>(null);

Expand Down Expand Up @@ -106,11 +87,25 @@ else if (exception instanceof AuthenticationUnavailableException)
return profile == null ? Optional.empty() : Optional.of(profile);
}

@Nullable
public GameProfile getCachedProfile(String name)
{
return skinCache.get(name).profileOptional().orElse(null);
}

public void cacheProfile(@NotNull PlayerProfile playerProfile)
{
var gameProfile = new MorphGameProfile(playerProfile);
skinCache.cache(gameProfile);
}

private final Map<String, CompletableFuture<Optional<GameProfile>>> onGoingRequests = new ConcurrentHashMap<>();

/**
* 通过给定的Profile获取与其对应的皮肤
* @param profile 目标GameProfile
*/
public CompletableFuture<Optional<GameProfile>> fetchSkinFromProfile(GameProfile profile)
public CompletableFuture<Optional<GameProfile>> fetchSkin(GameProfile profile)
{
if (profile.getProperties().containsKey("textures"))
{
Expand All @@ -136,20 +131,6 @@ public CompletableFuture<Optional<GameProfile>> fetchSkinFromProfile(GameProfile
}
}

@Nullable
public GameProfile getCachedProfile(String name)
{
return skinCache.get(name).profileOptional().orElse(null);
}

public void cacheProfile(@NotNull PlayerProfile playerProfile)
{
var gameProfile = new MorphGameProfile(playerProfile);
skinCache.cache(gameProfile);
}

private final Map<String, CompletableFuture<Optional<GameProfile>>> onGoingRequests = new ConcurrentHashMap<>();

/**
* 尝试获取与给定名称对应的皮肤
* @param profileName 目标名称
Expand Down Expand Up @@ -178,12 +159,12 @@ public CompletableFuture<Optional<GameProfile>> fetchSkin(String profileName)
if (prevReq != null)
return prevReq;

var req = getProfileAsyncV2(profileName)
var req = fetchPlayerInfoAsync(profileName)
.thenCompose(rawProfileOptional ->
{
if (rawProfileOptional.isPresent()) //如果查有此人,那么继续流程
{
return fetchSkinFromProfile(rawProfileOptional.get());
return fetchSkin(rawProfileOptional.get());
}
else if (cachedSkin.profileOptional().isPresent()) //否则,如果本地有缓存,那就使用本地缓存
{
Expand All @@ -199,6 +180,11 @@ else if (cachedSkin.profileOptional().isPresent()) //否则,如果本地有缓
}
});

req.exceptionally(t ->
{
onGoingRequests.remove(profileName);
return Optional.empty();
});
req.thenRun(() -> onGoingRequests.remove(profileName));

onGoingRequests.put(profileName, req);
Expand Down

0 comments on commit 9f87d9a

Please sign in to comment.