A few issues

Fix formatting of Discord => MC messages
Fix wrong message relay
Remove Minecraft code from the core
Fix double formatting
master
Chikachi 2017-07-08 16:52:37 +02:00
parent 40c4f02a72
commit daeaa2f2de
No known key found for this signature in database
GPG Key ID: 0136086A0AC09F5E
12 changed files with 800 additions and 732 deletions

13
.github/ISSUE_TEMPLATE.md vendored Normal file
View File

@ -0,0 +1,13 @@
### Base informations
**Minecraft version:**
**DiscordIntegration version:**
**Link to pastebin with (censored) config:** *Optional*
### Expected behavior
### Actual behavior
### Steps to reproduce

46
CODE_OF_CONDUCT.md Normal file
View File

@ -0,0 +1,46 @@
# Contributor Covenant Code of Conduct
## Our Pledge
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
## Our Standards
Examples of behavior that contributes to creating a positive environment include:
* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
Examples of unacceptable behavior by participants include:
* The use of sexualized language or imagery and unwelcome sexual attention or advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a professional setting
## Our Responsibilities
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
## Scope
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at chikachi@chikachi.net. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
[homepage]: http://contributor-covenant.org
[version]: http://contributor-covenant.org/version/1/4/

File diff suppressed because it is too large Load Diff

View File

