Skip to content

Commit

Permalink
add: 允许设置禁用伪装功能的世界
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Jan 12, 2025
1 parent 62f835a commit 24d0bcc
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 41 deletions.
46 changes: 40 additions & 6 deletions src/main/java/xyz/nifeather/morph/MorphManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ private void load()
logger.info("Default backend: %s".formatted(defaultBackend));

bannedDisguises = config.getBindableList(String.class, ConfigOption.BANNED_DISGUISES);
disabledWorlds = config.getBindableList(String.class, ConfigOption.DISGUISE_DISABLED_WORLDS);

disabledWorlds.onListChanged((diff, reason) ->
{
});

config.bind(allowHeadMorph, ConfigOption.ALLOW_HEAD_MORPH);
config.bind(allowAcquireMorph, ConfigOption.ALLOW_ACQUIRE_MORPHS);
config.bind(useClientRenderer, ConfigOption.USE_CLIENT_RENDERER);
Expand All @@ -251,6 +257,16 @@ private void load()
Bukkit.getPluginManager().callEvent(new ManagerFinishedInitializeEvent(this));
}

public boolean disguiseDisabledInWorld(World world)
{
return disguiseDisabledInWorld(world.getName());
}

public boolean disguiseDisabledInWorld(String worldName)
{
return disabledWorlds.contains(worldName);
}

private void update()
{
this.addSchedule(this::update);
Expand Down Expand Up @@ -356,6 +372,7 @@ public void updateLastPlayerMorphOperationTime(Player player)
}

private BindableList<String> bannedDisguises;
private BindableList<String> disabledWorlds;

/**
* 内部轮子,检查某个伪装是否被禁用建议使用 {@link MorphManager#disguiseDisabled(String)}
Expand Down Expand Up @@ -730,6 +747,12 @@ private DisguiseMeta preDisguise(MorphParameters parameters)
return null;
}

if (disguiseDisabledInWorld(player.getWorld()))
{
source.sendMessage(MessageUtils.prefixes(source, MorphStrings.disguiseDisabledInWorldString()));
return null;
}

// 调用早期事件
var earlyEventPassed = new PlayerMorphEarlyEvent(player, null, disguiseIdentifier, parameters.forceExecute).callEvent();
if (!parameters.forceExecute && !earlyEventPassed)
Expand Down Expand Up @@ -1463,16 +1486,24 @@ public List<OfflineDisguiseState> getAvaliableOfflineStates()
return offlineStorage.getAvaliableDisguiseStates();
}

public void disguiseFromState(DisguiseState state)
public boolean disguiseFromState(DisguiseState state)
{
var meta = getDisguiseMeta(state.getDisguiseIdentifier());
var result = DisguiseBuildResult.of(state, state.getProvider(), meta, null);
var playerMeta = getPlayerMeta(state.getPlayer());
var parameters = MorphParameters.create(state.getPlayer(), state.getDisguiseIdentifier());

if (this.preDisguise(parameters) == null)
return false;

this.postBuildDisguise(result, parameters, playerMeta);
this.applyDisguise(parameters, state, meta, playerMeta);

if (!this.applyDisguise(parameters, state, meta, playerMeta))
return false;

this.afterDisguise(result, parameters, playerMeta);

return true;
}

/**
Expand Down Expand Up @@ -1659,16 +1690,19 @@ public boolean reloadConfiguration()

this.scheduleOn(player, () ->
{
var config = this.getPlayerMeta(player);
if (!disguiseDisabled(s.getDisguiseIdentifier()) && config.getUnlockedDisguiseIdentifiers().contains(s.getDisguiseIdentifier()))
var parameter = MorphParameters.create(player, s.getDisguiseIdentifier());
if (this.preDisguise(parameter) == null)
return;

if (disguiseFromState(s))
{
disguiseFromState(s);
refreshClientState(s);

player.sendMessage(MessageUtils.prefixes(player, MorphStrings.recoverString()));
}
else
{
unMorph(nilCommandSource, player, true, true);
}
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public void registerAsChild(ArgumentBuilder<CommandSourceStack, ?> parentBuilder
operationAdd.registerAsChild(thisBuilder);
operationRemove.registerAsChild(thisBuilder);

super.registerAsChild(parentBuilder);
parentBuilder.then(thisBuilder);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,13 @@ public OptionSubCommand()
subCommands.add(getList("nofly_worlds", ConfigOption.NOFLY_WORLDS, null));
subCommands.add(getList("blacklist_tags", ConfigOption.BLACKLIST_TAGS, null));
subCommands.add(getList("blacklist_nbt_pattern", ConfigOption.BLACKLIST_PATTERNS, null));
//subCommands.add(getList("disabled_worlds", ConfigOption.DISGUISE_DISABLED_WORLDS, null));

subCommands.add(getToggle("ability_check_permissions", ConfigOption.DO_CHECK_ABILITY_PERMISSIONS, null));

subCommands.add(getToggle("towny_allow_flight_in_wilderness", ConfigOption.TOWNY_ALLOW_FLY_IN_WILDERNESS));
}

private CompletableFuture<Suggestions> suggestListOperation(CommandContext<CommandSourceStack> context,
SuggestionsBuilder suggestionsBuilder)
{
suggestionsBuilder.suggest("list").suggest("add").suggest("remove");

return suggestionsBuilder.buildFuture();
}

private IConvertibleBrigadier getList(String optionName, ConfigOption option,
@Nullable FormattableMessage displayName)
{
Expand Down Expand Up @@ -135,16 +128,6 @@ private IConvertibleBrigadier getToggle(String name, ConfigOption option, @Nulla
return new OptionSubCommands.BooleanOptionCommand(name, config, option);
}

private boolean parseBoolean(String input)
{
return "true".equalsIgnoreCase(input)
|| "t".equalsIgnoreCase(input)
|| "on".equalsIgnoreCase(input)
|| "1".equalsIgnoreCase(input)
|| "enable".equalsIgnoreCase(input)
|| "enabled".equalsIgnoreCase(input);
}

private final List<IConvertibleBrigadier> subCommands = new ObjectArrayList<>();

@Override
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/xyz/nifeather/morph/config/ConfigOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public enum ConfigOption

TOWNY_ALLOW_FLY_IN_WILDERNESS(townyNode().append("allow_fly_in_wilderness"), false),

DISGUISE_DISABLED_WORLDS(worldOptionNode().append("disabled_worlds"), new ArrayList<String>()),

VERSION(ConfigNode.create().append("version"), 0);

Expand Down Expand Up @@ -212,4 +213,8 @@ public static ConfigNode townyNode()
{
return integrationNode().append("towny");
}
private static ConfigNode worldOptionNode()
{
return ConfigNode.create().append("world_option");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public void reload()
});

//更新配置
int targetVersion = 38;
int targetVersion = 39;

var configVersion = getOrDefault(Integer.class, ConfigOption.VERSION);

Expand Down
47 changes: 33 additions & 14 deletions src/main/java/xyz/nifeather/morph/events/CommonEventProcessor.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package xyz.nifeather.morph.events;

import com.destroystokyo.paper.event.entity.EntityAddToWorldEvent;
import com.destroystokyo.paper.event.player.PlayerClientOptionsChangeEvent;
import com.destroystokyo.paper.event.player.PlayerPostRespawnEvent;
import de.themoep.inventorygui.InventoryGui;
import io.papermc.paper.registry.RegistryAccess;
import io.papermc.paper.registry.RegistryKey;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Registry;
import org.bukkit.attribute.Attribute;
import org.bukkit.block.data.type.CreakingHeart;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
Expand All @@ -27,6 +24,7 @@
import xiamomc.pluginbase.Annotations.Initializer;
import xiamomc.pluginbase.Annotations.Resolved;
import xiamomc.pluginbase.Bindables.Bindable;
import xiamomc.pluginbase.Bindables.BindableList;
import xyz.nifeather.morph.MorphManager;
import xyz.nifeather.morph.MorphPluginObject;
import xyz.nifeather.morph.RevealingHandler;
Expand Down Expand Up @@ -103,6 +101,27 @@ private void update()
}
}

@EventHandler
public void onEntityAddToWorld(EntityAddToWorldEvent e)
{
if (!(e.getEntity() instanceof Player player))
return;

var world = e.getWorld();

var state = morphs.getDisguiseStateFor(player);
if (state != null && morphs.disguiseDisabledInWorld(world))
{
this.scheduleOn(player, () ->
{
if (!player.getWorld().equals(world)) return;

player.sendMessage(MessageUtils.prefixes(player, MorphStrings.disguiseDisabledInWorldString()));
morphs.unMorph(player);
});
}
}

@EventHandler
public void onEntityDeath(EntityDeathEvent e)
{
Expand Down Expand Up @@ -158,18 +177,18 @@ public void onPlayerRespawn(PlayerPostRespawnEvent e)
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public void onPlayerTookDamage(EntityDamageEvent e)
{
if (e.getEntity() instanceof Player player)
{
var state = morphs.getDisguiseStateFor(player);
if (!(e.getEntity() instanceof Player player))
return;

if (state != null)
{
state.getSoundHandler().resetSoundTime();
var state = morphs.getDisguiseStateFor(player);

//如果伤害是0,那么取消事件
if (e.getDamage() > 0d)
state.setSkillCooldown(Math.max(state.getSkillCooldown(), cooldownOnDamage.get()), true);
}
if (state != null)
{
state.getSoundHandler().resetSoundTime();

//如果伤害是0,那么取消事件
if (e.getDamage() > 0d)
state.setSkillCooldown(Math.max(state.getSkillCooldown(), cooldownOnDamage.get()), true);
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/main/java/xyz/nifeather/morph/messages/MorphStrings.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,11 @@ public static FormattableMessage operationCancelledString()
"<red>操作被其他来源取消");
}

public static FormattableMessage disguiseDisabledInWorldString()
{
return getFormattable(getKey("disguise_disabled"), "[Fallback] 无法伪装,因为此功能在当前世界被禁用");
}

public static FormattableMessage revealed()
{
return getFormattable(getKey("disguise_revealed"),
Expand Down
14 changes: 13 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,18 @@ root:
# Disable this will also disable the bossbar ability for all disguises.
enabled: true

# Misc world options
world_option:
# Disabled worlds, players in these worlds will not be able to use the disguise feature
#
# Example:
# disabled_worlds:
# - example_world1
# - example_world2
disabled_worlds:
- example_world1
- example_world2

# Fly ability options
flying:
# The base distance for Hunger consumption.
Expand Down Expand Up @@ -359,4 +371,4 @@ root:
- SelectedItem

# Do not touch unless you know what you're doing!
version: 38
version: 39

0 comments on commit 24d0bcc

Please sign in to comment.