properties

This commit is contained in:
Pyrbu 2023-05-11 05:41:15 +01:00
parent 2f05783da9
commit 5a65995699
5 changed files with 63 additions and 8 deletions

@ -6,10 +6,12 @@ import com.github.retrooper.packetevents.protocol.entity.type.EntityTypes;
import io.github.retrooper.packetevents.factory.spigot.SpigotPacketEventsBuilder;
import lol.pyr.director.adventure.command.CommandManager;
import lol.pyr.director.adventure.command.MultiCommand;
import lol.pyr.director.adventure.parse.primitive.BooleanParser;
import lol.pyr.director.adventure.parse.primitive.IntegerParser;
import lol.pyr.znpcsplus.api.ZApiProvider;
import lol.pyr.znpcsplus.commands.*;
import lol.pyr.znpcsplus.commands.hologram.*;
import lol.pyr.znpcsplus.commands.parsers.EntityPropertyParser;
import lol.pyr.znpcsplus.commands.parsers.NpcEntryParser;
import lol.pyr.znpcsplus.commands.parsers.NpcTypeParser;
import lol.pyr.znpcsplus.config.Configs;
@ -192,7 +194,9 @@ public class ZNpcsPlus extends JavaPlugin {
CommandManager manager = new CommandManager(this, ADVENTURE, context -> {});
manager.registerParser(NpcTypeImpl.class, new NpcTypeParser(context -> {}));
manager.registerParser(NpcEntryImpl.class, new NpcEntryParser(context -> {}));
manager.registerParser(EntityPropertyImpl.class, new EntityPropertyParser(context -> {}));
manager.registerParser(Integer.class, new IntegerParser(context -> {}));
manager.registerParser(Boolean.class, new BooleanParser(context -> {}));
manager.registerCommand("npc", new MultiCommand()
.addSubcommand("action", new ActionCommand())

@ -3,6 +3,11 @@ package lol.pyr.znpcsplus.commands;
import lol.pyr.director.adventure.command.CommandContext;
import lol.pyr.director.adventure.command.CommandHandler;
import lol.pyr.director.common.command.CommandExecutionException;
import lol.pyr.znpcsplus.entity.EntityPropertyImpl;
import lol.pyr.znpcsplus.npc.NpcEntryImpl;
import lol.pyr.znpcsplus.npc.NpcImpl;
import lol.pyr.znpcsplus.npc.NpcRegistryImpl;
import net.kyori.adventure.text.Component;
import java.util.Collections;
import java.util.List;
@ -10,11 +15,23 @@ import java.util.List;
public class PropertiesCommand implements CommandHandler {
@Override
public void run(CommandContext context) throws CommandExecutionException {
context.send("Not implemented yet!");
NpcEntryImpl entry = context.parse(NpcEntryImpl.class);
NpcImpl npc = entry.getNpc();
EntityPropertyImpl<?> property = context.parse(EntityPropertyImpl.class);
if (!npc.getType().getAllowedProperties().contains(property)) context.halt(Component.text("Property " + property.getName() + " not allowed for npc type " + npc.getType().getName()));
// TODO: implement all the parsers for the types used in EntityPropertyImpl
Object value = context.parse(property.getType());
npc.UNSAFE_setProperty(property, value);
context.send(Component.text("Set property " + property.getName() + " for NPC " + entry.getId() + " to " + value.toString()));
}
@Override
public List<String> suggest(CommandContext context) throws CommandExecutionException {
if (context.argSize() == 1) return context.suggestCollection(NpcRegistryImpl.get().modifiableIds());
if (context.argSize() == 2) return context.suggestStream(context.suggestionParse(1, NpcEntryImpl.class)
.getNpc().getType().getAllowedProperties().stream().map(EntityPropertyImpl::getName));
return Collections.emptyList();
}
}

@ -0,0 +1,23 @@
package lol.pyr.znpcsplus.commands.parsers;
import lol.pyr.director.adventure.command.CommandContext;
import lol.pyr.director.adventure.parse.ParserType;
import lol.pyr.director.common.command.CommandExecutionException;
import lol.pyr.director.common.message.Message;
import lol.pyr.znpcsplus.entity.EntityPropertyImpl;
import java.util.Deque;
@SuppressWarnings("rawtypes")
public class EntityPropertyParser extends ParserType<EntityPropertyImpl/*<?>*/> {
public EntityPropertyParser(Message<CommandContext> message) {
super(message);
}
@Override
public EntityPropertyImpl<?> parse(Deque<String> deque) throws CommandExecutionException {
EntityPropertyImpl<?> property = EntityPropertyImpl.getByName(deque.pop());
if (property == null) throw new CommandExecutionException();
return property;
}
}

@ -14,17 +14,24 @@ import java.util.Map;
public class EntityPropertyImpl<T> implements EntityProperty<T> {
private final String name;
private final T defaultValue;
private final Class<T> clazz;
private final PropertySerializer<T> serializer;
private final PropertyDeserializer<T> deserializer;
public EntityPropertyImpl(String name, PropertySerializer<T> serializer, PropertyDeserializer<T> deserializer) {
this(name, null, serializer, deserializer);
public EntityPropertyImpl(String name, Class<T> type, PropertySerializer<T> serializer, PropertyDeserializer<T> deserializer) {
this(name, null, type, serializer, deserializer);
}
@SuppressWarnings("unchecked")
public EntityPropertyImpl(String name, T defaultValue, PropertySerializer<T> serializer, PropertyDeserializer<T> deserializer) {
this(name, defaultValue, (Class<T>) defaultValue.getClass(), serializer, deserializer);
}
private EntityPropertyImpl(String name, T defaultValue, Class<T> clazz, PropertySerializer<T> serializer, PropertyDeserializer<T> deserializer) {
this.name = name.toUpperCase();
this.defaultValue = defaultValue;
this.clazz = clazz;
this.serializer = serializer;
this.deserializer = deserializer;
BY_NAME.put(this.name, this);
@ -51,6 +58,10 @@ public class EntityPropertyImpl<T> implements EntityProperty<T> {
return defaultValue;
}
public Class<T> getType() {
return clazz;
}
private final static Map<String, EntityPropertyImpl<?>> BY_NAME = new HashMap<>();
public static EntityPropertyImpl<?> getByName(String name) {
@ -81,10 +92,10 @@ public class EntityPropertyImpl<T> implements EntityProperty<T> {
private final static PropertyDeserializer<SkinDescriptor> DESCRIPTOR_DESERIALIZER = BaseSkinDescriptor::deserialize;
public static EntityPropertyImpl<Boolean> SKIN_LAYERS = new EntityPropertyImpl<>("skin_layers", true, BOOLEAN_SERIALIZER, BOOLEAN_DESERIALIZER);
public static EntityPropertyImpl<SkinDescriptor> SKIN = new EntityPropertyImpl<>("skin", DESCRIPTOR_SERIALIZER, DESCRIPTOR_DESERIALIZER);
public static EntityPropertyImpl<NamedTextColor> GLOW = new EntityPropertyImpl<>("glow", COLOR_SERIALIZER, COLOR_DESERIALIZER);
public static EntityPropertyImpl<SkinDescriptor> SKIN = new EntityPropertyImpl<>("skin", SkinDescriptor.class, DESCRIPTOR_SERIALIZER, DESCRIPTOR_DESERIALIZER);
public static EntityPropertyImpl<NamedTextColor> GLOW = new EntityPropertyImpl<>("glow", NamedTextColor.class, COLOR_SERIALIZER, COLOR_DESERIALIZER);
public static EntityPropertyImpl<Boolean> FIRE = new EntityPropertyImpl<>("fire", false, BOOLEAN_SERIALIZER, BOOLEAN_DESERIALIZER);
public static EntityPropertyImpl<Boolean> INVISIBLE = new EntityPropertyImpl<>("invisible", false, BOOLEAN_SERIALIZER, BOOLEAN_DESERIALIZER);
public static EntityPropertyImpl<Boolean> SILENT = new EntityPropertyImpl<>("silent", false, BOOLEAN_SERIALIZER, BOOLEAN_DESERIALIZER);
public static EntityPropertyImpl<Component> NAME = new EntityPropertyImpl<>("name", COMPONENT_SERIALIZER, COMPONENT_DESERIALIZER);
public static EntityPropertyImpl<Component> NAME = new EntityPropertyImpl<>("name", Component.class, COMPONENT_SERIALIZER, COMPONENT_DESERIALIZER);
}

@ -26,10 +26,10 @@ import java.util.Map;
* 1.19 https://wiki.vg/index.php?title=Entity_metadata
*/
public interface MetadataFactory {
EntityData skinLayers();
EntityData skinLayers(boolean enabled);
EntityData effects(boolean onFire, boolean glowing, boolean invisible);
Collection<EntityData> name(Component name);
EntityData silent();
EntityData silent(boolean enabled);
MetadataFactory factory = get();