Make MSON immutable

This commit is contained in:
BuildTools 2015-06-29 22:39:15 +02:00 committed by Olof Larsson
parent 2cd6769481
commit a98be323ea
26 changed files with 710 additions and 592 deletions

View File

@ -23,6 +23,8 @@ import com.massivecraft.massivecore.adapter.MassiveSetAdapter;
import com.massivecraft.massivecore.adapter.MassiveTreeMapAdapter;
import com.massivecraft.massivecore.adapter.MassiveTreeSetAdapter;
import com.massivecraft.massivecore.adapter.ModdedEnumTypeAdapter;
import com.massivecraft.massivecore.adapter.MsonAdapter;
import com.massivecraft.massivecore.adapter.MsonEventAdapter;
import com.massivecraft.massivecore.adapter.PlayerInventoryAdapter;
import com.massivecraft.massivecore.adapter.UUIDAdapter;
import com.massivecraft.massivecore.chestgui.EngineChestGui;
@ -44,6 +46,8 @@ import com.massivecraft.massivecore.collections.MassiveTreeSet;
import com.massivecraft.massivecore.collections.MassiveTreeSetDef;
import com.massivecraft.massivecore.integration.vault.IntegrationVault;
import com.massivecraft.massivecore.mixin.EngineTeleportMixinCause;
import com.massivecraft.massivecore.mson.Mson;
import com.massivecraft.massivecore.mson.MsonEvent;
import com.massivecraft.massivecore.ps.PS;
import com.massivecraft.massivecore.ps.PSAdapter;
import com.massivecraft.massivecore.store.ExamineThread;
@ -52,6 +56,7 @@ import com.massivecraft.massivecore.util.IdUtil;
import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.util.PlayerUtil;
import com.massivecraft.massivecore.util.TimeUnit;
import com.massivecraft.massivecore.util.Txt;
import com.massivecraft.massivecore.xlib.gson.Gson;
import com.massivecraft.massivecore.xlib.gson.GsonBuilder;
import com.massivecraft.massivecore.xlib.gson.JsonArray;
@ -67,6 +72,7 @@ public class MassiveCore extends MassivePlugin
public final static String INSTANCE = "instance";
public final static String DEFAULT = "default";
public static final String NONE = Txt.parse("<silver>none");
public final static Set<String> NOTHING = MUtil.treeset("", "none", "null", "nothing");
public final static Set<String> REMOVE = MUtil.treeset("clear", "c", "delete", "del", "d", "erase", "e", "remove", "rem", "r", "reset", "res");
@ -115,6 +121,9 @@ public class MassiveCore extends MassivePlugin
.registerTypeAdapter(MassiveTreeSet.class, MassiveTreeSetAdapter.get())
.registerTypeAdapter(MassiveTreeSetDef.class, MassiveTreeSetAdapter.get())
.registerTypeAdapter(Mson.class, MsonAdapter.get())
.registerTypeAdapter(MsonEvent.class, MsonEventAdapter.get())
.registerTypeAdapter(BackstringEnumSet.class, BackstringEnumSetAdapter.get())
.registerTypeAdapter(Entry.class, EntryAdapter.get())

View File

