Skip to content

Commit

Permalink
WIP api v0
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Nov 6, 2024
1 parent be3d2c8 commit f7fa8de
Show file tree
Hide file tree
Showing 32 changed files with 209 additions and 66 deletions.
78 changes: 52 additions & 26 deletions src/main/java/xyz/nifeather/morph/MorphManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.nifeather.morph.api.v0.disguise.DisguiseValidateResult;
import xyz.nifeather.morph.api.v0.disguise.IMorphManager;
import xyz.nifeather.morph.api.v0.disguise.MorphParameters;
import xyz.nifeather.morph.backends.DisguiseBackend;
import xyz.nifeather.morph.backends.DisguiseWrapper;
import xyz.nifeather.morph.backends.WrapperProperties;
import xyz.nifeather.morph.backends.fallback.NilBackend;
import xyz.nifeather.morph.backends.server.ServerBackend;
import xyz.nifeather.morph.events.api.gameplay.*;
import xyz.nifeather.morph.api.v0.events.gameplay.*;
import xyz.nifeather.morph.misc.*;
import xyz.nifeather.morph.misc.playerList.PlayerListHandler;
import xyz.nifeather.morph.config.ConfigOption;
import xyz.nifeather.morph.config.MorphConfigManager;
import xyz.nifeather.morph.events.api.lifecycle.ManagerFinishedInitializeEvent;
import xyz.nifeather.morph.api.v0.events.lifecycle.ManagerFinishedInitializeEvent;
import xyz.nifeather.morph.interfaces.IManagePlayerData;
import xyz.nifeather.morph.messages.CommandStrings;
import xyz.nifeather.morph.messages.HintStrings;
Expand Down Expand Up @@ -66,7 +69,7 @@
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;

