forked from alkarinv/BattleArena
-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allows for easily specifying custom effects which can be read from a text file
- Loading branch information
Showing
6 changed files
with
539 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
plugin/src/main/java/org/battleplugins/arena/config/ColorParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.battleplugins.arena.config; | ||
|
||
import java.awt.Color; | ||
|
||
public class ColorParser implements ArenaConfigParser.Parser<Color> { | ||
|
||
@Override | ||
public Color parse(Object object) throws ParseException { | ||
if (!(object instanceof String value)) { | ||
throw new ParseException("Color must be a string!") | ||
.context("Provided color", object == null ? "null" : object.toString()) | ||
.cause(ParseException.Cause.INVALID_TYPE) | ||
.type(this.getClass()) | ||
.userError(); | ||
} | ||
|
||
return deserializeSingular(value); | ||
} | ||
|
||
public static org.bukkit.Color deserializeSingularBukkit(String contents) throws ParseException { | ||
Color color = deserializeSingular(contents); | ||
return org.bukkit.Color.fromRGB(color.getRed(), color.getGreen(), color.getBlue()); | ||
} | ||
|
||
public static Color deserializeSingular(String contents) throws ParseException { | ||
if (contents.startsWith("#")) { | ||
return Color.decode(contents); | ||
} else if (contents.contains(",")) { | ||
String[] split = contents.split(","); | ||
if (split.length != 3) { | ||
throw new ParseException("Color must have 3 values!") | ||
.context("Provided color", contents) | ||
.context("Expected format", "r,g,b") | ||
.cause(ParseException.Cause.INVALID_VALUE) | ||
.userError(); | ||
} | ||
|
||
return new Color(Integer.parseInt(split[0]), Integer.parseInt(split[1]), Integer.parseInt(split[2])); | ||
} else { | ||
return Color.getColor(contents); | ||
} | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
plugin/src/main/java/org/battleplugins/arena/config/CustomEffectParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package org.battleplugins.arena.config; | ||
|
||
import org.battleplugins.arena.util.CustomEffect; | ||
import org.bukkit.configuration.ConfigurationSection; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class CustomEffectParser<T extends CustomEffect<?>> implements ArenaConfigParser.Parser<T> { | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public T parse(Object object) throws ParseException { | ||
if (object instanceof String string) { | ||
return (T) deserializeSingular(string); | ||
} | ||
|
||
if (object instanceof ConfigurationSection section) { | ||
return (T) deserializeNode(section); | ||
} | ||
|
||
throw new ParseException("Invalid CustomEffect for object: " + object) | ||
.cause(ParseException.Cause.INVALID_TYPE) | ||
.type(this.getClass()) | ||
.userError(); | ||
} | ||
|
||
public static CustomEffect<?> deserializeSingular(String contents) throws ParseException { | ||
CustomEffect.EffectType<?> type; | ||
|
||
SingularValueParser.ArgumentBuffer buffer = SingularValueParser.parseNamed(contents, SingularValueParser.BraceStyle.CURLY, ';'); | ||
if (!buffer.hasNext()) { | ||
throw new ParseException("No data found for CustomEffect") | ||
.cause(ParseException.Cause.INVALID_TYPE) | ||
.type(CustomEffectParser.class) | ||
.userError(); | ||
} | ||
|
||
SingularValueParser.Argument root = buffer.pop(); | ||
if (root.key().equals("root")) { | ||
type = CustomEffect.EffectType.get(root.value()); | ||
if (type == null) { | ||
throw new ParseException("Invalid CustomEffect type: " + root.value()) | ||
.cause(ParseException.Cause.INVALID_TYPE) | ||
.type(CustomEffectParser.class) | ||
.userError(); | ||
} | ||
} else { | ||
throw new ParseException("Invalid CustomEffect root tag " + root.key()) | ||
.cause(ParseException.Cause.INTERNAL_ERROR) | ||
.type(CustomEffectParser.class); | ||
} | ||
|
||
Map<String, String> data = new HashMap<>(); | ||
while (buffer.hasNext()) { | ||
SingularValueParser.Argument argument = buffer.pop(); | ||
data.put(argument.key(), argument.value()); | ||
} | ||
|
||
CustomEffect<?> customEffect = type.create(builder -> { }); | ||
customEffect.deserialize(data); | ||
return customEffect; | ||
} | ||
|
||
private static CustomEffect<?> deserializeNode(ConfigurationSection section) throws ParseException { | ||
String type = section.getString("type"); | ||
if (type == null) { | ||
throw new ParseException("No type found for CustomEffect") | ||
.cause(ParseException.Cause.INVALID_TYPE) | ||
.type(CustomEffectParser.class) | ||
.userError(); | ||
} | ||
|
||
CustomEffect.EffectType<?> effectType = CustomEffect.EffectType.get(type); | ||
if (effectType == null) { | ||
throw new ParseException("Invalid CustomEffect type: " + type) | ||
.cause(ParseException.Cause.INVALID_TYPE) | ||
.type(CustomEffectParser.class) | ||
.userError(); | ||
} | ||
|
||
Map<String, String> data = new HashMap<>(); | ||
for (String key : section.getKeys(false)) { | ||
if (key.equals("type")) { | ||
continue; | ||
} | ||
|
||
data.put(key, section.getString(key)); | ||
} | ||
|
||
CustomEffect<?> customEffect = effectType.create(builder -> { }); | ||
customEffect.deserialize(data); | ||
return customEffect; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.