Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
eofitg committed Jun 8, 2023
2 parents eb1c0c4 + 2e930f9 commit e681d62
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 75 deletions.
9 changes: 8 additions & 1 deletion src/main/java/com/eofitg/hardcore/ConfigReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class ConfigReader {
private static FileConfiguration config = Hardcore.getInstance().getConfig();
private static List<String> playerNames = config.getStringList("playerNames");
private static List<String> cmdNames = config.getStringList("commandNames");
private static boolean state = config.getBoolean("on");
private static boolean state = config.getBoolean("enable");
public static List<String> getPlayerNames() {
return playerNames;
}
Expand All @@ -24,12 +24,19 @@ public static List<String> getCmdList(String cmd) {
public static boolean getState() {
return state;
}
public static int getPoint(String name) {
return config.getInt("point." + name);
}
public static void set(String key, Object value) {
config.set(key, value);
}
public static void setPoint(String name, int point) {
config.set("point." + name, point);
}
public static void reset(String name) {
if (playerNames.contains(name)) {
set("alive." + name, true);
set("point." + name, 0);
}
}
public static void reset() {
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/com/eofitg/hardcore/Hardcore.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.eofitg.hardcore;

import com.eofitg.hardcore.cmdoperation.CommandRegister;
import com.eofitg.hardcore.cmdoperation.TabCompleter;
import com.eofitg.hardcore.cmdoperation.TabCompleterRegister;
import com.eofitg.hardcore.listener.PlayerListener;
import com.eofitg.hardcore.listener.PointListener;
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.Objects;

public final class Hardcore extends JavaPlugin {
private static Hardcore instance;
Expand All @@ -28,9 +28,9 @@ public void onEnable() {
instance = this;
pluginName = instance.getName();
Bukkit.getPluginManager().registerEvents(new PlayerListener(), this);
//CommandRegister.register(ConfigReader.getCmdNames());
Objects.requireNonNull(Bukkit.getPluginCommand("hardcore")).setExecutor(new TabCompleter());
Objects.requireNonNull(Bukkit.getPluginCommand("hardcore")).setTabCompleter(new TabCompleter());
Bukkit.getPluginManager().registerEvents(new PointListener(), this);
CommandRegister.register(ConfigReader.getCmdNames());
TabCompleterRegister.register(ConfigReader.getCmdNames());

// 扫描所有在线玩家加入到玩家列表
if (ConfigReader.getState()) {
Expand All @@ -55,6 +55,14 @@ public void onEnable() {
}
}
}
} else {
for (Player player : Bukkit.getOnlinePlayers()) {
String playerName = player.getName();
List<String> playerNames = ConfigReader.getPlayerNames();
if (playerNames.contains(playerName)) {
player.setGameMode(GameMode.SURVIVAL);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/*
Deprecated.
Go to check TabCompleter.java
*/

package com.eofitg.hardcore.cmdoperation;

import com.eofitg.hardcore.ConfigReader;
Expand Down Expand Up @@ -34,21 +29,21 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
break;
}
case "on" : {
ConfigReader.set("on", true);
ConfigReader.set("enable", true);
Hardcore.getInstance().saveConfig();
sender.sendMessage("Hardcore mode is on.");
break;
}
case "off" : {
ConfigReader.set("on", false);
ConfigReader.set("enable", false);
Hardcore.getInstance().saveConfig();
sender.sendMessage("Hardcore mode is off.");
break;
}
case "reset" : {
ConfigReader.reset();
Hardcore.getInstance().saveConfig();
sender.sendMessage("Player state has reset.");
sender.sendMessage(ChatColor.BLUE + "Player state has reset.");
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,17 @@
package com.eofitg.hardcore.cmdoperation;

import com.eofitg.hardcore.ConfigReader;
import org.bukkit.Bukkit;

import java.util.List;
import java.util.Objects;

public class CommandRegister {
public static void register (List<String> cmdNames) {
public static void register(List<String> cmdNames) {
for (String cmdName : cmdNames) {
CommandRegister.register(cmdName);
}
}
public static void register (String cmdName) {
List<String> cmdList = ConfigReader.getCmdList(cmdName);
for (String s : cmdList) {
Objects.requireNonNull(Bukkit.getPluginCommand(s)).setExecutor(new CommandHandler());
}
public static void register(String cmdName) {
Objects.requireNonNull(Bukkit.getPluginCommand(cmdName)).setExecutor(new CommandHandler());
}
}
61 changes: 8 additions & 53 deletions src/main/java/com/eofitg/hardcore/cmdoperation/TabCompleter.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.eofitg.hardcore.cmdoperation;

import com.eofitg.hardcore.ConfigReader;
import com.eofitg.hardcore.Hardcore;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
Expand All @@ -11,61 +8,19 @@
import java.util.Arrays;
import java.util.List;

public class TabCompleter implements TabExecutor {
public class TabCompleter extends CommandHandler implements TabExecutor {
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
List<String> str = Arrays.asList("on", "off", "reset");
if (sender instanceof Player) {
if (args.length > 1) {
return null;
} else {
return str;
}
}
return null;
}

@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 == 0) {
sender.sendMessage(ChatColor.RED + "Empty parameters.");
return true;
}
String childCmd = args[0].toLowerCase();
switch (childCmd) {
case "help" : {
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7- &a/hardcore help &f- &7Get Help"));
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7- &a/hardcore on &f- &7Turn on the hardcore mode"));
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7- &a/hardcore off &f- &7Turn off the hardcore mode"));
sender.sendMessage(ChatColor.translateAlternateColorCodes('&', "&7- &a/hardcore reset &f- &7Reset players' survival states"));
break;
}
case "on" : {
ConfigReader.set("on", true);
Hardcore.getInstance().saveConfig();
sender.sendMessage(ChatColor.BLUE + "Hardcore mode is on.");
break;
}
case "off" : {
ConfigReader.set("on", false);
Hardcore.getInstance().saveConfig();
sender.sendMessage(ChatColor.BLUE + "Hardcore mode is off.");
break;
}
case "reset" : {
ConfigReader.reset();
Hardcore.getInstance().saveConfig();
sender.sendMessage("Player state has reset.");
break;
List<String> str = Arrays.asList("help", "on", "off", "reset");
if (sender instanceof Player) {
if (args.length > 1) {
return null;
} else {
return str;
}
}
return true;
}
return false;
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.eofitg.hardcore.cmdoperation;

import org.bukkit.Bukkit;

import java.util.List;
import java.util.Objects;

public class TabCompleterRegister {
public static void register(List<String> cmdNames) {
for (String cmdName : cmdNames) {
TabCompleterRegister.register(cmdName);
}
}
public static void register(String cmdName) {
Objects.requireNonNull(Bukkit.getPluginCommand(cmdName)).setTabCompleter(new TabCompleter());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public void onJoin(PlayerJoinEvent e) {
playerNames.add(playerName);
ConfigReader.set("playerNames", playerNames);
ConfigReader.set("alive." + playerName, true);
ConfigReader.set("point." + playerName, 0);
Hardcore.getInstance().saveConfig();
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);
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/eofitg/hardcore/listener/PointListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.eofitg.hardcore.listener;

import com.eofitg.hardcore.ConfigReader;
import com.eofitg.hardcore.Hardcore;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageEvent;

public class PointListener implements Listener {
@EventHandler
public void getDamage(EntityDamageEvent e) {
if (e.getEntity() instanceof Player) {
String name = e.getEntity().getName();
int damage = (int) e.getDamage();
//e.getEntity().sendMessage(name + " " + e.getCause() + " " + e.getDamage());
int point = ConfigReader.getPoint(name) - damage;
ConfigReader.setPoint(name, point);
Hardcore.getInstance().saveConfig();
}
}
}
3 changes: 2 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
playerNames:
alive:
point:
commandNames: [ hardcore ]
commands:
hardcore: [ hardcore, hardcoremode ]
on: false
enable: false

0 comments on commit e681d62

Please sign in to comment.