Skip to content
This repository has been archived by the owner on Sep 14, 2021. It is now read-only.

Commit

Permalink
merge: 1.0.1 into master
Browse files Browse the repository at this point in the history
merge: 1.0.1 into master
  • Loading branch information
Josh Walker committed Jun 25, 2020
2 parents 0f0a65f + 9133040 commit 8dfee77
Show file tree
Hide file tree
Showing 13 changed files with 102 additions and 47 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,36 @@ hex color codes as placeholders in chat.
- **tmc.command.shortcut.del** - Grants permission to use the del subcommand.
- **tmc.command.shortcut.set.\[1-Infinity\*]** - Sets the amount of shortcuts the player can create.
- **tmc.command.shortcut.set.unlimited** - Bypass the defaultShortcutLimit.
## API
You can now use TooManyColors in your own plugins through JitPack!

Add the following dependency to your pom.xml:
```maven
</repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.jaywalkn</groupId>
<artifactId>TooManyColors</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
```

Then in your onEnable function add:
```java
TMC tmc = (TMC) Bukkit.getPluginManager().getPlugin("TooManyColors");
```

You can then use the function:
```java
tmc.fetchPlaceholders("player uuid as string") // Returns HashMap<String placeholder, String hexCode>
```

## Help
If you don't understand something, you are experiencing problems, or you just want to say hi, feel free to join my [Discord Server](https://discord.gg/hzsTeMz).
Its kinda lonely with just me and my bots ;-;
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>wtf.cmyk</groupId>
<artifactId>toomanycolors</artifactId>
<version>1.0</version>
<version>1.0.1</version>
<packaging>jar</packaging>

<name>TooManyColors</name>
Expand Down Expand Up @@ -92,6 +92,7 @@
<groupId>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.32.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
Expand Down
24 changes: 10 additions & 14 deletions src/main/java/wtf/cmyk/toomanycolors/TMC.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package wtf.cmyk.toomanycolors;