@ -1,10 +1,10 @@
package com.massivecraft.massivecore;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import com.massivecraft.massivecore.Prioritized;
import com.massivecraft.massivecore.util.MUtil;
public class PriorityLines implements Prioritized, Comparable<PriorityLines>
@ -21,6 +21,7 @@ public class PriorityLines implements Prioritized, Comparable<PriorityLines>
public List<String> getLines() { return this.lines; }
public void setLines(List<String> lines) { this.lines = lines; }
public void setLines(String... lines) { this.setLines(Arrays.asList(lines)); }
public void setLines(String line) { this.setLines(Collections.singletonList(line)); }
// -------------------------------------------- //
// CONSTRUCT
@ -61,7 +62,7 @@ public class PriorityLines implements Prioritized, Comparable<PriorityLines>
{
int ret;
ret = Integer.valueOf(this.priority).compareTo(that.priority);
ret = Integer.compare(this.priority, that.priority);
if (ret != 0) return ret;
if (MUtil.equals(this.lines, that.lines)) return 0;
@ -89,15 +90,10 @@ public class PriorityLines implements Prioritized, Comparable<PriorityLines>
public boolean equals(Object obj)
{
if (this == obj) return true;
if (obj == null) return false;
if (!(obj instanceof PriorityLines)) return false;
PriorityLines other = (PriorityLines) obj;
if (lines == null)
{
if (other.lines != null) return false;
}
else if (!lines.equals(other.lines)) return false;
if (priority != other.priority) return false;
if ( ! MUtil.equals(this.lines, other.lines)) return false;
return true;
}

View File

@ -59,6 +59,16 @@ public class LowercaseEnumAdapter<T extends Enum<T>> implements JsonDeserializer
// UTIL
// -------------------------------------------- //
public T getEnumValueFrom(JsonElement json)
{
return getEnumValueFrom(json.toString());
}
public T getEnumValueFrom(String str)
{
return getEnumValueFrom(str, clazz);
}
public static <T> T[] getEnumValues(Class<T> clazz)
{
if (clazz == null) throw new IllegalArgumentException("passed clazz param is null");
@ -82,14 +92,13 @@ public class LowercaseEnumAdapter<T extends Enum<T>> implements JsonDeserializer
return string.toLowerCase();
}
public T getEnumValueFrom(JsonElement json)
public static<T extends Enum<T>> T getEnumValueFrom(String str, Class<T> clazz)
{
String jsonString = json.getAsString();
jsonString = getComparable(jsonString);
str = getComparable(str);
for (T value : getEnumValues(clazz))
{
if (getComparable(value).equals(jsonString)) return value;
if (getComparable(value).equals(str)) return value;
}
return null;

View File

@ -0,0 +1,41 @@
package com.massivecraft.massivecore.adapter;
import java.lang.reflect.Type;
import com.massivecraft.massivecore.mson.Mson;
import com.massivecraft.massivecore.xlib.gson.JsonDeserializationContext;
import com.massivecraft.massivecore.xlib.gson.JsonDeserializer;
import com.massivecraft.massivecore.xlib.gson.JsonElement;
import com.massivecraft.massivecore.xlib.gson.JsonNull;
import com.massivecraft.massivecore.xlib.gson.JsonParseException;
import com.massivecraft.massivecore.xlib.gson.JsonSerializationContext;
import com.massivecraft.massivecore.xlib.gson.JsonSerializer;
public class MsonAdapter implements JsonDeserializer<Mson>, JsonSerializer<Mson>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
public static MsonAdapter i = new MsonAdapter();
public static MsonAdapter get() { return i; }
public MsonAdapter() {}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public JsonElement serialize(Mson src, Type typeOfSrc, JsonSerializationContext context)
{
if (src == null) return JsonNull.INSTANCE;
return src.toJson();
}
@Override
public Mson deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
return Mson.fromJson(json.getAsJsonObject());
}
}

View File

@ -0,0 +1,40 @@
package com.massivecraft.massivecore.adapter;
import java.lang.reflect.Type;
import com.massivecraft.massivecore.mson.Mson;
import com.massivecraft.massivecore.mson.MsonEvent;
import com.massivecraft.massivecore.xlib.gson.JsonDeserializationContext;
import com.massivecraft.massivecore.xlib.gson.JsonDeserializer;
import com.massivecraft.massivecore.xlib.gson.JsonElement;
import com.massivecraft.massivecore.xlib.gson.JsonParseException;
import com.massivecraft.massivecore.xlib.gson.JsonSerializationContext;
import com.massivecraft.massivecore.xlib.gson.JsonSerializer;
public class MsonEventAdapter implements JsonDeserializer<MsonEvent>, JsonSerializer<MsonEvent>
{
// -------------------------------------------- //
// INSTANCE & CONSTRUCT
// -------------------------------------------- //
public static MsonEventAdapter i = new MsonEventAdapter();
public static MsonEventAdapter get() { return i; }
public MsonEventAdapter() {}
// -------------------------------------------- //
// OVERRIDE
// -------------------------------------------- //
@Override
public JsonElement serialize(MsonEvent src, Type typeOfSrc, JsonSerializationContext context)
{
return Mson.GSON.toJsonTree(src);
}
@Override
public MsonEvent deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
return Mson.GSON.fromJson(json, MsonEvent.class);
}
}

