Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
Refactor command structure and Logger's name
Browse files Browse the repository at this point in the history
The existing command structure has been refactored to enhance maintainability. Specific changes include the renaming and relocation of command classes, as well as the inclusion of a license header in these classes. The Logger's name in 'CherryCore.java' has also been updated.
  • Loading branch information
mathiasclari committed Jan 8, 2024
1 parent 00d9c22 commit c95b698
Show file tree
Hide file tree
Showing 16 changed files with 482 additions and 267 deletions.
27 changes: 23 additions & 4 deletions src/main/java/net/cherrycraft/cherrycore/CherryCore.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package net.cherrycraft.cherrycore;

import net.cherrycraft.cherrycore.command.CommandManager;
import net.cherrycraft.cherrycore.database.MySQL;
import net.cherrycraft.cherrycore.listener.CommandListener;
import net.cherrycraft.cherrycore.manager.LanguageManager;
import org.bukkit.command.CommandExecutor;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

Expand All @@ -11,9 +14,12 @@ public final class CherryCore extends JavaPlugin {

static CherryCore instance;
private final YamlConfiguration conf = new YamlConfiguration();
Logger logger = Logger.getLogger("ReefCore");
Logger logger = Logger.getLogger("CheeryCore");
LanguageManager languageManager = new LanguageManager();

private CommandManager commandManager;
private CommandListener commandListener;

public static CherryCore getInstance() {
return instance;
}
Expand All @@ -24,10 +30,16 @@ public void onEnable() {
this.saveDefaultConfig();
loadDatabase();
languageManager.loadAllLanguages();
Loader.registerCommands(this);
Loader.registerListeners(this);

// Managers
commandManager = new CommandManager(this);

// Listeners
commandListener = new CommandListener(this);

new Loader(this);
Loader.loadWorlds(this);
logger.info("ReefCore has been enabled.");
logger.info("CherryCore has been enabled.");
}

@Override
Expand All @@ -49,4 +61,11 @@ void loadDatabase() {
}


public CommandManager getCommandManager() {
return commandManager;
}

public CommandExecutor getCommandListener() {
return commandListener;
}
}
44 changes: 13 additions & 31 deletions src/main/java/net/cherrycraft/cherrycore/Loader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,33 @@

import net.cherrycraft.cherrycore.chatsystem.ChatHistoryLogger;
import net.cherrycraft.cherrycore.chatsystem.ChatSystem;
import net.cherrycraft.cherrycore.commands.gamemode.GMCommands;
import net.cherrycraft.cherrycore.commands.gamemode.sepperateCommands.GamemodeAdventure;
import net.cherrycraft.cherrycore.commands.gamemode.sepperateCommands.GamemodeCreative;
import net.cherrycraft.cherrycore.commands.gamemode.sepperateCommands.GamemodeSpectator;
import net.cherrycraft.cherrycore.commands.gamemode.sepperateCommands.GamemodeSurvival;
import net.cherrycraft.cherrycore.languageSystem.command.LanguageCommand;
import net.cherrycraft.cherrycore.manager.CommandManager;
import net.cherrycraft.cherrycore.manager.LanguageManager;
import net.cherrycraft.cherrycore.scoreboard.Scoreboard;
import net.cherrycraft.cherrycore.tablist.Tablist;
import org.bukkit.Bukkit;
import org.bukkit.event.Listener;

