Skip to content

Commit

Permalink
Improve triggers editor.
Browse files Browse the repository at this point in the history
  • Loading branch information
fullwall committed Mar 25, 2023
1 parent bdc2264 commit 4a55753
Show file tree
Hide file tree
Showing 28 changed files with 136 additions and 324 deletions.
8 changes: 5 additions & 3 deletions dist/pom.xml
@@ -1,4 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>net.citizensnpcs</groupId>
Expand Down Expand Up @@ -33,7 +35,7 @@
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
Expand Down Expand Up @@ -75,7 +77,7 @@
<version>${project.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>citizens-v1_18_R2</artifactId>
Expand Down
10 changes: 6 additions & 4 deletions main/src/main/java/net/citizensnpcs/commands/EditorCommands.java
@@ -1,6 +1,5 @@
package net.citizensnpcs.commands;

import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import net.citizensnpcs.api.command.Command;
Expand Down Expand Up @@ -48,15 +47,18 @@ public void equip(CommandContext args, Player player, NPC npc) throws CommandExc
desc = "Toggle the waypoint editor",
modifiers = { "path" },
min = 1,
max = 1,
flags = "*",
permission = "citizens.npc.edit.path")
@Requirements(selected = true, ownership = true)
public void path(CommandContext args, CommandSender player, NPC npc) {
public void path(CommandContext args, Player player, NPC npc) {
Editor editor = npc.getOrAddTrait(Waypoints.class).getEditor(player, args);
if (editor == null)
return;
Editor.enterOrLeave((Player) player, editor);
if (player.isConversing() && args.argsLength() > 1) {
player.acceptConversationInput(args.getJoinedStrings(1));
return;
}
Editor.enterOrLeave(player, editor);
}

@Command(
Expand Down
11 changes: 2 additions & 9 deletions main/src/main/java/net/citizensnpcs/commands/NPCCommands.java
Expand Up @@ -499,14 +499,8 @@ public void command(CommandContext args, CommandSender sender, NPC npc,
if (which == null)
throw new CommandException(Messages.NPC_COMMAND_INVALID_ERROR_MESSAGE,
Util.listValuesPretty(CommandTraitError.values()));
Player player = null;
if (args.argsLength() > 3) {
player = Bukkit.getPlayerExact(args.getString(3));
if (player == null) {
player = Bukkit.getPlayer(UUID.fromString(args.getString(3)));
}
}
commands.clearHistory(which, player);

commands.clearHistory(which, args.argsLength() > 3 ? args.getString(3) : args.getString(3));
Messaging.send(sender, Messages.NPC_COMMAND_ERRORS_CLEARED, Util.prettyEnum(which));
} else if (action.equalsIgnoreCase("sequential")) {
commands.setExecutionMode(commands.getExecutionMode() == ExecutionMode.SEQUENTIAL ? ExecutionMode.LINEAR
Expand Down Expand Up @@ -690,7 +684,6 @@ public void create(CommandContext args, CommandSender sender, NPC npc, @Flag("at
if (args.hasFlag('t') || temporaryTicks != null) {
registry = temporaryRegistry;
}

if (item != null) {
ItemStack stack = new ItemStack(Material.STONE, 1);
try {
Expand Down
6 changes: 5 additions & 1 deletion main/src/main/java/net/citizensnpcs/npc/CitizensNPC.java
Expand Up @@ -52,6 +52,7 @@
import net.citizensnpcs.trait.PacketNPC;
import net.citizensnpcs.trait.ScoreboardTrait;
import net.citizensnpcs.trait.SitTrait;
import net.citizensnpcs.trait.SkinLayers;
import net.citizensnpcs.trait.SneakTrait;
import net.citizensnpcs.util.ChunkCoord;
import net.citizensnpcs.util.Messages;
Expand Down Expand Up @@ -300,6 +301,10 @@ public boolean spawn(Location at, SpawnReason reason) {
entityController.create(at.clone(), this);
getEntity().setMetadata("NPC", new FixedMetadataValue(CitizensAPI.getPlugin(), true));

if (getEntity() instanceof SkinnableEntity && !hasTrait(SkinLayers.class)) {
((SkinnableEntity) getEntity()).setSkinFlags((byte) 0xFF);
}

Collection<Trait> onPreSpawn = traits.values();
for (Trait trait : onPreSpawn.toArray(new Trait[onPreSpawn.size()])) {
try {
Expand Down Expand Up @@ -530,7 +535,6 @@ public void update() {
}
updateCounter = 0;
}

updateCustomNameVisibility();

if (isLiving) {
Expand Down
Expand Up @@ -88,8 +88,8 @@ public NPC createNPC(EntityType type, UUID uuid, int id, String name) {
@Override
public NPC createNPCUsingItem(EntityType type, String name, ItemStack item) {
NPC npc = createNPC(type, name);
if (type == EntityType.DROPPED_ITEM || type == EntityType.FALLING_BLOCK || type == EntityType.GLOW_ITEM_FRAME
|| type == EntityType.ITEM_FRAME) {
if (type == EntityType.DROPPED_ITEM || type == EntityType.FALLING_BLOCK || type == EntityType.ITEM_FRAME
|| type.name().equals("GLOW_ITEM_FRAME") || type.name().equals("ITEM_DISPLAY")) {
npc.data().set(NPC.Metadata.ITEM_AMOUNT, item.getAmount());
npc.data().set(NPC.Metadata.ITEM_ID, item.getType().name());
npc.data().set(NPC.Metadata.ITEM_DATA, item.getData().getData());
Expand Down
13 changes: 12 additions & 1 deletion main/src/main/java/net/citizensnpcs/trait/CommandTrait.java
Expand Up @@ -118,7 +118,18 @@ private Transaction chargeCommandCosts(Player player, Hand hand) {
return action == null ? Transaction.success() : action.take(player);
}

public void clearHistory(CommandTraitError which, Player who) {
public void clearHistory(CommandTraitError which, String raw) {
if (which == CommandTraitError.ON_GLOBAL_COOLDOWN && raw != null) {
globalCooldowns.remove(BaseEncoding.base64().encode(raw.getBytes()));
return;
}
Player who = null;
if (raw != null) {
who = Bukkit.getPlayerExact(raw);
if (who == null) {
who = Bukkit.getPlayer(UUID.fromString(raw));
}
}
Collection<PlayerNPCCommand> toClear = Lists.newArrayList();
if (who != null) {
toClear.add(playerTracking.get(who.getUniqueId()));
Expand Down
Expand Up @@ -102,6 +102,7 @@ private NPC createHologram(String line, double heightOffset) {
if (Setting.PACKET_HOLOGRAMS.asBoolean()) {
hologramNPC.addTrait(PacketNPC.class);
}

hologramNPC.spawn(currentLoc.clone().add(0,
getEntityHeight()
+ (direction == HologramDirection.BOTTOM_UP ? heightOffset : getMaxHeight() - heightOffset),
Expand Down
Expand Up @@ -13,8 +13,6 @@
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.conversations.Conversation;
import org.bukkit.conversations.ConversationAbandonedEvent;
import org.bukkit.conversations.ConversationAbandonedListener;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.Action;
Expand Down Expand Up @@ -330,12 +328,7 @@ public void onPlayerChat(AsyncPlayerChatEvent event) {
@Override
public void run() {
conversation = TriggerEditPrompt.start(player, LinearWaypointEditor.this);
conversation.addConversationAbandonedListener(new ConversationAbandonedListener() {
@Override
public void conversationAbandoned(ConversationAbandonedEvent event) {
conversation = null;
}
});
conversation.addConversationAbandonedListener(e -> conversation = null);
}
});
} else if (message.equalsIgnoreCase("clear")) {
Expand All @@ -348,21 +341,13 @@ public void run() {
});
} else if (message.equalsIgnoreCase("toggle path") || message.equalsIgnoreCase("markers")) {
event.setCancelled(true);
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
togglePath();
}
});
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), () -> togglePath());
} else if (message.equalsIgnoreCase("cycle")) {
event.setCancelled(true);
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
cycle = !cycle;
Messaging.sendTr(event.getPlayer(), cycle ? Messages.LINEAR_WAYPOINT_EDITOR_CYCLE_SET
: Messages.LINEAR_WAYPOINT_EDITOR_CYCLE_UNSET);
}
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), () -> {
cycle = !cycle;
Messaging.sendTr(event.getPlayer(), cycle ? Messages.LINEAR_WAYPOINT_EDITOR_CYCLE_SET
: Messages.LINEAR_WAYPOINT_EDITOR_CYCLE_UNSET);
});
}
}
Expand Down
Expand Up @@ -173,17 +173,12 @@ public void onPlayerChat(AsyncPlayerChatEvent event) {
});
} else if (message.startsWith("delay")) {
event.setCancelled(true);
try {
delay = Integer.parseInt(message.split(" ")[1]);
if (currentGoal != null) {
currentGoal.setDelay(delay);
}
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(),
() -> Messaging.sendTr(sender, Messages.WANDER_WAYPOINTS_DELAY_SET, delay));
} catch (Exception e) {
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(),
() -> Messaging.sendErrorTr(sender, Messages.WANDER_WAYPOINTS_INVALID_DELAY));
delay = Util.parseTicks(message.split(" ")[1]);
if (currentGoal != null) {
currentGoal.setDelay(delay);
}
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(),
() -> Messaging.sendTr(sender, Messages.WANDER_WAYPOINTS_DELAY_SET, delay));
} else if (message.startsWith("worldguardregion")) {
event.setCancelled(true);
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), () -> {
Expand Down
Expand Up @@ -47,8 +47,12 @@ public void addTrigger(WaypointTrigger trigger) {

public void describeTriggers(CommandSender sender) {
String base = "";
for (WaypointTrigger trigger : getTriggers()) {
base += "\n - " + trigger.description();
if (triggers == null)
return;
for (int i = 0; i < triggers.size(); i++) {
base += "\n - " + triggers.get(i).description()
+ " [<hover:show_text:Remove trigger><click:run_command:/npc path remove_trigger " + i
+ "><u><red>-</click></hover>]";
}
Messaging.sendTr(sender, Messages.WAYPOINT_TRIGGER_LIST, base);
}
Expand Down
Expand Up @@ -35,7 +35,7 @@ public String description() {

@Override
public void onWaypointReached(NPC npc, Location waypoint) {
if (radius < 0) {
if (radius <= 0) {
for (Player player : npc.getEntity().getWorld().getPlayers()) {
for (String line : lines) {
Messaging.send(player, line);
Expand Down
Expand Up @@ -7,6 +7,7 @@
import org.bukkit.conversations.Prompt;
import org.bukkit.conversations.StringPrompt;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;

import net.citizensnpcs.api.util.Messaging;
Expand Down Expand Up @@ -48,7 +49,12 @@ public WaypointTrigger createFromShortInput(ConversationContext context, String

@Override
public String getPromptText(ConversationContext context) {
Messaging.sendTr((CommandSender) context.getForWhom(), Messages.CHAT_TRIGGER_PROMPT);
if (context.getSessionData("said") == Boolean.TRUE) {
Messaging.send((CommandSender) context.getForWhom(),
"Current lines:<br>- " + Joiner.on("<br>- ").join(lines));
} else {
Messaging.sendTr((CommandSender) context.getForWhom(), Messages.CHAT_TRIGGER_PROMPT);
}
return "";
}
}
@@ -1,32 +1,30 @@
package net.citizensnpcs.trait.waypoint.triggers;

import org.bukkit.command.CommandSender;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.NumericPrompt;
import org.bukkit.conversations.Prompt;
import org.bukkit.conversations.StringPrompt;

import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.Util;

public class DelayTriggerPrompt extends NumericPrompt implements WaypointTriggerPrompt {
public class DelayTriggerPrompt extends StringPrompt implements WaypointTriggerPrompt {
@Override
protected Prompt acceptValidatedInput(ConversationContext context, Number input) {
int delay = Math.max(input.intValue(), 0);
public Prompt acceptInput(ConversationContext context, String input) {
int delay = Math.max(0, Util.parseTicks(input));
context.setSessionData(WaypointTriggerPrompt.CREATED_TRIGGER_KEY, new DelayTrigger(delay));
return (Prompt) context.getSessionData(WaypointTriggerPrompt.RETURN_PROMPT_KEY);
}

@Override
public WaypointTrigger createFromShortInput(ConversationContext context, String input) {
try {
int delay = Math.max(Integer.parseInt(input), 0);
return new DelayTrigger(delay);
} catch (NumberFormatException ex) {
return null;
}
return new DelayTrigger(Math.max(0, Util.parseTicks(input)));
}

@Override
public String getPromptText(ConversationContext context) {
return Messaging.tr(Messages.DELAY_TRIGGER_PROMPT);
Messaging.sendTr((CommandSender) context.getForWhom(), Messages.DELAY_TRIGGER_PROMPT);
return "";
}
}
@@ -1,5 +1,6 @@
package net.citizensnpcs.trait.waypoint.triggers;

import org.bukkit.command.CommandSender;
import org.bukkit.conversations.ConversationContext;
import org.bukkit.conversations.NumericPrompt;
import org.bukkit.conversations.Prompt;
Expand Down Expand Up @@ -27,6 +28,7 @@ public WaypointTrigger createFromShortInput(ConversationContext context, String

@Override
public String getPromptText(ConversationContext context) {
return Messaging.tr(Messages.SPEED_TRIGGER_PROMPT);
Messaging.sendTr((CommandSender) context.getForWhom(), Messages.SPEED_TRIGGER_PROMPT);
return "";
}
}
Expand Up @@ -55,7 +55,8 @@ public WaypointTrigger createFromShortInput(ConversationContext context, String

@Override
public String getPromptText(ConversationContext context) {
return Messaging.tr(Messages.WAYPOINT_TRIGGER_TELEPORT_PROMPT);
Messaging.sendTr((CommandSender) context.getForWhom(), Messages.WAYPOINT_TRIGGER_TELEPORT_PROMPT);
return "";
}

private static final Pattern PATTERN = Pattern.compile("here|back|[\\p{L}]+?:[0-9]+?:[0-9]+?:[0-9]+?",
Expand Down
Expand Up @@ -39,31 +39,21 @@ public Prompt acceptInput(ConversationContext context, String input) {
WaypointTrigger returned = ((WaypointTriggerPrompt) prompt).createFromShortInput(context, extraInput);
if (returned != null) {
context.setSessionData(WaypointTriggerPrompt.CREATED_TRIGGER_KEY, returned);
return this;
context.setSessionData("said", false);
return (Prompt) context.getSessionData("previous");
}
}
return prompt;
}

@Override
public String getPromptText(ConversationContext context) {
WaypointTrigger returned = (WaypointTrigger) context.getSessionData(WaypointTriggerPrompt.CREATED_TRIGGER_KEY);

if (returned != null) {
if (editor.getCurrentWaypoint() != null) {
editor.getCurrentWaypoint().addTrigger(returned);
context.setSessionData(WaypointTriggerPrompt.CREATED_TRIGGER_KEY, null);
Messaging.sendTr((CommandSender) context.getForWhom(), Messages.WAYPOINT_TRIGGER_ADDED_SUCCESSFULLY,
returned.description());
editor.getCurrentWaypoint().describeTriggers((CommandSender) context.getForWhom());
} else {
Messaging.sendErrorTr((CommandSender) context.getForWhom(), Messages.WAYPOINT_TRIGGER_EDITOR_INACTIVE);
}
}
if (context.getSessionData("said") == Boolean.TRUE)
return "";
context.setSessionData("said", true);
context.setSessionData(WaypointTriggerPrompt.RETURN_PROMPT_KEY, this);
return Messaging.tr(Messages.WAYPOINT_TRIGGER_ADD_PROMPT, WaypointTriggerRegistry.describeValidTriggerNames());
context.setSessionData(WaypointTriggerPrompt.RETURN_PROMPT_KEY, context.getSessionData("previous"));
Messaging.sendTr((CommandSender) context.getForWhom(), Messages.WAYPOINT_TRIGGER_ADD_PROMPT,
WaypointTriggerRegistry.describeValidTriggerNames());
return "";
}
}

0 comments on commit 4a55753

Please sign in to comment.