Advertisement
jayhillx

StoredLifeInformation 2

Jan 22nd, 2023
1,258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.39 KB | None | 0 0
  1. package com.xlife.common.capability;
  2.  
  3. import net.minecraft.nbt.CompoundNBT;
  4. import net.minecraft.nbt.INBT;
  5. import net.minecraft.nbt.ListNBT;
  6. import net.minecraft.nbt.StringNBT;
  7. import net.minecraft.util.Util;
  8.  
  9. import java.util.Arrays;
  10. import java.util.UUID;
  11. import java.util.stream.Stream;
  12.  
  13. public class StoredLifeInformation implements ILifeInformation {
  14.  
  15.     private String playerName;
  16.     private UUID playerUUID;
  17.     private float storedMaxHealth;
  18.     private String storedTimeLiving;
  19.     private String[] life = new String[]{"", "", ""};
  20.     private String[][] lives = new String[][]{this.life};
  21.  
  22.     public String getPlayerName() {
  23.         return this.playerName;
  24.     }
  25.  
  26.     public void setPlayerName(String name) {
  27.         this.playerName = name;
  28.     }
  29.  
  30.     public UUID getPlayerUUID() {
  31.         return this.playerUUID;
  32.     }
  33.  
  34.     public void setPlayerUUID(UUID uuid) {
  35.         this.playerUUID = uuid;
  36.     }
  37.  
  38.     public float getMaxHealth() {
  39.         return this.storedMaxHealth;
  40.     }
  41.  
  42.     public void setMaxHealth(float amount) {
  43.         this.storedMaxHealth = amount;
  44.     }
  45.  
  46.     public String getTimeLiving() {
  47.         return this.storedTimeLiving;
  48.     }
  49.  
  50.     public void setTimeLiving(String time) {
  51.         this.storedTimeLiving = time;
  52.     }
  53.  
  54.     public String getCauseOfDeath(int fromLife) {
  55.         return this.lives[fromLife][0];
  56.     }
  57.  
  58.     public String getDeathMessage(int fromLife) {
  59.         return this.lives[fromLife][1];
  60.     }
  61.  
  62.     public String getTimeLasted(int fromLife) {
  63.         return this.lives[fromLife][2];
  64.     }
  65.  
  66.     public String[] getLife() {
  67.         return this.life;
  68.     }
  69.  
  70.     public void setLife(String[] life) {
  71.         this.life = life;
  72.     }
  73.  
  74.     public String[] getLifeInfo(int fromLife) {
  75.         return this.lives[fromLife];
  76.     }
  77.  
  78.     public void setLifeInfo(int fromLife, String cause, String message, String time) {
  79.         this.life[0] = cause;
  80.         this.life[1] = message;
  81.         this.life[2] = time;
  82.  
  83.         this.lives[fromLife] = this.life;
  84.     }
  85.  
  86.     /**
  87.      * Adds each player in map to the list written in the world cap.
  88.      */
  89.     public CompoundNBT writePlayer(CompoundNBT nbt) {
  90.         ListNBT players = new ListNBT();
  91.         this.playersMap.forEach((uuid, info) -> {
  92.             CompoundNBT data = new CompoundNBT();
  93.             data.putUniqueId("UUID", uuid);
  94.             data.put("Info", info);
  95.             players.add(data);
  96.         });
  97.         nbt.put("Players", players);
  98.         return nbt;
  99.     }
  100.  
  101.     public void readPlayer(CompoundNBT nbt) {
  102.         nbt.getList("Players", 10).forEach(e -> {
  103.             CompoundNBT data = (CompoundNBT)e;
  104.             data.getUniqueId("UUID");
  105.             data.getCompound("Info");
  106.  
  107.             this.readPlayerInfo(nbt);
  108.  
  109.             this.playersMap.put(this.playerUUID, data);
  110.         });
  111.     }
  112.  
  113.     public CompoundNBT writePlayerInfo(CompoundNBT nbt) {
  114.         ListNBT lives = new ListNBT();
  115.         lives.add(new StringNBT(Arrays.toString(this.getLife()).replace("]", "").replace("[", "")));
  116.  
  117.         nbt.put("Lives", lives);
  118.         nbt.putString("Name", this.getPlayerName());
  119.         nbt.putUniqueId("UUID", this.getPlayerUUID());
  120.         nbt.putFloat("Health", this.getMaxHealth());
  121.         nbt.putString("Living", this.getTimeLiving());
  122.         return nbt;
  123.     }
  124.  
  125.     public void readPlayerInfo(CompoundNBT nbt) {
  126.         if (nbt.contains("Lives", 10)) {
  127.             CompoundNBT livesNBT = nbt.getCompound("Lives");
  128.  
  129.             String[] causeArray = livesNBT.getString("").split(", ");
  130.             this.setLife(causeArray);
  131.         }
  132.         this.setPlayerName(nbt.getString("Name"));
  133.         this.setPlayerUUID(nbt.getUniqueId("UUID"));
  134.         this.setMaxHealth(nbt.getFloat("Health"));
  135.         this.setTimeLiving(nbt.getString("Living"));
  136.     }
  137.  
  138.     private static ListNBT makeTagList(Stream<INBT> items) {
  139.         return Util.make(new ListNBT(), list -> items.forEach(list::add));
  140.     }
  141.  
  142.     private static ListNBT makeStringList(Stream<String> items) {
  143.         return makeTagList(items.map(StringNBT::new));
  144.     }
  145.  
  146.     static String[] getStringListAsArray(CompoundNBT tag, String key) {
  147.         final INBT var = tag.get(key);
  148.         if (!(var instanceof ListNBT)) throw new IllegalArgumentException("Tag " + key + " is not list.");
  149.         return ((ListNBT) var).stream().map(INBT::getString).toArray(String[]::new);
  150.     }
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement