added command import from citizens

This commit is contained in:
D3v1s0m 2024-06-03 11:08:08 +05:30
parent cc83a4295e
commit b8da4d82eb
No known key found for this signature in database
GPG Key ID: FA1F770C7B1D40C1
3 changed files with 73 additions and 2 deletions

@ -52,7 +52,7 @@ public class CitizensImporter implements DataImporter {
this.propertyRegistry = propertyRegistry;
this.skinCache = skinCache;
this.dataFile = dataFile;
this.traitsRegistry = new CitizensTraitsRegistry(typeRegistry, propertyRegistry, skinCache);
this.traitsRegistry = new CitizensTraitsRegistry(typeRegistry, propertyRegistry, skinCache, taskScheduler);
this.npcRegistry = npcRegistry;
}

@ -3,6 +3,7 @@ package lol.pyr.znpcsplus.conversion.citizens.model;
import lol.pyr.znpcsplus.api.entity.EntityPropertyRegistry;
import lol.pyr.znpcsplus.api.npc.NpcTypeRegistry;
import lol.pyr.znpcsplus.conversion.citizens.model.traits.*;
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
import lol.pyr.znpcsplus.skin.cache.MojangSkinCache;
import java.util.HashMap;
@ -10,7 +11,7 @@ import java.util.HashMap;
public class CitizensTraitsRegistry {
private final HashMap<String, CitizensTrait> traitMap = new HashMap<>();
public CitizensTraitsRegistry(NpcTypeRegistry typeRegistry, EntityPropertyRegistry propertyRegistry, MojangSkinCache skinCache) {
public CitizensTraitsRegistry(NpcTypeRegistry typeRegistry, EntityPropertyRegistry propertyRegistry, MojangSkinCache skinCache, TaskScheduler taskScheduler) {
register(new LocationTrait());
register(new TypeTrait(typeRegistry));
register(new ProfessionTrait(propertyRegistry));
@ -19,6 +20,7 @@ public class CitizensTraitsRegistry {
register(new MirrorTrait(propertyRegistry, skinCache));
register(new SkinLayersTrait(propertyRegistry));
register(new LookTrait(propertyRegistry));
register(new CommandTrait(taskScheduler));
}
public CitizensTrait getByName(String name) {

@ -0,0 +1,69 @@
package lol.pyr.znpcsplus.conversion.citizens.model.traits;
import lol.pyr.znpcsplus.api.interaction.InteractionType;
import lol.pyr.znpcsplus.conversion.citizens.model.SectionCitizensTrait;
import lol.pyr.znpcsplus.interaction.InteractionActionImpl;
import lol.pyr.znpcsplus.interaction.consolecommand.ConsoleCommandAction;
import lol.pyr.znpcsplus.interaction.playercommand.PlayerCommandAction;
import lol.pyr.znpcsplus.npc.NpcImpl;
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
import org.bukkit.configuration.ConfigurationSection;
import org.jetbrains.annotations.NotNull;
import java.util.Set;
public class CommandTrait extends SectionCitizensTrait {
private final TaskScheduler scheduler;
public CommandTrait(TaskScheduler scheduler) {
super("commandtrait");
this.scheduler = scheduler;
}
@Override
public @NotNull NpcImpl apply(NpcImpl npc, ConfigurationSection section) {
ConfigurationSection commands = section.getConfigurationSection("commands");
if (commands != null) {
Set<String> keys = commands.getKeys(false);
if (keys != null) {
for (String key : keys) {
ConfigurationSection commandSection = commands.getConfigurationSection(key);
String command = commandSection.getString("command");
String hand = commandSection.getString("hand", "BOTH");
InteractionType clickType = wrapClickType(hand);
boolean isPlayerCommand = commandSection.getBoolean("player", true);
int cooldown = commandSection.getInt("cooldown", 0);
int delay = commandSection.getInt("delay", 0);
if (command != null) {
InteractionActionImpl action;
if (isPlayerCommand) {
action = new PlayerCommandAction(scheduler, command, clickType, cooldown, delay);
} else {
action = new ConsoleCommandAction(scheduler, command, clickType, cooldown, delay);
}
npc.addAction(action);
}
}
}
}
return npc;
}
private InteractionType wrapClickType(String hand) {
if (hand == null) {
return InteractionType.ANY_CLICK;
}
switch (hand) {
case "RIGHT":
case "SHIFT_RIGHT":
return InteractionType.RIGHT_CLICK;
case "LEFT":
case "SHIFT_LEFT":
return InteractionType.LEFT_CLICK;
case "BOTH":
return InteractionType.ANY_CLICK;
}
throw new IllegalStateException();
}
}