public class MorphManager extends MorphPluginObject implements IManagePlayerData
public class MorphManager extends MorphPluginObject implements IManagePlayerData, IMorphManager
{
private final List<DisguiseState> activeDisguises = ObjectLists.synchronize(new ObjectArrayList<>());

Expand Down Expand Up @@ -240,7 +243,7 @@ private void load()
config.bind(useClientRenderer, ConfigOption.USE_CLIENT_RENDERER);
config.bind(hideDisguisedPlayers, ConfigOption.HIDE_DISGUISED_PLAYERS_IN_TAB);

registerProviders(ObjectList.of(
registerDisguiseProviders(ObjectList.of(
new VanillaDisguiseProvider(),
new PlayerDisguiseProvider(),
//new ItemDisplayProvider(),
Expand Down Expand Up @@ -339,6 +342,7 @@ public boolean canMorph(Player player)
* @param uuid 玩家UUID
* @return 是否可以伪装
*/
@Override
public boolean canMorph(UUID uuid)
{
var val = uuidMoprhTimeMap.get(uuid);
Expand All @@ -352,7 +356,13 @@ public boolean canMorph(UUID uuid)
*/
public void updateLastPlayerMorphOperationTime(Player player)
{
uuidMoprhTimeMap.put(player.getUniqueId(), plugin.getCurrentTick());
this.updateLastPlayerMorphOperationTime(player.getUniqueId());
}

@Override
public void updateLastPlayerMorphOperationTime(UUID uuid)
{
uuidMoprhTimeMap.put(uuid, plugin.getCurrentTick());
}

private BindableList<String> bannedDisguises;
Expand All @@ -366,6 +376,12 @@ public BindableList<String> getBannedDisguises()
return bannedDisguises;
}

@Override
public List<String> getBannedDisguisesCopy()
{
return new ObjectArrayList<>(bannedDisguises);
}

//region 伪装提供器

private static final List<DisguiseProvider> providers = new ObjectArrayList<>();
Expand All @@ -391,12 +407,19 @@ public static DisguiseProvider getProvider(String id)
return providers.stream().filter(p -> p.getNameSpace().equals(splitedId[0])).findFirst().orElse(fallbackProvider);
}

@Override
@NotNull
public DisguiseProvider getDisguiseProvider(String namespaceIdentifier)
{
return getProvider(namespaceIdentifier);
}

/**
* 注册一个DisguiseProvider
* @param provider 目标Provider
* @return 操作是否成功
*/
public boolean registerProvider(DisguiseProvider provider)
public boolean registerDisguiseProvider(DisguiseProvider provider)
{
//logger.info("Registering disguise provider: " + provider.getNameSpace());

Expand All @@ -421,11 +444,11 @@ public boolean registerProvider(DisguiseProvider provider)
* @param providers Provider列表
* @return 所有操作是否成功
*/
public boolean registerProviders(List<DisguiseProvider> providers)
public boolean registerDisguiseProviders(List<DisguiseProvider> providers)
{
AtomicBoolean success = new AtomicBoolean(false);

providers.forEach(p -> success.set(registerProvider(p) || success.get()));
providers.forEach(p -> success.set(registerDisguiseProvider(p) || success.get()));

return success.get();
}
Expand Down Expand Up @@ -588,11 +611,7 @@ public void morphOrUnMorph(Player player, String key, @Nullable Entity targetEnt
public boolean morph(CommandSender source, Player player,
String key, @Nullable Entity targetEntity)
{
var parameters = MorphParameters.create(player, key)
.setSource(source)
.setTargetedEntity(targetEntity);

return this.doDisguise(parameters);
return this.morph(source, player, key, targetEntity, false);
}

/**
Expand Down Expand Up @@ -638,16 +657,16 @@ private boolean doDisguise(MorphParameters parameters)
var validateResult = validateDisguise(meta);
switch (validateResult)
{
case VALIDATE_NO_ISSUE -> {}
case DisguiseValidateResult.VALIDATE_NO_ISSUE -> {}

case VALIDATE_NO_PROVIDER ->
case DisguiseValidateResult.VALIDATE_NO_PROVIDER ->
{
logger.error("Unable to find any provider that matches the identifier '%s'".formatted(parameters.targetDisguiseIdentifier()));
source.sendMessage(MessageUtils.prefixes(source, MorphStrings.disguiseBannedOrNotSupportedString()));
return false;
}

case VALIDATE_PROVIDER_FAIL ->
case DisguiseValidateResult.VALIDATE_PROVIDER_FAIL ->
{
source.sendMessage(MessageUtils.prefixes(source, MorphStrings.invalidIdentityString()));
return false;
Expand Down Expand Up @@ -761,25 +780,25 @@ else if (!disguiseIdentifier.equals("minecraft:player")) // 禁止不带参数
return info;
}

public static final int VALIDATE_NO_ISSUE = 0;
public static final int VALIDATE_NO_PROVIDER = 1;
public static final int VALIDATE_PROVIDER_FAIL = 2;

public int validateDisguise(DisguiseMeta meta)
@Override
public int validateDisguise(String disguiseIdentifier)
{
var disguiseIdentifier = meta.getIdentifier();

// 查找provider
var strippedKey = disguiseIdentifier.split(":", 2);

var provider = getProvider(strippedKey[0]);

if (provider == MorphManager.fallbackProvider) // 如果没找到provider
return VALIDATE_NO_PROVIDER;
return DisguiseValidateResult.VALIDATE_NO_PROVIDER;
else if (!provider.isValid(disguiseIdentifier)) // 如果provider不认识这个ID
return VALIDATE_PROVIDER_FAIL;
return DisguiseValidateResult.VALIDATE_PROVIDER_FAIL;

return VALIDATE_NO_ISSUE;
return DisguiseValidateResult.VALIDATE_NO_ISSUE;
}

public int validateDisguise(DisguiseMeta meta)
{
return validateDisguise(meta.getIdentifier());
}

/**
Expand Down Expand Up @@ -1218,6 +1237,7 @@ public void unMorphAll(boolean ignoreOffline)
*
* @param player 目标玩家
*/
@Override
public void unMorph(Player player)
{
this.unMorph(player, player, false, false);
Expand All @@ -1229,6 +1249,7 @@ public void unMorph(Player player)
* @param player 目标玩家
* @param bypassPermission 是否绕过权限检查(强制取消伪装)
*/
@Override
public void unMorph(Player player, boolean bypassPermission)
{
this.unMorph(player, player, bypassPermission, false);
Expand All @@ -1249,6 +1270,7 @@ public void unMorph(Player player, boolean bypassPermission)
*
* @apiNote 如果 forceUnmorph 不为 true,则此操作可以被其他来源取消
*/
@Override
public void unMorph(@Nullable CommandSender source, Player player, boolean bypassPermission, boolean forceUnmorph)
{
// 确保source不为null
Expand Down Expand Up @@ -1362,6 +1384,7 @@ public void spawnParticle(Player player, Location location, double collX, double
* @param player 目标玩家
* @return 正在伪装时返回客户端预览是否可用并已启用,没在伪装时返回玩家的客户端设置
*/
@Override
public boolean clientViewAvailable(Player player)
{
var state = this.getDisguiseStateFor(player);
Expand All @@ -1377,11 +1400,13 @@ public boolean clientViewAvailable(Player player)
return playerOption.isClientSideSelfView() && state.getProvider().validForClient(state);
}

@Override
public void setSelfDisguiseVisible(Player player, boolean val, boolean saveToConfig)
{
this.setSelfDisguiseVisible(player, val, saveToConfig, clientHandler.getPlayerOption(player, true).isClientSideSelfView(), false);
}

@Override
public void setSelfDisguiseVisible(Player player, boolean value, boolean saveToConfig, boolean dontSetServerSide, boolean noClientCommand)
{
var state = getDisguiseStateFor(player);
Expand Down Expand Up @@ -1413,6 +1438,7 @@ public void setSelfDisguiseVisible(Player player, boolean value, boolean saveToC
* @param player 目标玩家
* @return 伪装状态,如果为null则表示玩家没有通过插件伪装
*/
@Override
@Nullable
public DisguiseState getDisguiseStateFor(@Nullable Player player)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import xyz.nifeather.morph.abilities.impl.potion.*;
import xyz.nifeather.morph.abilities.impl.onAttack.ExtraKnockbackAbility;
import xyz.nifeather.morph.abilities.impl.onAttack.PotionOnAttackAbility;
import xyz.nifeather.morph.events.api.lifecycle.AbilitiesFinishedInitializeEvent;
import xyz.nifeather.morph.api.v0.events.lifecycle.AbilitiesFinishedInitializeEvent;
import xyz.nifeather.morph.storage.skill.ISkillOption;
import xiamomc.pluginbase.Annotations.Initializer;
import xiamomc.pluginbase.Annotations.Resolved;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package xyz.nifeather.morph.api.v0.disguise;

public class DisguiseValidateResult
{
public static final int VALIDATE_NO_ISSUE = 0;
public static final int VALIDATE_NO_PROVIDER = 1;
public static final int VALIDATE_PROVIDER_FAIL = 2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package xyz.nifeather.morph.api.v0.disguise;

import net.kyori.adventure.text.Component;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xiamomc.pluginbase.Exceptions.NullDependencyException;

public interface IDisguiseState
{
@Nullable
Player tryGetPlayer();

/**
*
* @apiNote For a nullable method, use {@link IDisguiseState#tryGetPlayer()}
* @throws NullDependencyException If the player doesn't exist.
*/
@NotNull
Player getPlayer() throws NullDependencyException;

boolean isSelfViewing();

/**
* @return The display component used for the player
*/
Component getPlayerDisplay();
void setPlayerDisplay(@NotNull Component newName);

/**
* @return The display component used for others, like Bossbar, ChatOverride, and PAPI integration
*/
Component getServerDisplay();
void setServerDisplay(@NotNull Component newName);

/**
* Sets both the player and the server display to the given component
*/
void setCustomDisplayName(Component newName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package xyz.nifeather.morph.api.v0.disguise;

import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xyz.nifeather.morph.providers.disguise.DisguiseProvider;

import java.util.List;
import java.util.UUID;

public interface IMorphManager
{
boolean canMorph(UUID playerUUID);

void updateLastPlayerMorphOperationTime(UUID uuid);

List<String> getBannedDisguisesCopy();

boolean disguiseDisabled(String identifier);

@NotNull
DisguiseProvider getDisguiseProvider(String namespaceIdentifier);
boolean registerDisguiseProvider(DisguiseProvider provider);

boolean tryQuickDisguise(Player player);

boolean morph(MorphParameters parameters);

void unMorph(Player player);
void unMorph(Player player, boolean ignorePermissions);
void unMorph(@Nullable CommandSender source, Player player, boolean bypassPermission, boolean forceUnmorph);

/**
* @return See {@link DisguiseValidateResult}
*/
int validateDisguise(String identifier);

boolean clientViewAvailable(Player player);

void setSelfDisguiseVisible(Player player, boolean value, boolean saveToConfig, boolean dontSetServerSide, boolean noClientCommand);
void setSelfDisguiseVisible(Player player, boolean value, boolean saveToConfig);

IDisguiseState getDisguiseStateFor(Player player);

boolean grantMorphToPlayer(Player player, String disguiseIdentifier);

boolean revokeMorphFromPlayer(Player player, String disguiseIdentifier);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xyz.nifeather.morph.misc;
package xyz.nifeather.morph.api.v0.disguise;

import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package xyz.nifeather.morph.api.v0.disguise.backends;

public interface IDisguiseBackend<TInstance, TWrapper extends IDisguiseWrapper<TInstance>>
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package xyz.nifeather.morph.api.v0.disguise.backends;

public interface IDisguiseWrapper<TInstance>
{
/**
* @return The underlying disguise instance
*/
TInstance getInstance();

IDisguiseBackend<TInstance, ? extends IDisguiseWrapper<TInstance>> getBackend();
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xyz.nifeather.morph.events.api.gameplay;
package xyz.nifeather.morph.api.v0.events.gameplay;

import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xyz.nifeather.morph.events.api.gameplay;
package xyz.nifeather.morph.api.v0.events.gameplay;

import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xyz.nifeather.morph.events.api.gameplay;
package xyz.nifeather.morph.api.v0.events.gameplay;

import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xyz.nifeather.morph.events.api.gameplay;
package xyz.nifeather.morph.api.v0.events.gameplay;

import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package xyz.nifeather.morph.events.api.gameplay;
package xyz.nifeather.morph.api.v0.events.gameplay;

import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
Expand Down
Loading

0 comments on commit f7fa8de

Please sign in to comment.