Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.01 KB | None | 0 0
  1. //------------------------------------------------------------------------------------------------
  2. //
  3. // SG Craft - Dimension map
  4. //
  5. //------------------------------------------------------------------------------------------------
  6. package mod.gcewing.sg.util;
  7.  
  8. import java.util.*;
  9. import com.google.common.primitives.Ints;
  10.  
  11. import gcewing.sg.BaseUtils;
  12. import gcewing.sg.tileentity.SGBaseTE;
  13. import net.minecraft.nbt.*;
  14. import net.minecraft.world.*;
  15. import net.minecraft.world.storage.*;
  16. import net.minecraftforge.common.util.Constants;
  17. //import net.minecraftforge.common.*;
  18.  
  19. public class StargateMCMap extends WorldSavedData {
  20.  
  21. protected Map<String, ArrayList<SGAddressEntry>> addressToEntries = new HashMap<>();
  22.  
  23. public StargateMCMap(String name) {
  24. super(name);
  25. }
  26.  
  27. public static StargateMCMap get() {
  28. World world = BaseUtils.getWorldForDimension(0);
  29. return BaseUtils.getWorldData(world, StargateMCMap.class, "stargatemc-address_map");
  30. }
  31.  
  32. public static boolean isOnly(SGBaseTE te, String address) {
  33. return get().isOnlyInstanced(te,address);
  34. }
  35.  
  36. public static boolean hasGate(String address) {
  37. return get().getNumFor(address) != 0;
  38. }
  39. public static int getNumberOfGates(String address) {
  40. return get().getNumFor(address);
  41. }
  42. public static SGBaseTE getLastGate(String address) {
  43. return get().getLastFor(address);
  44. }
  45.  
  46. public static SGBaseTE getFirstGate(String address) {
  47. return get().getFirstFor(address);
  48. }
  49.  
  50. public static boolean canGateDial(String address) {
  51. return getNumberOfGates(address) == 0;
  52. }
  53.  
  54. public static SGBaseTE getReceivingGate(String address) {
  55. SGBaseTE pte = get().getLastPegasus(address);
  56. if (pte != null) return pte;
  57. SGBaseTE mte = get().getLastMilkyWay(address);
  58. if (mte != null) return mte;
  59. return null;
  60. }
  61.  
  62. public boolean isOnlyInstanced(SGBaseTE te, String address) {
  63. SGAddressEntry entry = new SGAddressEntry(te, address);
  64. if (getFirstFor(entry.getAddress()) != null && !getFirstFor(entry.getAddress()).equals(te)) return false;
  65. if (getFirstFor(entry.getAddress()) != null && !getLastFor(entry.getAddress()).equals(te)) return false;
  66. if (getFirstFor(entry.getAddress()) != null && getLastFor(entry.getAddress()) != null && !getFirstFor(entry.getAddress()).equals(getLastFor(entry.getAddress()))) return false;
  67. return true;
  68. }
  69.  
  70. public SGBaseTE getFirstFor(String address) {
  71. SGBaseTE te = null;
  72. while (getNumFor(address) > 0) {
  73. te = getForIndex(address,0);
  74. if (te != null) return te;
  75. }
  76. return null;
  77. }
  78.  
  79. public SGBaseTE getLastMilkyWay(String address) {
  80. SGBaseTE te = null;
  81. int count = getNumFor(address)-1;
  82. while (count >= 0) {
  83. if (addressToEntries.get(address).get(count).getType() == 1) te = getForIndex(address,count);
  84. if (te != null) return te;
  85. count--;
  86. }
  87. return null;
  88. }
  89.  
  90. public SGBaseTE getLastPegasus(String address) {
  91. SGBaseTE te = null;
  92. int count = getNumFor(address)-1;
  93. while (count >= 0) {
  94. if (addressToEntries.get(address).get(count).getType() == 2) te = getForIndex(address,count);
  95. if (te != null) return te;
  96. count--;
  97. }
  98. return null;
  99. }
  100.  
  101. public SGBaseTE getForIndex(String address, int index) {
  102. if (!addressToEntries.containsKey(address)) return null;
  103. if (addressToEntries.get(address).get(index) == null) return null;
  104. try {
  105. SGBaseTE te = addressToEntries.get(address).get(index).getGate();
  106. if (te != null && te.isMerged) return te;
  107. } catch (Exception e) {
  108. }
  109. System.out.println("Removing invalid gate " + index + " from SGMap for address : " + address);
  110. addressToEntries.get(address).remove(index);
  111. markDirty();
  112. return null;
  113. }
  114.  
  115. public SGBaseTE getLastFor(String address) {
  116. SGBaseTE te = null;
  117. while (getNumFor(address) > 0) {
  118. te = getForIndex(address,addressToEntries.get(address).size()-1);
  119. if (te != null) return te;
  120. }
  121. return null;
  122. }
  123.  
  124. public int getNumFor(String address) {
  125. if (addressToEntries.containsKey(address)) return addressToEntries.get(address).size();
  126. return 0;
  127. }
  128.  
  129. public void removeFor(SGBaseTE te, String address) {
  130. SGAddressEntry entry = new SGAddressEntry(te,address);
  131. if (addressToEntries.containsKey(entry.getAddress())) {
  132. ArrayList<SGAddressEntry> entries = new ArrayList<SGAddressEntry>(addressToEntries.get(entry.getAddress()));
  133. for (SGAddressEntry e : addressToEntries.get(entry.getAddress())) {
  134. if (entry.getDimension() == e.getDimension() && entry.getX() == e.getX() && entry.getY() == e.getY() && entry.getZ() == e.getZ()) entries.remove(e);
  135. }
  136. addressToEntries.replace(entry.getAddress(), entries);
  137. }
  138. markDirty();
  139. }
  140.  
  141. public static void addOrUpdateEntry(SGAddressEntry entry) {
  142. StargateMCMap.get().addOrUpdateFor(entry);
  143. }
  144.  
  145. public void addOrUpdateFor(SGAddressEntry entry) {
  146. if (addressToEntries.containsKey(entry.getAddress())) {
  147. ArrayList<SGAddressEntry> entries = addressToEntries.get(entry.getAddress());
  148. boolean found = false;
  149. for (SGAddressEntry e : addressToEntries.get(entry.getAddress())) {
  150. if (e.getDimension() == entry.getDimension() && e.getX() == entry.getX() && entry.getY() == e.getY() && e.getZ() == entry.getZ()) {
  151. if (e.getType() != entry.getGate().gateType || e.hasChevronUpgrade() != entry.getGate().hasChevronUpgrade) {
  152. System.out.println("Updating gate for : " + entry.getAddress());
  153. e.setType(entry.getGate().gateType);
  154. e.setHasChevronUpgrade(entry.getGate().hasChevronUpgrade);
  155. }
  156. found = true;
  157. }
  158. }
  159. if (!found) {
  160. System.out.println("Loading gate for : " + entry.getAddress());
  161. entries.add(entry);
  162. }
  163. addressToEntries.replace(entry.getAddress(), entries);
  164. } else {
  165. ArrayList<SGAddressEntry> entries = new ArrayList<SGAddressEntry>();
  166. entries.add(entry);
  167. addressToEntries.put(entry.getAddress(),entries);
  168. }
  169. markDirty();
  170. }
  171. public static void addOrUpdateEntryFor(SGBaseTE te, String address) {
  172. StargateMCMap.get().addOrUpdateFor(te,address);
  173. }
  174. public void addOrUpdateFor(SGBaseTE te, String address) {
  175. SGAddressEntry entry = new SGAddressEntry(te,address);
  176. addOrUpdateFor(entry);
  177. }
  178.  
  179. @Override
  180. public void readFromNBT(NBTTagCompound nbt) {
  181. NBTTagList addresses = nbt.getTagList("addresses",Constants.NBT.TAG_COMPOUND);
  182. if (addresses != null) System.out.println("Found addresses NBT with : " + addresses.tagCount() + " tags.");
  183. for (int i = 0; i < addresses.tagCount() ; i++) {
  184. NBTTagCompound tag = addresses.getCompoundTagAt(i);
  185. SGAddressEntry entry = SGAddressEntry.fromNBT(tag);
  186. System.out.println("Loading stargate entry from NBT: " + entry.getAddress() + "," + entry.getDimension() + "," + entry.getType() + "," + entry.getX() + "," + entry.getY() + "," + entry.getZ());
  187. addOrUpdateFor(entry);
  188. }
  189. }
  190.  
  191. @Override
  192. public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
  193. NBTTagList addresses = new NBTTagList();
  194. for (String address : this.addressToEntries.keySet()) {
  195. for (SGAddressEntry entry : this.addressToEntries.get(address)) {
  196. System.out.println("Saving stargate entry to NBT: " + entry.getAddress() + "," + entry.getDimension() + "," + entry.getType() + "," + entry.getX() + "," + entry.getY() + "," + entry.getZ());
  197. addresses.appendTag(entry.toNBT());
  198. }
  199. }
  200. System.out.println("Saving " + addresses.tagCount() + " addresses to StargateMCMap.");
  201. nbt.setTag("addresses", addresses);
  202. return nbt;
  203. }
  204.  
  205. }
  206.  
  207.  
  208.  
  209. /*
  210. * To change this license header, choose License Headers in Project Properties.
  211. * To change this template file, choose Tools | Templates
  212. * and open the template in the editor.
  213. */
  214. package mod.gcewing.sg.util;
  215. import gcewing.sg.BaseUtils;
  216. import gcewing.sg.tileentity.SGBaseTE;
  217. import net.minecraft.nbt.*;
  218. import net.minecraft.util.math.BlockPos;
  219. import gcewing.sg.util.SGLocation;
  220. import zmaster587.advancedRocketry.dimension.DimensionManager;
  221. import zmaster587.advancedRocketry.dimension.DimensionProperties;
  222. /**
  223. *
  224. * @author draks
  225. */
  226. public class SGAddressEntry {
  227.  
  228. private String address;
  229. private int x;
  230. private int y;
  231. private int z;
  232. private int dimension;
  233. private int type;
  234. private boolean hasChevronUpgrade;
  235.  
  236. public SGAddressEntry(SGBaseTE te, String address) {
  237. this.dimension = te.getWorld().provider.getDimension();
  238. this.address = address;
  239. this.x = te.getPos().getX();
  240. this.y = te.getPos().getY();
  241. this.z = te.getPos().getZ();
  242. this.hasChevronUpgrade = te.hasChevronUpgrade;
  243. this.type = te.gateType;
  244. }
  245.  
  246. public SGAddressEntry(String address, int dimension, int type, boolean hasChevronUpgrade, int posX, int posY, int posZ) {
  247. this.address = address;
  248. this.dimension = dimension;
  249. this.type = type;
  250. this.hasChevronUpgrade = hasChevronUpgrade;
  251. this.x = posX;
  252. this.y = posY;
  253. this.z = posZ;
  254. }
  255.  
  256. public static SGAddressEntry fromNBT(NBTTagCompound tag) {
  257. try {
  258. return new SGAddressEntry(
  259. tag.getString("address"),
  260. tag.getInteger("dimension"),
  261. tag.getInteger("gateType"),
  262. tag.getBoolean("hasChevronUpgrade"),
  263. tag.getInteger("x"),
  264. tag.getInteger("y"),
  265. tag.getInteger("z")
  266. );
  267. } catch (Exception e ) {
  268. return null;
  269. }
  270. }
  271.  
  272. public SGBaseTE getGate() {
  273. return SGBaseTE.at(new SGLocation(this.dimension, new BlockPos(this.x, this.y, this.z)));
  274. }
  275.  
  276. public String getAddress() {
  277. return this.address;
  278. }
  279. public int getDimension() {
  280. return this.dimension;
  281. }
  282. public int getX() {
  283. return this.x;
  284. }
  285. public int getY() {
  286. return this.y;
  287. }
  288. public int getZ() {
  289. return this.z;
  290. }
  291. public boolean hasChevronUpgrade() {
  292. return this.hasChevronUpgrade;
  293. }
  294.  
  295. public void setHasChevronUpgrade(boolean val) {
  296. this.hasChevronUpgrade = val;
  297. }
  298. public void setType(int val) {
  299. this.type = val;
  300. }
  301. public int getType() {
  302. return this.type;
  303. }
  304.  
  305. public NBTTagCompound toNBT() {
  306. NBTTagCompound tag = new NBTTagCompound();
  307. tag.setString("address", this.address);
  308. tag.setInteger("dimension", this.dimension);
  309. tag.setInteger("x", this.x);
  310. tag.setInteger("y", this.y);
  311. tag.setInteger("z", this.z);
  312. tag.setInteger("type", this.type);
  313. tag.setBoolean("hasChevronUpgrade", this.hasChevronUpgrade);
  314. return tag;
  315. }
  316.  
  317. public boolean equals(SGAddressEntry entry) {
  318. if (!entry.getAddress().equals(this.getAddress())) return false;
  319. if (entry.getDimension() != this.getDimension()) return false;
  320. if (entry.getX() != this.getX()) return false;
  321. if (entry.getY() != this.getY()) return false;
  322. if (entry.getZ() != this.getZ()) return false;
  323. return true;
  324. }
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement