added version placeholder

This commit is contained in:
Destro174 2020-09-12 15:18:50 +02:00
commit 08ee751b12
10 changed files with 683 additions and 0 deletions

39
.gitignore vendored Normal file
View File

@ -0,0 +1,39 @@
# Plugin
.idea
testserver
run
# Compiled class file
*.class
# Log file
*.log
# BlueJ files
*.ctxt
# Mobile Tools for Java (J2ME)
.mtj.tmp/
# IntelliJ
*.iml
*.ipr
*.iws
.idea/
out/
# Maven
target/
# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

46
pom.xml Normal file
View File

@ -0,0 +1,46 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alttd.destro174</groupId>
<artifactId>CustomMOTD</artifactId>
<version>1.0</version>
<build>
<finalName>${project.artifactId}</finalName>
<defaultGoal>clean package</defaultGoal>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>plugin.yml</include>
<include>lang.yml</include>
<include>config.yml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
<dependencies>
<dependency>
<groupId>com.destroystokyo.paper</groupId>
<artifactId>paper</artifactId>
<version>1.16.3-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,220 @@
package com.alttd.destro174.custommotd;
import java.util.ArrayList;
import java.util.List;
import com.alttd.destro174.custommotd.commands.custommotdCommand;
import com.alttd.destro174.custommotd.listener.PingListener;
import com.alttd.destro174.custommotd.storage.MOTDConfig;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.CachedServerIcon;
public class CustomMOTD extends JavaPlugin {
public static CustomMOTD CustomMOTD;
MOTDConfig motdfile, counterfile;
public static String Options = "";
public int counter;
public static List<String> motd;
public static CachedServerIcon icon;
public List<String> colorList(List<String> list){
List<String> lines = new ArrayList<String>();
for (String s : list) {
s = s.replace("%version%", Bukkit.getMinecraftVersion()); // 1.16 addon
lines.add(ChatColor.translateAlternateColorCodes('&', s));
}
return lines;
}
public static CustomMOTD getInstance() {
return CustomMOTD;
}
@Override
public void onEnable() {
// TODO Insert logic to be performed when the plugin is enabled
try {
CustomMOTD = this;
//getLogger().info("Altitude Custom MOTD starting");
loadConfig();
reloadConfig();
registerEvents();
registerCommands();
} catch (Throwable t) {
getLogger().severe("MOTD - An error has occured!");
if (!(t instanceof ExceptionInInitializerError)) {
t.printStackTrace();
}
getServer().getPluginManager().disablePlugin(this);
}
}
private void loadConfig() {
// TODO load Config files
saveDefaultConfig();
counter = 0;
motdfile = new MOTDConfig(this, "MOTD.yml", "MOTD.yml");
counterfile = new MOTDConfig(this, "counter.yml", "counter.yml");
counter = counterfile.getInt("counter");
//motd = motdfile.getStringList("MOTD");
setmotd(motdfile.getString("MOTD"));
//getLogger().info("setting motd to - " + motdfile.getString("MOTD"));
//getLogger().info("setting Counter to - " + counter);
for(String line : this.getConfig().getKeys(false)) {
if(!line.equalsIgnoreCase("mcmmo")) {
Options += line + " ";
}
}
//getLogger().info(Options);
}
public void reloadsettings() {
try {
reloadConfig();
motdfile.reload();
counterfile.reload();
counter = 0;
counter = counterfile.getInt("counter");
loadmotd(motdfile.getString("MOTD"));
//getLogger().info("setting MOTD to - " + motdfile.getString("MOTD"));
//getLogger().info("setting Counter to - " + counter);
Options = "";
for(String line : this.getConfig().getKeys(false)) {
Options += line + " ";
}
}
catch (Throwable t) {
getLogger().severe("MOTD - An error has occured!");
if (!(t instanceof ExceptionInInitializerError)) {
t.printStackTrace();
}
}
}
public boolean loadmotd(String enable) {
if(!enable.equalsIgnoreCase("mcmmo")) {
for(String line : this.getConfig().getKeys(false)) {
if(line.equalsIgnoreCase(enable)) {
try {
setmotd(enable);
}
catch (Throwable t) {
if (!(t instanceof ExceptionInInitializerError)) {
t.printStackTrace();
}
}
break;
}
}
return true;
}
return false;
}
public boolean loadmotd(CommandSender sender, String enable) {
if(!enable.equalsIgnoreCase("mcmmo")) {
for(String line : this.getConfig().getKeys(false)) {
if(line.equalsIgnoreCase(enable)) {
try {
setmotd(enable);
sender.sendMessage(ChatColor.YELLOW + "MOTD set to " + ChatColor.GOLD + enable + ChatColor.YELLOW + "." );
}
catch (Throwable t) {
sender.sendMessage(ChatColor.RED + "MOTD - An error has occured!");
if (!(t instanceof ExceptionInInitializerError)) {
t.printStackTrace();
}
}
return true;
}
}
sender.sendMessage(ChatColor.RED + "MOTD " + ChatColor.GOLD + enable + ChatColor.RED + " not found!");
return false;
}
return false;
}
public void setmotd(String motdname) {
if(motdname.equalsIgnoreCase("default") || motdname.equalsIgnoreCase("mcmmo")) {
//getLogger().info("MOTD - Checking for MCMMO counter: " + counter);
if(counter > 0) {
getLogger().info(" - Activating mcmmo MOTD");
List<String> lines = this.colorList(this.getConfig().getStringList("mcmmo"));
//getLogger().info(lines.get(0) + "\n" + lines.get(1));
motd = lines;
//motd = this.getConfig().getStringList(motd + ".LINES");
motdfile.set("MOTD", "default");
motdfile.save();
motdfile.reload();
return;
}
else {
List<String> lines = this.colorList(this.getConfig().getStringList(motdname));
//getLogger().info(lines.get(0) + "\n" + lines.get(1));
motd = lines;
//motd = this.getConfig().getStringList(motd + ".LINES");
motdfile.set("MOTD", "default");
motdfile.save();
motdfile.reload();
return;
}
}
else {
List<String> lines = this.colorList(this.getConfig().getStringList(motdname));
//getLogger().info(lines.get(0) + "\n" + lines.get(1));
motd = lines;
//motd = this.getConfig().getStringList(motd + ".LINES");
motdfile.set("MOTD", motdname);
motdfile.save();
motdfile.reload();
return;
}
}
public boolean counter(CommandSender sender, String args) {
if(args.equalsIgnoreCase("up")) {
counter += 1;
counterfile.set("counter", counter);
sender.sendMessage(ChatColor.YELLOW + "MOTD counter increased.");
counterfile.save();
reloadsettings();
return true;
}
else if(args.equalsIgnoreCase("down")) {
counter -= 1;
counterfile.set("counter", counter);
sender.sendMessage(ChatColor.YELLOW + "MOTD counter decreased.");
counterfile.save();
reloadsettings();
return true;
}
return false;
}
private void registerEvents() {
// TODO add all events
PluginManager pm = getServer().getPluginManager();
pm.registerEvents(new PingListener(), this);
//pm.registerEvents(blockListener, this);
}
private void registerCommands() {
// TODO add all commands
getCommand("custommotd").setExecutor(new custommotdCommand());
//getCommand("customicon").setExecutor(new customIconCommand());
}
}

