Skip to content

Commit

Permalink
Merge from develop
Browse files Browse the repository at this point in the history
  • Loading branch information
eofitg committed Jun 15, 2023
2 parents e681d62 + 5ae7499 commit f7be42c
Show file tree
Hide file tree
Showing 13 changed files with 673 additions and 114 deletions.
47 changes: 0 additions & 47 deletions src/main/java/com/eofitg/hardcore/ConfigReader.java

This file was deleted.

106 changes: 85 additions & 21 deletions src/main/java/com/eofitg/hardcore/Hardcore.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@

import com.eofitg.hardcore.cmdoperation.CommandRegister;
import com.eofitg.hardcore.cmdoperation.TabCompleterRegister;
import com.eofitg.hardcore.configuration.MainConfig;
import com.eofitg.hardcore.configuration.UserDataConfig;
import com.eofitg.hardcore.listener.PlayerListener;
import com.eofitg.hardcore.listener.PointListener;
import com.eofitg.hardcore.util.Leaderboard;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.List;
import java.util.*;

public final class Hardcore extends JavaPlugin {

private static Hardcore instance;
private static String pluginName;
public static Hardcore getInstance() {
Expand All @@ -22,54 +26,114 @@ public static String getPluginName() {
return pluginName;
}

// Store players' leaderboards displayed on their interfaces
public static Map<Player, Leaderboard> leaderboards = new HashMap<>();


@Override
public void onEnable() {
// Plugin startup logic
instance = this;
pluginName = instance.getName();
MainConfig.saveDefault();
// Register Listeners
Bukkit.getPluginManager().registerEvents(new PlayerListener(), this);
Bukkit.getPluginManager().registerEvents(new PointListener(), this);
CommandRegister.register(ConfigReader.getCmdNames());
TabCompleterRegister.register(ConfigReader.getCmdNames());
// Register Commands
CommandRegister.register(MainConfig.getCmdNames());
TabCompleterRegister.register(MainConfig.getCmdNames());

// 扫描所有在线玩家加入到玩家列表
if (ConfigReader.getState()) {
// Add all online players to the player list then toggle their game-mode
if (MainConfig.getState()) {
for (Player player : Bukkit.getOnlinePlayers()) {
String playerName = player.getName();
List<String> playerNames = ConfigReader.getPlayerNames();
if (!playerNames.contains(playerName)) {
playerNames.add(playerName);
ConfigReader.set("playerNames", playerNames);
ConfigReader.set("alive." + playerName, true);
Hardcore.getInstance().saveConfig();
// playerId = playerUuid + '/' + playerName
List<String> playerIdList = MainConfig.getPlayerIdList();
List<String> uuidList = MainConfig.getUuidList();
String uuid = player.getUniqueId().toString();
String playerId = uuid + "/" + player.getName();
// Create config file for this player
UserDataConfig userDataConfig;
userDataConfig = new UserDataConfig(player, player.getUniqueId().toString(), player.getName());
if (!uuidList.contains(uuid)) {
// new player
playerIdList.add(playerId);
uuidList.add(uuid);
MainConfig.setPlayerIdList(playerIdList);
MainConfig.setUuidList(uuidList);
MainConfig.save();
//
userDataConfig.init();
userDataConfig.save();
//
player.setGameMode(GameMode.SURVIVAL);
player.sendTitle(ChatColor.BLUE + "WELCOME, NEW PLAYER!", ChatColor.GRAY + "You only have one life and do your best to survive!", 10, 150, 10);
} else {
boolean playerState = ConfigReader.getPlayerState(playerName);
// existing player
if(!playerIdList.contains(playerId)) {
// Player name changed
for(int i = 0; i < playerIdList.size(); i ++) {
if(playerIdList.get(i).contains(uuid)) {
// Update player id (uuid + '/' + name)
playerIdList.set(i, playerId);
}
}
MainConfig.setPlayerIdList(playerIdList);
MainConfig.save();
}
boolean playerState = userDataConfig.getState();
if (!playerState) {
// player is dead
player.setGameMode(GameMode.SPECTATOR);
player.sendTitle(ChatColor.RED + "YOU HAVE DIED IN THIS SEASON!", ChatColor.GRAY + "Please wait for the reset!", 10, 150, 10);
} else {
// player is alive
player.setGameMode(GameMode.SURVIVAL);
player.sendTitle(ChatColor.GREEN + "YOU ARE ALIVE!", ChatColor.GRAY + "Survive and earn more points!", 10, 150, 10);
}
}
}
} else {
for (Player player : Bukkit.getOnlinePlayers()) {
String playerName = player.getName();
List<String> playerNames = ConfigReader.getPlayerNames();
if (playerNames.contains(playerName)) {
player.setGameMode(GameMode.SURVIVAL);
}

// Set the leaderboard
Leaderboard leaderboard = new Leaderboard(Hardcore.getInstance(), player, "Leaderboard");
leaderboards.put(player, leaderboard);
leaderboard.startShowing();
}
}
}

@Override
public void onDisable() {
// Plugin shutdown logic
if (!MainConfig.getState()) {
return;
}

// Restore players' game-mode to their original state
for (Player player : Bukkit.getOnlinePlayers()) {
List<String> playerIdList = MainConfig.getPlayerIdList();
List<String> uuidList = MainConfig.getUuidList();
String uuid = player.getUniqueId().toString();
String name = player.getName();
String playerId = uuid + "/" + name;
if (uuidList.contains(uuid) && new UserDataConfig(player, uuid, name).exists()) {
player.setGameMode(GameMode.valueOf(new UserDataConfig(player, uuid, name).getGameMode()));
if (!playerIdList.contains(playerId)) {
// Player name changed
for (int i = 0; i < playerIdList.size(); i++) {
if (playerIdList.get(i).contains(uuid)) {
// Update player id (uuid + '/' + name)
playerIdList.set(i, playerId);
}
}
MainConfig.setPlayerIdList(playerIdList);
MainConfig.save();
}
}
// Delete the leaderboard
leaderboards.get(player).turnOff();
}
leaderboards.clear();
instance = null;
pluginName = null;
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.eofitg.hardcore.cmdoperation;

import com.eofitg.hardcore.ConfigReader;
import com.eofitg.hardcore.Hardcore;
import com.eofitg.hardcore.configuration.MainConfig;

import java.util.List;

public class CommandChecker {
public static boolean conform (String requestCommand, String commandName) {
List<String> cmdList = ConfigReader.getCmdList(commandName);
List<String> cmdList = MainConfig.getCmdList(commandName);
String pluginName = Hardcore.getPluginName().toLowerCase();
if (requestCommand.startsWith(pluginName + ":")) {
requestCommand = requestCommand.substring(pluginName.length() + 1);
Expand Down
47 changes: 37 additions & 10 deletions src/main/java/com/eofitg/hardcore/cmdoperation/CommandHandler.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,52 @@
package com.eofitg.hardcore.cmdoperation;

import com.eofitg.hardcore.ConfigReader;
import com.eofitg.hardcore.Hardcore;
import com.eofitg.hardcore.configuration.MainConfig;
import com.eofitg.hardcore.configuration.UserDataConfig;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

public class CommandHandler implements CommandExecutor {

@Override
public boolean onCommand(CommandSender sender, Command command, String label, String args[]) {
if (CommandChecker.conform(label, "hardcore")) {
if (!sender.isOp()) {
sender.sendMessage(ChatColor.RED + "No permission.");
return true;
}
if (args.length > 2) {
sender.sendMessage(ChatColor.RED + "Invalid command.");
return true;
}
if (args.length == 0) {
sender.sendMessage(ChatColor.RED + "Empty parameters.");
return true;
}

String childCmd = args[0].toLowerCase();
if (args.length == 2) {
// /hardcore reset <online-player-name>
if (childCmd.equals("reset")) {
String name = args[1];
for (Player player : Bukkit.getOnlinePlayers()) {
String playerName = player.getName();
if (name.equalsIgnoreCase(playerName)) {
UserDataConfig userDataConfig = new UserDataConfig(player, player.getUniqueId().toString(), playerName);
userDataConfig.reset();
userDataConfig.save();
}
}
sender.sendMessage(ChatColor.BLUE + "Player " + args[1] + "'s state has reset.");
} else {
sender.sendMessage(ChatColor.RED + "Invalid command.");
}
return true;
}

switch (childCmd) {
case "help" : {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7- &a/hardcore help &f- &7Get Help"));
Expand All @@ -29,20 +56,19 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
break;
}
case "on" : {
ConfigReader.set("enable", true);
Hardcore.getInstance().saveConfig();
sender.sendMessage("Hardcore mode is on.");
MainConfig.setState(true);
MainConfig.save();
sender.sendMessage(ChatColor.BLUE + "Hardcore mode is on.");
break;
}
case "off" : {
ConfigReader.set("enable", false);
Hardcore.getInstance().saveConfig();
sender.sendMessage("Hardcore mode is off.");
MainConfig.setState(false);
MainConfig.save();
sender.sendMessage(ChatColor.BLUE + "Hardcore mode is off.");
break;
}
case "reset" : {
ConfigReader.reset();
Hardcore.getInstance().saveConfig();
UserDataConfig.reset_all();
sender.sendMessage(ChatColor.BLUE + "Player state has reset.");
break;
}
Expand All @@ -51,4 +77,5 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}
return false;
}

}
52 changes: 52 additions & 0 deletions src/main/java/com/eofitg/hardcore/configuration/MainConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.eofitg.hardcore.configuration;

import com.eofitg.hardcore.Hardcore;
import org.bukkit.configuration.file.FileConfiguration;

import java.util.List;

public class MainConfig {

private static final FileConfiguration config = Hardcore.getInstance().getConfig();
private static final List<String> playerIdList = config.getStringList("playerIdList");
private static final List<String> uuidList = config.getStringList("uuidList");
private static final List<String> cmdNames = config.getStringList("commandNames");
private static final boolean state = config.getBoolean("enable");

public static List<String> getPlayerIdList() {
return playerIdList;
}
public static List<String> getUuidList() {
return uuidList;
}
public static List<String> getCmdNames() {
return cmdNames;
}
public static List<String> getCmdList(String cmd) {
return config.getStringList("commands." + cmd);
}
public static boolean getState() {
return state;
}

public static void set(String key, Object value) {
config.set(key, value);
}
public static void setPlayerIdList(List<String> playerIdList) {
set("playerIdList", playerIdList);
}
public static void setUuidList(List<String> uuidList) {
set("uuidList", uuidList);
}
public static void setState(boolean state) {
set("enable", state);
}

public static void save() {
Hardcore.getInstance().saveConfig();
}
public static void saveDefault() {
Hardcore.getInstance().saveDefaultConfig();
}

}
Loading

0 comments on commit f7be42c

Please sign in to comment.