import com.tchristofferson.configupdater.ConfigUpdater;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import wtf.cmyk.toomanycolors.commands.*;
import wtf.cmyk.toomanycolors.listeners.ChatListener;
Expand All @@ -10,11 +11,11 @@
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public final class TMC extends JavaPlugin {
private static TMC instance;
private StorageProvider provider;
private CommandHandler commandHandler;

@Override
public void onEnable() {
Expand All @@ -28,8 +29,8 @@ public void onEnable() {
}

reloadConfig();
provider = new SQLiteProvider();
commandHandler = new CommandHandler();
provider = new SQLiteProvider(this);
CommandHandler commandHandler = new CommandHandler(this, provider);
provider.init();

commandHandler.register("help", new ShortcutCommand());
Expand All @@ -38,7 +39,7 @@ public void onEnable() {
commandHandler.register("list", new ShortcutListCommand());
getCommand("shortcut").setTabCompleter(commandHandler);
getCommand("shortcut").setExecutor(commandHandler);
getServer().getPluginManager().registerEvents(new ChatListener(), this);
getServer().getPluginManager().registerEvents(new ChatListener(provider), this);
getLogger().info("Successfully enabled TooManyColors!");
}

Expand All @@ -49,15 +50,10 @@ public void onDisable() {
getLogger().info("Successfully disabled TooManyColors!");
}

public static TMC getInstance() {
return instance;
}

public StorageProvider getProvider() {
return provider;
}

public CommandHandler getCommandHandler() {
return commandHandler;
public HashMap<String, String> fetchPlaceholders(String uuid) {
if(provider.isAccessible()) {
return provider.getAllPlaceholders(uuid);
}
return new HashMap<>();
}
}
18 changes: 13 additions & 5 deletions src/main/java/wtf/cmyk/toomanycolors/commands/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import wtf.cmyk.toomanycolors.TMC;
import wtf.cmyk.toomanycolors.storage.StorageProvider;
import wtf.cmyk.toomanycolors.utils.MessageUtils;

import java.util.ArrayList;
Expand All @@ -14,10 +15,17 @@
import java.util.stream.Collectors;

interface CommandInterface {
boolean onCommand(Player player, Command cmd, String commandLabel, String[] args);
boolean onCommand(CommandHandler handler, Player player, Command cmd, String commandLabel, String[] args);
}

public class CommandHandler implements TabExecutor {
protected final TMC plugin;
protected final StorageProvider provider;

public CommandHandler(TMC plugin, StorageProvider provider) {
this.plugin = plugin;
this.provider = provider;
}
private static final HashMap<String, CommandInterface> subCommands = new HashMap<>();

public void register(String name, CommandInterface command) {
Expand All @@ -36,14 +44,14 @@ public CommandInterface getExecutor(String name) {
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if(sender instanceof Player) {
if (args.length == 0) {
getExecutor("help").onCommand((Player) sender, command, label, args);
getExecutor("help").onCommand(this, (Player) sender, command, label, args);
return true;
}

if (exists(args[0])) {
getExecutor(args[0].toLowerCase()).onCommand((Player) sender, command, label, args);
getExecutor(args[0].toLowerCase()).onCommand(this, (Player) sender, command, label, args);
} else {
getExecutor("help").onCommand((Player) sender, command, label, args);
getExecutor("help").onCommand(this, (Player) sender, command, label, args);
return true;
}
} else {
Expand All @@ -68,7 +76,7 @@ public List<String> onTabComplete(CommandSender sender, Command command, String
}

if (args.length > 0 && exists(args[0])) {
HashMap<String, String> placeholderMap = TMC.getInstance().getProvider().getAllPlaceholders(((Player) sender).getUniqueId().toString());
HashMap<String, String> placeholderMap = provider.getAllPlaceholders(((Player) sender).getUniqueId().toString());
if (args[0].equalsIgnoreCase("del")) {
suggestions.addAll(placeholderMap.keySet());
return suggestions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
public class ShortcutCommand implements CommandInterface {

@Override
public boolean onCommand(Player player, Command cmd, String commandLabel, String[] args) {
public boolean onCommand(CommandHandler handler, Player player, Command cmd, String commandLabel, String[] args) {
player.sendMessage(MessageUtils.format("&f- &7shortcut &dset &e<placeholder> <#HEXCODE>"));
player.sendMessage(MessageUtils.format("&f- &7shortcut &ddel &e<placeholder>"));
player.sendMessage(MessageUtils.format("&f- &7shortcut &dlist"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,24 @@

import org.bukkit.command.Command;
import org.bukkit.entity.Player;
import wtf.cmyk.toomanycolors.TMC;
import wtf.cmyk.toomanycolors.utils.MessageUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ShortcutDelCommand implements CommandInterface {
private final TMC plugin = TMC.getInstance();

@Override
public boolean onCommand(Player player, Command command, String label, String[] args) {
public boolean onCommand(CommandHandler handler, Player player, Command command, String label, String[] args) {
if(player.hasPermission("tmc.command.shortcut.del")) {
if (args.length != 2) {
player.sendMessage(MessageUtils.formatWithPrefix("Usage: /shortcut del <placeholder>"));
return true;
}
if (!plugin.getProvider().hasPlaceholder(player.getUniqueId().toString(), args[1])) {
if (!handler.provider.hasPlaceholder(player.getUniqueId().toString(), args[1])) {
player.sendMessage(MessageUtils.formatWithPrefix("Placeholder not found."));
return true;
}

String color = plugin.getProvider().getHexColor(player.getUniqueId().toString(), args[1]);
plugin.getProvider().delPlaceholder(player.getUniqueId().toString(), args[1]);
String color = handler.provider.getHexColor(player.getUniqueId().toString(), args[1]);
handler.provider.delPlaceholder(player.getUniqueId().toString(), args[1]);
player.sendMessage(MessageUtils.format("Deleted placeholder mapping &e" + args[1] + "&7 to &e" + color));
} else {
player.sendMessage(MessageUtils.formatWithPrefix("You do not have permission to run this command!"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
public class ShortcutListCommand implements CommandInterface {

@Override
public boolean onCommand(Player player, Command cmd, String commandLabel, String[] args) {
HashMap<String, String> placeholderMap = TMC.getInstance().getProvider().getAllPlaceholders(player.getUniqueId().toString());
public boolean onCommand(CommandHandler handler,Player player, Command cmd, String commandLabel, String[] args) {
HashMap<String, String> placeholderMap = handler.provider.getAllPlaceholders(player.getUniqueId().toString());
if(placeholderMap.isEmpty()) {
player.sendMessage(MessageUtils.formatWithPrefix("No placeholders :o"));
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package wtf.cmyk.toomanycolors.commands;

import net.md_5.bungee.api.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.entity.Player;
import org.bukkit.permissions.PermissionAttachmentInfo;
import wtf.cmyk.toomanycolors.TMC;
import wtf.cmyk.toomanycolors.utils.MessageUtils;

import java.util.ArrayList;
Expand All @@ -14,23 +14,22 @@
import static java.lang.Integer.parseInt;

public class ShortcutSetCommand implements CommandInterface {
private final TMC plugin = TMC.getInstance();
@Override
public boolean onCommand(Player player, Command command, String label, String[] args) {
public boolean onCommand(CommandHandler handler, Player player, Command command, String label, String[] args) {
if(player.hasPermission("tmc.command.shortcut.set")) {
if (args.length != 3) {
player.sendMessage(MessageUtils.formatWithPrefix("Usage: /shortcut set <placeholder> <#HEXCODE>"));
return true;
}
if (plugin.getConfig().getStringList("blacklistedColors").contains(args[2])) {
if (handler.plugin.getConfig().getStringList("blacklistedColors").contains(args[2])) {
player.sendMessage(MessageUtils.formatWithPrefix("This color is blacklisted by administration."));
return true;
}
if(args[1].matches("&([a-fA-F]|[0-9]|[k-oK-O]|r|R|x|X)")) {
player.sendMessage(MessageUtils.formatWithPrefix("This placeholder is reserved."));
return true;
}
int shortcutLimit = plugin.getConfig().getInt("defaultShortcutLimit");
int shortcutLimit = handler.plugin.getConfig().getInt("defaultShortcutLimit");

Pattern pattern = Pattern.compile("^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$");
Matcher matcher = pattern.matcher(args[2]);
Expand All @@ -50,21 +49,21 @@ public boolean onCommand(Player player, Command command, String label, String[]
if(!nodes.isEmpty()) {
nodes.sort(Collections.reverseOrder());
int maxHomes = nodes.get(0);
if (plugin.getProvider().getTotalPlaceholders(player.getUniqueId().toString()) == maxHomes) {
if (handler.provider.getTotalPlaceholders(player.getUniqueId().toString()) == maxHomes) {
player.sendMessage(MessageUtils.formatWithPrefix("You have reached the placeholder limit, Overwrite or delete one of your existing placeholders."));
return true;
}
} else {
if (shortcutLimit != -1 && !plugin.getProvider().hasPlaceholder(player.getUniqueId().toString(), args[1])) {
if (plugin.getProvider().getTotalPlaceholders(player.getUniqueId().toString()) == shortcutLimit) {
if (shortcutLimit != -1 && !handler.provider.hasPlaceholder(player.getUniqueId().toString(), args[1])) {
if (handler.provider.getTotalPlaceholders(player.getUniqueId().toString()) == shortcutLimit) {
player.sendMessage(MessageUtils.formatWithPrefix("You have reached the placeholder limit, Overwrite or delete one of your existing placeholders."));
return true;
}
}
}
}
plugin.getProvider().setPlaceholder(player.getUniqueId().toString(), args[1], matcher.group());
player.sendMessage(MessageUtils.format("Created placeholder mapping &e" + args[1] + "&7 to &e" + args[2]));
handler.provider.setPlaceholder(player.getUniqueId().toString(), args[1], matcher.group());
player.sendMessage(MessageUtils.format("Created placeholder mapping &e" + args[1] + "&7 to " + ChatColor.of(args[2]) + args[2]));

} else {
player.sendMessage(MessageUtils.formatWithPrefix("You do not have permission to run this command!"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@
import org.bukkit.event.Listener;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import wtf.cmyk.toomanycolors.TMC;
import wtf.cmyk.toomanycolors.storage.StorageProvider;

import java.util.HashMap;

public class ChatListener implements Listener {
private final StorageProvider provider;

public ChatListener(StorageProvider storageProvider) {
this.provider = storageProvider;
}

@EventHandler
public void onPlayerChat(AsyncPlayerChatEvent e) {
String message = e.getMessage();
HashMap<String, String> placeholderMap = TMC.getInstance().getProvider().getAllPlaceholders(e.getPlayer().getUniqueId().toString());
HashMap<String, String> placeholderMap = provider.getAllPlaceholders(e.getPlayer().getUniqueId().toString());
for (String placeholder : placeholderMap.keySet())

message = message.replace(placeholder, ChatColor.of(placeholderMap.get(placeholder)).toString());
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/wtf/cmyk/toomanycolors/storage/SQLiteProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,23 @@

public class SQLiteProvider extends StorageProvider {
private Connection connection;
private TMC plugin;
private final TMC plugin;

public SQLiteProvider(TMC instance) {
super(instance);
this.plugin = instance;
}

@Override
public boolean isAccessible() {
try {
return connection != null && !connection.isClosed();
} catch (SQLException ignored) { }
return false;
}

@Override
public void init() {
plugin = TMC.getInstance();
connection = getConnection();
initializeTable();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package wtf.cmyk.toomanycolors.storage;

import org.bukkit.entity.Player;
import wtf.cmyk.toomanycolors.TMC;

import java.util.HashMap;

public abstract class StorageProvider {

public StorageProvider(TMC instance) { }

public abstract boolean isAccessible();

public abstract void init();

public abstract Boolean hasPlaceholder(String uuid, String placeholder);
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: 'TooManyColors'
version: '1.0'
version: '1.0.1'
main: wtf.cmyk.toomanycolors.TMC
api-version: '1.16'
prefix: 'TMC'
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/SQLiteProviderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;

public class SQLiteProviderTest {
private final SQLiteProvider provider = new SQLiteProvider();
private final SQLiteProvider provider = new SQLiteProvider(null);

@Test
void getterSetterTest() {
Expand Down

0 comments on commit 8dfee77

Please sign in to comment.