View File

@ -0,0 +1,50 @@
package com.alttd.destro174.custommotd;
public class MotdWrapper {
private String title;
private String line1;
private String line2;
private boolean restricted;
public MotdWrapper(String Title, String Line1, String Line2, boolean Restricted) {
this.title = Title;
this.line1 = Line1;
this.line2 = Line2;
this.restricted = Restricted;
}
public String getTitle() {
return title;
}
public String getLine1() {
return line1;
}
public String getLine2() {
return line2;
}
public void setLine1(String line1) {
this.line1 = line1;
}
public void setLine2(String line2) {
this.line2 = line2;
}
public boolean isRestricted() {
return restricted;
}
public void setRestricted(boolean restricted) {
this.restricted = restricted;
}
public void setTitle(String title) {
this.title = title;
}
}

View File

@ -0,0 +1,103 @@
package com.alttd.destro174.custommotd.commands;
import com.alttd.destro174.custommotd.CustomMOTD;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
public class custommotdCommand implements CommandExecutor/*, TabCompleter*/ {
public CustomMOTD instance = CustomMOTD.getInstance();
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (sender.hasPermission("custommotd.admin")) {
switch (args.length) {
case 0:
return help(sender);
case 1:
if (args[0].equalsIgnoreCase("help")) {
return help(sender);
}
if (args[0].equalsIgnoreCase("list")) {
sender.sendMessage(ChatColor.YELLOW + "Here's a list of all possible MOTD:");
sender.sendMessage(ChatColor.GOLD + CustomMOTD.Options);
return true;
}
if (args[0].equalsIgnoreCase("reload")) {
instance.reloadsettings();
sender.sendMessage("Custom MOTD reloaded!");
return true;
}
case 2:
if (args[0].equalsIgnoreCase("set")) {
if(!args[1].isEmpty()) {
if(args[1].equalsIgnoreCase("mcmmo")) {
sender.sendMessage(ChatColor.RED + "You can't set this MOTD!");
return false;
}else {
return CustomMOTD.getInstance().loadmotd(sender, args[1]);
}
}
}
if (args[0].equalsIgnoreCase("counter")) {
if(!args[1].isEmpty()) {
return CustomMOTD.getInstance().counter(sender, args[1]);
}
}
default:
return help(sender);
}
}
else {
sender.sendMessage(ChatColor.RED + "You do not have permission to use this command." );
return false;
}
}
private boolean help(CommandSender sender) {
sender.sendMessage("-----" + ChatColor.YELLOW + "Altitude Motd" + "-----");
sender.sendMessage(ChatColor.BOLD + "" + ChatColor.YELLOW + "/custommotd help " +
ChatColor.RESET + "- Show this help.");
sender.sendMessage(ChatColor.BOLD + "" + ChatColor.YELLOW + "/custommotd list " +
ChatColor.RESET + "- List all the custom MOTD.");
sender.sendMessage(ChatColor.BOLD + "" + ChatColor.YELLOW + "/custommotd set motdname" +
ChatColor.RESET + "- Set the MOTD.");
sender.sendMessage(ChatColor.BOLD + "" + ChatColor.YELLOW + "/custommotd counter up " +
ChatColor.RESET + "- Add one to the counter.");
sender.sendMessage(ChatColor.BOLD + "" + ChatColor.YELLOW + "/custommotd counter down " +
ChatColor.RESET + "- Remove one from the counter.");
sender.sendMessage(ChatColor.BOLD + "" + ChatColor.YELLOW + "/custommotd reload " +
ChatColor.RESET + "- Reload the config.");
return false;
}
/*@Override
public List<String> onTabComplete(CommandSender sender, Command cmd, String label, String[] args) {
List<String> completions = new ArrayList<>();
List<String> commands = new ArrayList<>();
if(args.length == 1) {
commands.add("set");
commands.add("reload");
commands.add("counter");
StringUtil.copyPartialMatches(args[0], commands, completions);
}
if(args.length == 2) {
if (args[0].equals("set")) {
completions = CustomMOTD.getmotdlist().stream()
.filter(wrapper -> !wrapper.isRestricted())
.map(wrapper -> wrapper.getTitle())
.collect(Collectors.toList());
} else if (args[0].equals("counter")) {
commands.add("up");
commands.add("down");
StringUtil.copyPartialMatches(args[1], commands, completions);
}
}
return completions;
}*/
}

View File

@ -0,0 +1,79 @@
package com.alttd.destro174.custommotd.listener;
import com.alttd.destro174.custommotd.CustomMOTD;
import com.alttd.destro174.custommotd.MotdWrapper;
import org.bukkit.ChatColor;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.server.ServerListPingEvent;
public class PingListener implements Listener {
public CustomMOTD instance = CustomMOTD.getInstance();
@EventHandler
public void onPing(ServerListPingEvent event) throws IllegalArgumentException, UnsupportedOperationException, Exception {
event.setMotd(CustomMOTD.motd.get(0) + "\n" + CustomMOTD.motd.get(1));
}
/*private List<String> colorList(List<String> list){
List<String> lines = new ArrayList<String>();
for (String s : list) {
lines.add(ChatColor.translateAlternateColorCodes('&', s));
}
return lines;
}*/
/*@EventHandler
public void onPing(ServerListPingEvent event) {
//event.setMotd(instance.getActiveMotd());
for(MotdWrapper wrapper : CustomMOTD.getmotdlist()) {
if(wrapper.getTitle().equalsIgnoreCase("default")) {
event.setMotd(ChatColor.translateAlternateColorCodes('&',
centerText(wrapper.getLine1(), 60)
+ "\n" + centerText(wrapper.getLine2(), 60)
));
}
}
}*/
// lineLength = 80 (Chat Length)
// lineLength = 45 (MOTD Length)
public String centerText(String text, int lineLength) {
char[] chars = text.toCharArray(); // Get a list of all characters in text
boolean isBold = false;
double length = 0;
ChatColor pholder = null;
for (int i = 0; i < chars.length; i++) { // Loop through all characters
// Check if the character is a ColorCode..
if (chars[i] == '&' && chars.length != (i + 1) && (pholder = ChatColor.getByChar(chars[i + 1])) != null) {
if (pholder != ChatColor.UNDERLINE && pholder != ChatColor.ITALIC
&& pholder != ChatColor.STRIKETHROUGH && pholder != ChatColor.MAGIC) {
isBold = (chars[i + 1] == 'l'); // Setting bold to true or false, depending on if the ChatColor is Bold.
length--; // Removing one from the length, of the string, because we don't wanna count color codes.
i += isBold ? 1 : 0;
}
} else {
// If the character is not a color code:
length++; // Adding a space
length += (isBold ? (chars[i] != ' ' ? 0.1555555555555556 : 0) : 0); // Adding 0.156 spaces if the character is bold.
}
}
double spaces = (lineLength - length) / 3; // Getting the spaces to add by (max line length - length) / 2
// Adding the spaces
StringBuilder builder = new StringBuilder();
for (int i = 0; i < spaces; i++) {
builder.append(' ');
}
String copy = builder.toString();
builder.append(text).append(copy);
return builder.toString();
}
}

