Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //------------------------------------------------------------------------------------------------
- //
- // SG Craft - Dimension map
- //
- //------------------------------------------------------------------------------------------------
- package mod.gcewing.sg.util;
- import java.util.*;
- import com.google.common.primitives.Ints;
- import gcewing.sg.BaseUtils;
- import gcewing.sg.tileentity.SGBaseTE;
- import net.minecraft.nbt.*;
- import net.minecraft.world.*;
- import net.minecraft.world.storage.*;
- import net.minecraftforge.common.util.Constants;
- //import net.minecraftforge.common.*;
- public class StargateMCMap extends WorldSavedData {
- protected Map<String, ArrayList<SGAddressEntry>> addressToEntries = new HashMap<>();
- public StargateMCMap(String name) {
- super(name);
- }
- public static StargateMCMap get() {
- World world = BaseUtils.getWorldForDimension(0);
- return BaseUtils.getWorldData(world, StargateMCMap.class, "stargatemc-address_map");
- }
- public static boolean isOnly(SGBaseTE te, String address) {
- return get().isOnlyInstanced(te,address);
- }
- public static boolean hasGate(String address) {
- return get().getNumFor(address) != 0;
- }
- public static int getNumberOfGates(String address) {
- return get().getNumFor(address);
- }
- public static SGBaseTE getLastGate(String address) {
- return get().getLastFor(address);
- }
- public static SGBaseTE getFirstGate(String address) {
- return get().getFirstFor(address);
- }
- public static boolean canGateDial(String address) {
- return getNumberOfGates(address) == 0;
- }
- public static SGBaseTE getReceivingGate(String address) {
- SGBaseTE pte = get().getLastPegasus(address);
- if (pte != null) return pte;
- SGBaseTE mte = get().getLastMilkyWay(address);
- if (mte != null) return mte;
- return null;
- }
- public boolean isOnlyInstanced(SGBaseTE te, String address) {
- SGAddressEntry entry = new SGAddressEntry(te, address);
- if (getFirstFor(entry.getAddress()) != null && !getFirstFor(entry.getAddress()).equals(te)) return false;
- if (getFirstFor(entry.getAddress()) != null && !getLastFor(entry.getAddress()).equals(te)) return false;
- if (getFirstFor(entry.getAddress()) != null && getLastFor(entry.getAddress()) != null && !getFirstFor(entry.getAddress()).equals(getLastFor(entry.getAddress()))) return false;
- return true;
- }
- public SGBaseTE getFirstFor(String address) {
- SGBaseTE te = null;
- while (getNumFor(address) > 0) {
- te = getForIndex(address,0);
- if (te != null) return te;
- }
- return null;
- }
- public SGBaseTE getLastMilkyWay(String address) {
- SGBaseTE te = null;
- int count = getNumFor(address)-1;
- while (count >= 0) {
- if (addressToEntries.get(address).get(count).getType() == 1) te = getForIndex(address,count);
- if (te != null) return te;
- count--;
- }
- return null;
- }
- public SGBaseTE getLastPegasus(String address) {
- SGBaseTE te = null;
- int count = getNumFor(address)-1;
- while (count >= 0) {
- if (addressToEntries.get(address).get(count).getType() == 2) te = getForIndex(address,count);
- if (te != null) return te;
- count--;
- }
- return null;
- }
- public SGBaseTE getForIndex(String address, int index) {
- if (!addressToEntries.containsKey(address)) return null;
- if (addressToEntries.get(address).get(index) == null) return null;
- try {
- SGBaseTE te = addressToEntries.get(address).get(index).getGate();
- if (te != null && te.isMerged) return te;
- } catch (Exception e) {
- }
- System.out.println("Removing invalid gate " + index + " from SGMap for address : " + address);
- addressToEntries.get(address).remove(index);
- markDirty();
- return null;
- }
- public SGBaseTE getLastFor(String address) {
- SGBaseTE te = null;
- while (getNumFor(address) > 0) {
- te = getForIndex(address,addressToEntries.get(address).size()-1);
- if (te != null) return te;
- }
- return null;
- }
- public int getNumFor(String address) {
- if (addressToEntries.containsKey(address)) return addressToEntries.get(address).size();
- return 0;
- }
- public void removeFor(SGBaseTE te, String address) {
- SGAddressEntry entry = new SGAddressEntry(te,address);
- if (addressToEntries.containsKey(entry.getAddress())) {
- ArrayList<SGAddressEntry> entries = new ArrayList<SGAddressEntry>(addressToEntries.get(entry.getAddress()));
- for (SGAddressEntry e : addressToEntries.get(entry.getAddress())) {
- if (entry.getDimension() == e.getDimension() && entry.getX() == e.getX() && entry.getY() == e.getY() && entry.getZ() == e.getZ()) entries.remove(e);
- }
- addressToEntries.replace(entry.getAddress(), entries);
- }
- markDirty();
- }
- public static void addOrUpdateEntry(SGAddressEntry entry) {
- StargateMCMap.get().addOrUpdateFor(entry);
- }
- public void addOrUpdateFor(SGAddressEntry entry) {
- if (addressToEntries.containsKey(entry.getAddress())) {
- ArrayList<SGAddressEntry> entries = addressToEntries.get(entry.getAddress());
- boolean found = false;
- for (SGAddressEntry e : addressToEntries.get(entry.getAddress())) {
- if (e.getDimension() == entry.getDimension() && e.getX() == entry.getX() && entry.getY() == e.getY() && e.getZ() == entry.getZ()) {
- if (e.getType() != entry.getGate().gateType || e.hasChevronUpgrade() != entry.getGate().hasChevronUpgrade) {
- System.out.println("Updating gate for : " + entry.getAddress());
- e.setType(entry.getGate().gateType);
- e.setHasChevronUpgrade(entry.getGate().hasChevronUpgrade);
- }
- found = true;
- }
- }
- if (!found) {
- System.out.println("Loading gate for : " + entry.getAddress());
- entries.add(entry);
- }
- addressToEntries.replace(entry.getAddress(), entries);
- } else {
- ArrayList<SGAddressEntry> entries = new ArrayList<SGAddressEntry>();
- entries.add(entry);
- addressToEntries.put(entry.getAddress(),entries);
- }
- markDirty();
- }
- public static void addOrUpdateEntryFor(SGBaseTE te, String address) {
- StargateMCMap.get().addOrUpdateFor(te,address);
- }
- public void addOrUpdateFor(SGBaseTE te, String address) {
- SGAddressEntry entry = new SGAddressEntry(te,address);
- addOrUpdateFor(entry);
- }
- @Override
- public void readFromNBT(NBTTagCompound nbt) {
- NBTTagList addresses = nbt.getTagList("addresses",Constants.NBT.TAG_COMPOUND);
- if (addresses != null) System.out.println("Found addresses NBT with : " + addresses.tagCount() + " tags.");
- for (int i = 0; i < addresses.tagCount() ; i++) {
- NBTTagCompound tag = addresses.getCompoundTagAt(i);
- SGAddressEntry entry = SGAddressEntry.fromNBT(tag);
- System.out.println("Loading stargate entry from NBT: " + entry.getAddress() + "," + entry.getDimension() + "," + entry.getType() + "," + entry.getX() + "," + entry.getY() + "," + entry.getZ());
- addOrUpdateFor(entry);
- }
- }
- @Override
- public NBTTagCompound writeToNBT(NBTTagCompound nbt) {
- NBTTagList addresses = new NBTTagList();
- for (String address : this.addressToEntries.keySet()) {
- for (SGAddressEntry entry : this.addressToEntries.get(address)) {
- System.out.println("Saving stargate entry to NBT: " + entry.getAddress() + "," + entry.getDimension() + "," + entry.getType() + "," + entry.getX() + "," + entry.getY() + "," + entry.getZ());
- addresses.appendTag(entry.toNBT());
- }
- }
- System.out.println("Saving " + addresses.tagCount() + " addresses to StargateMCMap.");
- nbt.setTag("addresses", addresses);
- return nbt;
- }
- }
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package mod.gcewing.sg.util;
- import gcewing.sg.BaseUtils;
- import gcewing.sg.tileentity.SGBaseTE;
- import net.minecraft.nbt.*;
- import net.minecraft.util.math.BlockPos;
- import gcewing.sg.util.SGLocation;
- import zmaster587.advancedRocketry.dimension.DimensionManager;
- import zmaster587.advancedRocketry.dimension.DimensionProperties;
- /**
- *
- * @author draks
- */
- public class SGAddressEntry {
- private String address;
- private int x;
- private int y;
- private int z;
- private int dimension;
- private int type;
- private boolean hasChevronUpgrade;
- public SGAddressEntry(SGBaseTE te, String address) {
- this.dimension = te.getWorld().provider.getDimension();
- this.address = address;
- this.x = te.getPos().getX();
- this.y = te.getPos().getY();
- this.z = te.getPos().getZ();
- this.hasChevronUpgrade = te.hasChevronUpgrade;
- this.type = te.gateType;
- }
- public SGAddressEntry(String address, int dimension, int type, boolean hasChevronUpgrade, int posX, int posY, int posZ) {
- this.address = address;
- this.dimension = dimension;
- this.type = type;
- this.hasChevronUpgrade = hasChevronUpgrade;
- this.x = posX;
- this.y = posY;
- this.z = posZ;
- }
- public static SGAddressEntry fromNBT(NBTTagCompound tag) {
- try {
- return new SGAddressEntry(
- tag.getString("address"),
- tag.getInteger("dimension"),
- tag.getInteger("gateType"),
- tag.getBoolean("hasChevronUpgrade"),
- tag.getInteger("x"),
- tag.getInteger("y"),
- tag.getInteger("z")
- );
- } catch (Exception e ) {
- return null;
- }
- }
- public SGBaseTE getGate() {
- return SGBaseTE.at(new SGLocation(this.dimension, new BlockPos(this.x, this.y, this.z)));
- }
- public String getAddress() {
- return this.address;
- }
- public int getDimension() {
- return this.dimension;
- }
- public int getX() {
- return this.x;
- }
- public int getY() {
- return this.y;
- }
- public int getZ() {
- return this.z;
- }
- public boolean hasChevronUpgrade() {
- return this.hasChevronUpgrade;
- }
- public void setHasChevronUpgrade(boolean val) {
- this.hasChevronUpgrade = val;
- }
- public void setType(int val) {
- this.type = val;
- }
- public int getType() {
- return this.type;
- }
- public NBTTagCompound toNBT() {
- NBTTagCompound tag = new NBTTagCompound();
- tag.setString("address", this.address);
- tag.setInteger("dimension", this.dimension);
- tag.setInteger("x", this.x);
- tag.setInteger("y", this.y);
- tag.setInteger("z", this.z);
- tag.setInteger("type", this.type);
- tag.setBoolean("hasChevronUpgrade", this.hasChevronUpgrade);
- return tag;
- }
- public boolean equals(SGAddressEntry entry) {
- if (!entry.getAddress().equals(this.getAddress())) return false;
- if (entry.getDimension() != this.getDimension()) return false;
- if (entry.getX() != this.getX()) return false;
- if (entry.getY() != this.getY()) return false;
- if (entry.getZ() != this.getZ()) return false;
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement