Advertisement
Guest User

Untitled

a guest
Oct 6th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. package theishiopian.sandbox.world;
  2.  
  3. import java.util.LinkedHashSet;
  4. import java.util.List;
  5. import javax.annotation.Nullable;
  6. import net.minecraft.nbt.NBTTagCompound;
  7. import net.minecraft.nbt.NBTTagList;
  8. import net.minecraft.util.math.BlockPos;
  9. import net.minecraft.world.World;
  10. import net.minecraft.world.WorldSavedData;
  11. import net.minecraftforge.common.util.Constants;
  12.  
  13. public class Tracking extends WorldSavedData
  14. {
  15.    
  16.     private final static LinkedHashSet<BlockPos>scarecrowLocations = new LinkedHashSet<BlockPos>();
  17.     private final static String NAME = "SCL[]";
  18.     private static Tracking instance = null;
  19.    
  20.     public Tracking()
  21.     {
  22.         super(NAME);
  23.     }
  24.    
  25.     @Nullable
  26.     public static Tracking get(World world)
  27.     {
  28.         if (world.isRemote)
  29.         {
  30.             return null;
  31.         }
  32.  
  33.         if (instance != null) {
  34.            
  35.             return instance;
  36.         }
  37.  
  38.         instance = (Tracking) world.loadItemData(Tracking.class, NAME);
  39.  
  40.         if (instance == null)
  41.         {
  42.             instance = new Tracking();
  43.         }
  44.  
  45.         return instance;
  46.     }
  47.    
  48.     //clears instance
  49.     public static void clearInstance()
  50.     {
  51.         instance = null;
  52.     }
  53.    
  54.     //save scarecrow data
  55.     public void save(World world)
  56.     {
  57.         world.setItemData(NAME, this);
  58.         markDirty();
  59.     }
  60.    
  61.     //returns read only list of scarecrows
  62.    
  63.     //adds scarecrow to list
  64.     public static void addScarecrow(BlockPos pos)
  65.     {
  66.         scarecrowLocations.add(pos);
  67.         System.out.println("Registering Scarecrow");
  68.     }
  69.    
  70.     //removes scarecrow from list
  71.     public static void removeScarecrow(BlockPos pos)
  72.     {
  73.         scarecrowLocations.remove(pos);
  74.         System.out.println("Unregistering Scarecrow");
  75.     }
  76.  
  77.     @Override
  78.     public void readFromNBT(NBTTagCompound scTag)
  79.     {
  80.         scarecrowLocations.clear();
  81.  
  82.         final NBTTagList scarecrows = scTag.getTagList("Scarecrows", Constants.NBT.TAG_COMPOUND);
  83.  
  84.         for (int i = 0; i < scarecrows.tagCount(); i++)
  85.         {
  86.             final NBTTagCompound locationTag = scarecrows.getCompoundTagAt(i);
  87.        
  88.             final BlockPos location = new BlockPos(locationTag.getInteger("posX"), locationTag.getInteger("posY"), locationTag.getInteger("posZ"));
  89.    
  90.             scarecrowLocations.add(location);
  91.         }
  92.     }
  93.  
  94.     @Override
  95.     public NBTTagCompound writeToNBT(NBTTagCompound scTag)
  96.     {
  97.         final NBTTagList locationList = new NBTTagList();
  98.  
  99.             for (BlockPos pos : scarecrowLocations)
  100.             {
  101.                 final NBTTagCompound locationTag = new NBTTagCompound();
  102.                 locationTag.setInteger("posX", pos.getX());
  103.                 locationTag.setInteger("posY", pos.getY());
  104.                 locationTag.setInteger("posZ", pos.getZ());
  105.                 locationList.appendTag(locationTag);
  106.             }
  107.         scTag.setTag("Scarecrows", locationList);
  108.  
  109.         return scTag;
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement