Cleanup the code a bit more

This commit is contained in:
Alex 2021-04-16 11:35:54 +02:00
parent 87aaf7f7b3
commit c6a0980f15
9 changed files with 62 additions and 88 deletions

View File

@ -96,14 +96,11 @@ public class Console implements CommandSender {
reader.setHandleUserInterrupt(false);
terminal = TerminalBuilder.builder().streams(in, out).system(true).jansi(true).build();
tabReader = LineReaderBuilder.builder().terminal(terminal).completer(new Completer() {
@Override
public void complete(LineReader reader, ParsedLine line, List<Candidate> candidates) {
String[] args = CustomStringUtils.splitStringToArgs(line.line());
List<String> tab = Limbo.getInstance().getPluginManager().getTabOptions(Limbo.getInstance().getConsole(), args);
for (String each : tab) {
candidates.add(new Candidate(each));
}
tabReader = LineReaderBuilder.builder().terminal(terminal).completer((reader, line, candidates) -> {
String[] args = CustomStringUtils.splitStringToArgs(line.line());
List<String> tab = Limbo.getInstance().getPluginManager().getTabOptions(Limbo.getInstance().getConsole(), args);
for (String each : tab) {
candidates.add(new Candidate(each));
}
}).build();
tabReader.setAutosuggestion(SuggestionType.NONE);
@ -149,7 +146,7 @@ public class Console implements CommandSender {
@Override
public void sendMessage(BaseComponent[] component) {
sendMessage(String.join("", Arrays.asList(component).stream().map(each -> each.toLegacyText()).collect(Collectors.toList())));
sendMessage(Arrays.stream(component).map(BaseComponent::toLegacyText).collect(Collectors.joining("")));
}
@Override
@ -159,7 +156,7 @@ public class Console implements CommandSender {
ConsoleTextOutput.appendText(ChatColor.stripColor("[" + date + " Info] " + message), true);
logs.println(ChatColor.stripColor("[" + date + " Info] " + message));
try {
reader.getOutput().append("[" + date + " Info] " + translateToConsole(message) + "\n");
reader.getOutput().append("[").append(date).append(" Info] ").append(translateToConsole(message)).append("\n");
reader.getOutput().flush();
} catch (IOException e) {
e.printStackTrace();

View File

@ -191,12 +191,7 @@ public class GUI extends JFrame {
GUI frame = new GUI();
frame.setVisible(true);
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
SystemInfo.printInfo();
}
});
Thread t1 = new Thread(SystemInfo::printInfo);
t1.start();
loadFinish = true;

View File

@ -20,10 +20,10 @@ public class SystemInfo {
long allocatedMemory = runtime.totalMemory();
long freeMemory = runtime.freeMemory();
sb.append("Free Memory: " + format.format(freeMemory / 1024 / 1024) + " MB\n");
sb.append("Allocated Memory: " + format.format(allocatedMemory / 1024 / 1024) + " MB\n");
sb.append("Max Memory: " + format.format(maxMemory / 1024 / 1024) + " MB\n");
sb.append("Memory Usage: " + format.format((allocatedMemory - freeMemory) / 1024 / 1024) + "/" + format.format(maxMemory / 1024 / 1024) + " MB (" + Math.round((double) (allocatedMemory - freeMemory) / (double) (maxMemory) * 100) + "%)\n");
sb.append("Free Memory: ").append(format.format(freeMemory / 1024 / 1024)).append(" MB\n");
sb.append("Allocated Memory: ").append(format.format(allocatedMemory / 1024 / 1024)).append(" MB\n");
sb.append("Max Memory: ").append(format.format(maxMemory / 1024 / 1024)).append(" MB\n");
sb.append("Memory Usage: ").append(format.format((allocatedMemory - freeMemory) / 1024 / 1024)).append("/").append(format.format(maxMemory / 1024 / 1024)).append(" MB (").append(Math.round((double) (allocatedMemory - freeMemory) / (double) (maxMemory) * 100)).append("%)\n");
sb.append("\n");
try {
@ -35,9 +35,9 @@ public class SystemInfo {
double systemLoad = operatingSystemMXBean.getSystemCpuLoad();
int processors = runtime.availableProcessors();
sb.append("Available Processors: " + processors + "\n");
sb.append("Process CPU Load: " + Math.round(processLoad * 100) + "%\n");
sb.append("System CPU Load: " + Math.round(systemLoad * 100) + "%\n");
sb.append("Available Processors: ").append(processors).append("\n");
sb.append("Process CPU Load: ").append(Math.round(processLoad * 100)).append("%\n");
sb.append("System CPU Load: ").append(Math.round(systemLoad * 100)).append("%\n");
GUI.sysText.setText(sb.toString());
} catch (Exception ignore) {
}

View File

@ -17,7 +17,7 @@ import java.util.Properties;
public class ServerProperties {
public static final String COMMENT = "For explaination of what each of the options does, please visit:\nhttps://github.com/LOOHP/Limbo/blob/master/src/main/resources/server.properties";
Optional<BufferedImage> favicon;
private BufferedImage favicon;
private final File file;
private final int maxPlayers;
private final int serverPort;
@ -91,7 +91,7 @@ public class ServerProperties {
try {
BufferedImage image = ImageIO.read(png);
if (image.getHeight() == 64 && image.getWidth() == 64) {
favicon = Optional.of(image);
favicon = image;
} else {
Limbo.getInstance().getConsole().sendMessage("Unable to load server-icon.png! The image is not 64 x 64 in size!");
}
@ -100,7 +100,7 @@ public class ServerProperties {
}
} else {
Limbo.getInstance().getConsole().sendMessage("No server-icon.png found");
favicon = Optional.empty();
favicon = null;
}
Limbo.getInstance().getConsole().sendMessage("Loaded server.properties");
@ -115,7 +115,7 @@ public class ServerProperties {
}
public Optional<BufferedImage> getFavicon() {
return favicon;
return Optional.ofNullable(favicon);
}
public File getFile() {

View File

@ -93,26 +93,11 @@ public class Metrics {
startSubmitting();
}
addCustomChart(new Metrics.SingleLineChart("players", new Callable<Integer>() {
@Override
public Integer call() throws Exception {
return Limbo.getInstance().getPlayers().size();
}
}));
addCustomChart(new Metrics.SingleLineChart("players", () -> Limbo.getInstance().getPlayers().size()));
addCustomChart(new Metrics.SimplePie("limbo_version", new Callable<String>() {
@Override
public String call() throws Exception {
return limboVersion;
}
}));
addCustomChart(new Metrics.SimplePie("limbo_version", () -> limboVersion));
addCustomChart(new Metrics.SimplePie("minecraftVersion", new Callable<String>() {
@Override
public String call() throws Exception {
return Limbo.getInstance().serverImplementationVersion;
}
}));
addCustomChart(new Metrics.SimplePie("minecraftVersion", () -> Limbo.getInstance().serverImplementationVersion));
}
/**

View File

@ -7,7 +7,7 @@ import java.util.List;
public class CustomStringUtils {
public static boolean arrayContains(String compare, String[] args, boolean IgnoreCase) {
return IgnoreCase ? Arrays.asList(args).stream().anyMatch(each -> each.equalsIgnoreCase(compare)) : Arrays.asList(args).stream().anyMatch(each -> each.equals(compare));
return IgnoreCase ? Arrays.stream(args).anyMatch(each -> each.equalsIgnoreCase(compare)) : Arrays.asList(args).contains(compare);
}
public static boolean arrayContains(String compare, String[] args) {
@ -34,7 +34,7 @@ public class CustomStringUtils {
}
tokens.add(sb.toString());
return tokens.toArray(new String[tokens.size()]);
return tokens.toArray(new String[0]);
}
public static int getIndexOfArg(String str, int ordinal) {

View File

@ -15,7 +15,7 @@ import java.util.UUID;
public class DataTypeIO {
public static void writeBlockPosition(DataOutputStream out, BlockPosition position) throws IOException {
out.writeLong(((position.getX() & 0x3FFFFFF) << 38) | ((position.getZ() & 0x3FFFFFF) << 12) | (position.getY() & 0xFFF));
out.writeLong(((long) (position.getX() & 0x3FFFFFF) << 38) | ((long) (position.getZ() & 0x3FFFFFF) << 12) | (position.getY() & 0xFFF));
}
public static void writeUUID(DataOutputStream out, UUID uuid) throws IOException {

View File

@ -16,8 +16,8 @@ import java.util.*;
public class YamlOrder extends PropertyUtils {
private static final String TRANSIENT = "transient";
private final Map<Class<?>, Map<String, Property>> propertiesCache = new HashMap<Class<?>, Map<String, Property>>();
private final Map<Class<?>, Set<Property>> readableProperties = new HashMap<Class<?>, Set<Property>>();
private final Map<Class<?>, Map<String, Property>> propertiesCache = new HashMap<>();
private final Map<Class<?>, Set<Property>> readableProperties = new HashMap<>();
private BeanAccess beanAccess = BeanAccess.DEFAULT;
private boolean allowReadOnlyProperties = false;
private boolean skipMissingProperties = false;
@ -40,47 +40,44 @@ public class YamlOrder extends PropertyUtils {
return propertiesCache.get(type);
}
Map<String, Property> properties = new LinkedHashMap<String, Property>();
Map<String, Property> properties = new LinkedHashMap<>();
boolean inaccessableFieldsExist = false;
switch (bAccess) {
case FIELD:
for (Class<?> c = type; c != null; c = c.getSuperclass()) {
for (Field field : c.getDeclaredFields()) {
int modifiers = field.getModifiers();
if (!Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers)
&& !properties.containsKey(field.getName())) {
properties.put(field.getName(), new FieldProperty(field));
}
if (bAccess == BeanAccess.FIELD) {
for (Class<?> c = type; c != null; c = c.getSuperclass()) {
for (Field field : c.getDeclaredFields()) {
int modifiers = field.getModifiers();
if (!Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers)
&& !properties.containsKey(field.getName())) {
properties.put(field.getName(), new FieldProperty(field));
}
}
break;
default:
try {
for (PropertyDescriptor property : Introspector.getBeanInfo(type)
.getPropertyDescriptors()) {
Method readMethod = property.getReadMethod();
if ((readMethod == null || !readMethod.getName().equals("getClass"))
&& !isTransient(property)) {
properties.put(property.getName(), new MethodProperty(property));
}
}
} else {
try {
for (PropertyDescriptor property : Introspector.getBeanInfo(type)
.getPropertyDescriptors()) {
Method readMethod = property.getReadMethod();
if ((readMethod == null || !readMethod.getName().equals("getClass"))
&& !isTransient(property)) {
properties.put(property.getName(), new MethodProperty(property));
}
} catch (IntrospectionException e) {
throw new YAMLException(e);
}
} catch (IntrospectionException e) {
throw new YAMLException(e);
}
for (Class<?> c = type; c != null; c = c.getSuperclass()) {
for (Field field : c.getDeclaredFields()) {
int modifiers = field.getModifiers();
if (!Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers)) {
if (Modifier.isPublic(modifiers)) {
properties.put(field.getName(), new FieldProperty(field));
} else {
inaccessableFieldsExist = true;
}
for (Class<?> c = type; c != null; c = c.getSuperclass()) {
for (Field field : c.getDeclaredFields()) {
int modifiers = field.getModifiers();
if (!Modifier.isStatic(modifiers) && !Modifier.isTransient(modifiers)) {
if (Modifier.isPublic(modifiers)) {
properties.put(field.getName(), new FieldProperty(field));
} else {
inaccessableFieldsExist = true;
}
}
}
break;
}
}
if (properties.isEmpty() && inaccessableFieldsExist) {
throw new YAMLException("No JavaBean properties found in " + type.getName());
@ -94,11 +91,11 @@ public class YamlOrder extends PropertyUtils {
return Boolean.TRUE.equals(fd.getValue(TRANSIENT));
}
public Set<Property> getProperties(Class<? extends Object> type) {
public Set<Property> getProperties(Class<?> type) {
return getProperties(type, beanAccess);
}
public Set<Property> getProperties(Class<? extends Object> type, BeanAccess bAccess) {
public Set<Property> getProperties(Class<?> type, BeanAccess bAccess) {
if (readableProperties.containsKey(type)) {
return readableProperties.get(type);
}
@ -107,7 +104,7 @@ public class YamlOrder extends PropertyUtils {
return properties;
}
protected Set<Property> createPropertySet(Class<? extends Object> type, BeanAccess bAccess) {
protected Set<Property> createPropertySet(Class<?> type, BeanAccess bAccess) {
Set<Property> properties = new LinkedHashSet<>();
Collection<Property> props = getPropertiesMap(type, bAccess).values();
for (Property property : props) {
@ -118,11 +115,11 @@ public class YamlOrder extends PropertyUtils {
return properties;
}
public Property getProperty(Class<? extends Object> type, String name) {
public Property getProperty(Class<?> type, String name) {
return getProperty(type, name, beanAccess);
}
public Property getProperty(Class<? extends Object> type, String name, BeanAccess bAccess) {
public Property getProperty(Class<?> type, String name, BeanAccess bAccess) {
Map<String, Property> properties = getPropertiesMap(type, bAccess);
Property property = properties.get(name);
if (property == null && skipMissingProperties) {

View File

@ -57,7 +57,7 @@ public class World {
1281525273222484040L, 9548107335L});
chunk.setHeightMaps(heightMap);
chunk.setBiomes(new int[256]);
chunk.setTileEntities(new ListTag<CompoundTag>(CompoundTag.class));
chunk.setTileEntities(new ListTag<>(CompoundTag.class));
}
}