Advertisement
jayhillx

StoredLifeInformation

Jan 19th, 2023
1,036
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.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.ArrayList;
  10. import java.util.Arrays;
  11. import java.util.List;
  12. import java.util.UUID;
  13. import java.util.stream.Stream;
  14.  
  15. /**
  16.  * getPlayerDataByUUID(UUID uuid) - iterates through the map of players saved in the world cap.
  17.  * getLife(int life) -
  18.  */
  19. public class StoredLifeInformation implements ILifeInformation {
  20.  
  21.     /** This is what is stored in the worlds' nbt. */
  22.     private float storedMaxHealth;
  23.     private int currentLife;
  24.     private UUID uuid;
  25.     private final String[][] lives = new String[][]{new String[]{"", "", "", ""}, new String[]{"", "", "", ""}, new String[]{"", "", "", ""}, new String[]{"", "", "", ""}, new String[]{"", "", "", ""}, new String[]{"", "", "", ""}, new String[]{"", "", "", ""}, new String[]{"", "", "", ""}, new String[]{"", "", "", ""}, new String[]{"", "", "", ""}};
  26.  
  27.     public String[] getLife(int life) {
  28.         return this.lives[life];
  29.     }
  30.  
  31.     public void setLifeInfo(int life, float amountOfHearts, String causeOfDeath, String deathMessage, String timeLasted) {
  32.         String[] array = this.getLife(life);
  33.         array[0] = "" + amountOfHearts;
  34.         array[1] = causeOfDeath;
  35.         array[2] = deathMessage;
  36.         array[3] = timeLasted;
  37.     }
  38.  
  39.     public int getCurrentLife() {
  40.         return this.currentLife;
  41.     }
  42.  
  43.     public void setCurrentLife(int life) {
  44.         this.currentLife = life;
  45.     }
  46.  
  47.     public float getMaxHealth() {
  48.         return this.storedMaxHealth;
  49.     }
  50.  
  51.     public void setMaxHealth(float amount) {
  52.         this.storedMaxHealth = amount;
  53.     }
  54.  
  55.     public UUID getUUID() {
  56.         return this.uuid;
  57.     }
  58.  
  59.     public void setUUID(UUID uuid) {
  60.         this.uuid = uuid;
  61.     }
  62.  
  63.     public void cloneStoredInfo(StoredLifeInformation info) {
  64.         info.setCurrentLife(this.currentLife);
  65.         info.setMaxHealth(this.storedMaxHealth);
  66.         info.setUUID(this.uuid);
  67.     }
  68.  
  69.     public CompoundNBT writePlayerData() {
  70.         CompoundNBT player = new CompoundNBT();
  71.         player.put("Lives", this.writeLifeData());
  72.         player.putInt("CurrentLife", this.getCurrentLife());
  73.         player.putUniqueId("UUID", this.getUUID());
  74.         return player;
  75.     }
  76.  
  77.     public ListNBT writeLifeData() {
  78.         ListNBT lives = new ListNBT();
  79.         CompoundNBT lifeInfo = new CompoundNBT();
  80.  
  81.         for (int i = 0; i < this.getCurrentLife(); ++i) {
  82.             for (String key : this.lives[i]) {
  83.  
  84.                 lifeInfo.put(key, this.writeStringListAsArray(Arrays.stream(this.getLife(i))));
  85.             }
  86.             lives.add(i, lifeInfo);
  87.         }
  88.         return lives;
  89.     }
  90.  
  91.     public ListNBT writeStringListAsArray(Stream<String> items) {
  92.         return this.makeTagList(items.map(StringNBT::new));
  93.     }
  94.  
  95.     public static String[] readStringListAsArray(CompoundNBT tag, String key) {
  96.         final INBT var = tag.get(key);
  97.         if (!(var instanceof ListNBT)) throw new IllegalArgumentException("Tag " + key + " is not list.");
  98.         return ((ListNBT)var).stream().map(INBT::getString).toArray(String[]::new);
  99.     }
  100.  
  101.     private ListNBT makeTagList(Stream<INBT> items) {
  102.         return Util.make(new ListNBT(), list -> items.forEach(list::add));
  103.     }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement