Skip to content

Commit

Permalink
Commands and translations overhaul (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
Grabsky authored May 23, 2024
1 parent 690bcfb commit 40cff59
Show file tree
Hide file tree
Showing 48 changed files with 2,921 additions and 2,109 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.oliver.fancynpcs.api;

import de.oliver.fancylib.LanguageConfig;
import de.oliver.fancylib.serverSoftware.schedulers.FancyScheduler;
import de.oliver.fancylib.translations.Translator;
import org.bukkit.Bukkit;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
Expand Down Expand Up @@ -32,5 +32,5 @@ static FancyNpcsPlugin get() {

AttributeManager getAttributeManager();

LanguageConfig getLanguageConfig();
Translator getTranslator();
}
31 changes: 12 additions & 19 deletions api/src/main/java/de/oliver/fancynpcs/api/Npc.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import de.oliver.fancylib.LanguageConfig;
import de.oliver.fancylib.MessageHelper;
import de.oliver.fancylib.RandomUtils;
import de.oliver.fancylib.translations.Translator;
import de.oliver.fancynpcs.api.events.NpcInteractEvent;
import de.oliver.fancynpcs.api.events.NpcInteractEvent.InteractionType;
import de.oliver.fancynpcs.api.util.Interval;
import de.oliver.fancynpcs.api.util.Interval.Unit;
import me.dave.chatcolorhandler.ChatColorHandler;
import me.dave.chatcolorhandler.ModernChatColorHandler;
import me.dave.chatcolorhandler.parsers.custom.PlaceholderAPIParser;
Expand All @@ -16,7 +17,6 @@
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;

import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import java.util.Random;
Expand All @@ -26,13 +26,12 @@
public abstract class Npc {

private static final NpcAttribute INVISIBLE_ATTRIBUTE = FancyNpcsPlugin.get().getAttributeManager().getAttributeByName(EntityType.PLAYER, "invisible");
private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("##.##");
private static final char[] localNameChars = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'k', 'l', 'm', 'n', 'o', 'r'};
protected final Map<UUID, Boolean> isTeamCreated = new ConcurrentHashMap<>();
protected final Map<UUID, Boolean> isVisibleForPlayer = new ConcurrentHashMap<>();
protected final Map<UUID, Boolean> isLookingAtPlayer = new ConcurrentHashMap<>();
protected final Map<UUID, Long> lastPlayerInteraction = new ConcurrentHashMap<>();
private final LanguageConfig lang = FancyNpcsPlugin.get().getLanguageConfig();
private final Translator translator = FancyNpcsPlugin.get().getTranslator();
protected NpcData data;
protected boolean saveToFile;

Expand Down Expand Up @@ -150,22 +149,17 @@ public void interact(Player player) {

public void interact(Player player, InteractionType interactionType) {
if (data.getInteractionCooldown() > 0) {
if (lastPlayerInteraction.containsKey(player.getUniqueId())) {
long nextAllowedInteraction = lastPlayerInteraction.get(player.getUniqueId()) + Math.round(data.getInteractionCooldown() * 1000L);
if (nextAllowedInteraction > System.currentTimeMillis()) {
if (!FancyNpcsPlugin.get().getFancyNpcConfig().isInteractionCooldownMessageDisabled()) {
float timeLeft = (nextAllowedInteraction - System.currentTimeMillis()) / 1000F;
String cooldownMessage = lang.get("on-interaction-cooldown", "time", DECIMAL_FORMAT.format(timeLeft));
MessageHelper.warning(player, cooldownMessage);
}
return;
}
final long interactionCooldownMillis = (long) (data.getInteractionCooldown() * 1000);
final long lastInteractionMillis = lastPlayerInteraction.getOrDefault(player.getUniqueId(), 0L);
final Interval interactionCooldownLeft = Interval.between(lastInteractionMillis + interactionCooldownMillis, System.currentTimeMillis(), Unit.MILLISECONDS);
if (interactionCooldownLeft.as(Unit.MILLISECONDS) > 0 && !FancyNpcsPlugin.get().getFancyNpcConfig().isInteractionCooldownMessageDisabled()) {
translator.translate("interaction_on_cooldown").replace("time", interactionCooldownLeft.toString()).send(player);
return;
}

lastPlayerInteraction.put(player.getUniqueId(), System.currentTimeMillis());
}

NpcInteractEvent npcInteractEvent = new NpcInteractEvent(this, data.getPlayerCommands(), data.getServerCommand(), data.getOnClick(), player, interactionType);
NpcInteractEvent npcInteractEvent = new NpcInteractEvent(this, data.getPlayerCommands(), data.getServerCommands(), data.getOnClick(), player, interactionType);
npcInteractEvent.callEvent();

if (npcInteractEvent.isCancelled()) {
Expand All @@ -190,8 +184,7 @@ public void interact(Player player, InteractionType interactionType) {
}

// serverCommand
if (data.getServerCommand() != null && data.getServerCommand().length() > 0) {
String command = data.getServerCommand();
for (String command : data.getServerCommands()) {
command = command.replace("{player}", player.getName());

String finalCommand = ChatColorHandler.translate(command, player, List.of(PlaceholderAPIParser.class));
Expand Down
25 changes: 18 additions & 7 deletions api/src/main/java/de/oliver/fancynpcs/api/NpcData.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public class NpcData {
private Map<NpcEquipmentSlot, ItemStack> equipment;
private Consumer<Player> onClick;
private boolean turnToPlayer;
private String serverCommand;
private List<String> playerCommands;
private List<String> serverCommands;
private List<String> messages;
private boolean sendMessagesRandomly;
private float interactionCooldown;
Expand All @@ -55,7 +55,7 @@ public NpcData(
Consumer<Player> onClick,
List<String> messages,
boolean sendMessagesRandomly,
String serverCommand,
List<String> serverCommands,
List<String> playerCommands,
float interactionCooldown,
Map<NpcAttribute, String> attributes,
Expand All @@ -76,7 +76,7 @@ public NpcData(
this.equipment = equipment;
this.onClick = onClick;
this.turnToPlayer = turnToPlayer;
this.serverCommand = serverCommand;
this.serverCommands = serverCommands;
this.playerCommands = playerCommands;
this.messages = messages;
this.sendMessagesRandomly = sendMessagesRandomly;
Expand Down Expand Up @@ -105,6 +105,7 @@ public NpcData(String name, UUID creator, Location location) {
};
this.turnToPlayer = false;
this.messages = new ArrayList<>();
this.serverCommands = new ArrayList<>();
this.playerCommands = new ArrayList<>();
this.sendMessagesRandomly = false;
this.interactionCooldown = 0;
Expand Down Expand Up @@ -253,16 +254,26 @@ public NpcData setTurnToPlayer(boolean turnToPlayer) {
return this;
}

public String getServerCommand() {
return serverCommand;
public List<String> getServerCommands() {
return serverCommands;
}

public NpcData setServerCommand(String serverCommand) {
this.serverCommand = serverCommand;
public NpcData setServerCommands(List<String> serverCommands) {
this.serverCommands = serverCommands;
isDirty = true;
return this;
}

public void addServerCommand(String command) {
serverCommands.add(command);
isDirty = true;
}

public void removeServerCommand(int index) {
serverCommands.remove(index);
isDirty = true;
}

public List<String> getPlayerCommands() {
return playerCommands;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.function.Consumer;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* Is fired when a player interacts with a NPC
*/
Expand All @@ -23,18 +22,18 @@ public class NpcInteractEvent extends Event implements Cancellable {
@Nullable
private final List<String> playerCommands;
@Nullable
private final String serverCommand;
private final List<String> serverCommands;
@NotNull
private final Consumer<Player> onClick;
@NotNull
private final Player player;
private final InteractionType interactionType;
private boolean isCancelled;

public NpcInteractEvent(@NotNull Npc npc, @Nullable List<String> playerCommands, @Nullable String serverCommand, @NotNull Consumer<Player> onClick, @NotNull Player player, @NotNull InteractionType interactionType) {
public NpcInteractEvent(@NotNull Npc npc, @Nullable List<String> playerCommands, @Nullable List<String> serverCommands, @NotNull Consumer<Player> onClick, @NotNull Player player, @NotNull InteractionType interactionType) {
this.npc = npc;
this.playerCommands = playerCommands;
this.serverCommand = serverCommand;
this.serverCommands = serverCommands;
this.onClick = onClick;
this.player = player;
this.interactionType = interactionType;
Expand All @@ -59,10 +58,10 @@ public static HandlerList getHandlerList() {
}

/**
* @return the command that the server will run
* @return the commands that the server will run
*/
public @Nullable String getServerCommand() {
return serverCommand;
public @Nullable List<String> getServerCommands() {
return serverCommands;
}

/**
Expand Down Expand Up @@ -104,7 +103,9 @@ public void setCancelled(boolean cancel) {
public enum InteractionType {
LEFT_CLICK,
RIGHT_CLICK,
/** {@link InteractionType#CUSTOM InteractionType#CUSTOM} represents interactions invoked by the API. */
/**
* {@link InteractionType#CUSTOM InteractionType#CUSTOM} represents interactions invoked by the API.
*/
CUSTOM
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,38 @@ public void setCancelled(boolean cancel) {
}

public enum NpcModification {
LOCATION,
SKIN,
ATTRIBUTE,
COLLIDABLE,
DISPLAY_NAME,
EQUIPMENT,
SERVER_COMMAND,
PLAYER_COMMAND,
SHOW_IN_TAB,
GLOWING,
GLOWING_COLOR,
TURN_TO_PLAYER,
CUSTOM_MESSAGE,
TYPE,
ATTRIBUTE,
COLLIDABLE,
INTERACTION_COOLDOWN,
LOCATION,
MIRROR_SKIN,
PLAYER_COMMAND,
SERVER_COMMAND,
SHOW_IN_TAB,
SKIN,
TURN_TO_PLAYER,
TYPE,
// Messages.
MESSAGE_ADD,
MESSAGE_SET,
MESSAGE_REMOVE,
MESSAGE_CLEAR,
MESSAGE_SEND_RANDOMLY,
// Player commands.
PLAYER_COMMAND_ADD,
PLAYER_COMMAND_SET,
PLAYER_COMMAND_REMOVE,
PLAYER_COMMAND_CLEAR,
PLAYER_COMMAND_SEND_RANDOMLY,
// Server commands.
SERVER_COMMAND_ADD,
SERVER_COMMAND_SET,
SERVER_COMMAND_REMOVE,
SERVER_COMMAND_CLEAR,
SERVER_COMMAND_SEND_RANDOMLY
}
}
Loading

0 comments on commit 40cff59

Please sign in to comment.