feat: add TeamManager
This commit is contained in:
parent
2a3caad9ae
commit
e0fc3b91e3
4 changed files with 273 additions and 23 deletions
|
@ -34,6 +34,7 @@ public class PlayerJoinQuitListener implements Listener {
|
|||
Bukkit.broadcast(this.plugin.getMessageUtils().getServiceMessage(e.getPlayer().getName(),
|
||||
"hat den Server verlassen."));
|
||||
}
|
||||
this.plugin.getTeamManager().setAFK(e.getPlayer().getName());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
|
@ -42,6 +43,8 @@ public class PlayerJoinQuitListener implements Listener {
|
|||
this.plugin.getNPCManager().removeClone(e.getPlayer());
|
||||
this.plugin.getNPCManager().syncClones(e.getPlayer());
|
||||
|
||||
this.plugin.getTeamManager().setTeam(e.getPlayer());
|
||||
|
||||
// Inject PacketReader
|
||||
final PacketReader packetReader = new PacketReader(this.plugin, e.getPlayer());
|
||||
packetReader.inject();
|
||||
|
|
|
@ -9,7 +9,6 @@ import com.mojang.authlib.GameProfile;
|
|||
import com.mojang.authlib.properties.Property;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.World;
|
||||
|
@ -21,8 +20,6 @@ import org.bukkit.entity.Player;
|
|||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.bukkit.scoreboard.Scoreboard;
|
||||
import org.bukkit.scoreboard.Team;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.JSONValue;
|
||||
|
@ -53,23 +50,12 @@ public class NPCManager {
|
|||
private final ArrayList<EntityPlayer> npcs = new ArrayList<EntityPlayer>();
|
||||
private final ArrayList<Integer> isDying = new ArrayList<Integer>();
|
||||
private final HashMap<UUID, PlayerInventory> inventories = new HashMap<UUID, PlayerInventory>();
|
||||
private final Scoreboard scoreboard;
|
||||
private Team afkTeam;
|
||||
|
||||
public NPCManager(final Varo plugin) {
|
||||
this.plugin = plugin;
|
||||
this.scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
|
||||
if (scoreboard.getTeam("afk") == null) {
|
||||
|
||||
this.afkTeam = scoreboard.registerNewTeam("afk");
|
||||
} else {
|
||||
this.afkTeam = scoreboard.getTeam("afk");
|
||||
}
|
||||
this.afkTeam.setPrefix("§8§l[AFK] ");
|
||||
this.afkTeam.setColor(ChatColor.GRAY);
|
||||
}
|
||||
|
||||
public void createClone(final Player player, final boolean shouldHaveAFKPrefix) {
|
||||
public void createClone(final Player player) {
|
||||
final Location location = player.getLocation();
|
||||
final MinecraftServer nmsServer = ((CraftServer) Bukkit.getServer()).getServer();
|
||||
final WorldServer nmsWorld = ((CraftWorld) player.getWorld()).getHandle();
|
||||
|
@ -90,13 +76,7 @@ public class NPCManager {
|
|||
final EntityPlayer npc = new EntityPlayer(nmsServer, nmsWorld, gameProfile, playerInteractManager);
|
||||
|
||||
npc.setLocation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
|
||||
|
||||
npc.setHealth((float) player.getHealth());
|
||||
|
||||
if (shouldHaveAFKPrefix) {
|
||||
this.afkTeam.addEntry(npc.getName());
|
||||
}
|
||||
|
||||
npcs.add(npc);
|
||||
|
||||
for (final Player connectionPlayer : Bukkit.getOnlinePlayers()) {
|
||||
|
@ -218,7 +198,6 @@ public class NPCManager {
|
|||
|
||||
// FIXME: Only for debug reasons -- Remove
|
||||
public void removeClone(final Player player) {
|
||||
this.afkTeam.removeEntry(player.getName());
|
||||
for (int i = 0; i < this.npcs.size(); i++) {
|
||||
final EntityPlayer npc = this.npcs.get(i);
|
||||
if (npc.getUniqueID().equals(player.getUniqueId())) {
|
||||
|
@ -253,7 +232,6 @@ public class NPCManager {
|
|||
@Override
|
||||
public void run() {
|
||||
nmsWorld.removeEntity(npc);
|
||||
afkTeam.removeEntry(npc.getName());
|
||||
npcs.remove(npc);
|
||||
}
|
||||
}.runTask(this.plugin);
|
||||
|
|
147
varo/src/main/java/de/cliffbreak/varo/managers/TeamManager.java
Normal file
147
varo/src/main/java/de/cliffbreak/varo/managers/TeamManager.java
Normal file
|
@ -0,0 +1,147 @@
|
|||
package de.cliffbreak.varo.managers;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scoreboard.Scoreboard;
|
||||
import org.bukkit.scoreboard.Team;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.JSONValue;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
import de.cliffbreak.varo.Varo;
|
||||
import de.cliffbreak.varo.uitls.JSONConfig;
|
||||
import de.cliffbreak.varo.uitls.WebUtils;
|
||||
|
||||
public class TeamManager {
|
||||
|
||||
private final Varo plugin;
|
||||
private final JSONConfig config;
|
||||
private final Scoreboard scoreboard;
|
||||
private final ArrayList<Team> teams;
|
||||
|
||||
public TeamManager(final Varo plugin) {
|
||||
this.plugin = plugin;
|
||||
this.config = new JSONConfig(new File("plugins/CliffbreakVaro", "teams.json"), getDefaults());
|
||||
this.config.save();
|
||||
|
||||
this.scoreboard = Bukkit.getScoreboardManager().getMainScoreboard();
|
||||
this.teams = new ArrayList<Team>();
|
||||
|
||||
this.init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
final JSONArray jsonTeams = config.getArray("Teams");
|
||||
for (final Object jsonRawTeam : jsonTeams) {
|
||||
final JSONObject jsonTeam = (JSONObject) jsonRawTeam;
|
||||
final Team team, afkTeam;
|
||||
if (jsonTeam.get("Name") == null)
|
||||
continue;
|
||||
if (scoreboard.getTeam((String) jsonTeam.get("Name")) == null) {
|
||||
team = this.scoreboard.registerNewTeam((String) jsonTeam.get("Name"));
|
||||
} else {
|
||||
team = scoreboard.getTeam((String) jsonTeam.get("Name"));
|
||||
}
|
||||
if (scoreboard.getTeam("afk" + jsonTeam.get("Name")) == null) {
|
||||
afkTeam = this.scoreboard.registerNewTeam("afk" + jsonTeam.get("Name"));
|
||||
} else {
|
||||
afkTeam = scoreboard.getTeam("afk" + jsonTeam.get("Name"));
|
||||
}
|
||||
teams.add(team);
|
||||
teams.add(afkTeam);
|
||||
team.setPrefix(ChatColor.valueOf((String) jsonTeam.get("Color")) + "§l[" + jsonTeam.get("Prefix") + "] ");
|
||||
afkTeam.setPrefix("§8§l[AFK] ");
|
||||
team.setColor(ChatColor.WHITE);
|
||||
afkTeam.setColor(ChatColor.valueOf((String) jsonTeam.get("Color")));
|
||||
for (final String entry : team.getEntries()) {
|
||||
team.removeEntry(entry);
|
||||
}
|
||||
for (final String entry : afkTeam.getEntries()) {
|
||||
team.removeEntry(entry);
|
||||
}
|
||||
for (final Object uuid : (JSONArray) jsonTeam.get("Players")) {
|
||||
try {
|
||||
final String result = WebUtils
|
||||
.GET("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid);
|
||||
if (result.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
final JSONObject profile = (JSONObject) JSONValue.parseWithException(result);
|
||||
team.addEntry((String) profile.get("name"));
|
||||
|
||||
} catch (IOException | ParseException e) {
|
||||
this.plugin.getLogger().info(
|
||||
"§c§lFehler:§r§c beim Verbinden mit der Mojang-API ist ein Fehler aufgetreten. Eventuell sind die Mojang Server down.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public Team getTeamByPlayer(final String player) {
|
||||
for (final Team team : this.teams) {
|
||||
if (team.hasEntry(player)) {
|
||||
return team;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void setAFK(String player) {
|
||||
Team oldTeam = getTeamByPlayer(player);
|
||||
for (final Team team : this.teams) {
|
||||
if (team.getName().equals("afk" + oldTeam.getName())) {
|
||||
team.addEntry(player);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void setTeam(Player player) {
|
||||
final JSONArray jsonTeams = config.getArray("Teams");
|
||||
for (final Object jsonRawTeam : jsonTeams) {
|
||||
JSONObject jsonTeam = (JSONObject) jsonRawTeam;
|
||||
if (((JSONArray) jsonTeam.get("Players")).contains(player.getUniqueId().toString().replace("-", ""))) {
|
||||
for (Team team : this.teams) {
|
||||
if (team.getName().equals(jsonTeam.get("Name").toString())) {
|
||||
team.addEntry(player.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private HashMap<String, Object> getDefaults() {
|
||||
final HashMap<String, Object> defaults = new HashMap<String, Object>();
|
||||
|
||||
final JSONObject team1 = new JSONObject();
|
||||
team1.put("Name", "Demo-Team");
|
||||
team1.put("Color", ChatColor.RED);
|
||||
team1.put("Prefix", "DMO");
|
||||
final JSONArray team1Players = new JSONArray();
|
||||
team1Players.add("a6019ccffe694bf780bcc3f3afa265dd");
|
||||
team1Players.add("069a79f444e94726a5befca90e38aaf5");
|
||||
team1.put("Players", team1Players);
|
||||
|
||||
final JSONObject team2 = new JSONObject();
|
||||
team2.put("Name", "Demo-Team 2");
|
||||
team2.put("Color", ChatColor.GREEN);
|
||||
team2.put("Prefix", "DM2");
|
||||
final JSONArray team2Players = new JSONArray();
|
||||
team2Players.add("dff082fcd52d4ad2b2d8d8202aea53ec");
|
||||
team2.put("Players", team2Players);
|
||||
|
||||
final JSONArray teams = new JSONArray();
|
||||
teams.add(team1);
|
||||
teams.add(team2);
|
||||
defaults.put("Teams", teams);
|
||||
return defaults;
|
||||
}
|
||||
}
|
122
varo/src/main/java/de/cliffbreak/varo/uitls/JSONConfig.java
Normal file
122
varo/src/main/java/de/cliffbreak/varo/uitls/JSONConfig.java
Normal file
|
@ -0,0 +1,122 @@
|
|||
package de.cliffbreak.varo.uitls;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileWriter;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.HashMap;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
|
||||
public class JSONConfig {
|
||||
|
||||
private final File file;
|
||||
private JSONObject json;
|
||||
private final JSONParser parser = new JSONParser();
|
||||
private final HashMap<String, Object> defaults;
|
||||
|
||||
public JSONConfig(final File file, final HashMap<String, Object> defaults) {
|
||||
this.file = file;
|
||||
this.defaults = defaults;
|
||||
reload();
|
||||
}
|
||||
|
||||
public void reload() {
|
||||
try {
|
||||
if (!this.file.getParentFile().exists()) {
|
||||
this.file.getParentFile().mkdirs();
|
||||
}
|
||||
if (!this.file.exists()) {
|
||||
final PrintWriter pw = new PrintWriter(file, "UTF-8");
|
||||
pw.print("{");
|
||||
pw.print("}");
|
||||
pw.flush();
|
||||
pw.close();
|
||||
}
|
||||
this.json = (JSONObject) this.parser.parse(new InputStreamReader(new FileInputStream(this.file), "UTF-8"));
|
||||
} catch (final Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public boolean save() {
|
||||
try {
|
||||
final JSONObject toSave = new JSONObject();
|
||||
|
||||
for (final String s : this.defaults.keySet()) {
|
||||
final Object o = this.defaults.get(s);
|
||||
if (o instanceof String) {
|
||||
toSave.put(s, getString(s));
|
||||
} else if (o instanceof Double) {
|
||||
toSave.put(s, getDouble(s));
|
||||
} else if (o instanceof Integer) {
|
||||
toSave.put(s, getInteger(s));
|
||||
} else if (o instanceof JSONObject) {
|
||||
toSave.put(s, getObject(s));
|
||||
} else if (o instanceof JSONArray) {
|
||||
toSave.put(s, getArray(s));
|
||||
}
|
||||
}
|
||||
|
||||
final TreeMap<String, Object> treeMap = new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER);
|
||||
treeMap.putAll(toSave);
|
||||
|
||||
final Gson g = new GsonBuilder().setPrettyPrinting().create();
|
||||
final String prettyJsonString = g.toJson(treeMap);
|
||||
|
||||
final FileWriter fw = new FileWriter(this.file);
|
||||
fw.write(prettyJsonString);
|
||||
fw.flush();
|
||||
fw.close();
|
||||
|
||||
return true;
|
||||
} catch (final Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private String getRawData(final String key) {
|
||||
return this.json.containsKey(key) ? this.json.get(key).toString()
|
||||
: (this.defaults.containsKey(key) ? this.defaults.get(key).toString() : key);
|
||||
}
|
||||
|
||||
private String getString(final String key) {
|
||||
return ChatColor.translateAlternateColorCodes('&', getRawData(key));
|
||||
}
|
||||
|
||||
private double getDouble(final String key) {
|
||||
try {
|
||||
return Double.parseDouble(getRawData(key));
|
||||
} catch (final Exception ex) {
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private double getInteger(final String key) {
|
||||
try {
|
||||
return Integer.parseInt(getRawData(key));
|
||||
} catch (final Exception ex) {
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
private JSONObject getObject(final String key) {
|
||||
return this.json.containsKey(key) ? (JSONObject) json.get(key)
|
||||
: (this.defaults.containsKey(key) ? (JSONObject) this.defaults.get(key) : new JSONObject());
|
||||
}
|
||||
|
||||
private JSONArray getArray(final String key) {
|
||||
return this.json.containsKey(key) ? (JSONArray) json.get(key)
|
||||
: (this.defaults.containsKey(key) ? (JSONArray) this.defaults.get(key) : new JSONArray());
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue