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.commands.EmoteCommand;
import lv.pi.animalrp.commands.InteractionCommand;
import lv.pi.animalrp.commands.ChatModClientCommand;
import lv.pi.animalrp.commands.ChatModCommand;
import lv.pi.animalrp.commands.ClearCooldownCommand;
import lv.pi.animalrp.commands.SexCommand;
@ -69,6 +70,7 @@ import net.milkbowl.vault.chat.Chat;
public class AnimalRP extends JavaPlugin {
public static HashMap<UUID, Animal> users;
public static HashMap<UUID, Boolean> isChatModOff;
public static HashMap<UUID, Boolean> isChatModOffClient;
public static HashMap<UUID, Cooldown> cooldowns;
public static HashMap<String, Animal> animals;
public static Emote emotes;
@ -91,7 +93,8 @@ public class AnimalRP extends JavaPlugin {
try {
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());
} catch (IOException e) {
@ -101,7 +104,8 @@ public class AnimalRP extends JavaPlugin {
try {
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();
AnimalRP.cooldowns.forEach((a, b) -> {
jsobj.add(a.toString(), b.toJson());
@ -113,7 +117,8 @@ public class AnimalRP extends JavaPlugin {
try {
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();
AnimalRP.isChatModOff.forEach((a, b) -> {
jsobj.addProperty(a.toString(), b);
@ -122,6 +127,19 @@ public class AnimalRP extends JavaPlugin {
} catch (IOException e) {
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() {
@ -136,13 +154,16 @@ public class AnimalRP extends JavaPlugin {
}
return true;
}
@Override
public void onEnable() {
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.isChatModOff = new HashMap<UUID, Boolean>();
AnimalRP.isChatModOffClient = new HashMap<UUID, Boolean>();
AnimalRP.cooldowns = new HashMap<UUID, Cooldown>();
AnimalRP.animals = new HashMap<String, Animal>();
@ -172,10 +193,12 @@ public class AnimalRP extends JavaPlugin {
if (folder.resolve("database.json").toFile().exists()) {
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()) {
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) {
e.printStackTrace();
@ -184,7 +207,8 @@ public class AnimalRP extends JavaPlugin {
if (folder.resolve("disabled.json").toFile().exists()) {
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()) {
AnimalRP.isChatModOff.put(UUID.fromString(entry.getKey()), entry.getValue().getAsBoolean());
@ -196,10 +220,25 @@ public class AnimalRP extends JavaPlugin {
if (folder.resolve("cooldowns.json").toFile().exists()) {
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()) {
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) {
e.printStackTrace();
@ -214,21 +253,32 @@ public class AnimalRP extends JavaPlugin {
getCommand("sex").setExecutor(new SexCommand());
ChatModCommand cmc = new ChatModCommand();
ChatModClientCommand cmcc = new ChatModClientCommand();
getCommand("chatmodoff").setExecutor(cmc);
getCommand("chatmodon").setExecutor(cmc);
getCommand("chatmodclientoff").setExecutor(cmcc);
getCommand("chatmodclienton").setExecutor(cmcc);
getCommand("clearcooldown").setExecutor(new ClearCooldownCommand());
getCommand("headpats").setExecutor(new InteractionCommand(Mood.HAPPY, "%s petted you! %s", "You petted %s! %s"));
getCommand("kiss").setExecutor(new InteractionCommand(Mood.CUTE, "%s kissed you.. 0////0 %s", "You kissed %s.. 0////0 %s"));
getCommand("bite").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"));
getCommand("headpats")
.setExecutor(new InteractionCommand(Mood.HAPPY, "%s petted you! %s", "You petted %s! %s"));
getCommand("kiss").setExecutor(
new InteractionCommand(Mood.CUTE, "%s kissed you.. 0////0 %s", "You kissed %s.. 0////0 %s"));
getCommand("bite")
.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 */
getCommand("hug").setExecutor(new InteractionCommand(Mood.HAPPY, "%s hugs you! How heartwarming. %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"));
getCommand("hug").setExecutor(new InteractionCommand(Mood.HAPPY, "%s hugs you! How heartwarming. %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;
import java.util.Optional;
import java.util.Random;
import java.util.UUID;
import org.bukkit.Bukkit;
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.util.Mood;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.text.Component;
import lv.pi.animalrp.AnimalRP;
class CustomChatRenderer implements ChatRenderer {
String message;
String messageDefault;
public CustomChatRenderer(String message) {
public CustomChatRenderer(String message, String messageDefault) {
this.message = message;
this.messageDefault = messageDefault;
}
@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) {
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);
}
};
@ -48,7 +60,8 @@ public class PlayerChat implements Listener {
} else {
if (random.nextDouble() < 0.08) {
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);
});
}
}
@ -66,9 +79,11 @@ public class PlayerChat implements Listener {
message = message.replaceAll("%teamColor", team == null ? "" : "<" + team.color().asHexString() + ">");
message = message.replaceAll("%animalColor", (animal != null && !chatModOff) ? "<" + animal.color + ">" : "");
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!
chatmodon:
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:
usage: Clears the specified user's cooldown.
purr: