Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Wertik committed Aug 28, 2020
1 parent 946cf43 commit a78d31e
Show file tree
Hide file tree
Showing 23 changed files with 1,236 additions and 0 deletions.
121 changes: 121 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?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>space.devport.wertik.custommessages</groupId>
<artifactId>CustomMessages</artifactId>
<version>1.0.1</version>

<name>CustomMessages</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<build>
<defaultGoal>clean install</defaultGoal>
<finalName>${project.name}-${project.version}</finalName>
<resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources/</directory>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<compilerVersion>1.8</compilerVersion>
<source>8</source>
<target>8</target>
<release>8</release>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<minimizeJar>true</minimizeJar>
<relocations>
<relocation>
<pattern>space.devport.utils</pattern>
<shadedPattern>${project.groupId}.utils</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>devport</id>
<url>http://play.pvpcraft.cz:8081/repository/devport/</url>
</repository>
<repository>
<id>placeholderapi</id>
<url>http://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.16.2-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>18.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>space.devport.utils</groupId>
<artifactId>DevportUtils</artifactId>
<version>LATEST</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.10.9</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package space.devport.wertik.custommessages;

import lombok.RequiredArgsConstructor;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import space.devport.wertik.custommessages.system.struct.MessageType;

@RequiredArgsConstructor
public class CustomMessagePlaceholders extends PlaceholderExpansion {

private final CustomMessagesPlugin plugin;

@Override
public String onPlaceholderRequest(Player player, @NotNull String params) {

if (player == null) return "no_player";

String[] args = params.split("_");

if (args.length < 2) return "not_enough_params";

MessageType type = MessageType.fromString(args[0]);

if (type == null) return "invalid_type";

if (args[0].toLowerCase().equals("message")) {
if (args.length > 2)
if (args[2].equalsIgnoreCase("formatted"))
return plugin.getMessageManager().parseMessage(player, type) != null ? plugin.getMessageManager().parseMessage(player, type) : "none";
else return "invalid_param";
return plugin.getUserManager().getUser(player).getMessage(type);
}

return "invalid_params";
}

@Override
public @NotNull String getIdentifier() {
return "custommessages";
}

@Override
public @NotNull String getAuthor() {
return String.join(", ", plugin.getDescription().getAuthors());
}

@Override
public @NotNull String getVersion() {
return plugin.getDescription().getVersion();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package space.devport.wertik.custommessages;

import space.devport.utils.text.language.LanguageDefaults;

public class CustomMessagesLanguage extends LanguageDefaults {

@Override
public void setDefaults() {
addDefault("Commands.Invalid-Type", "&cType &f%param% &cis invalid.");
addDefault("Commands.Invalid-Message", "&cMessage &f%param% &cis not valid in type &f%type%");
addDefault("Commands.Invalid-Player", "&cPlayer &f%player% &cis not online.");

addDefault("Commands.Set.Done", "&7Set message &f%message% &7in message type &f%type%");
addDefault("Commands.Set.Done-Others", "&7Set message &f%message% &7in message type &f%type% &7for &f%player%");

addDefault("Commands.Preview.Done-Others", "&7Sent preview to &f%player% &7in type &f%type%");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package space.devport.wertik.custommessages;

import lombok.Getter;
import me.clip.placeholderapi.PlaceholderAPIPlugin;
import space.devport.utils.DevportPlugin;
import space.devport.utils.UsageFlag;
import space.devport.wertik.custommessages.commands.CustomMessagesCommand;
import space.devport.wertik.custommessages.commands.subcommands.MenuSubCommand;
import space.devport.wertik.custommessages.commands.subcommands.PreviewSubCommand;
import space.devport.wertik.custommessages.commands.subcommands.ReloadSubCommand;
import space.devport.wertik.custommessages.commands.subcommands.SetSubCommand;
import space.devport.wertik.custommessages.listeners.Listeners;
import space.devport.wertik.custommessages.system.MessageManager;
import space.devport.wertik.custommessages.system.UserManager;

public class CustomMessagesPlugin extends DevportPlugin {

@Getter
private static CustomMessagesPlugin instance;

private CustomMessagePlaceholders placeholders;

@Getter
private MessageManager messageManager;

@Getter
private UserManager userManager;

@Getter
private Listeners listeners;

@Override
public void onPluginEnable() {
instance = this;

this.messageManager = new MessageManager(this);
messageManager.load();

this.userManager = new UserManager(this);
userManager.load();

setupPlaceholders();

this.listeners = new Listeners(this);
listeners.registerListeners();

new CustomMessagesLanguage();

addMainCommand(new CustomMessagesCommand())
.addSubCommand(new ReloadSubCommand())
.addSubCommand(new SetSubCommand())
.addSubCommand(new PreviewSubCommand())
.addSubCommand(new MenuSubCommand());
}

private void setupPlaceholders() {
if (getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) {

if (placeholders == null)
this.placeholders = new CustomMessagePlaceholders(this);

// Attempt to unregister
if (VersionUtil.compareVersions("2.10.9", PlaceholderAPIPlugin.getInstance().getDescription().getVersion()) > -1) {
if (this.placeholders.isRegistered()) {
this.placeholders.unregister();
consoleOutput.debug("Unregistered expansion");
}
}

this.placeholders.register();
consoleOutput.info("Found PlaceholderAPI! Registered expansion.");
}
}

@Override
public void onPluginDisable() {
this.userManager.save();
}

@Override
public void onReload() {
this.messageManager.load();
setupPlaceholders();
}

@Override
public UsageFlag[] usageFlags() {
return new UsageFlag[]{UsageFlag.COMMANDS, UsageFlag.CONFIGURATION, UsageFlag.MENUS, UsageFlag.CUSTOMISATION, UsageFlag.LANGUAGE};
}
}
65 changes: 65 additions & 0 deletions src/main/java/space/devport/wertik/custommessages/VersionUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package space.devport.wertik.custommessages;

import lombok.experimental.UtilityClass;
import org.bukkit.Bukkit;

import java.util.Arrays;
import java.util.StringTokenizer;

@UtilityClass
public class VersionUtil {

public int[] canonicalVersion(String version) {

int[] canonicalVersion = new int[]{0, 0, 0, 0};
StringTokenizer tokenizer = new StringTokenizer(version, ".");

String token = tokenizer.nextToken();
canonicalVersion[0] = Integer.parseInt(token);
token = tokenizer.nextToken();
StringTokenizer subTokenizer;

if (!token.contains("_")) {
canonicalVersion[1] = Integer.parseInt(token);
} else {
subTokenizer = new StringTokenizer(token, "_");

canonicalVersion[1] = Integer.parseInt(subTokenizer.nextToken());
canonicalVersion[3] = Integer.parseInt(subTokenizer.nextToken());
}

if (tokenizer.hasMoreTokens()) {
token = tokenizer.nextToken();

if (!token.contains("_")) {
canonicalVersion[2] = Integer.parseInt(token);
if (tokenizer.hasMoreTokens()) {
canonicalVersion[3] = Integer.parseInt(tokenizer.nextToken());
}
} else {
subTokenizer = new StringTokenizer(token, "_");

canonicalVersion[2] = Integer.parseInt(subTokenizer.nextToken());
canonicalVersion[3] = Integer.parseInt(subTokenizer.nextToken());
}
}

Bukkit.getLogger().info("Canonical version " + version + " -> " + Arrays.toString(canonicalVersion));
return canonicalVersion;
}

public int compareVersions(String version1, String version2) {

int[] canonical1 = canonicalVersion(version1);
int[] canonical2 = canonicalVersion(version2);

for (int n = 0; n < canonical1.length && n < canonical2.length; n++) {
if (canonical1[n] < canonical2[n])
return -1;
else if (canonical1[n] > canonical2[n])
return 1;
}

return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package space.devport.wertik.custommessages.commands;

import lombok.experimental.UtilityClass;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import space.devport.wertik.custommessages.CustomMessagesPlugin;
import space.devport.wertik.custommessages.system.struct.MessageType;

@UtilityClass
public class CommandUtils {

public MessageType parseType(CommandSender sender, String arg) {
try {
return MessageType.valueOf(arg.toUpperCase());
} catch (IllegalArgumentException ignored) {
}

CustomMessagesPlugin.getInstance().getLanguageManager().getPrefixed("Commands.Invalid-Type")
.replace("%param%", arg)
.send(sender);
return null;
}

public Player parsePlayer(CommandSender sender, String arg) {
Player player = Bukkit.getPlayer(arg);
if (player == null) {
CustomMessagesPlugin.getInstance().getLanguageManager().getPrefixed("Commands.Invalid-Player")
.replace("%param%", arg)
.send(sender);
return null;
}
return player;
}
}
Loading

0 comments on commit a78d31e

Please sign in to comment.