148 lines
5.2 KiB
Java
148 lines
5.2 KiB
Java
package xyz.twovb.sgm;
|
|
|
|
/*
|
|
* Created by 2vb - 5/7/2024
|
|
*/
|
|
|
|
import dev.rollczi.litecommands.bukkit.LiteBukkitMessages;
|
|
import dev.rollczi.litecommands.bukkit.LiteCommandsBukkit;
|
|
import lombok.Getter;
|
|
import net.megavex.scoreboardlibrary.api.ScoreboardLibrary;
|
|
import net.megavex.scoreboardlibrary.api.exception.NoPacketAdapterAvailableException;
|
|
import net.megavex.scoreboardlibrary.api.noop.NoopScoreboardLibrary;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.configuration.InvalidConfigurationException;
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.event.EventHandler;
|
|
import org.bukkit.event.Listener;
|
|
import org.bukkit.event.entity.EntityDamageByEntityEvent;
|
|
import org.bukkit.event.entity.FoodLevelChangeEvent;
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
import xyz.twovb.sgm.commands.handlers.InvalidArgsHandler;
|
|
import xyz.twovb.sgm.commands.handlers.NoPermissionsHandler;
|
|
import xyz.twovb.sgm.commands.impl.GameCommand;
|
|
import xyz.twovb.sgm.commands.impl.JailCommand;
|
|
import xyz.twovb.sgm.commands.impl.LevelCommand;
|
|
import xyz.twovb.sgm.commands.impl.SGMCommand;
|
|
import xyz.twovb.sgm.games.GameManager;
|
|
import xyz.twovb.sgm.games.impl.TestGame;
|
|
import xyz.twovb.sgm.games.impl.capturethebrick.CTB;
|
|
import xyz.twovb.sgm.levels.LevelManager;
|
|
import xyz.twovb.toolbox.managers.ConfigManager;
|
|
import xyz.twovb.toolbox.managers.PlaceholderManager;
|
|
import xyz.twovb.toolbox.utils.CustomLogger;
|
|
|
|
import java.io.IOException;
|
|
import java.util.Objects;
|
|
|
|
public final class SGM extends JavaPlugin implements Listener {
|
|
|
|
@Getter
|
|
private static SGM instance;
|
|
private FileConfiguration BuildInfo;
|
|
@Getter
|
|
private FileConfiguration Messages;
|
|
@Getter
|
|
private CustomLogger cLogger;
|
|
@Getter
|
|
private ConfigManager configManager;
|
|
|
|
@Getter
|
|
private GameManager gameManager;
|
|
@Getter
|
|
private LevelManager levelManager;
|
|
|
|
@Getter
|
|
private ScoreboardLibrary scoreboardLibrary;
|
|
|
|
@Override
|
|
public void onEnable() {
|
|
instance = this;
|
|
cLogger = new CustomLogger(this);
|
|
configManager = new ConfigManager(this);
|
|
gameManager = new GameManager();
|
|
levelManager = new LevelManager();
|
|
this.saveDefaultConfig();
|
|
Messages = configManager.loadConfig("messages.yml");
|
|
BuildInfo = new YamlConfiguration();
|
|
try {
|
|
BuildInfo.load(Objects.requireNonNull(this.getTextResource("build-info.yml")));
|
|
} catch (IOException | InvalidConfigurationException ignored) {
|
|
}
|
|
loadScoreboard();
|
|
loadGuis();
|
|
registerCommands();
|
|
registerGames();
|
|
registerPlaceholders();
|
|
levelManager.loadLevels();
|
|
Bukkit.getPluginManager().registerEvents(this, SGM.getInstance());
|
|
}
|
|
|
|
@EventHandler
|
|
public void onEntityDamage(EntityDamageByEntityEvent event) {
|
|
if (!(event.getEntity() instanceof Player damaged) && !(event.getDamager() instanceof Player damager)) return;
|
|
event.setCancelled(true);
|
|
}
|
|
|
|
@EventHandler
|
|
public void onFoodLose(FoodLevelChangeEvent event) {
|
|
event.setCancelled(true);
|
|
}
|
|
|
|
private void loadScoreboard() {
|
|
try {
|
|
scoreboardLibrary = ScoreboardLibrary.loadScoreboardLibrary(this);
|
|
} catch (NoPacketAdapterAvailableException e) {
|
|
// If no packet adapter was found, you can fallback to the no-op implementation:
|
|
scoreboardLibrary = new NoopScoreboardLibrary();
|
|
this.getCLogger().error("No scoreboard packet adapter found.");
|
|
}
|
|
}
|
|
|
|
private void loadGuis() {
|
|
saveResource("guis/initgame.xml", false);
|
|
saveResource("guis/mapgui.xml", false);
|
|
saveResource("guis/teampicker.xml", false);
|
|
saveResource("guis/activegames.xml", false);
|
|
saveResource("guis/joingame.xml", false);
|
|
}
|
|
|
|
private void registerPlaceholders() {
|
|
PlaceholderManager pm = new PlaceholderManager();
|
|
pm.registerPlaceholder("%commit.id%", BuildInfo.getString("commit.id"));
|
|
pm.registerPlaceholder("%commit.branch%", BuildInfo.getString("commit.branch"));
|
|
pm.registerPlaceholder("%commit.message%", BuildInfo.getString("commit.message"));
|
|
pm.registerPlaceholder("%name%", this.getName());
|
|
}
|
|
|
|
private void registerCommands() {
|
|
LiteCommandsBukkit.builder().commands(
|
|
// @formatter:off
|
|
new SGMCommand(),
|
|
new LevelCommand(),
|
|
new GameCommand(),
|
|
new JailCommand())
|
|
// @formatter:on
|
|
// @formatter:off
|
|
.invalidUsage(new InvalidArgsHandler())
|
|
.missingPermission(new NoPermissionsHandler())
|
|
.message(LiteBukkitMessages.PLAYER_NOT_FOUND, input -> Messages.getString("commands.invalid-player").replace("%player%", input))
|
|
.message(LiteBukkitMessages.PLAYER_ONLY, input -> Messages.getString("commands.player-only"))
|
|
// @formatter:on
|
|
.build();
|
|
}
|
|
|
|
private void registerGames() {
|
|
gameManager.registerGame(new CTB());
|
|
gameManager.registerGame(new TestGame());
|
|
}
|
|
|
|
@Override
|
|
public void onDisable() {
|
|
}
|
|
|
|
|
|
}
|