View File

@ -12,6 +12,7 @@ import java.util.Map.Entry;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
@ -970,6 +971,45 @@ public class MassiveCommand
return getUseageTemplate(false);
}
public String getCommandLine(String... args)
{
return getCommandLine(Arrays.asList(args));
}
public String getCommandLine(Iterable<String> args)
{
// Initiate ret
StringBuilder ret = new StringBuilder();
// First a slash
ret.append('/');
// Then parent commands
for (MassiveCommand parent : this.getCommandChain())
{
// Append parent
ret.append(parent.getAliases().get(0));
// Append space
ret.append(' ');
}
// Then ourself
ret.append(this.getAliases().get(0));
// Then args
for (String arg : args)
{
// First a space
ret.append(' ');
// Then the arg
ret.append(arg);
}
// Return ret
return ret.toString();
}
// -------------------------------------------- //
// TAB
// -------------------------------------------- //

View File

@ -5,6 +5,7 @@ import org.bukkit.entity.Player;
import com.massivecraft.massivecore.Lang;
import com.massivecraft.massivecore.cmd.MassiveCommand;
import com.massivecraft.massivecore.util.Txt;
public class ReqIsPlayer extends ReqAbstract
{
@ -30,7 +31,7 @@ public class ReqIsPlayer extends ReqAbstract
@Override
public String createErrorMessage(CommandSender sender, MassiveCommand command)
{
return Lang.COMMAND_SENDER_MUST_BE_PLAYER;
return Txt.parse(Lang.COMMAND_SENDER_MUST_BE_PLAYER);
}
}

View File

@ -5,6 +5,7 @@ import org.bukkit.entity.Player;
import com.massivecraft.massivecore.Lang;
import com.massivecraft.massivecore.cmd.MassiveCommand;
import com.massivecraft.massivecore.util.Txt;
public class ReqIsntPlayer extends ReqAbstract
{
@ -30,7 +31,7 @@ public class ReqIsntPlayer extends ReqAbstract
@Override
public String createErrorMessage(CommandSender sender, MassiveCommand command)
{
return Lang.COMMAND_SENDER_MUSNT_BE_PLAYER;
return Txt.parse(Lang.COMMAND_SENDER_MUSNT_BE_PLAYER);
}
}

View File

@ -1,9 +1,11 @@
package com.massivecraft.massivecore.collections;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import com.massivecraft.massivecore.CaseInsensitiveComparator;
import com.massivecraft.massivecore.ps.PS;
public class WorldExceptionSet
{
@ -39,11 +41,21 @@ public class WorldExceptionSet
return this.standard;
}
public boolean contains(PS ps)
{
return this.contains(ps.getWorld());
}
public boolean contains(World world)
{
return this.contains(world.getName());
}
public boolean contains(Location loc)
{
return this.contains(loc.getWorld());
}
public boolean contains(Entity entity)
{
return this.contains(entity.getWorld());

View File

@ -1,6 +1,9 @@
package com.massivecraft.massivecore.mixin;
import com.massivecraft.massivecore.mson.Mson;
public interface DisplayNameMixin
{
public Mson getDisplayNameMson(Object senderObject, Object watcherObject);
public String getDisplayName(Object senderObject, Object watcherObject);
}

View File

@ -1,6 +1,11 @@
package com.massivecraft.massivecore.mixin;
import com.massivecraft.massivecore.mson.Mson;
public abstract class DisplayNameMixinAbstract implements DisplayNameMixin
{
public Mson getDisplayNameMson(Object senderObject, Object watcherObject)
{
return Mson.fromParsedMessage(this.getDisplayName(senderObject, watcherObject));
}
}

View File

@ -3,6 +3,7 @@ package com.massivecraft.massivecore.mixin;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import com.massivecraft.massivecore.mson.Mson;
import com.massivecraft.massivecore.util.IdUtil;
public class DisplayNameMixinDefault extends DisplayNameMixinAbstract

View File

@ -141,6 +141,11 @@ public class Mixin
return getDisplayNameMixin().getDisplayName(senderObject, watcherObject);
}
public static Mson getDisplayNameMson(Object senderObject, Object watcherObject)
{
return getDisplayNameMixin().getDisplayNameMson(senderObject, watcherObject);
}
// -------------------------------------------- //
// STATIC EXPOSE: INVENTORY
// -------------------------------------------- //

