add disabling the animal chat modifications client side
All checks were successful
/ build (push) Successful in 39s

This commit is contained in:
Soph :3 2024-07-08 01:57:14 +03:00
parent e40439c2f8
commit cc57c63578
Signed by: sophie
GPG key ID: EDA5D222A0C270F2
4 changed files with 188 additions and 66 deletions

View file

@ -54,6 +54,7 @@ import lv.pi.animalrp.animals.Dog;
import lv.pi.animalrp.animals.Fox; import lv.pi.animalrp.animals.Fox;
import lv.pi.animalrp.commands.EmoteCommand; import lv.pi.animalrp.commands.EmoteCommand;
import lv.pi.animalrp.commands.InteractionCommand; import lv.pi.animalrp.commands.InteractionCommand;
import lv.pi.animalrp.commands.ChatModClientCommand;
import lv.pi.animalrp.commands.ChatModCommand; import lv.pi.animalrp.commands.ChatModCommand;
import lv.pi.animalrp.commands.ClearCooldownCommand; import lv.pi.animalrp.commands.ClearCooldownCommand;
import lv.pi.animalrp.commands.SexCommand; import lv.pi.animalrp.commands.SexCommand;
@ -69,6 +70,7 @@ import net.milkbowl.vault.chat.Chat;
public class AnimalRP extends JavaPlugin { public class AnimalRP extends JavaPlugin {
public static HashMap<UUID, Animal> users; public static HashMap<UUID, Animal> users;
public static HashMap<UUID, Boolean> isChatModOff; public static HashMap<UUID, Boolean> isChatModOff;
public static HashMap<UUID, Boolean> isChatModOffClient;
public static HashMap<UUID, Cooldown> cooldowns; public static HashMap<UUID, Cooldown> cooldowns;
public static HashMap<String, Animal> animals; public static HashMap<String, Animal> animals;
public static Emote emotes; public static Emote emotes;
@ -84,44 +86,60 @@ public class AnimalRP extends JavaPlugin {
File folder = getDataFolder(); File folder = getDataFolder();
HashMap<String, String> usersReformatted = new HashMap<String, String>(); HashMap<String, String> usersReformatted = new HashMap<String, String>();
for (Entry<UUID,Animal> ee : users.entrySet()) { for (Entry<UUID, Animal> ee : users.entrySet()) {
usersReformatted.put(ee.getKey().toString(), ee.getValue().name); usersReformatted.put(ee.getKey().toString(), ee.getValue().name);
} }
try { try {
Path filePath = folder.toPath().resolve("database.json"); Path filePath = folder.toPath().resolve("database.json");
if(!Files.exists(filePath)) Files.createFile(filePath); if (!Files.exists(filePath))
Files.createFile(filePath);
Files.write(filePath, gson.toJson(usersReformatted).getBytes()); Files.write(filePath, gson.toJson(usersReformatted).getBytes());
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
try { try {
Path filePath = folder.toPath().resolve("cooldowns.json"); Path filePath = folder.toPath().resolve("cooldowns.json");
if(!Files.exists(filePath)) Files.createFile(filePath); if (!Files.exists(filePath))
Files.createFile(filePath);
JsonObject jsobj = new JsonObject(); JsonObject jsobj = new JsonObject();
AnimalRP.cooldowns.forEach((a, b) -> { AnimalRP.cooldowns.forEach((a, b) -> {
jsobj.add(a.toString(), b.toJson()); jsobj.add(a.toString(), b.toJson());
}); });
Files.write(filePath, gson.toJson(jsobj).getBytes()); Files.write(filePath, gson.toJson(jsobj).getBytes());
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
try { try {
Path filePath = folder.toPath().resolve("disabled.json"); Path filePath = folder.toPath().resolve("disabled.json");
if(!Files.exists(filePath)) Files.createFile(filePath); if (!Files.exists(filePath))
Files.createFile(filePath);
JsonObject jsobj = new JsonObject(); JsonObject jsobj = new JsonObject();
AnimalRP.isChatModOff.forEach((a, b) -> { AnimalRP.isChatModOff.forEach((a, b) -> {
jsobj.addProperty(a.toString(), b); jsobj.addProperty(a.toString(), b);
}); });
Files.write(filePath, gson.toJson(jsobj).getBytes()); Files.write(filePath, gson.toJson(jsobj).getBytes());
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
try {
Path filePath = folder.toPath().resolve("disabled-client.json");
if (!Files.exists(filePath))
Files.createFile(filePath);
JsonObject jsobj = new JsonObject();
AnimalRP.isChatModOffClient.forEach((a, b) -> {
jsobj.addProperty(a.toString(), b);
});
Files.write(filePath, gson.toJson(jsobj).getBytes());
} catch (IOException e) {
e.printStackTrace();
}
} }
public boolean setupVault() { public boolean setupVault() {
@ -129,20 +147,23 @@ public class AnimalRP extends JavaPlugin {
return false; return false;
} }
RegisteredServiceProvider<Chat> rsp = getServer().getServicesManager().getRegistration(Chat.class); RegisteredServiceProvider<Chat> rsp = getServer().getServicesManager().getRegistration(Chat.class);
if(rsp != null) { if (rsp != null) {
AnimalRP.vaultChat = rsp.getProvider(); AnimalRP.vaultChat = rsp.getProvider();
} else { } else {
return false; return false;
} }
return true; return true;
} }
@Override @Override
public void onEnable() { public void onEnable() {
if(!setupVault()) { if (!setupVault()) {
this.getLogger().log(Level.WARNING, "!!! Missing vault, prefix/suffix will not be included in chat formatting."); this.getLogger().log(Level.WARNING,
"!!! Missing vault, prefix/suffix will not be included in chat formatting.");
} }
AnimalRP.users = new HashMap<UUID, Animal>(); AnimalRP.users = new HashMap<UUID, Animal>();
AnimalRP.isChatModOff = new HashMap<UUID, Boolean>(); AnimalRP.isChatModOff = new HashMap<UUID, Boolean>();
AnimalRP.isChatModOffClient = new HashMap<UUID, Boolean>();
AnimalRP.cooldowns = new HashMap<UUID, Cooldown>(); AnimalRP.cooldowns = new HashMap<UUID, Cooldown>();
AnimalRP.animals = new HashMap<String, Animal>(); AnimalRP.animals = new HashMap<String, Animal>();
@ -156,13 +177,13 @@ public class AnimalRP extends JavaPlugin {
animals.put("dog", new Dog()); animals.put("dog", new Dog());
animals.put("bee", new Bee()); animals.put("bee", new Bee());
animals.forEach((z,b) -> { animals.forEach((z, b) -> {
pm.registerEvents(b, this); pm.registerEvents(b, this);
}); });
Path folder = getDataFolder().toPath(); Path folder = getDataFolder().toPath();
if(!Files.exists(folder)) { if (!Files.exists(folder)) {
try { try {
Files.createDirectory(folder); Files.createDirectory(folder);
} catch (IOException e) { } catch (IOException e) {
@ -170,23 +191,26 @@ public class AnimalRP extends JavaPlugin {
} }
} }
if(folder.resolve("database.json").toFile().exists()) { if (folder.resolve("database.json").toFile().exists()) {
try { try {
JsonObject z = JsonParser.parseString(Files.readString(folder.resolve("database.json"))).getAsJsonObject(); JsonObject z = JsonParser.parseString(Files.readString(folder.resolve("database.json")))
.getAsJsonObject();
for (Entry<String,JsonElement> entry : z.entrySet()) { for (Entry<String, JsonElement> entry : z.entrySet()) {
AnimalRP.users.put(UUID.fromString(entry.getKey()), AnimalRP.animals.get(entry.getValue().getAsString())); AnimalRP.users.put(UUID.fromString(entry.getKey()),
AnimalRP.animals.get(entry.getValue().getAsString()));
} }
} catch (JsonSyntaxException | IOException e) { } catch (JsonSyntaxException | IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
if(folder.resolve("disabled.json").toFile().exists()) { if (folder.resolve("disabled.json").toFile().exists()) {
try { try {
JsonObject z = JsonParser.parseString(Files.readString(folder.resolve("disabled.json"))).getAsJsonObject(); JsonObject z = JsonParser.parseString(Files.readString(folder.resolve("disabled.json")))
.getAsJsonObject();
for (Entry<String,JsonElement> entry : z.entrySet()) { for (Entry<String, JsonElement> entry : z.entrySet()) {
AnimalRP.isChatModOff.put(UUID.fromString(entry.getKey()), entry.getValue().getAsBoolean()); AnimalRP.isChatModOff.put(UUID.fromString(entry.getKey()), entry.getValue().getAsBoolean());
} }
} catch (JsonSyntaxException | IOException e) { } catch (JsonSyntaxException | IOException e) {
@ -194,12 +218,27 @@ public class AnimalRP extends JavaPlugin {
} }
} }
if(folder.resolve("cooldowns.json").toFile().exists()) { if (folder.resolve("cooldowns.json").toFile().exists()) {
try { try {
JsonObject z = JsonParser.parseString(Files.readString(folder.resolve("cooldowns.json"))).getAsJsonObject(); JsonObject z = JsonParser.parseString(Files.readString(folder.resolve("cooldowns.json")))
.getAsJsonObject();
for (Entry<String,JsonElement> entry : z.entrySet()) { for (Entry<String, JsonElement> entry : z.entrySet()) {
AnimalRP.cooldowns.put(UUID.fromString(entry.getKey()), Cooldown.fromJson(entry.getValue().getAsJsonObject())); AnimalRP.cooldowns.put(UUID.fromString(entry.getKey()),
Cooldown.fromJson(entry.getValue().getAsJsonObject()));
}
} catch (JsonSyntaxException | IOException e) {
e.printStackTrace();
}
}
if (folder.resolve("disabled-client.json").toFile().exists()) {
try {
JsonObject z = JsonParser.parseString(Files.readString(folder.resolve("disabled-client.json")))
.getAsJsonObject();
for (Entry<String, JsonElement> entry : z.entrySet()) {
AnimalRP.isChatModOffClient.put(UUID.fromString(entry.getKey()), entry.getValue().getAsBoolean());
} }
} catch (JsonSyntaxException | IOException e) { } catch (JsonSyntaxException | IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -214,21 +253,32 @@ public class AnimalRP extends JavaPlugin {
getCommand("sex").setExecutor(new SexCommand()); getCommand("sex").setExecutor(new SexCommand());
ChatModCommand cmc = new ChatModCommand(); ChatModCommand cmc = new ChatModCommand();
ChatModClientCommand cmcc = new ChatModClientCommand();
getCommand("chatmodoff").setExecutor(cmc); getCommand("chatmodoff").setExecutor(cmc);
getCommand("chatmodon").setExecutor(cmc); getCommand("chatmodon").setExecutor(cmc);
getCommand("chatmodclientoff").setExecutor(cmcc);
getCommand("chatmodclienton").setExecutor(cmcc);
getCommand("clearcooldown").setExecutor(new ClearCooldownCommand()); getCommand("clearcooldown").setExecutor(new ClearCooldownCommand());
getCommand("headpats").setExecutor(new InteractionCommand(Mood.HAPPY, "%s petted you! %s", "You petted %s! %s")); getCommand("headpats")
getCommand("kiss").setExecutor(new InteractionCommand(Mood.CUTE, "%s kissed you.. 0////0 %s", "You kissed %s.. 0////0 %s")); .setExecutor(new InteractionCommand(Mood.HAPPY, "%s petted you! %s", "You petted %s! %s"));
getCommand("bite").setExecutor(new InteractionCommand(Mood.ANGRY, "%s bit you!! Σ(っ゚Д゚)っ %s", "You bit %s! (○`д´)ノシ %s")); getCommand("kiss").setExecutor(
getCommand("bzz").setExecutor(new InteractionCommand(Mood.ANGRY, "%s bzzs at you!! :3 %s", "You bzzed at %s :3 %s")); new InteractionCommand(Mood.CUTE, "%s kissed you.. 0////0 %s", "You kissed %s.. 0////0 %s"));
getCommand("purr").setExecutor(new InteractionCommand(Mood.CUTE, "You hear the soft sound of %s purring on you... %s", "You jump on %s, and start purring. %s")); getCommand("bite")
getCommand("scratch").setExecutor(new InteractionCommand(Mood.ANGRY, "%s SCRATCHES YOU! Ow! %s", "You channel your inner evil, and scratch %s! %s")); .setExecutor(new InteractionCommand(Mood.ANGRY, "%s bit you!! Σ(っ゚Д゚)っ %s", "You bit %s! (○`д´)ノシ %s"));
getCommand("bzz")
.setExecutor(new InteractionCommand(Mood.ANGRY, "%s bzzs at you!! :3 %s", "You bzzed at %s :3 %s"));
getCommand("purr").setExecutor(new InteractionCommand(Mood.CUTE,
"You hear the soft sound of %s purring on you... %s", "You jump on %s, and start purring. %s"));
getCommand("scratch").setExecutor(new InteractionCommand(Mood.ANGRY, "%s SCRATCHES YOU! Ow! %s",
"You channel your inner evil, and scratch %s! %s"));
/* Contributed by Simo__28 */ /* Contributed by Simo__28 */
getCommand("hug").setExecutor(new InteractionCommand(Mood.HAPPY, "%s hugs you! How heartwarming. %s", "You hug %s! How heartwarming. %s")); getCommand("hug").setExecutor(new InteractionCommand(Mood.HAPPY, "%s hugs you! How heartwarming. %s",
getCommand("cuddle").setExecutor(new InteractionCommand(Mood.CUTE, "%s cuddles with you. %s", "You and %s start cuddling. How cute! %s")); "You hug %s! How heartwarming. %s"));
getCommand("cuddle").setExecutor(new InteractionCommand(Mood.CUTE, "%s cuddles with you. %s",
"You and %s start cuddling. How cute! %s"));
/* */ /* */
} }
} }

View file

@ -0,0 +1,53 @@
package lv.pi.animalrp.commands;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import lv.pi.animalrp.AnimalRP;
public class ChatModClientCommand implements CommandExecutor {
@Override
public boolean onCommand(@NotNull CommandSender arg0, @NotNull Command arg1, @NotNull String arg2,
@NotNull String[] arg3) {
if (!(arg0 instanceof Player)) {
arg0.sendMessage(AnimalRP.mm.deserialize("<gray>I'm sorry console. :(</gray>"));
return true;
}
Player player = (Player) arg0;
boolean off = false;
if (arg2.endsWith("off")) {
off = true;
}
boolean isDisabled = false;
if (AnimalRP.isChatModOffClient.get(player.getUniqueId()) != null) {
isDisabled = true;
}
if (isDisabled) { // client chat modifications are turned off
if (off) { // asking to be turned off
arg0.sendMessage(
AnimalRP.mm.deserialize("<red>Chat modifications for your client are already disabled!"));
} else { // asking to be turned on
arg0.sendMessage(AnimalRP.mm.deserialize("<green>Chat modifications for your client are enabled!"));
AnimalRP.isChatModOffClient.remove(player.getUniqueId());
}
} else { // chat modifications are turned on
if (off) { // asking to be turned off
arg0.sendMessage(AnimalRP.mm.deserialize("<red>Chat modifications for your client are disabled!"));
AnimalRP.isChatModOffClient.put(player.getUniqueId(), true);
} else { // asking to be turned on
arg0.sendMessage(
AnimalRP.mm.deserialize("<green>Chat modifications for your client are already enabled!!"));
}
}
return true;
}
}

View file

@ -1,6 +1,8 @@
package lv.pi.animalrp.listeners; package lv.pi.animalrp.listeners;
import java.util.Optional;
import java.util.Random; import java.util.Random;
import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -15,20 +17,30 @@ import io.papermc.paper.event.player.AsyncChatEvent;
import lv.pi.animalrp.animals.Animal; import lv.pi.animalrp.animals.Animal;
import lv.pi.animalrp.util.Mood; import lv.pi.animalrp.util.Mood;
import net.kyori.adventure.audience.Audience; import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import lv.pi.animalrp.AnimalRP; import lv.pi.animalrp.AnimalRP;
class CustomChatRenderer implements ChatRenderer { class CustomChatRenderer implements ChatRenderer {
String message; String message;
String messageDefault;
public CustomChatRenderer(String message) { public CustomChatRenderer(String message, String messageDefault) {
this.message = message; this.message = message;
this.messageDefault = messageDefault;
} }
@Override @Override
public @NotNull Component render(@NotNull Player source, @NotNull Component sourceDisplayName, @NotNull Component message, public @NotNull Component render(@NotNull Player source, @NotNull Component sourceDisplayName,
@NotNull Component message,
@NotNull Audience viewer) { @NotNull Audience viewer) {
Optional<UUID> uuid = viewer.get(Identity.UUID);
if (uuid.isPresent()) {
if (AnimalRP.isChatModOffClient.get(uuid.get()) != null) {
return AnimalRP.mm.deserialize(this.messageDefault);
}
}
return AnimalRP.mm.deserialize(this.message); return AnimalRP.mm.deserialize(this.message);
} }
}; };
@ -42,13 +54,14 @@ public class PlayerChat implements Listener {
Boolean chatModOff = false; Boolean chatModOff = false;
Team team = Bukkit.getServer().getScoreboardManager().getMainScoreboard().getPlayerTeam(event.getPlayer()); Team team = Bukkit.getServer().getScoreboardManager().getMainScoreboard().getPlayerTeam(event.getPlayer());
if(animal != null) { if (animal != null) {
if(AnimalRP.isChatModOff.get(event.getPlayer().getUniqueId()) != null) { if (AnimalRP.isChatModOff.get(event.getPlayer().getUniqueId()) != null) {
chatModOff = true; chatModOff = true;
} else { } else {
if(random.nextDouble() < 0.08) { if (random.nextDouble() < 0.08) {
Bukkit.getScheduler().runTask(AnimalRP.getPlugin(AnimalRP.class), () -> { Bukkit.getScheduler().runTask(AnimalRP.getPlugin(AnimalRP.class), () -> {
event.getPlayer().getWorld().playSound(event.getPlayer().getLocation(), animal.moodSounds.get(Mood.HAPPY), 10F, 1); event.getPlayer().getWorld().playSound(event.getPlayer().getLocation(),
animal.moodSounds.get(Mood.HAPPY), 10F, 1);
}); });
} }
} }
@ -56,7 +69,7 @@ public class PlayerChat implements Listener {
String format = "%prefix%teamColor%animalColor%name%suffix<reset>: %message"; String format = "%prefix%teamColor%animalColor%name%suffix<reset>: %message";
String message = format; String message = format;
if(AnimalRP.vaultChat != null) { if (AnimalRP.vaultChat != null) {
message = message.replaceAll("%prefix", AnimalRP.vaultChat.getPlayerPrefix(event.getPlayer())); message = message.replaceAll("%prefix", AnimalRP.vaultChat.getPlayerPrefix(event.getPlayer()));
message = message.replaceAll("%suffix", AnimalRP.vaultChat.getPlayerSuffix(event.getPlayer())); message = message.replaceAll("%suffix", AnimalRP.vaultChat.getPlayerSuffix(event.getPlayer()));
} else { } else {
@ -66,9 +79,11 @@ public class PlayerChat implements Listener {
message = message.replaceAll("%teamColor", team == null ? "" : "<" + team.color().asHexString() + ">"); message = message.replaceAll("%teamColor", team == null ? "" : "<" + team.color().asHexString() + ">");
message = message.replaceAll("%animalColor", (animal != null && !chatModOff) ? "<" + animal.color + ">" : ""); message = message.replaceAll("%animalColor", (animal != null && !chatModOff) ? "<" + animal.color + ">" : "");
message = message.replaceAll("%name", event.getPlayer().getName()); message = message.replaceAll("%name", event.getPlayer().getName());
message = message.replaceAll("%message", (animal != null && !chatModOff) ? animal.chatTransformations(AnimalRP.mm.serialize(event.message())) : AnimalRP.mm.serialize(event.message())); String messageDefault = message.replaceAll("%message", AnimalRP.mm.serialize(event.message()));
message = message.replaceAll("%message",
(animal != null && !chatModOff) ? animal.chatTransformations(AnimalRP.mm.serialize(event.message()))
: AnimalRP.mm.serialize(event.message()));
event.renderer(new CustomChatRenderer(message)); event.renderer(new CustomChatRenderer(message, messageDefault));
} }
} }

View file

@ -17,6 +17,10 @@ commands:
usage: Disable the chat modification done by animals! usage: Disable the chat modification done by animals!
chatmodon: chatmodon:
usage: Enable the chat modification done by animals! usage: Enable the chat modification done by animals!
chatmodclientoff:
usage: Disable all viewing of chat modifications
chatmodclienton:
usage: View all chat modifications of animals
clearcooldown: clearcooldown:
usage: Clears the specified user's cooldown. usage: Clears the specified user's cooldown.
purr: purr: