mirror of
https://github.com/FancyInnovations/FancyPlugins.git
synced 2025-12-06 07:43:36 +00:00
fancynpcs: Move CenterCMD and TurnToPlayerDistanceCMD
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package de.oliver.fancynpcs.commands.npc;
|
||||
|
||||
import de.oliver.fancylib.translations.Translator;
|
||||
import de.oliver.fancynpcs.FancyNpcs;
|
||||
import de.oliver.fancynpcs.api.Npc;
|
||||
import de.oliver.fancynpcs.api.NpcData;
|
||||
import de.oliver.fancynpcs.api.events.NpcModifyEvent;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.incendo.cloud.annotations.Command;
|
||||
import org.incendo.cloud.annotations.Permission;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public enum CenterCMD {
|
||||
INSTANCE; // SINGLETON
|
||||
|
||||
private final Translator translator = FancyNpcs.getInstance().getTranslator();
|
||||
|
||||
@Command("npc center <npc>")
|
||||
@Permission("fancynpcs.command.npc.center")
|
||||
public void onCenter(
|
||||
final @NotNull CommandSender sender,
|
||||
final @NotNull Npc npc
|
||||
) {
|
||||
NpcData npcData = npc.getData();
|
||||
Location location = npcData.getLocation();
|
||||
|
||||
if (location == null) {
|
||||
translator.translate("npc_center_failure_no_location").replace("npc", npcData.getName()).send(sender);
|
||||
return;
|
||||
}
|
||||
|
||||
// Center the NPC on the block
|
||||
Location centeredLocation = location.clone();
|
||||
centeredLocation.setX(centeredLocation.getBlockX() + 0.5);
|
||||
centeredLocation.setY(centeredLocation.getY());
|
||||
centeredLocation.setZ(centeredLocation.getBlockZ() + 0.5);
|
||||
|
||||
// Trigger the modify event
|
||||
if (new NpcModifyEvent(npc, NpcModifyEvent.NpcModification.LOCATION, centeredLocation, sender).callEvent()) {
|
||||
npcData.setLocation(centeredLocation);
|
||||
npc.updateForAll();
|
||||
|
||||
translator.translate("npc_center_success")
|
||||
.replace("npc", npcData.getName())
|
||||
.replace("x", String.format("%.2f", centeredLocation.getX()))
|
||||
.replace("y", String.format("%.2f", centeredLocation.getY()))
|
||||
.replace("z", String.format("%.2f", centeredLocation.getZ()))
|
||||
.send(sender);
|
||||
} else {
|
||||
translator.translate("command_npc_modification_cancelled").send(sender);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package de.oliver.fancynpcs.commands.npc;
|
||||
|
||||
import de.oliver.fancylib.translations.Translator;
|
||||
import de.oliver.fancynpcs.FancyNpcs;
|
||||
import de.oliver.fancynpcs.api.Npc;
|
||||
import de.oliver.fancynpcs.api.events.NpcModifyEvent;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.incendo.cloud.annotations.Argument;
|
||||
import org.incendo.cloud.annotations.Command;
|
||||
import org.incendo.cloud.annotations.Permission;
|
||||
import org.incendo.cloud.annotations.parser.Parser;
|
||||
import org.incendo.cloud.annotations.suggestion.Suggestions;
|
||||
import org.incendo.cloud.context.CommandContext;
|
||||
import org.incendo.cloud.context.CommandInput;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public enum TurnToPlayerDistanceCMD {
|
||||
INSTANCE;
|
||||
|
||||
private final Translator translator = FancyNpcs.getInstance().getTranslator();
|
||||
|
||||
// Storing in a static variable to avoid re-creating the array each time suggestion is requested.
|
||||
private final List<String> DISTANCE_SUGGESTIONS = List.of("default");
|
||||
|
||||
@Command("npc turn_to_player_distance <npc> <distance>")
|
||||
@Permission("fancynpcs.command.npc.turn_to_player_distance")
|
||||
public void onTurnToPlayerDistance(
|
||||
final @NotNull CommandSender sender,
|
||||
final @NotNull Npc npc,
|
||||
final @Argument(parserName = "TurnToPlayerDistanceCMD/distance") int distance
|
||||
) {
|
||||
if (distance < -1) {
|
||||
translator.translate("npc_turn_to_player_distance_invalid").send(sender);
|
||||
return;
|
||||
}
|
||||
|
||||
if (new NpcModifyEvent(npc, NpcModifyEvent.NpcModification.TURN_TO_PLAYER_DISTANCE, distance, sender).callEvent()) {
|
||||
npc.getData().setTurnToPlayerDistance(distance);
|
||||
|
||||
if (distance == -1) {
|
||||
// Using default distance
|
||||
int defaultDistance = FancyNpcs.getInstance().getFancyNpcConfig().getTurnToPlayerDistance();
|
||||
translator.translate("npc_turn_to_player_distance_default")
|
||||
.replace("npc", npc.getData().getName())
|
||||
.replace("distance", String.valueOf(defaultDistance))
|
||||
.send(sender);
|
||||
} else {
|
||||
// Using custom distance
|
||||
translator.translate("npc_turn_to_player_distance_set")
|
||||
.replace("npc", npc.getData().getName())
|
||||
.replace("distance", String.valueOf(distance))
|
||||
.send(sender);
|
||||
}
|
||||
} else {
|
||||
translator.translate("command_npc_modification_cancelled").send(sender);
|
||||
}
|
||||
}
|
||||
|
||||
/* PARSERS AND SUGGESTIONS */
|
||||
|
||||
@Parser(name = "TurnToPlayerDistanceCMD/distance", suggestions = "TurnToPlayerDistanceCMD/distance")
|
||||
public @NotNull Integer parse(final CommandContext<CommandSender> context, final CommandInput input) {
|
||||
// If 'default' string is provided, it is being handled as -1.
|
||||
if (input.peekString().equalsIgnoreCase("default")) {
|
||||
input.readString();
|
||||
return -1;
|
||||
}
|
||||
// Otherwise, reading next argument as int.
|
||||
return input.readInteger();
|
||||
}
|
||||
|
||||
@Suggestions("TurnToPlayerDistanceCMD/distance")
|
||||
public @NotNull List<String> suggest(final CommandContext<CommandSender> context, final CommandInput input) {
|
||||
return DISTANCE_SUGGESTIONS;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user