View File

@ -1,8 +1,7 @@
package com.massivecraft.massivecore.money;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
public abstract class MoneyMixinAbstract implements MoneyMixin
{
@ -48,7 +47,7 @@ public abstract class MoneyMixinAbstract implements MoneyMixin
public boolean move(String fromId, String toId, String byId, double amount, String category, String message)
{
return this.move(fromId, toId, byId, amount, (category == null ? null : Arrays.asList(category)), message);
return this.move(fromId, toId, byId, amount, (category == null ? null : Collections.singletonList(category)), message);
}
public boolean move(String fromId, String toId, String byId, double amount, Collection<String> categories)
{
@ -56,11 +55,11 @@ public abstract class MoneyMixinAbstract implements MoneyMixin
}
public boolean move(String fromId, String toId, String byId, double amount, String category)
{
return this.move(fromId, toId, byId, amount, (category == null ? null : Arrays.asList(category)), null);
return this.move(fromId, toId, byId, amount, (category == null ? null : Collections.singletonList(category)), null);
}
public boolean move(String fromId, String toId, String byId, double amount)
{
return this.move(fromId, toId, byId, amount, new ArrayList<String>(), null);
return this.move(fromId, toId, byId, amount, Collections.<String>emptyList(), null);
}
// -------------------------------------------- //

File diff suppressed because it is too large Load Diff

View File

@ -6,11 +6,12 @@ import java.util.Objects;
import org.bukkit.inventory.ItemStack;
import com.massivecraft.massivecore.cmd.MassiveCommand;
import com.massivecraft.massivecore.nms.NmsItem;
import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.util.Txt;
public class MsonEvent implements Serializable
public final class MsonEvent implements Serializable
{
// -------------------------------------------- //
// CONSTANTS
@ -22,10 +23,10 @@ public class MsonEvent implements Serializable
// FIELDS
// -------------------------------------------- //
protected final MsonEventAction action;
private final MsonEventAction action;
public MsonEventAction getEventActionType() { return this.action; }
protected final String value;
private final String value;
public String getActionText() { return this.value; }
// -------------------------------------------- //
@ -57,11 +58,27 @@ public class MsonEvent implements Serializable
{
return MsonEvent.valueOf(MsonEventAction.SUGGEST_COMMAND , replace);
}
public static MsonEvent replace(MassiveCommand cmd, String... args)
{
return MsonEvent.replace(cmd.getCommandLine(args));
}
public static MsonEvent replace(MassiveCommand cmd, Iterable<String> args)
{
return MsonEvent.replace(cmd.getCommandLine(args));
}
public static MsonEvent performCmd(String cmd)
{
return MsonEvent.valueOf(MsonEventAction.RUN_COMMAND, cmd);
}
public static MsonEvent performCmd(MassiveCommand cmd, String... args)
{
return MsonEvent.performCmd(cmd.getCommandLine(args));
}
public static MsonEvent performCmd(MassiveCommand cmd, Iterable<String> args)
{
return MsonEvent.performCmd(cmd.getCommandLine(args));
}
// showText
public static MsonEvent hoverText(String hoverText)

View File

@ -2,5 +2,5 @@ package com.massivecraft.massivecore.mson;
public interface MsonReplacement
{
public Object getReplacement(String match);
public Mson getReplacement(String match);
}

View File

@ -1,10 +1,7 @@
package com.massivecraft.massivecore.mson;
import static com.massivecraft.massivecore.mson.Mson.mson;
import static org.bukkit.ChatColor.BLUE;
import static org.bukkit.ChatColor.GREEN;
import static org.bukkit.ChatColor.RED;
import static org.bukkit.ChatColor.YELLOW;
import static org.bukkit.ChatColor.*;
import com.massivecraft.massivecore.collections.MassiveList;
import com.massivecraft.massivecore.util.Txt;
@ -23,89 +20,110 @@ public class Test
}
}
public static Mson mson;
public static void test()
{
Mson mson;
mson = mson("hello");
test("ofString", mson);
test("ofString");
mson = mson(
mson("hello")
);
test("ofMson", mson);
test("ofMson");
mson = mson(
new MassiveList<String>("hello ", "you!")
);
test("ofCollection", mson);
test("ofCollection");
mson = mson("test")
.color(BLUE)
.addChild(" test2").link("www.massivecraft.com")
.addChild(" test3 ")
.addChildren("this ", "is only ", "one way to do it!")
.root()
.tooltip("Holy moly!");
test("child", mson);
mson = mson(
mson = mson
(
"test",
mson
(
" test2",
mson
(
" test3",
mson().text(" Test4, children: ").addChild("Child1 ").addSiblings("Sibling 1 ", "Sibling 2 ", "Sibling 3 ").addSibling("Sibling 4").root()
).tooltip("Holy moly!");
test("sibling", mson);
mson("this ", "is only ", "one way to do it!")
)
).link("www.massivecraft,com")
).color(BLUE)
.tooltip("Holy Moly");
test("child");
mson = Mson.fromParsedMessage(Txt.parse("white <i>in<em>fo <b><em><bold>bad <lime>green"));
test("parsed", mson);
test("parsed");
mson = Mson.parse("white <i>in<em>fo <b><bold>bad <lime>green");
test("parsed2", mson);
test("parsed2");
mson = Mson.parse("<i>insert here %s whatever <g> you <b>%s <g>could wish for!", "!1337!", "herpi derp");
test("parseFormat", mson);
test("parseFormat");
Mson format = Mson.format("Just a %s simple string! :)", "very");
test("format", format);
// Test split
mson = mson(format.split(Txt.REGEX_WHITESPACE.toString()));
test("split", mson);
mson = Mson.format("Just a %s simple string! :)", "very");
test("format");
// -------------------------------------------- //
// TEST REPLACE
// -------------------------------------------- //
mson = mson("1 2 3 4 5 6 1 7 tests").addChild(" 01010101").root().replace('1', '0');
test("charr", mson);
mson = mson("1 2 3 4 5 6 1 7 tests", " 01010101").getRoot().replace('1', '0');
test("charr");
mson = mson("1 2 3 4 5 6 1 7 tests").addChild(" 01010101").root().replace("1", "0");
test("sequence", mson);
mson = mson("1 2 3 4 5 6 1 7 tests", " 01010101").getRoot().replace("1", "0");
test("sequence");
mson = mson("1 2 3 4 5 6 1 7 tests").addChild(" 01010101").root().replaceAll("1", "0");
test("regex", mson);
mson = mson("1 2 3 4 5 6 1 7 tests", " 01010101").getRoot().replaceAll("1", "0");
test("regex");
// -------------------------------------------- //
// TEST SPECIAL REPLACE ALL
// -------------------------------------------- //
// replace string mson
mson = mson("1 2 3 4 5 6 1 7 tests").color(BLUE).addChild(" 1+1+1").addChild("herpiderp").root().replaceAll("1", mson("0"));
test("replaceAll1", mson);
mson = mson("hellohello").addChild("hello").addChild("hello").root().replaceAll("hello", mson("lol"));
test("overload", mson);
mson = mson
(
"1 2 3 4 5 6 1 7 tests",
" 1+1+1",
"herpiderp"
).style(BLUE).replaceAll("1", mson("0"));
test("replaceAll1");
mson = mson
(
"hellohello",
"hello",
"hello"
).replaceAll("hello", mson("lol"));
test("overload");
Mson toReplace = mson("hallo");
// replace mson mson
mson = mson("1 2 3 4 5 6 7 tests").addChild(toReplace).addSibling(" miep").root().replaceAll(toReplace, mson("tests"));
test("replaceAll2", mson);
mson = mson
(
"1 2 3 4 5 6 7 tests ",
toReplace,
" miep"
).replaceAll(toReplace, mson("tests"));
test("replaceAll2");
mson = mson("1 2 3 4 5 6 7 tests").addChild("hallo").addChild("hallo").addChild("hallo").addSibling(" miep").root().replaceAll(mson("hallo"), mson("tests"));
test("overload2", mson);
mson = mson
(
"1 2 3 4 5 6 7 tests ",
"hallo",
"hallo",
"hallo",
" miep"
).replaceAll(mson("hallo"), mson("tests"));
test("overload2");
// -------------------------------------------- //
// EXAMPLES
@ -119,7 +137,7 @@ public class Test
mson("[deny]").style(GREEN).command("/asfd blah deny"),
"?"
).style(YELLOW);
test("Would you like to [allow] or [deny]?", mson);
test("Would you like to [allow] or [deny]?");
// -------------------------------------------- //
// SPONGE MIMIC
@ -131,10 +149,15 @@ public class Test
mson("Sponges are ").color(YELLOW),
mson("invincible!").color(RED)
);
test("Sponges are invincible", mson);
test("Sponges are invincible");
}
public static void test(String name)
{
test(name, mson);
}
public static void test(String name, Mson mson)
{
System.out.println(name + ": " + mson);

View File

@ -796,7 +796,7 @@ public class Coll<E> extends CollAbstract<E>
{
String id = idToEntry.getKey();
Entry<JsonElement, Long> remoteEntry = idToEntry.getValue();
loadFromRemote(id, remoteEntry);
loadFromRemoteFixed(id, remoteEntry);
}
}

