Skip to content

Commit

Permalink
ADD Module File Configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
teraprath authored May 8, 2023
1 parent 6412f86 commit 2987887
Show file tree
Hide file tree
Showing 14 changed files with 647 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>net.spigotcloud</groupId>
<artifactId>lobby</artifactId>
<version>1.2.3-SNAPSHOT</version>
<version>1.2.5-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Lobby</name>
Expand Down
77 changes: 77 additions & 0 deletions src/main/java/net/spigotcloud/lobby/Lobby.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package net.spigotcloud.lobby;

import net.spigotcloud.lobby.api.LobbyAPI;
import net.spigotcloud.lobby.command.BaseCommand;
import net.spigotcloud.lobby.command.BuildCommand;
import net.spigotcloud.lobby.command.ModulesCommand;
import net.spigotcloud.lobby.command.ReloadCommand;
import net.spigotcloud.lobby.listener.JoinQuitListener;
import net.spigotcloud.lobby.listener.ProtectListener;
import net.spigotcloud.lobby.module.ModuleLoader;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

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

public final class Lobby extends JavaPlugin {

private static Lobby instance;
private final BaseCommand command = new BaseCommand();
private final Set<Player> buildPlayers = new HashSet<>();
private ModuleLoader moduleLoader;

@Override
public void onEnable() {

instance = this;

this.moduleLoader = new ModuleLoader();

moduleLoader.reload();
moduleLoader.enable();

registerCommands();
registerEvents();


}

private void registerCommands() {
getCommand("lobby").setExecutor(this.command);
command.registerSubCommand("reload", new ReloadCommand());
command.registerSubCommand("modules", new ModulesCommand());
command.registerSubCommand("build", new BuildCommand());
}

private void registerEvents() {
final PluginManager pm = getServer().getPluginManager();
pm.registerEvents(new JoinQuitListener(), this);
pm.registerEvents(new ProtectListener(), this);
}

@Override
public void onDisable() {
moduleLoader.disable();
}

public BaseCommand getBaseCommand() {
return this.command;
}


public ModuleLoader getModuleLoader() {
return this.moduleLoader;
}

public Set<Player> getBuildPlayers() {
return this.buildPlayers;
}

public static Lobby getInstance() {
return instance;
}


}
12 changes: 12 additions & 0 deletions src/main/java/net/spigotcloud/lobby/api/LobbyAPI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.spigotcloud.lobby.api;

import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;

public class LobbyAPI {

public static Plugin getPlugin() {
return Bukkit.getServer().getPluginManager().getPlugin("Lobby");
}

}
79 changes: 79 additions & 0 deletions src/main/java/net/spigotcloud/lobby/command/BaseCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package net.spigotcloud.lobby.command;

import net.spigotcloud.lobby.Lobby;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.plugin.PluginDescriptionFile;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

// TODO: Custom messages

public class BaseCommand implements CommandExecutor, TabCompleter {

private final HashMap<String, SubCommand> subCommands;

public BaseCommand() {
this.subCommands = new HashMap<>();
}

public void registerSubCommand(@Nonnull String command, @Nonnull SubCommand subCommand) {
this.subCommands.put(command, subCommand);
}

public void unregister(@Nonnull String command) {
this.subCommands.remove(command);
}


@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

if (args.length > 0) {

if (subCommands.containsKey(args[0])) {
SubCommand subCommand = subCommands.get(args[0]);
if (sender.hasPermission(subCommand.getPermission())) {
subCommand.onCommand(sender, command, label, args);
} else {
sender.sendMessage("No permission.");
}
} else {
sender.sendMessage("Sub-Command does not exists.");
}

} else {
PluginDescriptionFile desc = Lobby.getInstance().getDescription();
sender.sendMessage(desc.getName() + " v" + desc.getVersion() + " by " + desc.getAuthors().get(0));
}

return false;
}

