actions base

This commit is contained in:
Pyrbu 2023-04-25 18:14:15 +01:00
parent 3f9c027005
commit eb6d1fc5c9
8 changed files with 54 additions and 59 deletions

@ -1,22 +0,0 @@
package io.github.znetworkw.znpcservers.exception;
import java.io.Serial;
/**
* @author xCodiq - 20/04/2023
*/
public class CommandException extends Exception {
@Serial private static final long serialVersionUID = 1L;
public CommandException(String message) {
super(message);
}
public CommandException(String message, Throwable cause) {
super(message, cause);
}
public CommandException(Throwable cause) {
super(cause);
}
}

@ -1,7 +0,0 @@
package io.github.znetworkw.znpcservers.exception;
public class CommandExecuteException extends CommandException {
public CommandExecuteException(String message, Throwable cause) {
super(message, cause);
}
}

@ -1,7 +0,0 @@
package io.github.znetworkw.znpcservers.exception;
public class CommandPermissionException extends CommandException {
public CommandPermissionException(String message) {
super(message);
}
}

@ -1,4 +1,4 @@
package io.github.znetworkw.znpcservers.commands.list.inventory; package io.github.znetworkw.znpcservers.gui;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.base.Splitter; import com.google.common.base.Splitter;
@ -65,7 +65,7 @@ public class ConversationGUI extends ZInventory {
for (int i = slots - (getRows() - 9); i < min; ++i) { for (int i = slots - (getRows() - 9); i < min; ++i) {
Conversation conversation = ConfigurationConstants.NPC_CONVERSATIONS.get(i); Conversation conversation = ConfigurationConstants.NPC_CONVERSATIONS.get(i);
this.addItem(ItemStackBuilder.forMaterial(Material.PAPER).setName(ChatColor.GREEN + conversation.getName()).setLore("&7this conversation has &b" + conversation.getTexts().size() + " &7texts,", "&7it will activate when a player is on a &b" + conversation.getRadius() + "x" + conversation.getRadius() + " &7radius,", "&7or when a player interacts with an npc.", "&7when the conversation is finish, there is a &b" + conversation.getDelay() + "s &7delay to start again.", "&f&lUSES", " &bLeft-click &7to manage texts.", " &bRight-click &7to add a new text.", " &bQ &7to change the radius.", " &bMiddle-click &7to change the cooldown.").build(), i - ((getRows() - 9) * (pageID - 1)), clickEvent -> { this.addItem(ItemStackBuilder.forMaterial(Material.PAPER).setName(ChatColor.GREEN + conversation.getName()).setLore("&7this conversation has &b" + conversation.getTexts().size() + " &7texts,", "&7it will activate when a player is on a &b" + conversation.getRadius() + "x" + conversation.getRadius() + " &7radius,", "&7or when a player interacts with an npc.", "&7when the conversation is finish, there is a &b" + conversation.getCooldown() + "s &7delay to start again.", "&f&lUSES", " &bLeft-click &7to manage texts.", " &bRight-click &7to add a new text.", " &bQ &7to change the radius.", " &bMiddle-click &7to change the cooldown.").build(), i - ((getRows() - 9) * (pageID - 1)), clickEvent -> {
if (clickEvent.getClick() == ClickType.DROP) { if (clickEvent.getClick() == ClickType.DROP) {
Utils.sendTitle(this.getPlayer(), "&b&lCHANGE RADIUS", "&7Type the new radius..."); Utils.sendTitle(this.getPlayer(), "&b&lCHANGE RADIUS", "&7Type the new radius...");
EventService.addService(User.get(this.getPlayer()), AsyncPlayerChatEvent.class).addConsumer(event -> { EventService.addService(User.get(this.getPlayer()), AsyncPlayerChatEvent.class).addConsumer(event -> {
@ -226,7 +226,7 @@ public class ConversationGUI extends ZInventory {
for (int i = slots - (getRows() - 9); i < min; i++) { for (int i = slots - (getRows() - 9); i < min; i++) {
ConversationKey conversationKey = this.conversation.getTexts().get(i); ConversationKey conversationKey = this.conversation.getTexts().get(i);
this.addItem(ItemStackBuilder.forMaterial(Material.NAME_TAG).setName(ChatColor.AQUA + conversationKey.getTextFormatted() + "....").setLore("&7this conversation text has a delay of &b" + conversationKey.getDelay() + "s &7to be executed,", "&7the sound for the text is &b" + (conversationKey.getSoundName() == null ? "NONE" : conversationKey.getSoundName()) + "&7,", "&7before sending the text there is a delay of &b" + conversationKey.getDelay() + "s", "&7the index for the text is &b" + i + "&7,", "&7and the conversation has currently &b" + conversationKey.getActions().size() + " actions&7.", "&f&lUSES", " &bLeft-click &7to change the position.", " &bRight-click &7to remove text.", " &bLeft-Shift-click &7to change the sound.", " &bMiddle-click &7to change the delay.", " &bRight-Shift-click &7to edit the text.", " &bQ &7to manage actions.").build(), i, clickEvent -> { this.addItem(ItemStackBuilder.forMaterial(Material.NAME_TAG).setName(ChatColor.AQUA + conversationKey.getTextFormatted() + "....").setLore("&7this conversation text has a delay of &b" + conversationKey.getCooldown() + "s &7to be executed,", "&7the sound for the text is &b" + (conversationKey.getSoundName() == null ? "NONE" : conversationKey.getSoundName()) + "&7,", "&7before sending the text there is a delay of &b" + conversationKey.getCooldown() + "s", "&7the index for the text is &b" + i + "&7,", "&7and the conversation has currently &b" + conversationKey.getActions().size() + " actions&7.", "&f&lUSES", " &bLeft-click &7to change the position.", " &bRight-click &7to remove text.", " &bLeft-Shift-click &7to change the sound.", " &bMiddle-click &7to change the delay.", " &bRight-Shift-click &7to edit the text.", " &bQ &7to manage actions.").build(), i, clickEvent -> {
if (clickEvent.getClick() == ClickType.SHIFT_LEFT) { if (clickEvent.getClick() == ClickType.SHIFT_LEFT) {
Utils.sendTitle(this.getPlayer(), "&c&lCHANGE SOUND", "&7Type the new sound..."); Utils.sendTitle(this.getPlayer(), "&c&lCHANGE SOUND", "&7Type the new sound...");
EventService.addService(User.get(this.getPlayer()), AsyncPlayerChatEvent.class).addConsumer(event -> { EventService.addService(User.get(this.getPlayer()), AsyncPlayerChatEvent.class).addConsumer(event -> {

@ -8,11 +8,7 @@ import lol.pyr.znpcsplus.ZNPCsPlus;
import lol.pyr.znpcsplus.npc.NPC; import lol.pyr.znpcsplus.npc.NPC;
import lol.pyr.znpcsplus.npc.NPCRegistry; import lol.pyr.znpcsplus.npc.NPCRegistry;
import lol.pyr.znpcsplus.user.User; import lol.pyr.znpcsplus.user.User;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.inventory.ClickType;
import java.util.List;
public class InteractionPacketListener implements PacketListener { public class InteractionPacketListener implements PacketListener {
@Override @Override
@ -28,17 +24,9 @@ public class InteractionPacketListener implements PacketListener {
if (npc == null) return; if (npc == null) return;
ZNPCsPlus.SCHEDULER.runNextTick(() -> { ZNPCsPlus.SCHEDULER.runNextTick(() -> {
Bukkit.getServer().getPluginManager().callEvent(new NPCInteractEvent(player, clickType, npc)); for (NPCAction action : npc.getActions()) {
List<NPCAction> actions = npc.getNpcPojo().getClickActions(); if (action.getCooldown() > 0 && !user.actionCooldownCheck(action)) continue;
if (actions == null) return; action.run(player);
for (NPCAction action : actions) {
if (action.getClickType() != ClickType.DEFAULT && action.getClickType() != clickType) continue;
if (action.getDelay() > 0) {
int actionId = npc.getNpcPojo().getClickActions().indexOf(action);
if (user.getLastClicked().containsKey(actionId) && System.nanoTime() - user.getLastClicked().get(actionId) < action.getFixedDelay()) continue;
user.getLastClicked().put(actionId, System.nanoTime());
}
action.run(user, action.getAction());
} }
}); });
} }

@ -0,0 +1,25 @@
package lol.pyr.znpcsplus.interaction;
import org.bukkit.entity.Player;
import java.util.UUID;
public abstract class NPCAction {
private final UUID id;
private final long delay;
protected NPCAction(long delay) {
this.id = UUID.randomUUID();
this.delay = delay;
}
public UUID getUuid() {
return id;
}
public long getCooldown() {
return delay;
}
public abstract void run(Player player);
}

@ -2,14 +2,12 @@ package lol.pyr.znpcsplus.npc;
import lol.pyr.znpcsplus.entity.PacketEntity; import lol.pyr.znpcsplus.entity.PacketEntity;
import lol.pyr.znpcsplus.entity.PacketLocation; import lol.pyr.znpcsplus.entity.PacketLocation;
import lol.pyr.znpcsplus.interaction.NPCAction;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.HashMap; import java.util.*;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class NPC { public class NPC {
protected static final Set<NPC> _ALL_NPCS = new HashSet<>(); protected static final Set<NPC> _ALL_NPCS = new HashSet<>();
@ -21,6 +19,7 @@ public class NPC {
private NPCType type; private NPCType type;
private final Map<NPCProperty<?>, Object> propertyMap = new HashMap<>(); private final Map<NPCProperty<?>, Object> propertyMap = new HashMap<>();
private final Set<NPCAction> actions = new HashSet<>();
public NPC(World world, NPCType type, PacketLocation location) { public NPC(World world, NPCType type, PacketLocation location) {
this.worldName = world.getName(); this.worldName = world.getName();
@ -124,4 +123,12 @@ public class NPC {
public void removeProperty(NPCProperty<?> key) { public void removeProperty(NPCProperty<?> key) {
propertyMap.remove(key); propertyMap.remove(key);
} }
public Collection<NPCAction> getActions() {
return Collections.unmodifiableSet(actions);
}
public void addAction(NPCAction action) {
actions.add(action);
}
} }

@ -1,6 +1,7 @@
package lol.pyr.znpcsplus.user; package lol.pyr.znpcsplus.user;
import io.github.znetworkw.znpcservers.user.EventService; import io.github.znetworkw.znpcservers.user.EventService;
import lol.pyr.znpcsplus.interaction.NPCAction;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -27,6 +28,7 @@ public class User {
private final UUID uuid; private final UUID uuid;
private long lastNPCInteraction; private long lastNPCInteraction;
private final Map<UUID, Long> actionCooldownMap = new HashMap<>();
private final List<EventService<?>> eventServices = new ArrayList<>(); private final List<EventService<?>> eventServices = new ArrayList<>();
public User(UUID uuid) { public User(UUID uuid) {
@ -38,7 +40,7 @@ public class User {
} }
public boolean canInteract() { public boolean canInteract() {
if (System.currentTimeMillis() - lastNPCInteraction > 1000L) { if (System.currentTimeMillis() - lastNPCInteraction > 100L) {
lastNPCInteraction = System.currentTimeMillis(); lastNPCInteraction = System.currentTimeMillis();
return true; return true;
} }
@ -52,4 +54,13 @@ public class User {
public UUID getUuid() { public UUID getUuid() {
return uuid; return uuid;
} }
public boolean actionCooldownCheck(NPCAction action) {
UUID id = action.getUuid();
if (System.currentTimeMillis() - actionCooldownMap.getOrDefault(id, 0L) >= action.getCooldown()) {
actionCooldownMap.put(id, System.currentTimeMillis());
return true;
}
return false;
}
} }