View File

@ -17,6 +17,7 @@ import com.massivecraft.massivecore.MassiveCore;
import com.massivecraft.massivecore.mixin.Mixin;
import com.massivecraft.massivecore.util.IdUtil;
import com.massivecraft.massivecore.util.MUtil;
import com.massivecraft.massivecore.util.TimeUnit;
public class EngineScheduledTeleport extends EngineAbstract
{
@ -45,7 +46,7 @@ public class EngineScheduledTeleport extends EngineAbstract
this.teleporteeIdToScheduledTeleport.put(st.getTeleporteeId(), st);
st.setDueMillis(System.currentTimeMillis() + st.getDelaySeconds()*1000);
st.setDueMillis(System.currentTimeMillis() + st.getDelaySeconds()*TimeUnit.MILLIS_PER_SECOND);
return old;
}
@ -111,7 +112,7 @@ public class EngineScheduledTeleport extends EngineAbstract
public void cancelTeleport(PlayerMoveEvent event)
{
// If the player moved from one block to another ...
if (MUtil.isSameBlock(event.getFrom(), event.getTo())) return;
if (MUtil.isSameBlock(event)) return;
// ... cancel teleport!
Player player = event.getPlayer();

View File

@ -1500,12 +1500,12 @@ public class MUtil
// -------------------------------------------- //
public static <T extends Number> T limitNumber(T d, T min, T max)
{
if (min instanceof Number && d.doubleValue() < min.doubleValue())
if (d.doubleValue() < min.doubleValue())
{
return min;
}
if (max instanceof Number && d.doubleValue() > max.doubleValue())
if (d.doubleValue() > max.doubleValue())
{
return max;
}