Skip to content

Commit

Permalink
Bungee
Browse files Browse the repository at this point in the history
  • Loading branch information
VedantMulay committed Jan 31, 2024
1 parent 8880532 commit 71e5213
Show file tree
Hide file tree
Showing 12 changed files with 685 additions and 12 deletions.
52 changes: 51 additions & 1 deletion src/main/java/io/github/vedantmulay/neptuneapi/bukkit/Utils.java
Original file line number Diff line number Diff line change
@@ -1,2 +1,52 @@
package io.github.vedantmulay.neptuneapi.bukkit;public class Utils {
/*
* Copyright (c) 2024 Lumina Games
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package io.github.vedantmulay.neptuneapi.bukkit;

import org.bukkit.ChatColor;

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

public class Utils {

public static String cc(String message) {
Pattern pattern = Pattern.compile("#[a-fA-F0-9]{6}");
Matcher matcher = pattern.matcher(message);
while (matcher.find()) {
String hexCode = message.substring(matcher.start(), matcher.end());
String replaceSharp = hexCode.replace('#', 'x');

char[] ch = replaceSharp.toCharArray();
StringBuilder builder = new StringBuilder("");
for (char c : ch) {
builder.append("&" + c);
}

message = message.replace(hexCode, builder.toString());
matcher = pattern.matcher(message);
}
return ChatColor.translateAlternateColorCodes('&', message);
}

}
Original file line number Diff line number Diff line change
@@ -1,2 +1,87 @@
package io.github.vedantmulay.neptuneapi.bukkit.commands;public class CommandManager {
/*
* Copyright (c) 2024 Lumina Games
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package io.github.vedantmulay.neptuneapi.bukkit.commands;

import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;
import java.util.Map;

public class CommandManager implements CommandExecutor {

private final Map<String, NeptuneCommand> commands;
private final JavaPlugin plugin;
public static String noPermissionMessage;
public static String playerOnlyCommandMessage;

public CommandManager(JavaPlugin plugin) {
this.plugin = plugin;
this.commands = new HashMap<>();
}

public static void setNoPermissionMessage(String noPermissionMessage) {
CommandManager.noPermissionMessage = noPermissionMessage;
}

public static void setPlayerOnlyCommandMessage(String playerOnlyCommandMessage) {
CommandManager.playerOnlyCommandMessage = playerOnlyCommandMessage;
}

public void registerCommand(String commandName, NeptuneCommand executor) {
commands.put(commandName, executor);
plugin.getCommand(commandName).setExecutor(this);
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
String commandName = command.getName().toLowerCase();

if (commands.containsKey(commandName)) {
NeptuneCommand executor = commands.get(commandName);

// Check player-only
if (executor.isPlayerOnly() && !(sender instanceof Player)) {
sender.sendMessage(playerOnlyCommandMessage);
return true;
}

// Check permission
if (executor.getPermission() != null && !sender.hasPermission(executor.getPermission())) {
sender.sendMessage(noPermissionMessage);
return true;
}

// Execute the command
return executor.onCommand(sender, command, label, args);
}

return false; // Command not found
}


}
Original file line number Diff line number Diff line change
@@ -1,2 +1,35 @@
package io.github.vedantmulay.neptuneapi.bukkit.commands.subcommand;public class Command {
/*
* Copyright (c) 2024 Lumina Games
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package io.github.vedantmulay.neptuneapi.bukkit.commands;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;

public interface NeptuneCommand {

boolean isPlayerOnly();
String getPermission();
String getSyntax();
boolean onCommand(CommandSender sender, Command command, String label, String[] args);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2024 Lumina Games
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package io.github.vedantmulay.neptuneapi.bukkit.commands.subcommand;


import org.bukkit.command.CommandSender;

public interface SubCommand {

boolean isPlayerOnly();

String getPermission();

String getSyntax();

void execute(CommandSender sender, String[] args);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright (c) 2024 Lumina Games
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package io.github.vedantmulay.neptuneapi.bukkit.commands.subcommand;


import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.HashMap;
import java.util.Map;

public class SubCommandManager implements CommandExecutor {

private final Map<String, SubCommand> commands;
public static String noPermissionMessage;
public static String unknownCommandMessage;
public static String playerOnlyCommandMessage;

public SubCommandManager() {
this.commands = new HashMap<>();
}

public static void setNoPermissionMessage(String noPermissionMessage) {
SubCommandManager.noPermissionMessage = noPermissionMessage;
}

public static void setUnknownCommandMessage(String unknownCommandMessage) {
SubCommandManager.unknownCommandMessage = unknownCommandMessage;
}

public static void setPlayerOnlyCommandMessage(String playerOnlyCommandMessage) {
SubCommandManager.playerOnlyCommandMessage = playerOnlyCommandMessage;
}


public void registerCommand(String commandName, SubCommand neptuneCommand) {
commands.put(commandName.toLowerCase(), neptuneCommand);
}

@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (args.length == 0) {
// Handle commands without sub-commands
sender.sendMessage("Usage: /" + label + " <command>");
return true;
}

String subCommand = args[0].toLowerCase();
if (commands.containsKey(subCommand)) {
SubCommand neptuneCommand = commands.get(subCommand);

// Check permissions
if (neptuneCommand.getPermission() != null && !sender.hasPermission(neptuneCommand.getPermission())) {
sender.sendMessage(noPermissionMessage);
return true;
}

// Check if the command is player-only
if (neptuneCommand.isPlayerOnly() && !(sender instanceof Player)) {
sender.sendMessage(playerOnlyCommandMessage);
return true;
}

// Execute the command with the appropriate context
neptuneCommand.execute(sender, args);
return true;
}

// Unknown command or sub-command
sender.sendMessage(unknownCommandMessage);
return false;
}
}
Loading

0 comments on commit 71e5213

Please sign in to comment.