Fix crash, changed some command code

This commit is contained in:
Chikachi 2017-07-02 22:35:47 +02:00
parent a13b85aa53
commit 40c4f02a72
No known key found for this signature in database
GPG Key ID: 0136086A0AC09F5E
7 changed files with 33 additions and 22 deletions

View File

@ -29,6 +29,7 @@ import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault; import javax.annotation.ParametersAreNonnullByDefault;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
@ParametersAreNonnullByDefault @ParametersAreNonnullByDefault
@ -85,7 +86,7 @@ public class CommandDiscord extends CommandBase {
@Override @Override
public boolean checkPermission(MinecraftServer server, ICommandSender sender) { public boolean checkPermission(MinecraftServer server, ICommandSender sender) {
return sender.canUseCommand(3, getName()); return sender.canUseCommand(4, getName());
} }
@Override @Override
@ -104,7 +105,7 @@ public class CommandDiscord extends CommandBase {
} }
} }
return new ArrayList<>(); return Collections.emptyList();
} }
@Override @Override

View File

@ -24,8 +24,8 @@ import net.minecraftforge.fml.common.FMLCommonHandler;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
public class SubCommandOnline { class SubCommandOnline {
public static void execute(ICommandSender sender) { static void execute(ICommandSender sender) {
boolean isDiscord = sender instanceof DiscordCommandSender; boolean isDiscord = sender instanceof DiscordCommandSender;
List<String> playerNames = new ArrayList<>(); List<String> playerNames = new ArrayList<>();

View File

@ -20,16 +20,17 @@ import com.google.common.base.Joiner;
import net.minecraft.command.ICommandSender; import net.minecraft.command.ICommandSender;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.util.text.TextComponentString; import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.common.DimensionManager; import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.fml.common.FMLCommonHandler; import net.minecraftforge.fml.common.FMLCommonHandler;
import java.text.DecimalFormat; import java.text.DecimalFormat;
import java.util.*; import java.util.*;
public class SubCommandTps { class SubCommandTps {
private static final DecimalFormat timeFormatter = new DecimalFormat("########0.000"); private static final DecimalFormat timeFormatter = new DecimalFormat("########0.000");
public static void execute(ICommandSender sender, ArrayList<String> args) { static void execute(ICommandSender sender, ArrayList<String> args) {
boolean isDiscord = sender instanceof DiscordCommandSender; boolean isDiscord = sender instanceof DiscordCommandSender;
boolean colored = args.stream().anyMatch(arg -> arg.equalsIgnoreCase("--color")); boolean colored = args.stream().anyMatch(arg -> arg.equalsIgnoreCase("--color"));
@ -48,6 +49,7 @@ public class SubCommandTps {
int maxDimensionNameLength = Math.max(CoreUtils.getMinLength(dimensionMap.values()), CoreUtils.getMaxLength(dimensionMap.values())); int maxDimensionNameLength = Math.max(CoreUtils.getMinLength(dimensionMap.values()), CoreUtils.getMaxLength(dimensionMap.values()));
SortedSet<Integer> sortedDimensionIds = new TreeSet<>(dimensionMap.keySet()); SortedSet<Integer> sortedDimensionIds = new TreeSet<>(dimensionMap.keySet());
String color;
for (Integer dimensionId : sortedDimensionIds) { for (Integer dimensionId : sortedDimensionIds) {
String dimensionName = dimensionMap.get(dimensionId); String dimensionName = dimensionMap.get(dimensionId);
@ -55,30 +57,35 @@ public class SubCommandTps {
double worldTickTime = CoreUtils.mean(minecraftServer.worldTickTimes.get(dimensionId)) * 1.0E-6D; double worldTickTime = CoreUtils.mean(minecraftServer.worldTickTimes.get(dimensionId)) * 1.0E-6D;
double worldTPS = Math.min(1000.0 / worldTickTime, 20); double worldTPS = Math.min(1000.0 / worldTickTime, 20);
color = colored && !isDiscord ? CoreUtils.tpsToColorString(worldTPS, false) : "";
tpsTimes.add( tpsTimes.add(
String.format( String.format(
"%s%s : Mean tick time: %s ms. Mean TPS: %s", "%s%s : Mean tick time: %s ms. Mean TPS: %s",
colored ? CoreUtils.tpsToColorString(worldTPS, isDiscord) : "", colored && isDiscord ? CoreUtils.tpsToColorString(worldTPS, true) : "",
String.format( String.format(
"Dim %s %s", "Dim %s %s",
CoreUtils.padLeft(dimensionId + "", maxDimensionIdLength), CoreUtils.padLeft(dimensionId + "", maxDimensionIdLength),
CoreUtils.padRight(dimensionName, maxDimensionNameLength) CoreUtils.padRight(dimensionName, maxDimensionNameLength)
), ),
CoreUtils.padLeft(timeFormatter.format(worldTickTime), 6), CoreUtils.padLeft(color + timeFormatter.format(worldTickTime) + TextFormatting.RESET, 6),
CoreUtils.padLeft(timeFormatter.format(worldTPS), 6) CoreUtils.padLeft(color + timeFormatter.format(worldTPS) + TextFormatting.RESET, 6)
) )
); );
} }
double meanTickTime = CoreUtils.mean(minecraftServer.tickTimeArray) * 1.0E-6D; double meanTickTime = CoreUtils.mean(minecraftServer.tickTimeArray) * 1.0E-6D;
double meanTPS = Math.min(1000.0 / meanTickTime, 20); double meanTPS = Math.min(1000.0 / meanTickTime, 20);
color = colored && !isDiscord ? CoreUtils.tpsToColorString(meanTPS, false) : "";
tpsTimes.add( tpsTimes.add(
String.format( String.format(
"%s%s : Mean tick time: %s ms. Mean TPS: %s", "%s%s : Mean tick time: %s ms. Mean TPS: %s",
colored ? CoreUtils.tpsToColorString(meanTPS, isDiscord) : "", colored && isDiscord ? CoreUtils.tpsToColorString(meanTPS, true) : "",
CoreUtils.padRight("Overall", maxDimensionIdLength + maxDimensionNameLength + 5), CoreUtils.padRight("Overall", maxDimensionIdLength + maxDimensionNameLength + 5),
CoreUtils.padLeft(timeFormatter.format(meanTickTime), 6), CoreUtils.padLeft(color + timeFormatter.format(meanTickTime) + TextFormatting.RESET, 6),
CoreUtils.padLeft(timeFormatter.format(meanTPS), 6) CoreUtils.padLeft(color + timeFormatter.format(meanTPS) + TextFormatting.RESET, 6)
) )
); );

View File

@ -44,10 +44,6 @@ public class CoreUtils {
return Joiner.on(" ").join(words); return Joiner.on(" ").join(words);
} }
public static String tpsToColorString(double tps) {
return tpsToColorString(tps, true);
}
public static String tpsToColorString(double tps, boolean isDiscord) { public static String tpsToColorString(double tps, boolean isDiscord) {
if (19 <= tps) { if (19 <= tps) {
return isDiscord ? "+ " : TextFormatting.GREEN.toString(); return isDiscord ? "+ " : TextFormatting.GREEN.toString();

View File

@ -25,6 +25,7 @@ import net.dv8tion.jda.core.entities.TextChannel;
import net.dv8tion.jda.core.events.ReadyEvent; import net.dv8tion.jda.core.events.ReadyEvent;
import net.dv8tion.jda.core.hooks.ListenerAdapter; import net.dv8tion.jda.core.hooks.ListenerAdapter;
import javax.security.auth.login.LoginException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -100,6 +101,14 @@ public class DiscordClient extends ListenerAdapter {
this.jda = builder this.jda = builder
.buildAsync(); .buildAsync();
} catch (LoginException e) {
CoreLogger.Log(
String.format(
"Failed to connect to Discord: %s",
e.getMessage()
),
true
);
} catch (Exception e) { } catch (Exception e) {
CoreLogger.Log("Failed to connect to Discord", true); CoreLogger.Log("Failed to connect to Discord", true);
e.printStackTrace(); e.printStackTrace();
@ -156,8 +165,6 @@ public class DiscordClient extends ListenerAdapter {
} }
public void broadcast(MessageConfig message, List<Long> channels) { public void broadcast(MessageConfig message, List<Long> channels) {
if (channels == null || channels.size() == 0) return;
broadcast(new Message(message), channels); broadcast(new Message(message), channels);
} }
@ -166,7 +173,7 @@ public class DiscordClient extends ListenerAdapter {
} }
public void broadcast(Message message, List<Long> channels) { public void broadcast(Message message, List<Long> channels) {
if (channels == null || channels.size() == 0) return; if (channels == null || channels.size() == 0 || this.jda == null || this.jda.getStatus() != JDA.Status.CONNECTED) return;
for (Long channelId : channels) { for (Long channelId : channels) {
TextChannel channel = this.jda.getTextChannelById(channelId); TextChannel channel = this.jda.getTextChannelById(channelId);

View File

@ -113,7 +113,7 @@ public class Message {
private String formatText(String text, Channel channel) { private String formatText(String text, Channel channel) {
String message = text; String message = text;
if (this.arguments == null) { if (this.arguments == null) {
this.arguments = new HashMap<>(); this.arguments = new HashMap<>();
} }

View File

@ -21,8 +21,8 @@ public class MinecraftMessagesConfig {
private transient static final String CHAT_MESSAGE_NORMAL = "**[{USER}]** {MESSAGE}"; private transient static final String CHAT_MESSAGE_NORMAL = "**[{USER}]** {MESSAGE}";
private transient static final String CHAT_MESSAGE_WEBHOOK = "{MESSAGE}"; private transient static final String CHAT_MESSAGE_WEBHOOK = "{MESSAGE}";
private transient static final String COMMAND_NORMAL = "**[{USER}]** executed **{COMMAND}** with the arguments **{ARGUMENTS}**"; private transient static final String COMMAND_NORMAL = "**[{USER}]** executed **{COMMAND} {ARGUMENTS}**";
private transient static final String COMMAND_WEBHOOK = "*executed **{COMMAND}** with the arguments **{ARGUMENTS}***"; private transient static final String COMMAND_WEBHOOK = "*executed **{COMMAND} {ARGUMENTS}***";
private transient static final String PLAYER_JOIN_NORMAL = "**{USER}** just joined the server!"; private transient static final String PLAYER_JOIN_NORMAL = "**{USER}** just joined the server!";
private transient static final String PLAYER_JOIN_WEBHOOK = "*Joined the server!*"; private transient static final String PLAYER_JOIN_WEBHOOK = "*Joined the server!*";