1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-11-26 04:25:15 +01:00

Apply new async chat events from paper

This commit is contained in:
montlikadani 2021-03-02 11:06:15 +01:00
parent 297b99a0ba
commit b2d88fc3d2
5 changed files with 188 additions and 91 deletions

View File

@ -48,9 +48,11 @@ import com.gamingmesh.jobs.listeners.JobsPaymentListener;
import com.gamingmesh.jobs.listeners.PistonProtectionListener;
import com.gamingmesh.jobs.selection.SelectionManager;
import com.gamingmesh.jobs.stuff.*;
import com.gamingmesh.jobs.stuff.complement.JobsChatEvent;
import com.gamingmesh.jobs.stuff.complement.Complement;
import com.gamingmesh.jobs.stuff.complement.Complement1;
import com.gamingmesh.jobs.stuff.complement.Complement2;
import com.gamingmesh.jobs.stuff.complement.KyoriChatEvent;
import com.gamingmesh.jobs.tasks.BufferedPaymentThread;
import com.gamingmesh.jobs.tasks.DatabaseSaveThread;
@ -742,8 +744,13 @@ public class Jobs extends JavaPlugin {
} catch (ClassNotFoundException e) {
}
complement = (Version.isCurrentEqualOrHigher(Version.v1_16_R3) && kyoriSupported) ? new Complement2()
: new Complement1();
if (Version.isCurrentEqualOrHigher(Version.v1_16_R3) && kyoriSupported) {
complement = new Complement2();
getServer().getPluginManager().registerEvents(new KyoriChatEvent(this), this);
} else {
complement = new Complement1();
getServer().getPluginManager().registerEvents(new JobsChatEvent(this), this);
}
// register economy
Bukkit.getScheduler().runTask(this, new HookEconomyTask(this));

View File

@ -50,7 +50,6 @@ import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.inventory.InventoryType.SlotType;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerChangedWorldEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerItemBreakEvent;
@ -84,35 +83,17 @@ import com.gamingmesh.jobs.container.JobProgression;
import com.gamingmesh.jobs.container.JobsArmorChangeEvent;
import com.gamingmesh.jobs.container.JobsArmorChangeEvent.EquipMethod;
import com.gamingmesh.jobs.container.JobsPlayer;
import com.gamingmesh.jobs.stuff.Util;
public class JobsListener implements Listener {
private Jobs plugin;
private final HashMap<UUID, Long> interactDelay = new HashMap<>();
private final Map<UUID, Long> interactDelay = new HashMap<>();
public JobsListener(Jobs plugin) {
this.plugin = plugin;
}
@EventHandler(priority = EventPriority.NORMAL)
public void AsyncPlayerChatEvent(final AsyncPlayerChatEvent event) {
if (event.isCancelled() || Util.getJobsEditorMap().isEmpty())
return;
final Player player = event.getPlayer();
if (!Util.getJobsEditorMap().containsKey(player.getUniqueId()))
return;
final String msg = Util.getJobsEditorMap().remove(player.getUniqueId());
if (msg == null)
return;
plugin.getServer().getScheduler().runTask(plugin, () -> player.performCommand(msg + event.getMessage()));
event.setCancelled(true);
}
private boolean isInteractOk(Player player) {
if (!interactDelay.containsKey(player.getUniqueId())) {
interactDelay.put(player.getUniqueId(), System.currentTimeMillis());
@ -215,12 +196,12 @@ public class JobsListener implements Listener {
return;
Sign sign = (Sign) block.getState();
String FirstLine = Jobs.getInstance().getComplement().getLine(sign, 0);
String FirstLine = plugin.getComplement().getLine(sign, 0);
if (!CMIChatColor.stripColor(FirstLine).equalsIgnoreCase(CMIChatColor.stripColor(Jobs.getLanguage().getMessage("signs.topline"))))
return;
String command = CMIChatColor.stripColor(Jobs.getInstance().getComplement().getLine(sign, 1));
String command = CMIChatColor.stripColor(plugin.getComplement().getLine(sign, 1));
for (String key : Jobs.getGCManager().keys) {
if (command.equalsIgnoreCase(CMIChatColor.stripColor(Jobs.getLanguage().getMessage("signs.secondline." + key)))) {
command = key;
@ -228,8 +209,8 @@ public class JobsListener implements Listener {
}
}
player.performCommand("jobs " + command + " " + CMIChatColor.stripColor(Jobs.getInstance().getComplement().getLine(sign, 2))
+ " " + CMIChatColor.stripColor(Jobs.getInstance().getComplement().getLine(sign, 3)));
player.performCommand("jobs " + command + " " + CMIChatColor.stripColor(plugin.getComplement().getLine(sign, 2))
+ " " + CMIChatColor.stripColor(plugin.getComplement().getLine(sign, 3)));
}
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
@ -243,7 +224,7 @@ public class JobsListener implements Listener {
Player player = event.getPlayer();
Sign sign = (Sign) block.getState();
String firstLine = Jobs.getInstance().getComplement().getLine(sign, 0);
String firstLine = plugin.getComplement().getLine(sign, 0);
if (firstLine.contains(Jobs.getLanguage().getMessage("signs.topline")) && !player.hasPermission("jobs.command.signs")) {
event.setCancelled(true);
player.sendMessage(Jobs.getLanguage().getMessage("signs.cantdestroy"));
@ -275,10 +256,10 @@ public class JobsListener implements Listener {
Sign sign = (Sign) block.getState();
if (!CMIChatColor.stripColor(Jobs.getInstance().getComplement().getLine(event, 0)).equalsIgnoreCase("[Jobs]"))
if (!CMIChatColor.stripColor(plugin.getComplement().getLine(event, 0)).equalsIgnoreCase("[Jobs]"))
return;
final SignTopType type = SignTopType.getType(CMIChatColor.stripColor(Jobs.getInstance().getComplement().getLine(event, 1)));
final SignTopType type = SignTopType.getType(CMIChatColor.stripColor(plugin.getComplement().getLine(event, 1)));
if (type == null)
return;
@ -289,7 +270,7 @@ public class JobsListener implements Listener {
return;
}
String jobname = CMIChatColor.stripColor(Jobs.getInstance().getComplement().getLine(sign, 2)).toLowerCase();
String jobname = CMIChatColor.stripColor(plugin.getComplement().getLine(sign, 2)).toLowerCase();
final Job job = Jobs.getJob(jobname);
if (type == SignTopType.toplist && job == null) {
player.sendMessage(Jobs.getLanguage().getMessage("command.top.error.nojob"));
@ -297,7 +278,7 @@ public class JobsListener implements Listener {
}
boolean special = false;
String numberString = CMIChatColor.stripColor(Jobs.getInstance().getComplement().getLine(sign, 3)).toLowerCase();
String numberString = CMIChatColor.stripColor(plugin.getComplement().getLine(sign, 3)).toLowerCase();
if (numberString.contains("s")) {
numberString = numberString.replace("s", "");
special = true;
@ -335,11 +316,11 @@ public class JobsListener implements Listener {
if (!Jobs.getGCManager().SignsEnabled)
return;
if (CMIChatColor.stripColor(Jobs.getInstance().getComplement().getLine(event, 0))
if (CMIChatColor.stripColor(plugin.getComplement().getLine(event, 0))
.equalsIgnoreCase(CMIChatColor.stripColor(Jobs.getLanguage().getMessage("signs.topline"))) && !CMIChatColor.stripColor(
Jobs.getInstance().getComplement().getLine(event, 1))
plugin.getComplement().getLine(event, 1))
.equalsIgnoreCase("toplist"))
Jobs.getInstance().getComplement().setLine(event, 0, convert(Jobs.getLanguage().getMessage("signs.topline")));
plugin.getComplement().setLine(event, 0, convert(Jobs.getLanguage().getMessage("signs.topline")));
else
return;
@ -349,74 +330,26 @@ public class JobsListener implements Listener {
return;
}
String command = CMIChatColor.stripColor(Jobs.getInstance().getComplement().getLine(event, 1)).toLowerCase();
String command = CMIChatColor.stripColor(plugin.getComplement().getLine(event, 1)).toLowerCase();
for (String key : Jobs.getGCManager().keys) {
if (command.equalsIgnoreCase(CMIChatColor.stripColor(Jobs.getLanguage().getMessage("signs.secondline." + key)))) {
Jobs.getInstance().getComplement().setLine(event, 1, convert(Jobs.getLanguage().getMessage("signs.secondline." + key)));
plugin.getComplement().setLine(event, 1, convert(Jobs.getLanguage().getMessage("signs.secondline." + key)));
break;
}
}
Job job = Jobs.getJob(CMIChatColor.stripColor(Jobs.getInstance().getComplement().getLine(event, 2)));
Job job = Jobs.getJob(CMIChatColor.stripColor(plugin.getComplement().getLine(event, 2)));
if (job == null)
return;
String color = Jobs.getGCManager().SignsColorizeJobName ? job.getChatColor().toString() : "";
Jobs.getInstance().getComplement().setLine(event, 2, convert(color + job.getName()));
plugin.getComplement().setLine(event, 2, convert(color + job.getName()));
}
private String convert(String line) {
return Pattern.compile("&([0-9a-fk-or])").matcher(CMIChatColor.translate(line)).replaceAll("\u00a7$1");
}
// Adding to chat prefix job name
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerChat(AsyncPlayerChatEvent event) {
if (!Jobs.getGCManager().getModifyChat())
return;
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(event.getPlayer());
String honorific = jPlayer != null ? jPlayer.getDisplayHonorific() : "";
if (honorific.equals(" "))
honorific = "";
event.setFormat(event.getFormat().replace("%1$s", honorific + "%1$s"));
}
// Changing chat prefix variable to job name
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPlayerChatLow(AsyncPlayerChatEvent event) {
if (Jobs.getGCManager().getModifyChat())
return;
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(event.getPlayer());
String honorific = jPlayer != null ? jPlayer.getDisplayHonorific() : "";
if (honorific.equals(" "))
honorific = "";
String format = event.getFormat();
if (format.contains("{jobs}")) {
event.setFormat(format.replace("{jobs}", honorific));
}
}
// Changing chat prefix variable to job name
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onPlayerChatHigh(AsyncPlayerChatEvent event) {
if (Jobs.getGCManager().getModifyChat())
return;
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(event.getPlayer());
String honorific = jPlayer != null ? jPlayer.getDisplayHonorific() : "";
if (honorific.equals(" "))
honorific = "";
String format = event.getFormat();
if (format.contains("{jobs}")) {
event.setFormat(format.replace("{jobs}", honorific));
}
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onWorldLoad(WorldLoadEvent event) {
World world = event.getWorld();
@ -456,9 +389,9 @@ public class JobsListener implements Listener {
if (iih.hasItemMeta()) {
ItemMeta meta = iih.getItemMeta();
if (meta.hasDisplayName())
name = Jobs.getInstance().getComplement().getDisplayName(meta);
name = plugin.getComplement().getDisplayName(meta);
if (meta.hasLore())
lore = Jobs.getInstance().getComplement().getLore(meta);
lore = plugin.getComplement().getLore(meta);
}
String meinOk = null;

View File

@ -14,13 +14,13 @@ import org.bukkit.inventory.meta.ItemMeta;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer;
public final class Complement2 implements Complement {
public class Complement2 implements Complement {
private String serialize(Component component) {
protected String serialize(Component component) {
return PlainComponentSerializer.plain().serialize(component);
}
private Component deserialize(String t) {
protected Component deserialize(String t) {
return PlainComponentSerializer.plain().deserialize(t);
}

View File

@ -0,0 +1,81 @@
package com.gamingmesh.jobs.stuff.complement;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.container.JobsPlayer;
import com.gamingmesh.jobs.stuff.Util;
@SuppressWarnings("deprecation")
public class JobsChatEvent implements Listener {
private Jobs plugin;
public JobsChatEvent(Jobs plugin) {
this.plugin = plugin;
}
@EventHandler(priority = EventPriority.NORMAL)
public void asyncPlayerChatEvent(final AsyncPlayerChatEvent event) {
if (event.isCancelled() || Util.getJobsEditorMap().isEmpty())
return;
final String msg = Util.getJobsEditorMap().remove(event.getPlayer().getUniqueId());
if (msg != null) {
plugin.getServer().getScheduler().runTask(plugin,
() -> event.getPlayer().performCommand(msg + event.getMessage()));
event.setCancelled(true);
}
}
// Adding to chat prefix job name
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerChat(AsyncPlayerChatEvent event) {
if (!Jobs.getGCManager().getModifyChat())
return;
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(event.getPlayer());
String honorific = jPlayer != null ? jPlayer.getDisplayHonorific() : "";
if (honorific.equals(" "))
honorific = "";
event.setFormat(event.getFormat().replace("%1$s", honorific + "%1$s"));
}
// Changing chat prefix variable to job name
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPlayerChatLow(AsyncPlayerChatEvent event) {
if (Jobs.getGCManager().getModifyChat())
return;
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(event.getPlayer());
String honorific = jPlayer != null ? jPlayer.getDisplayHonorific() : "";
if (honorific.equals(" "))
honorific = "";
String format = event.getFormat();
if (format.contains("{jobs}")) {
event.setFormat(format.replace("{jobs}", honorific));
}
}
// Changing chat prefix variable to job name
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onPlayerChatHigh(AsyncPlayerChatEvent event) {
if (Jobs.getGCManager().getModifyChat())
return;
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(event.getPlayer());
String honorific = jPlayer != null ? jPlayer.getDisplayHonorific() : "";
if (honorific.equals(" "))
honorific = "";
String format = event.getFormat();
if (format.contains("{jobs}")) {
event.setFormat(format.replace("{jobs}", honorific));
}
}
}

View File

@ -0,0 +1,76 @@
package com.gamingmesh.jobs.stuff.complement;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.container.JobsPlayer;
import com.gamingmesh.jobs.stuff.Util;
import io.papermc.paper.event.player.AsyncChatEvent;
import net.kyori.adventure.text.Component;
public final class KyoriChatEvent extends Complement2 implements Listener {
private Jobs plugin;
public KyoriChatEvent(Jobs plugin) {
this.plugin = plugin;
}
@EventHandler(priority = EventPriority.NORMAL)
public void asyncChatEvent(final AsyncChatEvent event) {
if (event.isCancelled() || Util.getJobsEditorMap().isEmpty())
return;
final String msg = Util.getJobsEditorMap().remove(event.getPlayer().getUniqueId());
if (msg != null) {
plugin.getServer().getScheduler().runTask(plugin,
() -> event.getPlayer().performCommand(msg + serialize(event.message())));
event.setCancelled(true);
}
}
// Adding to chat prefix job name
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerChat(AsyncChatEvent event) {
if (!Jobs.getGCManager().getModifyChat())
return;
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(event.getPlayer());
String honorific = jPlayer != null ? jPlayer.getDisplayHonorific() : "";
if (honorific.equals(" "))
honorific = "";
event.message(Component.text(honorific + "%1$s"));
}
// Changing chat prefix variable to job name
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPlayerChatLow(AsyncChatEvent event) {
if (Jobs.getGCManager().getModifyChat())
return;
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(event.getPlayer());
String honorific = jPlayer != null ? jPlayer.getDisplayHonorific() : "";
if (honorific.equals(" "))
honorific = "";
event.formatter().chat(Component.translatable("{jobs}"), Component.text(honorific));
}
// Changing chat prefix variable to job name
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onPlayerChatHigh(AsyncChatEvent event) {
if (Jobs.getGCManager().getModifyChat())
return;
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(event.getPlayer());
String honorific = jPlayer != null ? jPlayer.getDisplayHonorific() : "";
if (honorific.equals(" "))
honorific = "";
event.formatter().chat(Component.translatable("{jobs}"), Component.text(honorific));
}
}