feat: 添加几个工具类

Signed-off-by: 502647092 <admin@yumc.pw>
merge/1/MERGE
502647092 2017-01-30 12:53:47 +08:00
parent c15a26b29b
commit 92e9fae869
6 changed files with 338 additions and 9 deletions

View File

@ -135,5 +135,10 @@
<artifactId>ProtocolLib</artifactId>
<version>4.2.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.sk89q.worldedit</groupId>
<artifactId>worldedit-bukkit</artifactId>
<version>6.1.5</version>
</dependency>
</dependencies>
</project>

View File

@ -26,7 +26,7 @@ public abstract class AbstractInjectConfig {
private static String INJECT_TYPE_ERROR = "配置节点 %s 数据类型不匹配 应该为: %s 但实际为: %s!";
private static String INJECT_ERROR = "自动注入配置失败 可能造成插件运行错误 %s: %s!";
private static String PATH_NOT_FOUND = "配置节点 %s 丢失 将使用默认值!";
private ConfigurationSection config;
protected ConfigurationSection config;
/**
*
@ -243,4 +243,8 @@ public abstract class AbstractInjectConfig {
Log.d(ex);
}
}
public ConfigurationSection getConfig() {
return config;
}
}

View File

@ -1,9 +1,9 @@
package pw.yumc.YumCore.config.inject;
import pw.yumc.YumCore.config.FileConfig;
import java.io.File;
import pw.yumc.YumCore.config.FileConfig;
/**
*
*
@ -11,8 +11,6 @@ import java.io.File;
* @author
*/
public abstract class InjectConfig extends AbstractInjectConfig {
protected FileConfig config;
public InjectConfig() {
this(new FileConfig());
}
@ -22,7 +20,6 @@ public abstract class InjectConfig extends AbstractInjectConfig {
}
public InjectConfig(FileConfig config) {
this.config = config;
inject(config);
}
@ -36,14 +33,14 @@ public abstract class InjectConfig extends AbstractInjectConfig {
* @return
*/
public FileConfig getConfig() {
return config;
return (FileConfig) config;
}
/**
*
*/
public void reload() {
config.reload();
getConfig().reload();
inject(config);
}
@ -52,6 +49,6 @@ public abstract class InjectConfig extends AbstractInjectConfig {
*/
public void save() {
save(config);
config.save();
getConfig().save();
}
}

View File

@ -0,0 +1,25 @@
package pw.yumc.YumCore.mc;
/**
* Minecraft
*
* @author
* @since 2017/1/26 0026
*/
public class MinecraftTools {
/**
*
*
* @param address
*
* @return {@link ServerInfo}
*/
public static ServerInfo getServerInfo(String address) {
if (address.contains(":")) {
String[] args = address.split(":");
return new ServerInfo(args[0], Integer.parseInt(args[1]));
} else {
return new ServerInfo(address);
}
}
}

View File

@ -0,0 +1,165 @@
package pw.yumc.YumCore.mc;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.charset.Charset;
/**
* Minecraft
*
* @author
* @since 2017/1/26 0026
*/
public class ServerInfo {
private String address = "localhost";
private int port = 25565;
private int timeout = 1500;
private int pingVersion = -1;
private int protocolVersion = -1;
private String gameVersion = "初始化中...";
private String motd = "初始化中...";
private int playersOnline = -1;
private int maxPlayers = -1;
/**
* Minecraft
*
* @param address
* 25565
*/
public ServerInfo(String address) {
this(address, 25565);
}
/**
* Minecraft
*
* @param address
*
* @param port
*
*/
public ServerInfo(String address, int port) {
this.address = address;
this.port = port;
}
/**
*
*
* @return
*/
public boolean fetchData() {
try {
Socket socket = new Socket();
OutputStream outputStream;
DataOutputStream dataOutputStream;
InputStream inputStream;
InputStreamReader inputStreamReader;
socket.setSoTimeout(this.timeout);
socket.connect(new InetSocketAddress(this.getAddress(), this.getPort()), this.getTimeout());
outputStream = socket.getOutputStream();
dataOutputStream = new DataOutputStream(outputStream);
inputStream = socket.getInputStream();
inputStreamReader = new InputStreamReader(inputStream, Charset.forName("UTF-16BE"));
dataOutputStream.write(new byte[] { (byte) 0xFE, (byte) 0x01 });
int packetId = inputStream.read();
if (packetId == -1) { throw new IOException("Premature end of stream."); }
if (packetId != 0xFF) { throw new IOException("Invalid packet ID (" + packetId + ")."); }
int length = inputStreamReader.read();
if (length == -1) { throw new IOException("Premature end of stream."); }
if (length == 0) { throw new IOException("Invalid string length."); }
char[] chars = new char[length];
if (inputStreamReader.read(chars, 0, length) != length) { throw new IOException("Premature end of stream."); }
String string = new String(chars);
if (string.startsWith("§")) {
String[] data = string.split("\0");
this.pingVersion = Integer.parseInt(data[0].substring(1));
this.protocolVersion = Integer.parseInt(data[1]);
this.gameVersion = data[2];
this.motd = data[3];
this.playersOnline = Integer.parseInt(data[4]);
this.maxPlayers = Integer.parseInt(data[5]);
} else {
String[] data = string.split("§");
this.motd = data[0];
this.playersOnline = Integer.parseInt(data[1]);
this.maxPlayers = Integer.parseInt(data[2]);
}
dataOutputStream.close();
outputStream.close();
inputStreamReader.close();
inputStream.close();
socket.close();
} catch (IOException exception) {
gameVersion = "获取失败!";
motd = "获取失败!";
return false;
}
return true;
}
public String getAddress() {
return this.address;
}
public int getPort() {
return this.port;
}
public void setTimeout(int timeout) {
this.timeout = timeout;
}
public int getTimeout() {
return this.timeout;
}
public int getPingVersion() {
return this.pingVersion;
}
public int getProtocolVersion() {
return this.protocolVersion;
}
public String getGameVersion() {
return this.gameVersion;
}
public String getMotd() {
return this.motd;
}
public int getPlayersOnline() {
return this.playersOnline;
}
public int getMaxPlayers() {
return this.maxPlayers;
}
}

View File

@ -0,0 +1,133 @@
package pw.yumc.YumCore.text;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.ChatColor;
/**
*
*
* @since 201667 2:22:12
* @author
*/
public class Scroller {
private static final char COLOUR_CHAR = '\u00A7';
private String content;
private int position;
private final List<String> list = new ArrayList<>();
private ChatColor colour;
/**
* @param text
*
*/
public Scroller(final String text) {
this(text, 16);
}
/**
* @param text
*
* @param size
*
*/
public Scroller(final String text, final int size) {
this(text, size, 1);
}
/**
* @param text
*
* @param size
*
* @param skip
*
*/
public Scroller(final String text, final int size, final int skip) {
this(text, size, skip, '&');
}
/**
* @param text
*
* @param size
*
* @param skip
*
* @param colorChar
*
*/
public Scroller(String text, int size, int skip, final char colorChar) {
this.colour = ChatColor.RESET;
StringBuilder result;
if (text.length() < size) {
result = new StringBuilder(text);
while (result.length() < size) {
result.append(" ");
}
text = result.toString();
}
size -= 2;
if (size < 1) {
size = 1;
}
if (skip < 0) {
skip = 0;
}
if (colorChar != COLOUR_CHAR) {
text = ChatColor.translateAlternateColorCodes(colorChar, text);
}
for (int i = 0; i < text.length() - size; ++i) {
this.list.add(text.substring(i, i + size));
}
result = new StringBuilder();
for (int i = 0; i < skip; ++i) {
System.out.println(i);
list.add(text.substring(text.length() - size + (i > size ? size : i), text.length()) + result);
if (result.length() < size) {
result.append(" ");
}
}
for (int i = 0; i < size - skip; ++i) {
System.out.println(i);
list.add(text.substring(text.length() - size + skip + i, text.length()) + result + text.substring(0, i));
}
for (int i = 0; i < skip && i <= result.length(); ++i) {
System.out.println(i);
list.add(result.substring(0, result.length() - i) + text.substring(0, size - (skip > size ? size : skip) + i));
}
}
/**
*
*
* @return
*/
public String next() {
StringBuilder result = this.getNext();
if (result.charAt(result.length() - 1) == COLOUR_CHAR) {
result.setCharAt(result.length() - 1, ' ');
}
if (result.charAt(0) == COLOUR_CHAR) {
final ChatColor var2 = ChatColor.getByChar(result.charAt(1));
if (var2 != null) {
colour = var2;
result = getNext();
if (result.charAt(0) != 32) {
result.setCharAt(0, ' ');
}
}
}
return colour + result.toString();
}
/**
* List
*
* @return
*/
private StringBuilder getNext() {
return new StringBuilder(this.list.get(this.position++ % this.list.size()));
}
}