View File

@ -0,0 +1,101 @@
package com.alttd.destro174.custommotd.storage;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
public class MOTDConfig extends YamlConfiguration {
private File file;
private String defaults;
private JavaPlugin plugin;
/**
* Creates new PluginFile, without defaults
* @param plugin - Your plugin
* @param fileName - Name of the file
*/
public MOTDConfig(JavaPlugin plugin, String fileName) {
this(plugin, fileName, null);
}
/**
* Creates new PluginFile, with defaults
* @param plugin - Your plugin
* @param fileName - Name of the file
* @param defaultsName - Name of the defaults
*/
public MOTDConfig(JavaPlugin plugin, String fileName, String defaultsName) {
this.plugin = plugin;
this.defaults = defaultsName;
this.file = new File(plugin.getDataFolder(), fileName);
reload();
}
/**
* Reload configuration
*/
public void reload() {
if (!file.exists()) {
try {
file.getParentFile().mkdirs();
file.createNewFile();
} catch (IOException exception) {
exception.printStackTrace();
plugin.getLogger().severe("Error while creating file " + file.getName());
}
}
try {
load(file);
if (defaults != null) {
InputStreamReader reader = new InputStreamReader(plugin.getResource(defaults));
FileConfiguration defaultsConfig = YamlConfiguration.loadConfiguration(reader);
setDefaults(defaultsConfig);
options().copyDefaults(true);
reader.close();
save();
}
} catch (IOException exception) {
exception.printStackTrace();
plugin.getLogger().severe("Error while loading file " + file.getName());
} catch (InvalidConfigurationException exception) {
exception.printStackTrace();
plugin.getLogger().severe("Error while loading file " + file.getName());
}
}
/**
* Save configuration
*/
public void save() {
try {
options().indent(2);
save(file);
} catch (IOException exception) {
exception.printStackTrace();
plugin.getLogger().severe("Error while saving file " + file.getName());
}
}
}

View File

@ -0,0 +1,2 @@
MOTD:
default

View File

@ -0,0 +1,31 @@
# 1. If someone tries to activate a new custom MOTD when another one is already active,
# they are both set to active. I need it to either auto disable all the others,
# or notify the person who issued the command that another is already active,
# and which one it is.
#2. I'd like a command that will list all the available custom motds, just their names
#3. Whenever I add a custom motd it requires a reboot AND sometimes
# it resets to the default config if changes were made before the server shut down.
# I need it to not reset the config if I upload a new one before issuing a restart,
# and I'd also like a reload command for the plugin so we don't need to reboot.
#### This plugin will not work if the default and MCMMO entry are removed
# The MCMMO motd will activate when the default motd is on and the counter is higher then 0
# any new motd added to this list must all be lowercase, if a capital is used the plugin won't read them propperly
default:
- ' &eThis is the first line'
- ' &4This is the second line!'
mcmmo:
- ' &eThis is the first line'
- ' &4This is the second line!'
custom1:
- ' &eThis is the first line'
- ' &4This is the second line!'
justatest:
- ' &eThis is the first line'
- ' &4This is the second line!'

View File

@ -0,0 +1,12 @@
name: CustomMOTD
main: alttd.destro.custommotd.CustomMOTD
version: 1.0
author: destro174
commands:
custommotd:
aliases: [cmotd]
description: Set motd in-game or in console.
customicon:
aliases: [cicon]
description: Change the servericon in game or from console.