Shuffle some internal stuff to API so that modules will compile
This commit is contained in:
@@ -19,6 +19,12 @@
|
||||
<description>API implemented by the Elastic Portal Suite</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.google.code.gson</groupId>
|
||||
<artifactId>gson</artifactId>
|
||||
<version>2.2.4</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>net.md-5</groupId>
|
||||
<artifactId>bungeecord-config</artifactId>
|
||||
|
66
api/src/main/java/net/md_5/bungee/Util.java
Normal file
66
api/src/main/java/net/md_5/bungee/Util.java
Normal file
@@ -0,0 +1,66 @@
|
||||
package net.md_5.bungee;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
/**
|
||||
* Series of utility classes to perform various operations.
|
||||
*/
|
||||
public class Util
|
||||
{
|
||||
|
||||
private static final int DEFAULT_PORT = 25565;
|
||||
|
||||
/**
|
||||
* Method to transform human readable addresses into usable address objects.
|
||||
*
|
||||
* @param hostline in the format of 'host:port'
|
||||
* @return the constructed hostname + port.
|
||||
*/
|
||||
public static InetSocketAddress getAddr(String hostline)
|
||||
{
|
||||
String[] split = hostline.split( ":" );
|
||||
int port = DEFAULT_PORT;
|
||||
if ( split.length > 1 )
|
||||
{
|
||||
port = Integer.parseInt( split[1] );
|
||||
}
|
||||
return new InetSocketAddress( split[0], port );
|
||||
}
|
||||
|
||||
/**
|
||||
* Formats an integer as a hex value.
|
||||
*
|
||||
* @param i the integer to format
|
||||
* @return the hex representation of the integer
|
||||
*/
|
||||
public static String hex(int i)
|
||||
{
|
||||
return String.format( "0x%02X", i );
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a pretty one line version of a {@link Throwable}. Useful for
|
||||
* debugging.
|
||||
*
|
||||
* @param t the {@link Throwable} to format.
|
||||
* @return a string representing information about the {@link Throwable}
|
||||
*/
|
||||
public static String exception(Throwable t)
|
||||
{
|
||||
// TODO: We should use clear manually written exceptions
|
||||
StackTraceElement[] trace = t.getStackTrace();
|
||||
return t.getClass().getSimpleName() + " : " + t.getMessage()
|
||||
+ ( ( trace.length > 0 ) ? " @ " + t.getStackTrace()[0].getClassName() + ":" + t.getStackTrace()[0].getLineNumber() : "" );
|
||||
}
|
||||
|
||||
public static String csv(Iterable<?> objects)
|
||||
{
|
||||
return format( objects, ", " );
|
||||
}
|
||||
|
||||
public static String format(Iterable<?> objects, String separators)
|
||||
{
|
||||
return Joiner.on( separators ).join( objects );
|
||||
}
|
||||
}
|
@@ -0,0 +1,121 @@
|
||||
package net.md_5.bungee.chat;
|
||||
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import net.md_5.bungee.api.chat.BaseComponent;
|
||||
import net.md_5.bungee.api.chat.ClickEvent;
|
||||
import net.md_5.bungee.api.chat.HoverEvent;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class BaseComponentSerializer
|
||||
{
|
||||
|
||||
protected void deserialize(JsonObject object, BaseComponent component, JsonDeserializationContext context)
|
||||
{
|
||||
if ( object.has( "color" ) )
|
||||
{
|
||||
component.setColor( ChatColor.valueOf( object.get( "color" ).getAsString().toUpperCase() ) );
|
||||
}
|
||||
if ( object.has( "bold" ) )
|
||||
{
|
||||
component.setBold( object.get( "bold" ).getAsBoolean() );
|
||||
}
|
||||
if ( object.has( "italic" ) )
|
||||
{
|
||||
component.setItalic( object.get( "italic" ).getAsBoolean() );
|
||||
}
|
||||
if ( object.has( "underlined" ) )
|
||||
{
|
||||
component.setUnderlined( object.get( "underlined" ).getAsBoolean() );
|
||||
}
|
||||
if ( object.has( "strikethrough" ) )
|
||||
{
|
||||
component.setUnderlined( object.get( "strikethrough" ).getAsBoolean() );
|
||||
}
|
||||
if ( object.has( "obfuscated" ) )
|
||||
{
|
||||
component.setUnderlined( object.get( "obfuscated" ).getAsBoolean() );
|
||||
}
|
||||
if ( object.has( "extra" ) )
|
||||
{
|
||||
component.setExtra( Arrays.<BaseComponent>asList( context.<BaseComponent[]>deserialize( object.get( "extra" ), BaseComponent[].class ) ) );
|
||||
}
|
||||
|
||||
//Events
|
||||
if ( object.has( "clickEvent" ) )
|
||||
{
|
||||
JsonObject event = object.getAsJsonObject( "clickEvent" );
|
||||
component.setClickEvent( new ClickEvent(
|
||||
ClickEvent.Action.valueOf( event.get( "action" ).getAsString().toUpperCase() ),
|
||||
event.get( "value" ).getAsString() ) );
|
||||
}
|
||||
if ( object.has( "hoverEvent" ) )
|
||||
{
|
||||
JsonObject event = object.getAsJsonObject( "hoverEvent" );
|
||||
BaseComponent[] res;
|
||||
if ( event.get( "value" ).isJsonArray() )
|
||||
{
|
||||
res = context.deserialize( event.get( "value" ), BaseComponent[].class );
|
||||
} else
|
||||
{
|
||||
res = new BaseComponent[]
|
||||
{
|
||||
context.<BaseComponent>deserialize( event.get( "value" ), BaseComponent.class )
|
||||
};
|
||||
}
|
||||
component.setHoverEvent( new HoverEvent( HoverEvent.Action.valueOf( event.get( "action" ).getAsString().toUpperCase() ), res ) );
|
||||
}
|
||||
}
|
||||
|
||||
protected void serialize(JsonObject object, BaseComponent component, JsonSerializationContext context)
|
||||
{
|
||||
if ( component.getColorRaw() != null )
|
||||
{
|
||||
object.addProperty( "color", component.getColorRaw().getName() );
|
||||
}
|
||||
if ( component.isBoldRaw() != null )
|
||||
{
|
||||
object.addProperty( "bold", component.isBoldRaw() );
|
||||
}
|
||||
if ( component.isItalicRaw() != null )
|
||||
{
|
||||
object.addProperty( "italic", component.isItalicRaw() );
|
||||
}
|
||||
if ( component.isUnderlinedRaw() != null )
|
||||
{
|
||||
object.addProperty( "underlined", component.isUnderlinedRaw() );
|
||||
}
|
||||
if ( component.isStrikethroughRaw() != null )
|
||||
{
|
||||
object.addProperty( "strikethrough", component.isStrikethroughRaw() );
|
||||
}
|
||||
if ( component.isObfuscatedRaw() != null )
|
||||
{
|
||||
object.addProperty( "obfuscated", component.isObfuscatedRaw() );
|
||||
}
|
||||
|
||||
if ( component.getExtra() != null )
|
||||
{
|
||||
object.add( "extra", context.serialize( component.getExtra() ) );
|
||||
}
|
||||
|
||||
//Events
|
||||
if ( component.getClickEvent() != null )
|
||||
{
|
||||
JsonObject clickEvent = new JsonObject();
|
||||
clickEvent.addProperty( "action", component.getClickEvent().getAction().toString().toLowerCase() );
|
||||
clickEvent.addProperty( "value", component.getClickEvent().getValue() );
|
||||
object.add( "clickEvent", clickEvent );
|
||||
}
|
||||
if ( component.getHoverEvent() != null )
|
||||
{
|
||||
JsonObject hoverEvent = new JsonObject();
|
||||
hoverEvent.addProperty( "action", component.getHoverEvent().getAction().toString().toLowerCase() );
|
||||
hoverEvent.add( "value", context.serialize( component.getHoverEvent().getValue() ) );
|
||||
object.add( "hoverEvent", hoverEvent );
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,69 @@
|
||||
package net.md_5.bungee.chat;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import net.md_5.bungee.api.chat.BaseComponent;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import net.md_5.bungee.api.chat.TranslatableComponent;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class ComponentSerializer implements JsonSerializer<BaseComponent>, JsonDeserializer<BaseComponent>
|
||||
{
|
||||
|
||||
private final static Gson gson = new GsonBuilder().
|
||||
registerTypeAdapter( BaseComponent.class, new ComponentSerializer() ).
|
||||
registerTypeAdapter( TextComponent.class, new TextComponentSerializer() ).
|
||||
registerTypeAdapter( TranslatableComponent.class, new TranslatableComponentSerializer() ).
|
||||
create();
|
||||
|
||||
public static BaseComponent[] parse(String json)
|
||||
{
|
||||
if ( json.startsWith( "[" ) )
|
||||
{ //Array
|
||||
return gson.fromJson( json, BaseComponent[].class );
|
||||
}
|
||||
return new BaseComponent[]
|
||||
{
|
||||
gson.fromJson( json, BaseComponent.class )
|
||||
};
|
||||
}
|
||||
|
||||
public static String toString(BaseComponent component)
|
||||
{
|
||||
return gson.toJson( component );
|
||||
}
|
||||
|
||||
public static String toString(BaseComponent... components)
|
||||
{
|
||||
return gson.toJson( components );
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseComponent deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
if ( json.isJsonPrimitive() )
|
||||
{
|
||||
return new TextComponent( json.getAsString() );
|
||||
}
|
||||
JsonObject object = json.getAsJsonObject();
|
||||
if ( object.has( "translate" ) )
|
||||
{
|
||||
return context.deserialize( json, TranslatableComponent.class );
|
||||
}
|
||||
return context.deserialize( json, TextComponent.class );
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(BaseComponent src, Type typeOfSrc, JsonSerializationContext context)
|
||||
{
|
||||
return context.serialize( src, src.getClass() );
|
||||
}
|
||||
}
|
@@ -0,0 +1,40 @@
|
||||
package net.md_5.bungee.chat;
|
||||
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonPrimitive;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
public class TextComponentSerializer extends BaseComponentSerializer implements JsonSerializer<TextComponent>, JsonDeserializer<TextComponent>
|
||||
{
|
||||
|
||||
@Override
|
||||
public TextComponent deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
TextComponent component = new TextComponent();
|
||||
JsonObject object = json.getAsJsonObject();
|
||||
deserialize( object, component, context );
|
||||
component.setText( object.get( "text" ).getAsString() );
|
||||
return component;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(TextComponent src, Type typeOfSrc, JsonSerializationContext context)
|
||||
{
|
||||
if ( !src.hasFormatting() )
|
||||
{
|
||||
return new JsonPrimitive( src.getText() );
|
||||
}
|
||||
JsonObject object = new JsonObject();
|
||||
serialize( object, src, context );
|
||||
object.addProperty( "text", src.getText() );
|
||||
return object;
|
||||
}
|
||||
}
|
@@ -0,0 +1,45 @@
|
||||
package net.md_5.bungee.chat;
|
||||
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import net.md_5.bungee.api.chat.BaseComponent;
|
||||
import net.md_5.bungee.api.chat.TranslatableComponent;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class TranslatableComponentSerializer extends BaseComponentSerializer implements JsonSerializer<TranslatableComponent>, JsonDeserializer<TranslatableComponent>
|
||||
{
|
||||
|
||||
@Override
|
||||
public TranslatableComponent deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
|
||||
{
|
||||
TranslatableComponent component = new TranslatableComponent();
|
||||
JsonObject object = json.getAsJsonObject();
|
||||
deserialize( object, component, context );
|
||||
component.setTranslate( object.get( "translate" ).getAsString() );
|
||||
if ( object.has( "with" ) )
|
||||
{
|
||||
component.setWith( Arrays.asList( (BaseComponent[]) context.deserialize( object.get( "with" ), BaseComponent[].class ) ) );
|
||||
}
|
||||
return component;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(TranslatableComponent src, Type typeOfSrc, JsonSerializationContext context)
|
||||
{
|
||||
JsonObject object = new JsonObject();
|
||||
serialize( object, src, context );
|
||||
object.addProperty( "translate", src.getTranslate() );
|
||||
if ( src.getWith() != null )
|
||||
{
|
||||
object.add( "with", context.serialize( src.getWith() ) );
|
||||
}
|
||||
return object;
|
||||
}
|
||||
}
|
49
api/src/main/java/net/md_5/bungee/command/PlayerCommand.java
Normal file
49
api/src/main/java/net/md_5/bungee/command/PlayerCommand.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package net.md_5.bungee.command;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.base.Predicate;
|
||||
import com.google.common.collect.Iterables;
|
||||
import net.md_5.bungee.api.CommandSender;
|
||||
import net.md_5.bungee.api.ProxyServer;
|
||||
import net.md_5.bungee.api.connection.ProxiedPlayer;
|
||||
import net.md_5.bungee.api.plugin.Command;
|
||||
import net.md_5.bungee.api.plugin.TabExecutor;
|
||||
|
||||
/**
|
||||
* @deprecated internal use only
|
||||
*/
|
||||
@Deprecated
|
||||
public abstract class PlayerCommand extends Command implements TabExecutor
|
||||
{
|
||||
|
||||
public PlayerCommand(String name)
|
||||
{
|
||||
super( name );
|
||||
}
|
||||
|
||||
public PlayerCommand(String name, String permission, String... aliases)
|
||||
{
|
||||
super( name, permission, aliases );
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<String> onTabComplete(CommandSender sender, String[] args)
|
||||
{
|
||||
final String lastArg = ( args.length > 0 ) ? args[args.length - 1].toLowerCase() : "";
|
||||
return Iterables.transform( Iterables.filter( ProxyServer.getInstance().getPlayers(), new Predicate<ProxiedPlayer>()
|
||||
{
|
||||
@Override
|
||||
public boolean apply(ProxiedPlayer player)
|
||||
{
|
||||
return player.getName().toLowerCase().startsWith( lastArg );
|
||||
}
|
||||
} ), new Function<ProxiedPlayer, String>()
|
||||
{
|
||||
@Override
|
||||
public String apply(ProxiedPlayer player)
|
||||
{
|
||||
return player.getName();
|
||||
}
|
||||
} );
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user