Skip to content

Commit

Permalink
hide session, only show one confirmation
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianVennen committed Dec 2, 2022
1 parent 1a8b360 commit e36373b
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
The last MOTD id and session are now stored and reused.
- Running `/motdgg apply` will use the session and id of the last MOTD you applied or uploaded
- Running `/motdgg editor` will reuse the same session and id but upload your MOTD and server icon again
- The session id is no long shown in chat for non-console senders on 1.13+
- Only show a single confirmation message
8 changes: 8 additions & 0 deletions src/main/java/gg/motd/api/MOTD.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,12 @@ public SaveResponse save(APIClient client) throws IOException {
String response = client.request("api/save.php", "POST", new Gson().toJson(this));
return new Gson().fromJson(response, SaveResponse.class);
}

public String getUrl() {
return "https://motd.gg/" + this.getId();
}

public String getSessionUrL() {
return "https://motd.gg/" + this.getId() + "?s=" + this.getSession();
}
}
13 changes: 10 additions & 3 deletions src/main/java/gg/motd/bukkit/MOTDApplyCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
}

try {
// set live MOTD
parent.plugin.motd = parent.getPlugin().getClient().getMotd(id);
parent.plugin.motd.setSession(session);
} catch (IOException e) {
Expand All @@ -55,9 +56,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
return true;
}

// set live MOTD
sender.sendMessage(ChatColor.GREEN + "Applied new MOTD.");

boolean success = true;
// write MOTD to server.properties
try {
Path propertiesPath = Paths.get("server.properties");
Expand All @@ -70,6 +69,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
catch (IOException e) {
sender.sendMessage(ChatColor.RED + "Failed to write server.properties. Check your log for details.");
parent.plugin.getLogger().log(Level.SEVERE, "Failed to write server.properties: ", e);
success = false;
}

// write server icon to server-icon.png
Expand All @@ -93,12 +93,19 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
catch (Exception e) {
sender.sendMessage(ChatColor.RED + "Failed to load server icon. You will have to restart your server to apply the change. Check your log for details.");
parent.plugin.getLogger().log(Level.SEVERE, "Failed to load server icon: ", e);
success = false;
}
} catch (IOException e) {
sender.sendMessage(ChatColor.RED + "Failed to write server-icon.png. Check your log for details.");
parent.plugin.getLogger().log(Level.SEVERE, "Failed to write server-icon.png: ", e);
success = false;
}
}

if (success) {
sender.sendMessage(ChatColor.GREEN + "Applied new MOTD and server icon.");
}

return true;
}
}
23 changes: 21 additions & 2 deletions src/main/java/gg/motd/bukkit/MOTDEditorCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import gg.motd.api.MOTD;
import gg.motd.api.SaveResponse;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
Expand Down Expand Up @@ -57,8 +60,24 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
return true;
}

sender.sendMessage(ChatColor.GREEN + "Edit your MOTD here: " + ChatColor.AQUA +
"https://motd.gg/" + parent.plugin.motd.getId() + "?s=" + parent.plugin.motd.getSession());
if (sender instanceof ConsoleCommandSender || !parent.plugin.hasSpigotMethod()) {
sender.sendMessage(ChatColor.GREEN + "Edit your MOTD here: " + ChatColor.AQUA +
parent.plugin.motd.getSessionUrL());
}
else {
TextComponent message = new TextComponent();
message.setText("Edit your MOTD here: ");
message.setColor(ChatColor.GREEN.asBungee());

TextComponent link = new TextComponent();
link.setColor(ChatColor.AQUA.asBungee());
link.setText(parent.plugin.motd.getUrl());
link.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, parent.plugin.motd.getSessionUrL()));
message.addExtra(link);

sender.spigot().sendMessage(message);
}

return true;
}
}
12 changes: 12 additions & 0 deletions src/main/java/gg/motd/bukkit/MOTDGGPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import gg.motd.api.APIClient;
import gg.motd.api.MOTD;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.CachedServerIcon;

import java.lang.reflect.Method;
import java.util.Objects;

public class MOTDGGPlugin extends JavaPlugin {
Expand All @@ -28,4 +30,14 @@ public void onEnable() {
public APIClient getClient() {
return client;
}

public boolean hasSpigotMethod() {
Class<CommandSender> sender = CommandSender.class;
try {
Method spigot = sender.getMethod("spigot");
return true;
} catch (NoSuchMethodException e) {
return false;
}
}
}

0 comments on commit e36373b

Please sign in to comment.