Skip to content

Commit

Permalink
ISSUE #18 Add input validation for click_actions
Browse files Browse the repository at this point in the history
Refactor placeholder formatting to only apply json formatting if
configured.
  • Loading branch information
Aust1n46 committed Jun 26, 2022
1 parent d913a10 commit 8527cb5
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 63 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>mineverse.Aust1n46.chat</groupId>
<artifactId>VentureChat</artifactId>
<version>3.4.1_1.19_patch</version>
<version>3.4.2_1.19_patch</version>
<url>https://bitbucket.org/Aust1n46/venturechat/src/master</url>
<scm>
<url>https://bitbucket.org/Aust1n46/venturechat/src/master</url>
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/mineverse/Aust1n46/chat/ClickAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package mineverse.Aust1n46.chat;

public enum ClickAction {
SUGGEST_COMMAND, RUN_COMMAND, OPEN_URL;

private final String jsonValue;

ClickAction() {
jsonValue = name().toLowerCase();
}

@Override
public String toString() {
return jsonValue;
}
}
8 changes: 5 additions & 3 deletions src/main/java/mineverse/Aust1n46/chat/json/JsonAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

import java.util.List;

import mineverse.Aust1n46.chat.ClickAction;

public class JsonAttribute {
private String name;
private List<String> hoverText;
private String clickAction;
private ClickAction clickAction;
private String clickText;

public JsonAttribute(String name, List<String> hoverText, String clickAction, String clickText) {
public JsonAttribute(String name, List<String> hoverText, ClickAction clickAction, String clickText) {
this.name = name;
this.hoverText = hoverText;
this.clickAction = clickAction;
Expand All @@ -23,7 +25,7 @@ public List<String> getHoverText() {
return hoverText;
}

public String getClickAction() {
public ClickAction getClickAction() {
return clickAction;
}

Expand Down
96 changes: 52 additions & 44 deletions src/main/java/mineverse/Aust1n46/chat/json/JsonFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,66 @@

import org.bukkit.configuration.ConfigurationSection;

import mineverse.Aust1n46.chat.ClickAction;
import mineverse.Aust1n46.chat.MineverseChat;
import mineverse.Aust1n46.chat.utilities.Format;

public class JsonFormat {
private static MineverseChat plugin = MineverseChat.getInstance();
private static HashMap<String, JsonFormat> jsonFormats;
private static MineverseChat plugin = MineverseChat.getInstance();
private static HashMap<String, JsonFormat> jsonFormats;

private List<JsonAttribute> jsonAttributes;
private int priority;
private String name;
private List<JsonAttribute> jsonAttributes;
private int priority;
private String name;

public JsonFormat(String name, int priority, List<JsonAttribute> jsonAttributes) {
this.name = name;
this.priority = priority;
this.jsonAttributes = jsonAttributes;
}
public JsonFormat(String name, int priority, List<JsonAttribute> jsonAttributes) {
this.name = name;
this.priority = priority;
this.jsonAttributes = jsonAttributes;
}

public static void initialize() {
jsonFormats = new HashMap<String, JsonFormat>();
ConfigurationSection jsonFormatSection = plugin.getConfig().getConfigurationSection("jsonformatting");
for (String jsonFormat : jsonFormatSection.getKeys(false)) {
int priority = jsonFormatSection.getInt(jsonFormat + ".priority", 0);
List<JsonAttribute> jsonAttributes = new ArrayList<>();
ConfigurationSection jsonAttributeSection = jsonFormatSection.getConfigurationSection(jsonFormat + ".json_attributes");
if (jsonAttributeSection != null) {
for (String attribute : jsonAttributeSection.getKeys(false)) {
List<String> hoverText = jsonAttributeSection.getStringList(attribute + ".hover_text");
String clickAction = jsonAttributeSection.getString(attribute + ".click_action", "");
String clickText = jsonAttributeSection.getString(attribute + ".click_text", "");
jsonAttributes.add(new JsonAttribute(attribute, hoverText, clickAction, clickText));
}
}
jsonFormats.put(jsonFormat.toLowerCase(), new JsonFormat(jsonFormat, priority, jsonAttributes));
}
}
public static void initialize() {
jsonFormats = new HashMap<String, JsonFormat>();
ConfigurationSection jsonFormatSection = plugin.getConfig().getConfigurationSection("jsonformatting");
for (String jsonFormat : jsonFormatSection.getKeys(false)) {
int priority = jsonFormatSection.getInt(jsonFormat + ".priority", 0);
List<JsonAttribute> jsonAttributes = new ArrayList<>();
ConfigurationSection jsonAttributeSection = jsonFormatSection.getConfigurationSection(jsonFormat + ".json_attributes");
if (jsonAttributeSection != null) {
for (String attribute : jsonAttributeSection.getKeys(false)) {
List<String> hoverText = jsonAttributeSection.getStringList(attribute + ".hover_text");
String clickActionText = jsonAttributeSection.getString(attribute + ".click_action", "");
try {
ClickAction clickAction = ClickAction.valueOf(clickActionText.toUpperCase());
String clickText = jsonAttributeSection.getString(attribute + ".click_text", "");
jsonAttributes.add(new JsonAttribute(attribute, hoverText, clickAction, clickText));
} catch (IllegalArgumentException | NullPointerException exception) {
plugin.getServer().getConsoleSender()
.sendMessage(Format.FormatStringAll("&8[&eVentureChat&8]&c - Illegal click_action: " + clickActionText + " in jsonFormat: " + jsonFormat));
}
}
}
jsonFormats.put(jsonFormat.toLowerCase(), new JsonFormat(jsonFormat, priority, jsonAttributes));
}
}

public static Collection<JsonFormat> getJsonFormats() {
return jsonFormats.values();
}
public static Collection<JsonFormat> getJsonFormats() {
return jsonFormats.values();
}

public static JsonFormat getJsonFormat(String name) {
return jsonFormats.get(name.toLowerCase());
}
public static JsonFormat getJsonFormat(String name) {
return jsonFormats.get(name.toLowerCase());
}

public String getName() {
return name;
}
public String getName() {
return name;
}

public int getPriority() {
return priority;
}
public List<JsonAttribute> getJsonAttributes() {
return jsonAttributes;
}
public int getPriority() {
return priority;
}

public List<JsonAttribute> getJsonAttributes() {
return jsonAttributes;
}
}
36 changes: 22 additions & 14 deletions src/main/java/mineverse/Aust1n46/chat/utilities/Format.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Sound;
Expand Down Expand Up @@ -112,28 +113,35 @@ private static String convertPlaceholders(String s, JsonFormat format, Mineverse
formattedPlaceholder = Format.FormatStringAll(PlaceholderAPI.setBracketPlaceholders(icp.getPlayer(), placeholder));
temp += convertToJsonColors(lastCode + remaining.substring(0, indexStart)) + ",";
lastCode = getLastCode(lastCode + remaining.substring(0, indexStart));
String action = "";
String text = "";
String hover = "";
boolean placeholderHasJsonAttribute = false;
for (JsonAttribute jsonAttribute : format.getJsonAttributes()) {
if (placeholder.contains(jsonAttribute.getName().replace("{", "").replace("}", ""))) {
action = jsonAttribute.getClickAction();
text = Format.FormatStringAll(
final String action = jsonAttribute.getClickAction().toString();
final String text = Format.FormatStringAll(
PlaceholderAPI.setBracketPlaceholders(icp.getPlayer(), jsonAttribute.getClickText()));
final StringBuilder hover = new StringBuilder();
for (String st : jsonAttribute.getHoverText()) {
hover += Format.FormatStringAll(st) + "\n";
hover.append(Format.FormatStringAll(st) + "\n");
}
final String hoverText;
if(!hover.isEmpty()) {
hoverText = Format.FormatStringAll(
PlaceholderAPI.setBracketPlaceholders(icp.getPlayer(), hover.substring(0, hover.length() - 1)));
} else {
hoverText = StringUtils.EMPTY;
}
temp += convertToJsonColors(lastCode + formattedPlaceholder,
",\"clickEvent\":{\"action\":\"" + action + "\",\"value\":\"" + text
+ "\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":["
+ convertToJsonColors(hoverText) + "]}}")
+ ",";
placeholderHasJsonAttribute = true;
break;
}
}
if(!hover.isEmpty()) {
hover = Format.FormatStringAll(
PlaceholderAPI.setBracketPlaceholders(icp.getPlayer(), hover.substring(0, hover.length() - 1)));
if (!placeholderHasJsonAttribute) {
temp += convertToJsonColors(lastCode + formattedPlaceholder) + ",";
}
temp += convertToJsonColors(lastCode + formattedPlaceholder,
",\"clickEvent\":{\"action\":\"" + action + "\",\"value\":\"" + text
+ "\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":["
+ convertToJsonColors(hover) + "]}}")
+ ",";
lastCode = getLastCode(lastCode + formattedPlaceholder);
remaining = remaining.substring(indexEnd);
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ messageremovertext: '&c&o<message removed>'
# The name of the group is the permissions node for the format
# Example: venturechat.json.Owner is the node for the group Owner
# A lower priority overrides a higher priority if a player has more than 1 group
# Possible options for click_name and click_prefix are suggest_command, run_command, and open_url
# Possible options for click_action are suggest_command, run_command, and open_url
jsonformatting:
Default: # This default format is required! Do not delete or rename it!
priority: 2147483647 # Integer.MAX_VALUE
Expand Down

0 comments on commit 8527cb5

Please sign in to comment.