This repository has been archived by the owner on Jan 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor command structure and Logger's name
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
1 parent
00d9c22
commit c95b698
Showing
16 changed files
with
482 additions
and
267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/main/java/net/cherrycraft/cherrycore/command/CommandManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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; | ||
|
@@ -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!"; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
src/main/java/net/cherrycraft/cherrycore/command/commands/GamemodeAdventure.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/net/cherrycraft/cherrycore/command/commands/GamemodeCreative.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!"; | ||
} | ||
} |
Oops, something went wrong.