Files
CustomNPC-Plus/src/main/java/noppes/npcs/controllers/data/Party.java
T

264 lines
8.3 KiB
Java
Raw Normal View History

2023-10-30 21:06:01 -04:00
package noppes.npcs.controllers.data;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
2023-10-30 21:06:01 -04:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import noppes.npcs.NoppesUtilServer;
2023-10-30 21:06:01 -04:00
import noppes.npcs.api.handler.IPlayerQuestData;
import noppes.npcs.api.handler.data.IQuest;
import noppes.npcs.controllers.PlayerDataController;
import noppes.npcs.controllers.QuestController;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
2023-10-31 23:26:57 -04:00
import java.util.*;
2023-10-30 21:06:01 -04:00
public class Party {
private final UUID partyUUID;
2023-10-30 21:06:01 -04:00
private UUID partyLeader;
2023-10-31 23:26:57 -04:00
private final HashMap<UUID, String> partyMembers = new HashMap<>();
private final ArrayList<UUID> partyOrder = new ArrayList<>();
2023-10-30 21:06:01 -04:00
private int currentQuestID = -1;
private boolean friendlyFire;
//Client-sided
private String partyLeaderName;
public Party() {
this.partyUUID = UUID.randomUUID();
2023-10-30 21:06:01 -04:00
}
public Party(UUID uuid) {
this.partyUUID = uuid;
}
public UUID getPartyUUID() {
return this.partyUUID;
2023-10-30 21:06:01 -04:00
}
public int getCurrentQuestID() {
return currentQuestID;
}
public void setCurrentQuestID(int currentQuestID) {
this.currentQuestID = currentQuestID;
}
2023-10-31 23:26:57 -04:00
public boolean addPlayer(EntityPlayer player) {
if(partyMembers.containsKey(player.getUniqueID())){
return false;
}
2023-10-30 21:06:01 -04:00
PlayerData playerData = PlayerDataController.Instance.getPlayerData(player);
playerData.partyUUID = this.partyUUID;
2023-10-31 23:26:57 -04:00
partyMembers.put(player.getUniqueID(), player.getCommandSenderName());
partyOrder.add(player.getUniqueID());
return true;
2023-10-30 21:06:01 -04:00
}
public boolean removePlayer(EntityPlayer player) {
if (player == null) return false;
2023-10-31 23:26:57 -04:00
UUID uuid = player.getUniqueID();
2023-10-31 23:26:57 -04:00
if(partyMembers.containsKey(uuid)){
partyMembers.remove(uuid);
partyOrder.remove(uuid);
if(uuid.equals(partyLeader)){
if(partyMembers.size() > 0){
partyLeader = partyOrder.get(0);
}
}
return true;
}
return false;
2023-10-30 21:06:01 -04:00
}
public boolean removePlayer(String playerName) {
return playerName != null && this.removePlayer(NoppesUtilServer.getPlayerByName(playerName));
2023-10-30 21:06:01 -04:00
}
public boolean hasPlayer(EntityPlayer player) {
return partyMembers.containsKey(player.getUniqueID());
2023-10-31 23:26:57 -04:00
}
public boolean hasPlayer(String playerName) {
UUID uuid = getUUID(playerName);
if (uuid == null){
return false;
}
return partyMembers.containsKey(uuid);
}
public EntityPlayer getPartyLeader() {
return NoppesUtilServer.getPlayer(this.partyLeader);
}
@SideOnly(Side.CLIENT)
public String getPartyLeaderName() {
return this.partyLeaderName;
}
public boolean setLeader(EntityPlayer player){
UUID uuid = player.getUniqueID();
if(partyLeader != null && partyLeader.equals(uuid)){
2023-10-31 23:26:57 -04:00
return false;
}
if(!partyMembers.containsKey(uuid)){
return false;
}
// Remove New Leader
partyOrder.remove(uuid);
// Add New Leader to Front
partyOrder.add(0, uuid);
// Set New Leader
partyLeader = uuid;
return true;
2023-10-30 21:06:01 -04:00
}
public Collection<String> getPlayerNames() {
return this.getPlayerNames(false);
}
public Collection<String> getPlayerNames(boolean lowercase) {
Collection<String> names = this.partyMembers.values();
if (!lowercase) {
return names;
}
ArrayList<String> lowerNames = new ArrayList<>();
for (String s : names) {
lowerNames.add(s.toLowerCase());
}
return lowerNames;
}
public Collection<UUID> getPlayerUUIDs() {
return this.partyMembers.keySet();
}
2023-10-30 21:06:01 -04:00
// To Be Called DURING Invite, Leave, Quest Switch, Leader Switch, etc.
public static boolean validateQuest(int questID, Collection<EntityPlayer> players){
IQuest quest = QuestController.Instance.get(questID);
if(quest == null){
return false;
}
boolean everyoneNeedsQuest = quest.getPartyRequirements() == 1;
if(everyoneNeedsQuest){
// TODO: Get EntityPlayer from WORLD using UUID Collection
// for(EntityPlayer player: players){
// if(player != null){
// PlayerData playerData = PlayerDataController.Instance.getPlayerData(player);
// if(playerData != null){
// IPlayerQuestData questData = playerData.getQuestData();
// if(questData != null){
// if(!questData.hasActiveQuest(questID)){
// return false;
// }
// } else{
// return false;
// }
// }
// }
// }
return true;
}
else {
// Check if Party Leader has Quest
return true;
}
}
public void toggleFriendlyFire() {
this.friendlyFire = !this.friendlyFire;
}
public boolean friendlyFire() {
return this.friendlyFire;
}
2023-10-30 21:06:01 -04:00
public static UUID getUUID(String name) {
UUID uuid = null;
try {
BufferedReader in = new BufferedReader(new InputStreamReader(new URL("https://api.mojang.com/users/profiles/minecraft/" + name).openStream()));
String uuidString;
uuidString = (((JsonObject)new JsonParser().parse(in)).get("id")).toString().replaceAll("\"", "");
uuidString = uuidString.replaceAll("(\\w{8})(\\w{4})(\\w{4})(\\w{4})(\\w{12})", "$1-$2-$3-$4-$5");
in.close();
uuid = UUID.fromString(uuidString);
} catch (Exception ignored) {}
return uuid;
}
public void readFromNBT(NBTTagCompound compound) {
this.partyLeaderName = compound.getString("PartyLeader");
this.currentQuestID = compound.getInteger("PartyQuestID");
this.friendlyFire = compound.getBoolean("FriendlyFire");
NBTTagList list = compound.getTagList("PartyMembers", 10);
for (int i = 0; i < list.tagCount(); i++) {
NBTTagCompound tagCompound = list.getCompoundTagAt(i);
UUID uuid = UUID.fromString(tagCompound.getString("UUID"));
String playerName = tagCompound.getString("PlayerName");
this.partyOrder.add(uuid);
this.partyMembers.put(uuid, playerName);
}
}
public NBTTagCompound writeToNBT() {
NBTTagCompound compound = new NBTTagCompound();
compound.setString("PartyUUID", this.partyUUID.toString());
compound.setInteger("PartyQuestID", this.currentQuestID);
compound.setBoolean("FriendlyFire", this.friendlyFire);
NBTTagList list = new NBTTagList();
for (UUID uuid : this.partyOrder) {
NBTTagCompound uuidCompound = new NBTTagCompound();
uuidCompound.setString("UUID", uuid.toString());
uuidCompound.setString("PlayerName", this.partyMembers.get(uuid));
list.appendTag(uuidCompound);
}
compound.setTag("PartyMembers", list);
compound.setString("PartyLeader", getPartyLeader().getCommandSenderName());
Quest quest = (Quest) QuestController.Instance.get(this.getCurrentQuestID());
if (quest != null) {
compound.setString("QuestName", quest.getName());
compound.setString("QuestCategory", quest.getCategory().getName());
}
return compound;
}
public void readClientNBT(NBTTagCompound compound) {
this.currentQuestID = compound.getInteger("PartyQuestID");
this.friendlyFire = compound.getBoolean("FriendlyFire");
}
public NBTTagCompound writeClientNBT() {
NBTTagCompound compound = new NBTTagCompound();
compound.setInteger("PartyQuestID", this.currentQuestID);
compound.setBoolean("FriendlyFire", this.friendlyFire);
return compound;
}
2023-10-30 21:06:01 -04:00
}