mirror of
https://github.com/AtlasMediaGroup/TotalFreedomMod.git
synced 2025-06-30 20:16:41 +00:00
I'm tired of seeing prs that make small code reformats so im doing a commit to fix it all at once
This commit is contained in:
@ -5,15 +5,18 @@ import ca.momothereal.mojangson.value.*;
|
||||
|
||||
import static ca.momothereal.mojangson.MojangsonToken.*;
|
||||
|
||||
public class MojangsonFinder {
|
||||
public class MojangsonFinder
|
||||
{
|
||||
|
||||
/**
|
||||
* Automatically detects the appropriate MojangsonValue from the given value.
|
||||
*
|
||||
* @param value The value to parse
|
||||
* @return The resulting MojangsonValue. If the type couldn't be found, it falls back to MojangsonString
|
||||
* @throws MojangsonParseException if the given value could not be parsed
|
||||
*/
|
||||
public static MojangsonValue readFromValue(String value) throws MojangsonParseException {
|
||||
public static MojangsonValue readFromValue(String value) throws MojangsonParseException
|
||||
{
|
||||
MojangsonValue val = new MojangsonString();
|
||||
val.read(value);
|
||||
return val;
|
||||
|
@ -1,6 +1,7 @@
|
||||
package ca.momothereal.mojangson;
|
||||
|
||||
public enum MojangsonToken {
|
||||
public enum MojangsonToken
|
||||
{
|
||||
|
||||
COMPOUND_START(0, "Compound_Start", '{'),
|
||||
COMPOUND_END(1, "Compound_End", '}'),
|
||||
@ -22,26 +23,31 @@ public enum MojangsonToken {
|
||||
private String name;
|
||||
private char symbol;
|
||||
|
||||
MojangsonToken(int id, String name, char symbol) {
|
||||
MojangsonToken(int id, String name, char symbol)
|
||||
{
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.symbol = symbol;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
public int getId()
|
||||
{
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
public String getName()
|
||||
{
|
||||
return name;
|
||||
}
|
||||
|
||||
public char getSymbol() {
|
||||
public char getSymbol()
|
||||
{
|
||||
return symbol;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
public String toString()
|
||||
{
|
||||
return String.valueOf(symbol);
|
||||
}
|
||||
}
|
||||
|
@ -1,34 +1,41 @@
|
||||
package ca.momothereal.mojangson.ex;
|
||||
|
||||
public class MojangsonParseException extends Exception {
|
||||
public class MojangsonParseException extends Exception
|
||||
{
|
||||
|
||||
private ParseExceptionReason reason;
|
||||
|
||||
public MojangsonParseException(String message, ParseExceptionReason reason) {
|
||||
public MojangsonParseException(String message, ParseExceptionReason reason)
|
||||
{
|
||||
super(message);
|
||||
this.reason = reason;
|
||||
}
|
||||
|
||||
public ParseExceptionReason getReason() {
|
||||
public ParseExceptionReason getReason()
|
||||
{
|
||||
return reason;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
public String getMessage()
|
||||
{
|
||||
return reason.getMessage() + ": " + super.getMessage();
|
||||
}
|
||||
|
||||
public enum ParseExceptionReason {
|
||||
public enum ParseExceptionReason
|
||||
{
|
||||
INVALID_FORMAT_NUM("Given value is not numerical"),
|
||||
UNEXPECTED_SYMBOL("Unexpected symbol in Mojangson string");
|
||||
|
||||
private String message;
|
||||
|
||||
ParseExceptionReason(String message) {
|
||||
ParseExceptionReason(String message)
|
||||
{
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
public String getMessage()
|
||||
{
|
||||
return message;
|
||||
}
|
||||
}
|
||||
|
@ -7,35 +7,43 @@ import java.util.*;
|
||||
|
||||
import static ca.momothereal.mojangson.MojangsonToken.*;
|
||||
|
||||
public class MojangsonCompound extends HashMap<String, List<MojangsonValue>> implements MojangsonValue<Map<String, MojangsonValue>> {
|
||||
public class MojangsonCompound extends HashMap<String, List<MojangsonValue>> implements MojangsonValue<Map<String, MojangsonValue>>
|
||||
{
|
||||
|
||||
private final int C_COMPOUND_START = 0; // Parsing context
|
||||
private final int C_COMPOUND_PAIR_KEY = 1; // Parsing context
|
||||
private final int C_COMPOUND_PAIR_VALUE = 2; // Parsing context
|
||||
|
||||
public MojangsonCompound() {
|
||||
public MojangsonCompound()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public MojangsonCompound(Map map) {
|
||||
public MojangsonCompound(Map map)
|
||||
{
|
||||
super(map);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(StringBuilder builder) {
|
||||
public void write(StringBuilder builder)
|
||||
{
|
||||
builder.append(COMPOUND_START);
|
||||
boolean start = true;
|
||||
|
||||
for (String key : keySet()) {
|
||||
if (start) {
|
||||
for (String key : keySet())
|
||||
{
|
||||
if (start)
|
||||
{
|
||||
start = false;
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.append(ELEMENT_SEPERATOR);
|
||||
}
|
||||
|
||||
builder.append(key).append(ELEMENT_PAIR_SEPERATOR);
|
||||
List<MojangsonValue> value = get(key);
|
||||
for(MojangsonValue val : value)
|
||||
for (MojangsonValue val : value)
|
||||
{
|
||||
val.write(builder);
|
||||
}
|
||||
@ -44,46 +52,60 @@ public class MojangsonCompound extends HashMap<String, List<MojangsonValue>> imp
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(String string) throws MojangsonParseException {
|
||||
public void read(String string) throws MojangsonParseException
|
||||
{
|
||||
int context = C_COMPOUND_START;
|
||||
String tmp_key = "", tmp_val = "";
|
||||
int scope = 0;
|
||||
boolean inString = false;
|
||||
|
||||
for (int index = 0; index < string.length(); index++) {
|
||||
for (int index = 0; index < string.length(); index++)
|
||||
{
|
||||
Character character = string.charAt(index);
|
||||
|
||||
if (character == STRING_QUOTES.getSymbol()) {
|
||||
if (character == STRING_QUOTES.getSymbol())
|
||||
{
|
||||
inString = !inString;
|
||||
}
|
||||
if (character == WHITE_SPACE.getSymbol()) {
|
||||
if (character == WHITE_SPACE.getSymbol())
|
||||
{
|
||||
if (!inString)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if ((character == COMPOUND_START.getSymbol() || character == ARRAY_START.getSymbol()) && !inString) {
|
||||
if ((character == COMPOUND_START.getSymbol() || character == ARRAY_START.getSymbol()) && !inString)
|
||||
{
|
||||
scope++;
|
||||
}
|
||||
if ((character == COMPOUND_END.getSymbol() || character == ARRAY_END.getSymbol()) && !inString) {
|
||||
if ((character == COMPOUND_END.getSymbol() || character == ARRAY_END.getSymbol()) && !inString)
|
||||
{
|
||||
scope--;
|
||||
}
|
||||
if (context == C_COMPOUND_START) {
|
||||
if (character != COMPOUND_START.getSymbol()) {
|
||||
if (context == C_COMPOUND_START)
|
||||
{
|
||||
if (character != COMPOUND_START.getSymbol())
|
||||
{
|
||||
parseException(index, character);
|
||||
return;
|
||||
}
|
||||
context++;
|
||||
continue;
|
||||
}
|
||||
if (context == C_COMPOUND_PAIR_KEY) {
|
||||
if (character == ELEMENT_PAIR_SEPERATOR.getSymbol() && scope <= 1) {
|
||||
if (context == C_COMPOUND_PAIR_KEY)
|
||||
{
|
||||
if (character == ELEMENT_PAIR_SEPERATOR.getSymbol() && scope <= 1)
|
||||
{
|
||||
context++;
|
||||
continue;
|
||||
}
|
||||
tmp_key += character;
|
||||
continue;
|
||||
}
|
||||
if (context == C_COMPOUND_PAIR_VALUE) {
|
||||
if ((character == ELEMENT_SEPERATOR.getSymbol() || character == COMPOUND_END.getSymbol()) && scope <= 1 && !inString) {
|
||||
if (context == C_COMPOUND_PAIR_VALUE)
|
||||
{
|
||||
if ((character == ELEMENT_SEPERATOR.getSymbol() || character == COMPOUND_END.getSymbol()) && scope <= 1 && !inString)
|
||||
{
|
||||
context = C_COMPOUND_PAIR_KEY;
|
||||
computeIfAbsent(tmp_key, k -> new ArrayList<>()).add(MojangsonFinder.readFromValue(tmp_val));
|
||||
tmp_key = tmp_val = "";
|
||||
@ -95,11 +117,12 @@ public class MojangsonCompound extends HashMap<String, List<MojangsonValue>> imp
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, MojangsonValue> getValue() {
|
||||
public Map<String, MojangsonValue> getValue()
|
||||
{
|
||||
HashMap<String, MojangsonValue> hack = new HashMap<>();
|
||||
for(String string : keySet())
|
||||
for (String string : keySet())
|
||||
{
|
||||
for(MojangsonValue value : get(string))
|
||||
for (MojangsonValue value : get(string))
|
||||
{
|
||||
hack.put(string, value);
|
||||
}
|
||||
@ -108,11 +131,13 @@ public class MojangsonCompound extends HashMap<String, List<MojangsonValue>> imp
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class getValueClass() {
|
||||
public Class getValueClass()
|
||||
{
|
||||
return Map.class;
|
||||
}
|
||||
|
||||
private void parseException(int index, char symbol) throws MojangsonParseException {
|
||||
private void parseException(int index, char symbol) throws MojangsonParseException
|
||||
{
|
||||
throw new MojangsonParseException("Index: " + index + ", symbol: \'" + symbol + "\'", MojangsonParseException.ParseExceptionReason.UNEXPECTED_SYMBOL);
|
||||
}
|
||||
}
|
||||
|
@ -3,43 +3,54 @@ package ca.momothereal.mojangson.value;
|
||||
import ca.momothereal.mojangson.MojangsonToken;
|
||||
import ca.momothereal.mojangson.ex.MojangsonParseException;
|
||||
|
||||
public class MojangsonString implements MojangsonValue<String> {
|
||||
public class MojangsonString implements MojangsonValue<String>
|
||||
{
|
||||
private String value;
|
||||
|
||||
public MojangsonString() {
|
||||
public MojangsonString()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public MojangsonString(String value) {
|
||||
public MojangsonString(String value)
|
||||
{
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
public String getValue()
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
public void setValue(String value)
|
||||
{
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(StringBuilder builder) {
|
||||
public void write(StringBuilder builder)
|
||||
{
|
||||
builder.append(MojangsonToken.STRING_QUOTES).append(value).append(MojangsonToken.STRING_QUOTES);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class getValueClass() {
|
||||
public Class getValueClass()
|
||||
{
|
||||
return String.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void read(String string) throws MojangsonParseException {
|
||||
public void read(String string) throws MojangsonParseException
|
||||
{
|
||||
Character lastChar = string.charAt(string.length() - 1);
|
||||
Character firstChar = string.charAt(0);
|
||||
|
||||
if (firstChar == MojangsonToken.STRING_QUOTES.getSymbol() && lastChar == MojangsonToken.STRING_QUOTES.getSymbol()) {
|
||||
if (firstChar == MojangsonToken.STRING_QUOTES.getSymbol() && lastChar == MojangsonToken.STRING_QUOTES.getSymbol())
|
||||
{
|
||||
value = string.substring(1, string.length() - 1);
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
value = string;
|
||||
}
|
||||
}
|
||||
|
@ -4,18 +4,22 @@ import ca.momothereal.mojangson.ex.MojangsonParseException;
|
||||
|
||||
/**
|
||||
* Represents a value inside a compound or array.
|
||||
*
|
||||
* @param <T> The type of value this MojangsonValue holds
|
||||
*/
|
||||
public interface MojangsonValue<T> {
|
||||
public interface MojangsonValue<T>
|
||||
{
|
||||
|
||||
/**
|
||||
* Writes the value to a StringBuilder buffer.
|
||||
*
|
||||
* @param builder The buffer to write to
|
||||
*/
|
||||
void write(StringBuilder builder);
|
||||
|
||||
/**
|
||||
* Parses and updates the current value to the given string representation
|
||||
*
|
||||
* @param string The string representation of the value
|
||||
* @throws MojangsonParseException if the given value cannot be parsed
|
||||
*/
|
||||
@ -23,12 +27,14 @@ public interface MojangsonValue<T> {
|
||||
|
||||
/**
|
||||
* Gets the current literal value
|
||||
*
|
||||
* @return The current literal value of the MojangsonValue
|
||||
*/
|
||||
T getValue();
|
||||
|
||||
/**
|
||||
* Gets the literal value's class
|
||||
*
|
||||
* @return The literal value's class
|
||||
*/
|
||||
Class getValueClass();
|
||||
|
Reference in New Issue
Block a user