@ -42,17 +42,15 @@ public class DiscordIntegration {
public void onPreInit(FMLPreInitializationEvent event) {
coreProxy.onPreInit(event.getModConfigurationDirectory());
char softReset = '\ufffd';
Patterns.addDiscordToMinecraftPattern(Patterns.strikeThroughPattern, TextFormatting.STRIKETHROUGH + "$1" + softReset);
Patterns.addDiscordToMinecraftPattern(Patterns.underlinePattern, TextFormatting.UNDERLINE + "$1" + softReset);
Patterns.addDiscordToMinecraftPattern(Patterns.italicMePattern, TextFormatting.ITALIC + "$1" + softReset);
Patterns.addDiscordToMinecraftPattern(Patterns.italicPattern, TextFormatting.ITALIC + "$1" + softReset);
Patterns.addDiscordToMinecraftPattern(Patterns.boldPattern, TextFormatting.BOLD + "$1" + softReset);
Patterns.addDiscordToMinecraftPattern(Patterns.strikeThroughPattern, TextFormatting.STRIKETHROUGH + "$1\ufffd");
Patterns.addDiscordToMinecraftPattern(Patterns.underlinePattern, TextFormatting.UNDERLINE + "$1\ufffd");
Patterns.addDiscordToMinecraftPattern(Patterns.italicMePattern, TextFormatting.ITALIC + "$1\ufffd");
Patterns.addDiscordToMinecraftPattern(Patterns.italicPattern, TextFormatting.ITALIC + "$1\ufffd");
Patterns.addDiscordToMinecraftPattern(Patterns.boldPattern, TextFormatting.BOLD + "$1\ufffd");
Patterns.addDiscordToMinecraftPattern(Patterns.multiCodePattern, "$1");
Patterns.addDiscordToMinecraftPattern(Patterns.singleCodePattern, "$1");
Patterns.addMinecraftFormattingPattern(Pattern.compile("(?i)(([\u00a7&]([0-9A-FK-OR]))|" + softReset + ")"), new Patterns.ReplacementCallback() {
Patterns.addMinecraftFormattingPattern(Pattern.compile("(?i)((\u00c2?[\u00a7&]([0-9A-FK-OR]))|\ufffd)"), new Patterns.ReplacementCallback() {
private ArrayList<TextFormatting> layers = new ArrayList<>();
@Override
@ -62,7 +60,7 @@ public class DiscordIntegration {
@Override
public String replace(ArrayList<String> groups) {
if (groups.get(0).charAt(0) == softReset) {
if (groups.get(0).charAt(0) == '\ufffd') {
if (layers.size() > 0) {
layers.remove(layers.size() - 1);
return TextFormatting.RESET + Joiner.on("").join(layers.stream().map(TextFormatting::toString).collect(Collectors.toList()));
@ -71,11 +69,12 @@ public class DiscordIntegration {
return TextFormatting.RESET.toString();
}
String modifier = String.valueOf('\u00a7') + groups.get(2).substring(1);
String modifier = String.valueOf("\u00a7") + groups.get(2).substring(groups.get(2).length() - 1);
for (TextFormatting textFormatting : TextFormatting.values()) {
if (modifier.equalsIgnoreCase(textFormatting.toString())) {
layers.add(textFormatting);
break;
}
}

View File

@ -44,6 +44,9 @@ public class SubCommandConfig {
case "save":
Configuration.save();
break;
case "clean":
Configuration.saveClean();
break;
default:
sender.sendMessage(new TextComponentString("Unknown command"));
break;

View File

@ -15,10 +15,6 @@
package chikachi.discord.core;
import com.google.common.base.Joiner;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.util.text.TextComponentUtils;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.fml.common.FMLCommonHandler;
import java.util.Collection;
import java.util.Map;
@ -46,11 +42,11 @@ public class CoreUtils {
public static String tpsToColorString(double tps, boolean isDiscord) {
if (19 <= tps) {
return isDiscord ? "+ " : TextFormatting.GREEN.toString();
return isDiscord ? "+ " : "\u00a7a";
} else if (15 <= tps) {
return isDiscord ? "! " : TextFormatting.YELLOW.toString();
return isDiscord ? "! " : "\u00a7e";
} else {
return isDiscord ? "- " : TextFormatting.RED.toString();
return isDiscord ? "- " : "\u00a7c";
}
}

View File

@ -160,11 +160,7 @@ public class DiscordClient extends ListenerAdapter {
return this.jda.getSelfUser();
}
public void broadcast(MessageConfig message, Long... channels) {
broadcast(message, Arrays.asList(channels));
}
public void broadcast(MessageConfig message, List<Long> channels) {
void broadcast(MessageConfig message, List<Long> channels) {
broadcast(new Message(message), channels);
}
@ -173,7 +169,9 @@ public class DiscordClient extends ListenerAdapter {
}
public void broadcast(Message message, List<Long> channels) {
if (channels == null || channels.size() == 0 || this.jda == null || this.jda.getStatus() != JDA.Status.CONNECTED) return;
if (channels == null || channels.size() == 0 || this.jda == null || this.jda.getStatus() != JDA.Status.CONNECTED) {
return;
}
for (Long channelId : channels) {
TextChannel channel = this.jda.getTextChannelById(channelId);
@ -187,7 +185,7 @@ public class DiscordClient extends ListenerAdapter {
}
}
channel.sendMessage(message.getFormattedText(channel)).queue();
channel.sendMessage(message.getFormattedTextDiscord(channel)).queue();
}
}
}

View File

@ -78,11 +78,6 @@ public class Message {
return webhookMessage;
}
public Message setAuthor(String author) {
this.author = author;
return this;
}
public Message setMessage(MessageConfig message) {
this.message = message;
return this;
@ -107,11 +102,16 @@ public class Message {
return this.author;
}
private String formatText(String text) {
return formatText(text, null);
public Message setAuthor(String author) {
this.author = author;
return this;
}
private String formatText(String text, Channel channel) {
return formatText(text, channel, true);
}
private String formatText(String text, Channel channel, boolean isDiscord) {
String message = text;
if (this.arguments == null) {
@ -167,18 +167,18 @@ public class Message {
}
}
return (this.prefix != null && this.prefix.trim().length() > 0 ? this.prefix.trim() + " " : "") + Patterns.minecraftToDiscord(message);
return (this.prefix != null && this.prefix.trim().length() > 0 ? this.prefix.trim() + " " : "") + (isDiscord ? Patterns.minecraftToDiscord(message) : Patterns.discordToMinecraft(message));
}
public String getUnformattedText() {
private String getUnformattedText() {
return this.message != null ? this.message.normal : "";
}
public String getFormattedText() {
return getFormattedText(null);
String getFormattedTextDiscord(Channel channel) {
return formatText(getUnformattedText(), channel, true);
}
public String getFormattedText(Channel channel) {
return formatText(getUnformattedText(), channel);
public String getFormattedTextMinecraft() {
return formatText(getUnformattedText(), null, false);
}
}

View File

@ -34,21 +34,13 @@ public class Patterns {
public static final Pattern singleCodePattern = Pattern.compile("`(.*)`");
public static final Pattern multiCodePattern = Pattern.compile("```(.*)```");
public static final Pattern tagPattern = Pattern.compile("@([^\\s]+)");
static final Pattern tagPattern = Pattern.compile("@([^\\s]+)");
private static final HashMap<Pattern, String> discordToMinecraftPatterns = new HashMap<>();
private static final HashMap<Pattern, String> minecraftToDiscordPatterns = new HashMap<>();
private static final HashMap<Pattern, ReplacementCallback> minecraftFormattingPatterns = new HashMap<>();
private static final HashMap<Pattern, ReplacementCallback> discordFormattingPatterns = new HashMap<>();
public interface ReplacementCallback {
String pre(String text);
String replace(ArrayList<String> groups);
String post(String text);
}
public static void addDiscordToMinecraftPattern(Pattern pattern, String replacement) {
discordToMinecraftPatterns.put(pattern, replacement);
}
@ -65,7 +57,7 @@ public class Patterns {
discordFormattingPatterns.put(pattern, replacement);
}
public static String discordToMinecraft(String content) {
static String discordToMinecraft(String content) {
if (content == null) {
return "";
}
@ -81,7 +73,7 @@ public class Patterns {
return content;
}
public static String minecraftToDiscord(String content) {
static String minecraftToDiscord(String content) {
if (content == null) {
return "";
}
@ -119,4 +111,12 @@ public class Patterns {
return content;
}
public interface ReplacementCallback {
String pre(String text);
String replace(ArrayList<String> groups);
String post(String text);
}
}

View File

@ -27,11 +27,12 @@ import java.io.FileWriter;
import java.io.IOException;
public class Configuration {
private static File directory;
private static File configFile;
private static ConfigWrapper config;
public static void onPreInit(String directoryPath) {
File directory = new File(directoryPath);
directory = new File(directoryPath);
//noinspection ResultOfMethodCallIgnored
directory.mkdirs();
@ -41,12 +42,8 @@ public class Configuration {
load();
}
public static void load() {
if (configFile == null) {
return;
}
Gson gson = new GsonBuilder()
private static Gson createGson() {
return new GsonBuilder()
.registerTypeAdapter(ChannelConfigType.class, new ChannelConfigTypeAdapter())
.registerTypeAdapter(DimensionConfigType.class, new DimensionConfigTypeAdapter())
.registerTypeAdapter(MessageConfig.class, new MessageConfigAdapter())
@ -54,6 +51,14 @@ public class Configuration {
.serializeNulls()
.setPrettyPrinting()
.create();
}
public static void load() {
if (configFile == null) {
return;
}
Gson gson = createGson();
if (!configFile.exists()) {
config = new ConfigWrapper();
@ -83,7 +88,8 @@ public class Configuration {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException ignored) {}
} catch (IOException ignored) {
}
}
}
}
@ -92,13 +98,7 @@ public class Configuration {
}
public static void save() {
Gson gson = new GsonBuilder()
.registerTypeAdapter(ChannelConfigType.class, new ChannelConfigTypeAdapter())
.registerTypeAdapter(DimensionConfigType.class, new DimensionConfigTypeAdapter())
.registerTypeAdapter(MessageConfig.class, new MessageConfigAdapter())
.setVersion(3.0)
.setPrettyPrinting()
.create();
Gson gson = createGson();
try {
FileWriter writer = new FileWriter(configFile);
@ -109,6 +109,20 @@ public class Configuration {
}
}
public static void saveClean() {
Gson gson = createGson();
try {
FileWriter writer = new FileWriter(new File(directory, CoreConstants.MODID + "_clean.json"));
ConfigWrapper cleanConfig = new ConfigWrapper();
cleanConfig.fillFields();
writer.write(gson.toJson(cleanConfig));
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static ConfigWrapper getConfig() {
return config;
}

View File

@ -15,10 +15,10 @@
package chikachi.discord.listener;
import chikachi.discord.DiscordCommandSender;
import chikachi.discord.DiscordIntegrationLogger;
import chikachi.discord.IMCHandler;
import chikachi.discord.core.DiscordClient;
import chikachi.discord.core.Message;
import chikachi.discord.core.Patterns;
import chikachi.discord.core.config.ConfigWrapper;
import chikachi.discord.core.config.Configuration;
import chikachi.discord.core.config.discord.CommandConfig;
@ -67,8 +67,8 @@ public class DiscordListener extends ListenerAdapter {
channelConfig = discordConfig.channels.channels.get(channelId);
dimensions = channelConfig.relayChat.getDimensions(discordConfig.channels.generic.relayChat);
} else {
channelConfig = discordConfig.channels.generic;
dimensions = discordConfig.channels.generic.relayChat.getDimensions();
// Don't relay messages from channels not configured
return;
}
if (dimensions == null) {
@ -125,9 +125,7 @@ public class DiscordListener extends ListenerAdapter {
HashMap<String, String> arguments = new HashMap<>();
arguments.put(
"MESSAGE",
Patterns.discordToMinecraft(
event.getMessage().getContent()
)
event.getMessage().getContent()
);
Message message = new Message()
@ -136,7 +134,8 @@ public class DiscordListener extends ListenerAdapter {
.setArguments(arguments);
for (EntityPlayerMP player : players) {
player.sendMessage(new TextComponentString(message.getFormattedText()));
DiscordIntegrationLogger.Log(message.getFormattedTextMinecraft());
player.sendMessage(new TextComponentString(message.getFormattedTextMinecraft()));
}
}
}

View File

@ -4,7 +4,7 @@
"description": "MC <=> Discord communication",
"version": "${version}",
"mcversion": "${mcversion}",
"url": "",
"url": "https://discordintegration.net",
"updateUrl": "",
"authorList": ["Chikachi"],
"credits": "",