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

107 lines
3.3 KiB
Java
Raw Normal View History

package noppes.npcs.controllers.data;
2018-09-13 12:03:28 -04:00
import java.util.HashMap;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
2018-09-13 12:03:28 -04:00
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import noppes.npcs.EventHooks;
import noppes.npcs.controllers.FactionController;
import noppes.npcs.scripted.NpcAPI;
import noppes.npcs.scripted.event.FactionEvent;
2022-11-15 15:25:53 -05:00
import noppes.npcs.api.entity.IPlayer;
import noppes.npcs.api.handler.IPlayerFactionData;
2018-09-13 12:03:28 -04:00
2022-06-12 01:26:23 -05:00
public class PlayerFactionData implements IPlayerFactionData {
private final PlayerData parent;
2018-09-13 12:03:28 -04:00
public HashMap<Integer,Integer> factionData = new HashMap<Integer,Integer>();
2022-06-12 01:26:23 -05:00
public PlayerFactionData() {
parent = null;
}
public PlayerFactionData(PlayerData parent) {
this.parent = parent;
}
2018-09-13 12:03:28 -04:00
public void loadNBTData(NBTTagCompound compound) {
HashMap<Integer,Integer> factionData = new HashMap<Integer,Integer>();
if(compound == null)
return;
NBTTagList list = compound.getTagList("FactionData", 10);
if(list == null){
return;
}
for(int i = 0; i < list.tagCount(); i++)
{
NBTTagCompound nbttagcompound = list.getCompoundTagAt(i);
factionData.put(nbttagcompound.getInteger("Faction"),nbttagcompound.getInteger("Points"));
}
this.factionData = factionData;
}
public void saveNBTData(NBTTagCompound compound) {
NBTTagList list = new NBTTagList();
for(int faction : factionData.keySet()){
NBTTagCompound nbttagcompound = new NBTTagCompound();
nbttagcompound.setInteger("Faction", faction);
nbttagcompound.setInteger("Points", factionData.get(faction));
list.appendTag(nbttagcompound);
}
compound.setTag("FactionData", list);
}
public int getFactionPoints(int id) {
if(!factionData.containsKey(id)){
2022-01-10 15:21:48 -06:00
Faction faction = FactionController.getInstance().get(id);
2018-09-13 12:03:28 -04:00
factionData.put(id, faction == null? -1 : faction.defaultPoints);
}
return factionData.get(id);
}
public void increasePoints(int factionId, int points, EntityPlayer player) {
EventHooks.onFactionPoints(new FactionEvent.FactionPoints((IPlayer) NpcAPI.Instance().getIEntity((EntityPlayerMP) player), FactionController.getInstance().get(factionId), points < 0, points));
2018-09-13 12:03:28 -04:00
if(!factionData.containsKey(factionId)){
2022-01-10 15:21:48 -06:00
Faction faction = FactionController.getInstance().get(factionId);
2018-09-13 12:03:28 -04:00
factionData.put(factionId, faction == null? -1 : faction.defaultPoints);
}
factionData.put(factionId, factionData.get(factionId) + points);
}
public NBTTagCompound getPlayerGuiData(){
NBTTagCompound compound = new NBTTagCompound();
saveNBTData(compound);
NBTTagList list = new NBTTagList();
for(int id : factionData.keySet()){
2022-01-10 15:21:48 -06:00
Faction faction = FactionController.getInstance().get(id);
2018-09-13 12:03:28 -04:00
if(faction == null || faction.hideFaction)
continue;
NBTTagCompound com = new NBTTagCompound();
faction.writeNBT(com);
list.appendTag(com);
}
compound.setTag("FactionList", list);
return compound;
}
2022-06-12 01:26:23 -05:00
public int getPoints(int id) {
return this.getFactionPoints(id);
}
public void addPoints(int id, int points) {
if (parent != null) {
this.increasePoints(id, points, parent.player);
}
}
public void setPoints(int id, int points) {
if (parent != null) {
increasePoints(id, points-getFactionPoints(id), parent.player);
}
}
2018-09-13 12:03:28 -04:00
}