Skip to content

Commit

Permalink
misc: Improve(and fix?) PAPI support
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Nov 7, 2024
1 parent be3d2c8 commit 0a1f49f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,4 @@ public interface IPlaceholderProvider
*/
@Nullable
public String resolvePlaceholder(Player player, String params);

/**
* 获取ID的匹配模式
* @return {@link MatchMode}
*/
public MatchMode getMatchMode();
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xiamomc.pluginbase.Managers.DependencyManager;
import xyz.nifeather.morph.misc.integrations.placeholderapi.builtin.AvaliableDisguisesProvider;
import xyz.nifeather.morph.misc.integrations.placeholderapi.builtin.StateNameProvider;
Expand All @@ -14,6 +16,7 @@
public class PlaceholderIntegration extends PlaceholderExpansion
{
private static final List<IPlaceholderProvider> providers = new ObjectArrayList<>();
private static final Logger log = LoggerFactory.getLogger(PlaceholderIntegration.class);

public PlaceholderIntegration(DependencyManager depManager)
{
Expand All @@ -26,7 +29,7 @@ public PlaceholderIntegration(DependencyManager depManager)
@Override
public @NotNull String getIdentifier()
{
return "morph";
return "feathermorph";
}

@Override
Expand All @@ -38,7 +41,7 @@ public PlaceholderIntegration(DependencyManager depManager)
@Override
public @NotNull String getVersion()
{
return "1.0.10";
return "1.4.0";
}

private void addPlaceholders(List<IPlaceholderProvider> providerList)
Expand All @@ -56,34 +59,36 @@ public boolean addPlaceholderProvider(IPlaceholderProvider provider)
if (providers.stream().anyMatch(p -> providerEquals(p, provider)))
return false;

providers.add(0, provider);
providers.addFirst(provider);
return true;
}

private boolean providerEquals(IPlaceholderProvider source, IPlaceholderProvider target)
{
if (source == null || target == null) return false;

return source.getMatchMode() == target.getMatchMode()
&& source.getPlaceholderIdentifier().equals(target.getPlaceholderIdentifier());
return source.getPlaceholderIdentifier().equals(target.getPlaceholderIdentifier());
}

private final String defaultString = "???";
private static final String defaultString = "???";

@Override
public @Nullable String onPlaceholderRequest(Player player, @NotNull String params)
public @Nullable String onPlaceholderRequest(Player player, @NotNull String param)
{
if (player == null) return defaultString;

param = param.replaceFirst(getIdentifier() + "_", "");
var paramSpilt = param.split("_", 2);

if (paramSpilt.length != 2)
return null;

var provider = providers.stream()
.filter(p -> p.getMatchMode() == MatchMode.Exact
? params.equalsIgnoreCase(p.getPlaceholderIdentifier())
: params.startsWith(p.getPlaceholderIdentifier()))
.filter(p -> p.getPlaceholderIdentifier().equalsIgnoreCase(paramSpilt[0]))
.findFirst().orElse(null);

if (provider == null) return defaultString;

var val = provider.resolvePlaceholder(player, params);
return val == null ? defaultString : val;
return provider.resolvePlaceholder(player, paramSpilt[1]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,47 @@
import xyz.nifeather.morph.interfaces.IManagePlayerData;
import xyz.nifeather.morph.messages.MessageUtils;
import xyz.nifeather.morph.misc.integrations.placeholderapi.IPlaceholderProvider;
import xyz.nifeather.morph.misc.integrations.placeholderapi.MatchMode;

public class AvaliableDisguisesProvider extends MorphPluginObject implements IPlaceholderProvider
{
@Override
public @NotNull String getPlaceholderIdentifier()
{
return "avaliable";
return "available";
}

@Resolved
private IManagePlayerData data;

@Override
public @Nullable String resolvePlaceholder(Player player, String params)
public @Nullable String resolvePlaceholder(Player player, String param)
{
var type = params.replace(getPlaceholderIdentifier(), "");
var builder = new StringBuilder();
var list = data.getAvaliableDisguisesFor(player);

var locale = MessageUtils.getServerLocale();

if (type.equalsIgnoreCase("_name"))
switch (param)
{
list.forEach(i ->
case "name" ->
{
builder.append(PlainTextComponentSerializer.plainText().serialize(i.asComponent(locale)));
list.forEach(i ->
{
builder.append(PlainTextComponentSerializer.plainText().serialize(i.asComponent(locale)));

if (list.iterator().hasNext())
builder.append(", ");
});
if (list.iterator().hasNext())
builder.append(", ");
});

return builder.toString();
}
else if (type.equalsIgnoreCase("_count"))
{
return String.valueOf(list.size());
return builder.toString();
}

case "count" ->
{
return String.valueOf(list.size());
}
}

return null;
}

@Override
public MatchMode getMatchMode()
{
return MatchMode.Prefixes;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import xyz.nifeather.morph.MorphManager;
import xyz.nifeather.morph.MorphPluginObject;
import xyz.nifeather.morph.misc.integrations.placeholderapi.IPlaceholderProvider;
import xyz.nifeather.morph.misc.integrations.placeholderapi.MatchMode;

public class StateNameProvider extends MorphPluginObject implements IPlaceholderProvider
{
Expand All @@ -22,30 +21,32 @@ public class StateNameProvider extends MorphPluginObject implements IPlaceholder
private MorphManager morphs;

@Override
public @Nullable String resolvePlaceholder(Player player, String params)
public @Nullable String resolvePlaceholder(Player player, String param)
{
var type = params.replace(getPlaceholderIdentifier(), "");
var state = morphs.getDisguiseStateFor(player);

if (type.equalsIgnoreCase("_id"))
switch (param)
{
return state != null
? state.getDisguiseIdentifier()
: "???";
}
else if (type.equalsIgnoreCase("_name"))
{
return state != null
case "id" ->
{
return state != null
? state.getDisguiseIdentifier()
: "???";
}

case "name" ->
{
return state != null
? PlainTextComponentSerializer.plainText().serialize(state.getServerDisplay())
: "???";
}

case "status" ->
{
return state != null ? "true" : "false";
}
}

return null;
}

@Override
public MatchMode getMatchMode()
{
return MatchMode.Prefixes;
}
}

0 comments on commit 0a1f49f

Please sign in to comment.