@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {

ArrayList<String> list = new ArrayList<>();
String current = args[args.length - 1].toLowerCase();


if (args.length == 1) {
subCommands.forEach(((s, subCommand) -> {
list.add(s.toLowerCase());
}));
} else {
SubCommand subCommand = subCommands.get(args[0]);
if (subCommand != null) {
return subCommand.onTabComplete(sender, command, label, args);
}
}

list.removeIf(s -> !s.toLowerCase().startsWith(current));
return list;
}
}
46 changes: 46 additions & 0 deletions src/main/java/net/spigotcloud/lobby/command/BuildCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package net.spigotcloud.lobby.command;

import net.spigotcloud.lobby.Lobby;
import org.bukkit.GameMode;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.List;

// TODO: Custom messages

public class BuildCommand extends SubCommand {

public BuildCommand() {
super("build");
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {

if (!(sender instanceof Player)) {
// TODO: Send message to console
return false;
}

final Player player = (Player) sender;

if (Lobby.getInstance().getBuildPlayers().contains(player)) {
Lobby.getInstance().getBuildPlayers().remove(player);
player.setGameMode(GameMode.SURVIVAL);
player.sendMessage("Build Mode: off");
} else {
Lobby.getInstance().getBuildPlayers().add(player);
player.setGameMode(GameMode.CREATIVE);
player.sendMessage("Build Mode: on");
}

return false;
}

@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
return null;
}
}
33 changes: 33 additions & 0 deletions src/main/java/net/spigotcloud/lobby/command/ModulesCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package net.spigotcloud.lobby.command;

// TODO: Custom messages

import net.spigotcloud.lobby.Lobby;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;

import java.util.List;

public class ModulesCommand extends SubCommand {

public ModulesCommand() {
super("modules");
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
sender.sendMessage("");
sender.sendMessage("§6§lLobby Modules");
Lobby.getInstance().getModuleLoader().getModules().forEach(module -> {
sender.sendMessage("§8- §7" + module.getDescriptionFile().getName() + " v" + module.getDescriptionFile().getVersion() + " by " + module.getDescriptionFile().getAuthor());
});
return false;
}

@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
return null;
}

}

30 changes: 30 additions & 0 deletions src/main/java/net/spigotcloud/lobby/command/ReloadCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package net.spigotcloud.lobby.command;

import net.spigotcloud.lobby.Lobby;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;

import java.util.List;

// TODO: Custom messages

public class ReloadCommand extends SubCommand {

public ReloadCommand() {
super("reload");
}

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
sender.sendMessage("Reloading modules and files...");
Lobby.getInstance().getModuleLoader().reload();
sender.sendMessage("Lobby reload successfully.");
return false;
}

@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
return null;
}

}
23 changes: 23 additions & 0 deletions src/main/java/net/spigotcloud/lobby/command/SubCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.spigotcloud.lobby.command;

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

import java.util.List;

public abstract class SubCommand {

private final String permission;

public SubCommand(String permission) {
this.permission = permission;
}

public abstract boolean onCommand(CommandSender sender, Command command, String label, String[] args);
public abstract List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args);

public String getPermission() {
return "lobby.command." + this.permission;
}

}
35 changes: 35 additions & 0 deletions src/main/java/net/spigotcloud/lobby/listener/JoinQuitListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.spigotcloud.lobby.listener;

// TODO: Custom join messages

import net.spigotcloud.lobby.Lobby;
import org.bukkit.GameMode;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;

public class JoinQuitListener implements Listener {

@EventHandler
public void onPlayerJoin(PlayerJoinEvent e) {
final Player player = e.getPlayer();
e.setJoinMessage(null);
player.getInventory().clear();
player.setHealth(20);
player.setFoodLevel(20);
player.setExp(0);
player.setTotalExperience(0);
player.setLevel(0);
player.setGameMode(GameMode.SURVIVAL);
}

@EventHandler
public void onPlayerQuit(PlayerQuitEvent e) {
final Player player = e.getPlayer();
e.setQuitMessage(null);
Lobby.getInstance().getBuildPlayers().remove(player);
}

}
Loading

0 comments on commit 2987887

Please sign in to comment.