public class Loader implements Listener {

private final CherryCore plugin;

public static void registerCommands(CherryCore plugin) {
registeredCommand(new LanguageCommand("language"), plugin);
registeredCommand(new GMCommands("gm"), plugin);
registeredCommand(new GamemodeCreative("gmc"), plugin);
registeredCommand(new GamemodeSurvival("gms"), plugin);
registeredCommand(new GamemodeAdventure("gma"), plugin);
registeredCommand(new GamemodeSpectator("gmsp"), plugin);
}


private static void registeredCommand(CommandManager command, CherryCore plugin) {
command.register(plugin);
plugin.getLogger().info("Command '" + command.getCommandName() + "' has been registered.");
}
public Loader(CherryCore plugin) {
this.plugin = plugin;

public static void registerListeners(CherryCore plugin) {
// Register listeners here
//Bukkit.getPluginManager().registerEvents(new ExampleListener(), plugin);
Bukkit.getPluginManager().registerEvents(new ChatHistoryLogger(), plugin);
Bukkit.getPluginManager().registerEvents(new LanguageManager(), plugin);
Bukkit.getPluginManager().registerEvents(new ChatSystem(), plugin);
Bukkit.getPluginManager().registerEvents(new Scoreboard(), plugin);
Bukkit.getPluginManager().registerEvents(new Scoreboard(), plugin);
Bukkit.getPluginManager().registerEvents(new Tablist(), plugin);
registerListener(new ChatHistoryLogger(),
new LanguageManager(),
new ChatSystem(),
new Scoreboard(),
new Tablist());
}

public static void loadWorlds(CherryCore plugin) {
// Load worlds here
//WorldLoader.loadWorld("world");
//WorldLoader.loadWorld("townymap");
}

private void registerListener(Listener... listeners) {
for (Listener listener : listeners)
plugin.getServer().getPluginManager().registerEvents(listener, plugin);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* (c) 2024 CherryCraft. All rights reserved.
*
* This software is the confidential and proprietary information of CherryCraft
* ("Confidential Information"). You shall not disclose such Confidential Information
* and shall use it only in accordance with the terms of the license agreement you
* entered into with CherryCraft.
*
* UNAUTHORIZED COPYING, DISTRIBUTION, OR REPRODUCTION OF THIS SOFTWARE, IN WHOLE OR
* IN PART, IS STRICTLY PROHIBITED. UNLESS OTHERWISE EXPRESSLY AGREED UPON IN A
* WRITTEN AGREEMENT, CHERRYCRAFT PROVIDES THIS SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
* NON-INFRINGEMENT.
*
* For inquiries, please contact CherryCraft at [email protected].
*/

package net.cherrycraft.cherrycore.command;

import net.cherrycraft.cherrycore.CherryCore;
import net.cherrycraft.cherrycore.command.commands.*;
import net.cherrycraft.cherrycore.languageSystem.command.LanguageCommand;
import net.cherrycraft.cherrycore.manager.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.Plugin;

import java.util.HashSet;
import java.util.Set;

public class CommandManager {

private final CherryCore plugin;

private final Set<Command> commands = new HashSet<>();

public CommandManager(CherryCore plugin) {
this.plugin = plugin;

registerCommand(new LanguageCommand("language"));
registerCommand(new GMCommands("gm"));
registerCommand(new GamemodeCreative("gmc"));
registerCommand(new GamemodeSurvival("gms"));
registerCommand(new GamemodeAdventure("gma"));
registerCommand(new GamemodeSpectator("gmsp"));
}

private void registerCommand(Command command) {
PluginCommand pluginCommand = plugin.getCommand(command.getCommandName());
if (pluginCommand != null) {
pluginCommand.setExecutor(plugin.getCommandListener());
commands.add(command);
((Plugin) plugin).getLogger().info("Command '" + command.getCommandName() + "' has been registered.");
} else {
plugin.getLogger().warning("Failed to register command: " + command.getCommandName());
}
}

public boolean runCommand(CommandSender sender, String label, String[] args) {
System.out.println("Attempting to run command: " + label);
for (Command command : commands) {
System.out.println(command.getCommandName() + "=" + label);
if (command.getCommandName().equalsIgnoreCase(label)) {
String permission = command.getPermission();
if (permission != null && !sender.hasPermission("cherrycore." + permission)) {
sender.sendMessage("<red>" + command.getPermissionMessage());
return false;
}
return command.execute(sender, args);
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
package net.cherrycraft.cherrycore.commands.gamemode;
/*
* (c) 2024 CherryCraft. All rights reserved.
*
* This software is the confidential and proprietary information of CherryCraft
* ("Confidential Information"). You shall not disclose such Confidential Information
* and shall use it only in accordance with the terms of the license agreement you
* entered into with CherryCraft.
*
* UNAUTHORIZED COPYING, DISTRIBUTION, OR REPRODUCTION OF THIS SOFTWARE, IN WHOLE OR
* IN PART, IS STRICTLY PROHIBITED. UNLESS OTHERWISE EXPRESSLY AGREED UPON IN A
* WRITTEN AGREEMENT, CHERRYCRAFT PROVIDES THIS SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
* NON-INFRINGEMENT.
*
* For inquiries, please contact CherryCraft at [email protected].
*/

import net.cherrycraft.cherrycore.manager.CommandManager;
package net.cherrycraft.cherrycore.command.commands;

import net.cherrycraft.cherrycore.manager.Command;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.command.CommandSender;
Expand All @@ -9,24 +27,21 @@
import static net.cherrycraft.cherrycore.chatsystem.utils.PlayerValidator.isPlayerNameValid;


public class GMCommands extends CommandManager {
public class GMCommands extends Command {

public GMCommands(String commandName) {
super("gm");
}

@Override
public void execute(CommandSender sender, String[] args) {
if (!sender.hasPermission("cherrycore.gamemode")) {
sender.sendMessage("<red>You don't have permission to use this command!");
return;
}
public boolean execute(CommandSender sender, String[] args) {
Player targetPlayer = null;
if (args.length > 1) {
if (isPlayerNameValid(args[1])) {
targetPlayer = Bukkit.getPlayer(args[1]);
} else {
sender.sendMessage("<red>The player name is not valid!");
return;
return false;
}
}
Player player = targetPlayer != null ? targetPlayer : (Player) sender;
Expand Down Expand Up @@ -56,5 +71,16 @@ public void execute(CommandSender sender, String[] args) {
player.setGameMode(GameMode.SPECTATOR);
break;
}
return true;
}

@Override
public String getPermission() {
return "gamemode";
}

@Override
public String getPermissionMessage() {
return "You don't have permission to use this command!";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* (c) 2024 CherryCraft. All rights reserved.
*
* This software is the confidential and proprietary information of CherryCraft
* ("Confidential Information"). You shall not disclose such Confidential Information
* and shall use it only in accordance with the terms of the license agreement you
* entered into with CherryCraft.
*
* UNAUTHORIZED COPYING, DISTRIBUTION, OR REPRODUCTION OF THIS SOFTWARE, IN WHOLE OR
* IN PART, IS STRICTLY PROHIBITED. UNLESS OTHERWISE EXPRESSLY AGREED UPON IN A
* WRITTEN AGREEMENT, CHERRYCRAFT PROVIDES THIS SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
* NON-INFRINGEMENT.
*
* For inquiries, please contact CherryCraft at [email protected].
*/

package net.cherrycraft.cherrycore.command.commands;

import net.cherrycraft.cherrycore.manager.Command;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import static net.cherrycraft.cherrycore.chatsystem.utils.PlayerValidator.isPlayerNameValid;

public class GamemodeAdventure extends Command {

public GamemodeAdventure(String commandName) {
super("gma");
}

@Override
public boolean execute(CommandSender sender, String[] args) {
Player targetPlayer = null;
if (args.length > 1) {
if (isPlayerNameValid(args[1])) {
targetPlayer = Bukkit.getPlayer(args[1]);
} else {
sender.sendMessage("<red>The player name is not valid!");
return false;
}
}
Player player = targetPlayer != null ? targetPlayer : (Player) sender;

player.setGameMode(GameMode.ADVENTURE);
return true;
}

@Override
public String getPermission() {
return "gamemode";
}

@Override
public String getPermissionMessage() {
return "You don't have permission to use this command!";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* (c) 2024 CherryCraft. All rights reserved.
*
* This software is the confidential and proprietary information of CherryCraft
* ("Confidential Information"). You shall not disclose such Confidential Information
* and shall use it only in accordance with the terms of the license agreement you
* entered into with CherryCraft.
*
* UNAUTHORIZED COPYING, DISTRIBUTION, OR REPRODUCTION OF THIS SOFTWARE, IN WHOLE OR
* IN PART, IS STRICTLY PROHIBITED. UNLESS OTHERWISE EXPRESSLY AGREED UPON IN A
* WRITTEN AGREEMENT, CHERRYCRAFT PROVIDES THIS SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
* NON-INFRINGEMENT.
*
* For inquiries, please contact CherryCraft at [email protected].
*/

package net.cherrycraft.cherrycore.command.commands;

import net.cherrycraft.cherrycore.manager.Command;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import static net.cherrycraft.cherrycore.chatsystem.utils.PlayerValidator.isPlayerNameValid;


public class GamemodeCreative extends Command {
public GamemodeCreative(String commandName) {
super("gmc");
}

@Override
public boolean execute(CommandSender sender, String[] args) {
Player targetPlayer = null;
if (args.length > 1) {
if (isPlayerNameValid(args[1])) {
targetPlayer = Bukkit.getPlayer(args[1]);
} else {
sender.sendMessage("<red>The player name is not valid!");
return false;
}
}
Player player = targetPlayer != null ? targetPlayer : (Player) sender;
player.setGameMode(GameMode.CREATIVE);
return true;
}

@Override
public String getPermission() {
return "gamemode";
}

@Override
public String getPermissionMessage() {
return "You don't have permission to use this command!";
}
}
Loading

0 comments on commit c95b698

Please sign in to comment.