Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
769dfc3f8e | ||
|
|
8c061a057c | ||
|
|
e41a8e3db5 | ||
|
|
1fc0538ff4 | ||
|
|
fff868cf25 | ||
|
|
51b91b3c3d | ||
|
|
a20ed832f4 |
No files matched your search
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>me.dadus33</groupId>
|
||||
<artifactId>ChatItem</artifactId>
|
||||
<version>1.1.9</version>
|
||||
<version>1.2.2</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
|
||||
@@ -46,13 +46,14 @@ public class JSONManipulatorCurrent implements JSONManipulator{
|
||||
}
|
||||
rgx = regex;
|
||||
JsonArray rep = new JsonArray();
|
||||
repl = escapeBackslash(repl);
|
||||
JsonArray use = parser.parse(Translator.toJSON(repl)).getAsJsonArray();
|
||||
|
||||
JsonObject hover = parser.parse("{\"action\":\"show_item\", \"value\": \"\"}").getAsJsonObject();
|
||||
Object nmsStack = asNMSCopy.invoke(null, item);
|
||||
Object tag = nbtTagCompoundClass.newInstance();
|
||||
tag = saveNmsItemStackMethod.invoke(nmsStack, tag);
|
||||
String jsonRep = tag.toString();
|
||||
String jsonRep = escapeBackslash(tag.toString());
|
||||
hover.addProperty("value", jsonRep);
|
||||
for (JsonElement ob : use)
|
||||
ob.getAsJsonObject().add("hoverEvent", hover);
|
||||
@@ -111,7 +112,8 @@ public class JSONManipulatorCurrent implements JSONManipulator{
|
||||
for (int j = 0; j < splits.length; ++j) {
|
||||
boolean endDot = (j == splits.length - 1) && isLast;
|
||||
if (!splits[j].isEmpty() && !endDot) {
|
||||
JsonObject fix = parser.parse(o.toString()).getAsJsonObject();
|
||||
String st = o.toString();
|
||||
JsonObject fix = parser.parse(st).getAsJsonObject();
|
||||
fix.addProperty("text", splits[j]);
|
||||
rep.add(fix);
|
||||
}
|
||||
@@ -227,7 +229,8 @@ public class JSONManipulatorCurrent implements JSONManipulator{
|
||||
for (int j = 0; j < splits.length; ++j) {
|
||||
boolean endDot = (j == splits.length - 1) && isLast;
|
||||
if (!splits[j].isEmpty() && !endDot) {
|
||||
JsonObject fix = parser.parse(o.toString()).getAsJsonObject();
|
||||
String st = o.toString();
|
||||
JsonObject fix = parser.parse(st).getAsJsonObject();
|
||||
fix.addProperty("text", splits[j]);
|
||||
replacer.add(fix);
|
||||
}
|
||||
@@ -286,4 +289,10 @@ public class JSONManipulatorCurrent implements JSONManipulator{
|
||||
}
|
||||
|
||||
|
||||
private static String escapeBackslash(String json){
|
||||
json = json.replace("\\", "\\\\");
|
||||
return json;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package me.dadus33.chatitem.json;
|
||||
|
||||
|
||||
import com.google.gson.JsonParser;
|
||||
import org.bukkit.ChatColor;
|
||||
|
||||
/*
|
||||
@@ -11,7 +10,6 @@ I only modified his work to make it compatible with what I was building.
|
||||
public class Translator {
|
||||
private static final StringBuilder BUILDER = new StringBuilder("");
|
||||
private static final StringBuilder STYLE = new StringBuilder();
|
||||
private static final JsonParser JSON_PARSER = new JsonParser();
|
||||
|
||||
static String toJSON(String message) {
|
||||
if (message == null || message.isEmpty())
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
package me.dadus33.chatitem.listeners;
|
||||
|
||||
import me.dadus33.chatitem.utils.Storage;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
@@ -46,19 +44,23 @@ public class ChatEventListener implements Listener {
|
||||
}
|
||||
return;
|
||||
}
|
||||
String s = e.getMessage();
|
||||
for(String placeholder : c.PLACEHOLDERS){
|
||||
s = s.replace(placeholder, c.PLACEHOLDERS.get(0));
|
||||
}
|
||||
int occurrences = countOccurrences(c.PLACEHOLDERS.get(0), s);
|
||||
if(occurrences>c.MAX_OCCURRENCES){
|
||||
e.setCancelled(true);
|
||||
|
||||
}
|
||||
String oldmsg = e.getMessage();
|
||||
e.setMessage(e.getMessage().concat(e.getPlayer().getName()));
|
||||
Bukkit.getConsoleSender().sendMessage(String.format(e.getFormat(), e.getPlayer().getDisplayName(), oldmsg));
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@EventHandler(priority = EventPriority.NORMAL)
|
||||
public void onCommand(PlayerCommandPreprocessEvent e){
|
||||
Command cmd = Bukkit.getPluginCommand(e.getMessage().split(" ")[0].substring(1));
|
||||
if(!c.ALLOWED_COMMANDS.contains(cmd)){
|
||||
return;
|
||||
}
|
||||
boolean found = false;
|
||||
|
||||
for (String rep : c.PLACEHOLDERS)
|
||||
@@ -81,6 +83,15 @@ public class ChatEventListener implements Listener {
|
||||
}
|
||||
return;
|
||||
}
|
||||
String s = e.getMessage();
|
||||
for(String placeholder : c.PLACEHOLDERS){
|
||||
s = s.replace(placeholder, c.PLACEHOLDERS.get(0));
|
||||
}
|
||||
int occurrences = countOccurrences(c.PLACEHOLDERS.get(0), s);
|
||||
if(occurrences>c.MAX_OCCURRENCES){
|
||||
e.setCancelled(true);
|
||||
|
||||
}
|
||||
|
||||
e.setMessage(e.getMessage().concat(e.getPlayer().getName()));
|
||||
|
||||
@@ -90,4 +101,20 @@ public class ChatEventListener implements Listener {
|
||||
public void setStorage(Storage st) {
|
||||
c = st;
|
||||
}
|
||||
|
||||
private int countOccurrences(String findStr, String str){
|
||||
int lastIndex = 0;
|
||||
int count = 0;
|
||||
while(lastIndex != -1){
|
||||
|
||||
lastIndex = str.indexOf(findStr,lastIndex);
|
||||
|
||||
if(lastIndex != -1){
|
||||
count ++;
|
||||
lastIndex += findStr.length();
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,14 +17,13 @@ import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.HashMap;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
|
||||
public class ChatPacketListener extends PacketAdapter {
|
||||
|
||||
private final static String NAME = Pattern.quote("{name}");
|
||||
private final static String AMOUNT = Pattern.quote("{amount}");
|
||||
private final static String TIMES = Pattern.quote("{times}");
|
||||
private final static String NAME = "{name}";
|
||||
private final static String AMOUNT = "{amount}";
|
||||
private final static String TIMES = "{times}";
|
||||
private ChatItem instance;
|
||||
private Storage c;
|
||||
|
||||
@@ -68,6 +67,10 @@ public class ChatPacketListener extends PacketAdapter {
|
||||
}
|
||||
|
||||
PacketContainer packet = e.getPacket();
|
||||
|
||||
if(packet.getChatComponents().readSafely(0)==null){ // null check for some cases of messages from other plugins
|
||||
return;
|
||||
}
|
||||
String json = packet.getChatComponents().readSafely(0).getJson();
|
||||
|
||||
boolean found = false;
|
||||
@@ -121,27 +124,29 @@ public class ChatPacketListener extends PacketAdapter {
|
||||
}
|
||||
if (inHand.getAmount() == 1) {
|
||||
if (c.FORCE_ADD_AMOUNT) {
|
||||
amount = amount.replaceAll(TIMES, "1");
|
||||
replacer = replacer.replaceAll(AMOUNT, amount);
|
||||
amount = amount.replace(TIMES, "1");
|
||||
replacer = replacer.replace(AMOUNT, amount);
|
||||
} else {
|
||||
replacer = replacer.replaceAll(AMOUNT, "");
|
||||
replacer = replacer.replace(AMOUNT, "");
|
||||
}
|
||||
} else {
|
||||
amount = amount.replaceAll(TIMES, String.valueOf(inHand.getAmount()));
|
||||
replacer = replacer.replaceAll(AMOUNT, amount);
|
||||
amount = amount.replace(TIMES, String.valueOf(inHand.getAmount()));
|
||||
replacer = replacer.replace(AMOUNT, amount);
|
||||
}
|
||||
if (dname) {
|
||||
replacer = replacer.replaceAll(NAME, inHand.getItemMeta().getDisplayName());
|
||||
String trp = inHand.getItemMeta().getDisplayName();
|
||||
replacer = replacer.replace(NAME, trp);
|
||||
} else {
|
||||
HashMap<Short, String> translationSection = c.TRANSLATIONS.get(inHand.getType().name());
|
||||
if(translationSection==null){
|
||||
replacer = replacer.replaceAll(NAME, materialToName(inHand.getType()));
|
||||
String trp = materialToName(inHand.getType());
|
||||
replacer = replacer.replace(NAME, trp);
|
||||
}else {
|
||||
String translated = translationSection.get(inHand.getDurability());
|
||||
if (translated != null) {
|
||||
replacer = replacer.replaceAll(NAME, translated);
|
||||
replacer = replacer.replace(NAME, translated);
|
||||
} else {
|
||||
replacer = replacer.replaceAll(NAME, materialToName(inHand.getType()));
|
||||
replacer = replacer.replace(NAME, materialToName(inHand.getType()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,16 +2,12 @@ package me.dadus33.chatitem.utils;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import me.dadus33.chatitem.ChatItem;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Level;
|
||||
|
||||
|
||||
public class Storage {
|
||||
@@ -26,7 +22,7 @@ public class Storage {
|
||||
public final String DENY_MESSAGE;
|
||||
public final String RELOAD_MESSAGE;
|
||||
final Integer CONFIG_VERSION;
|
||||
public final List<Command> ALLOWED_COMMANDS = new ArrayList<>();
|
||||
public final Integer MAX_OCCURRENCES;
|
||||
|
||||
public Storage(FileConfiguration conf) {
|
||||
Set<String> keys = conf.getConfigurationSection("Translations").getKeys(false);
|
||||
@@ -50,21 +46,7 @@ public class Storage {
|
||||
DENY_IF_NO_ITEM = conf.getBoolean("deny-if-no-item");
|
||||
DENY_MESSAGE = color(conf.getString("deny-message"));
|
||||
RELOAD_MESSAGE = color(conf.getString("reload-success"));
|
||||
final List<String> cmds = conf.getStringList("commands");
|
||||
Bukkit.getScheduler().runTaskLaterAsynchronously(ChatItem.getInstance(), new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
for(String s : cmds){
|
||||
Command c = Bukkit.getPluginCommand(s);
|
||||
if(c!=null) {
|
||||
ALLOWED_COMMANDS.add(Bukkit.getPluginCommand(s));
|
||||
}
|
||||
else
|
||||
ChatItem.getInstance().getLogger().log(Level.WARNING, ChatColor.RED.toString().concat("Unknown command ")
|
||||
.concat(s).concat(" found in config! Ignoring occurrence..."));
|
||||
}
|
||||
}
|
||||
}, 100L);
|
||||
MAX_OCCURRENCES = conf.getInt("max-occurrences");
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#https://www.spigotmc.org/resources/chatitem.19064/
|
||||
|
||||
#Version info, DON'T TOUCH
|
||||
config-version: 4
|
||||
config-version: 5
|
||||
|
||||
|
||||
#The placeholders you want to use for the item. You can add as many as you want.
|
||||
@@ -42,11 +42,11 @@ deny-message: "&c&lYou have no item in hand!"
|
||||
# The message you will get after reloading the plugin using /cireload.
|
||||
reload-success: "&b&lSuccessful reload!"
|
||||
|
||||
# Here you can choose which commands will the plugin attempt to parse. Console commands will all be ignored,
|
||||
# however player commands will be taken into consideration. To add a command, simply add an element to the list
|
||||
# bellow containing an alias (or name) for that command.
|
||||
commands:
|
||||
- "msg"
|
||||
# The message a player will get when attempting to write more than 'max-occurrences' of placeholders in chat.
|
||||
max-reached-message: "&c&lYou can only add 3 placeholders in chat!"
|
||||
|
||||
# The maximum number of occurences of the item placeholder in chat.
|
||||
max-occurrences: 3
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user