Skip to content

Commit

Permalink
add: Initial support for specifying disguise properties
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Jan 3, 2025
1 parent ae962fd commit 3b37389
Show file tree
Hide file tree
Showing 25 changed files with 618 additions and 52 deletions.
11 changes: 7 additions & 4 deletions src/main/java/xyz/nifeather/morph/MorphManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -901,18 +901,21 @@ private void postBuildDisguise(DisguiseBuildResult result,
{
// 同步伪装属性
var propertyHandler = state.disguisePropertyHandler();
propertyHandler.setProperties(disguiseProperties.get(state.getEntityType()));
var properties = disguiseProperties.get(state.getEntityType());
propertyHandler.initProperties(properties);
propertyHandler.updateFromPropertiesInput(parameters.properties);

propertyHandler.getAll().forEach((property, value) ->
{
wrapper.writeProperty((SingleProperty<Object>) property, value);
});
}

// 初始化nbt
var wrapperCompound = provider.getInitialNbtCompound(state, targetEntity, false);
var initialNbtCompound = provider.getInitialNbtCompound(state, targetEntity, false);

if (wrapperCompound != null)
state.getDisguiseWrapper().mergeCompound(wrapperCompound);
if (initialNbtCompound != null)
state.getDisguiseWrapper().mergeCompound(initialNbtCompound);

// 设定显示名称
if (targetEntity != null && targetEntity.customName() != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected <X> void onPropertyWrite(SingleProperty<X> property, X value)
{
var properties = DisguiseProperties.INSTANCE.getOrThrow(LlamaProperties.class);

if (property.equals(properties.VARIANT))
if (property.equals(properties.COLOR))
{
var val = (Llama.Color) value;

Expand Down
83 changes: 59 additions & 24 deletions src/main/java/xyz/nifeather/morph/commands/MorphCommand.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package xyz.nifeather.morph.commands;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import io.papermc.paper.command.brigadier.CommandSourceStack;
import io.papermc.paper.command.brigadier.Commands;
import io.papermc.paper.command.brigadier.argument.ArgumentTypes;
import net.kyori.adventure.key.Key;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import xiamomc.pluginbase.Annotations.Initializer;
import xiamomc.pluginbase.Annotations.Resolved;
import xiamomc.pluginbase.Messages.FormattableMessage;
Expand All @@ -24,9 +23,13 @@
import xyz.nifeather.morph.messages.MessageUtils;
import xyz.nifeather.morph.messages.MorphStrings;
import xyz.nifeather.morph.misc.DisguiseMeta;
import xyz.nifeather.morph.misc.MorphParameters;
import xyz.nifeather.morph.misc.disguiseProperty.DisguiseProperties;
import xyz.nifeather.morph.misc.disguiseProperty.SingleProperty;
import xyz.nifeather.morph.misc.gui.DisguiseSelectScreenWrapper;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@SuppressWarnings("UnstableApiUsage")
Expand Down Expand Up @@ -56,10 +59,10 @@ public boolean register(Commands dispatcher)
Commands.argument("id", ArgumentTypes.key())
.suggests(this::suggestID)
.executes(this::execWithID)
//.then(
// Commands.argument("properties", propertyArgument)
// .executes(this::execExperimental)
//)
.then(
Commands.argument("properties", propertyArgument)
.executes(this::execWithProperties)
)
)
.build());

Expand All @@ -69,20 +72,16 @@ public boolean register(Commands dispatcher)
@Initializer
private void load()
{
this.propertyArgument.setProperty("morph:frog_variant", List.of("cold", "warm"));
this.propertyArgument.setProperty("morph:cat_variant", List.of("tabby", "black"));
}

private int execExperimental(CommandContext<CommandSourceStack> context)
{
var input = ValueMapArgumentType.get("properties", context);

input.forEach((k, v) ->
DisguiseProperties.INSTANCE.getAll().forEach((type, properties) ->
{
context.getSource().getSender().sendMessage("Key '%s', Value '%s'".formatted(k, v));
});
for (SingleProperty<?> property : properties.getValues())
{
var name = property.id();
var values = property.validInputs();

return 1;
propertyArgument.setProperty(name, values);
}
});
}

public @NotNull CompletableFuture<Suggestions> suggestID(CommandContext<CommandSourceStack> context, SuggestionsBuilder suggestionsBuilder)
Expand Down Expand Up @@ -136,20 +135,56 @@ public int executeNoArg(CommandContext<CommandSourceStack> context)
private int execWithID(CommandContext<CommandSourceStack> context)
{
var sender = context.getSource().getExecutor();
var executor = context.getSource().getExecutor();

if (!(executor instanceof Player player))
{
sender.sendMessage(MessageUtils.prefixes(sender, "Only players can execute disguise command"));

if (!(sender instanceof Player player))
return Command.SINGLE_SUCCESS;
}

String inputID = context.getArgument("id", Key.class).toString();
this.doDisguise(context.getSource().getSender(), player, inputID, null);

return 1;
}

private void doDisguise(CommandSender sender, Player who, String id, @Nullable Map<String, String> properties)
{
//伪装冷却
if (!morphManager.canMorph(player))
if (!morphManager.canMorph(who))
{
sender.sendMessage(MessageUtils.prefixes(player, MorphStrings.disguiseCoolingDownString()));
sender.sendMessage(MessageUtils.prefixes(sender, MorphStrings.disguiseCoolingDownString()));
return;
}

var parameters = MorphParameters.create(who, id)
.setSource(sender)
.setTargetedEntity(who.getTargetEntity(5));

if (properties != null)
parameters.withProperties(properties);

morphManager.morph(parameters);
}

private int execWithProperties(CommandContext<CommandSourceStack> context)
{
var sender = context.getSource().getSender();
var executor = context.getSource().getExecutor();

if (!(executor instanceof Player player))
{
sender.sendMessage(MessageUtils.prefixes(sender, "Only players can execute disguise command"));

return Command.SINGLE_SUCCESS;
}

String inputID = context.getArgument("id", Key.class).toString();
morphManager.morph(sender, player, inputID, player.getTargetEntity(5));
var propertiesInput = ValueMapArgumentType.get("properties", context);
var disguiseInput = context.getArgument("id", Key.class).toString();

this.doDisguise(sender, player, disguiseInput, propertiesInput);

return 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ public Map<String, String> parse(StringReader reader) throws CommandSyntaxExcept
// 读完了但是没有遇到闭合括号!
if (peekReader.peek() != ']')
throw new SimpleCommandExceptionType(Component.translatable("parsing.expected", "]")).createWithContext(reader);
else
reader.skip();

for (KeyValuePair pair : values)
{
Expand Down Expand Up @@ -370,7 +372,7 @@ public KeyValuePair parseOnce(StringReader reader, char terminator, char endOfSt
if (next == '=' && isKey)
{
isKey = false;
valueCursor = reader.canRead() ? reader.getCursor() + 1 : reader.getCursor();
valueCursor = reader.getCursor();

continue;
}
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/xyz/nifeather/morph/misc/MorphParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* 用于传递给morph方法的杂项参数
*/
Expand Down Expand Up @@ -33,6 +37,8 @@ public void setDisguiseIdentifier(String id)
@Nullable
public Entity targetedEntity;

public final Map<String, String> properties = new ConcurrentHashMap<>();

public MorphParameters setSource(CommandSender sender)
{
this.commandSource = sender;
Expand Down Expand Up @@ -68,6 +74,20 @@ public MorphParameters setForceExecute(boolean val)
return this;
}

public MorphParameters withProperties(Map<String, String> map)
{
this.properties.putAll(map);

return this;
}

public MorphParameters withProperties(@NotNull String key, @NotNull String value)
{
this.properties.put(key, value);

return this;
}

private MorphParameters(Player targetPlayer, String disguiseID)
{
this.targetPlayer = targetPlayer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package xyz.nifeather.morph.misc.disguiseProperty;

import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import org.bukkit.entity.EntityType;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import xiamomc.pluginbase.Exceptions.NullDependencyException;
import xyz.nifeather.morph.misc.disguiseProperty.values.*;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

Expand Down Expand Up @@ -37,6 +40,11 @@ public DisguiseProperties()
register(EntityType.CREEPER, new CreeperProperties());
}

public Map<EntityType, AbstractProperties> getAll()
{
return new Object2ObjectOpenHashMap<>(handlerMap);
}

public void register(EntityType type, AbstractProperties properties)
{
if (handlerMap.containsKey(type))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,22 @@ public class PropertyHandler
@Nullable
private AbstractProperties properties;

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

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

private void addProperty(SingleProperty<?> property)
public void updateFromPropertiesInput(Map<String, String> input)
{
var results = this.properties.readFromPropertiesInput(input);
results.forEach(this::writeGeneric);
}

private void initProperty(SingleProperty<?> property)
{
var random = property.getRandomValues();
if (!random.isEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import it.unimi.dsi.fastutil.objects.ObjectArrayList;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class SingleProperty<T>
Expand Down Expand Up @@ -33,6 +34,26 @@ public SingleProperty(String identifier, T defaultValue, Class<T> type)
this.type = type;
}

private final List<String> validValues = new ObjectArrayList<>();

public List<String> validInputs()
{
return new ObjectArrayList<>(validValues);
}

public SingleProperty<T> withValidInput(Collection<String> input)
{
this.validValues.addAll(input);

return this;
}
public SingleProperty<T> withValidInput(String... input)
{
this.validValues.addAll(Arrays.stream(input).toList());

return this;
}

private final List<T> randomValues = new ObjectArrayList<>();

public List<T> getRandomValues()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package xyz.nifeather.morph.misc.disguiseProperty.values;

import it.unimi.dsi.fastutil.Pair;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import xyz.nifeather.morph.FeatherMorphMain;
import xyz.nifeather.morph.misc.disguiseProperty.SingleProperty;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public abstract class AbstractProperties
{
Expand Down Expand Up @@ -40,4 +45,22 @@ public List<SingleProperty<?>> getValues()
{
return new ObjectArrayList<>(values);
}

@Nullable
protected abstract Pair<SingleProperty<?>, Object> parseSingleInput(String key, String value);

public final Map<SingleProperty<?>, Object> readFromPropertiesInput(Map<String, String> propertiesInput)
{
var map = new ConcurrentHashMap<SingleProperty<?>, Object>();

propertiesInput.forEach((key, value) ->
{
var pair = this.parseSingleInput(key, value);

if (pair != null)
map.put(pair.key(), pair.value());
});

return map;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
package xyz.nifeather.morph.misc.disguiseProperty.values;

import it.unimi.dsi.fastutil.Pair;
import org.jetbrains.annotations.Nullable;
import xyz.nifeather.morph.misc.disguiseProperty.SingleProperty;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ArmorStandProperties extends AbstractProperties
{
public final SingleProperty<Boolean> SHOW_ARMS = getSingle("show_arms", false);
public final SingleProperty<Boolean> SHOW_ARMS = getSingle("armor_stand_show_arms", false).withValidInput("true", "false");

public ArmorStandProperties()
{
registerSingle(SHOW_ARMS);
}

@Override
protected @Nullable Pair<SingleProperty<?>, Object> parseSingleInput(String key, String value)
{
if (key.equals(SHOW_ARMS.id()))
return Pair.of(SHOW_ARMS, Boolean.valueOf(value));

return null;
}
}
Loading

0 comments on commit 3b37389

Please sign in to comment.