Clarified network code & set linkdata to always be written to network

together.
This commit is contained in:
CannibalVox 2013-12-07 02:16:50 -06:00
parent d9609ea60b
commit 81cc7053de
7 changed files with 62 additions and 50 deletions

View file

@ -4,6 +4,7 @@ import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import StevenDimDoors.mod_pocketDim.watcher.ClientLinkData;
import net.minecraft.network.INetworkManager; import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet250CustomPayload; import net.minecraft.network.packet.Packet250CustomPayload;
import StevenDimDoors.mod_pocketDim.core.PocketManager; import StevenDimDoors.mod_pocketDim.core.PocketManager;
@ -40,16 +41,16 @@ public class ServerPacketHandler implements IPacketHandler
} }
} }
private static class LinkWatcher implements IUpdateWatcher<Point4D> private static class LinkWatcher implements IUpdateWatcher<ClientLinkData>
{ {
@Override @Override
public void onCreated(Point4D message) public void onCreated(ClientLinkData message)
{ {
sendLinkPacket(PacketConstants.CREATE_LINK_PACKET_ID, message); sendLinkPacket(PacketConstants.CREATE_LINK_PACKET_ID, message);
} }
@Override @Override
public void onDeleted(Point4D message) public void onDeleted(ClientLinkData message)
{ {
sendLinkPacket(PacketConstants.DELETE_LINK_PACKET_ID, message); sendLinkPacket(PacketConstants.DELETE_LINK_PACKET_ID, message);
} }
@ -77,7 +78,7 @@ public class ServerPacketHandler implements IPacketHandler
} }
} }
private static void sendLinkPacket(byte id, Point4D data) private static void sendLinkPacket(byte id, ClientLinkData message)
{ {
try try
{ {
@ -85,7 +86,7 @@ public class ServerPacketHandler implements IPacketHandler
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream writer = new DataOutputStream(buffer); DataOutputStream writer = new DataOutputStream(buffer);
writer.writeByte(id); writer.writeByte(id);
Point4D.write(data, writer); message.write(writer);
writer.close(); writer.close();
packet.channel = PacketConstants.CHANNEL_NAME; packet.channel = PacketConstants.CHANNEL_NAME;
packet.data = buffer.toByteArray(); packet.data = buffer.toByteArray();

View file

@ -469,7 +469,7 @@ public class DDTeleporter
{ {
if(PocketManager.isBlackListed(link.destination().getDimension())) if(PocketManager.isBlackListed(link.destination().getDimension()))
{ {
link=PocketManager.getDimensionData(link.source().getDimension()).createLink(link.source,LinkTypes.SAFE_EXIT,link.orientation); link=PocketManager.getDimensionData(link.source().getDimension()).createLink(link.link.point,LinkTypes.SAFE_EXIT,link.link.orientation);
} }
else else
{ {
@ -545,7 +545,7 @@ public class DDTeleporter
// To avoid loops, don't generate a destination if the player is // To avoid loops, don't generate a destination if the player is
// already in a non-pocket dimension. // already in a non-pocket dimension.
NewDimData current = PocketManager.getDimensionData(link.source.getDimension()); NewDimData current = PocketManager.getDimensionData(link.link.point.getDimension());
if (current.isPocketDimension()) if (current.isPocketDimension())
{ {
Point4D source = link.source(); Point4D source = link.source();
@ -569,7 +569,7 @@ public class DDTeleporter
{ {
World startWorld = PocketManager.loadDimension(link.source().getDimension()); World startWorld = PocketManager.loadDimension(link.source().getDimension());
World destWorld = PocketManager.loadDimension(link.destination().getDimension()); World destWorld = PocketManager.loadDimension(link.destination().getDimension());
TileEntity doorTE = startWorld.getBlockTileEntity(link.source().getX(), link.source().getY(), link.source.getZ()); TileEntity doorTE = startWorld.getBlockTileEntity(link.source().getX(), link.source().getY(), link.link.point.getZ());
if(doorTE instanceof TileEntityDimDoor) if(doorTE instanceof TileEntityDimDoor)
{ {
if((TileEntityDimDoor.class.cast(doorTE).hasGennedPair)) if((TileEntityDimDoor.class.cast(doorTE).hasGennedPair))
@ -599,7 +599,7 @@ public class DDTeleporter
} }
private static boolean generateSafeExit(DimLink link, DDProperties properties) private static boolean generateSafeExit(DimLink link, DDProperties properties)
{ {
NewDimData current = PocketManager.getDimensionData(link.source.getDimension()); NewDimData current = PocketManager.getDimensionData(link.link.point.getDimension());
return generateSafeExit(current.root(), link, properties); return generateSafeExit(current.root(), link, properties);
} }
@ -608,7 +608,7 @@ public class DDTeleporter
// A dungeon exit acts the same as a safe exit, but has the chance of // A dungeon exit acts the same as a safe exit, but has the chance of
// taking the user to any non-pocket dimension, excluding Limbo and The End. // taking the user to any non-pocket dimension, excluding Limbo and The End.
NewDimData current = PocketManager.getDimensionData(link.source.getDimension()); NewDimData current = PocketManager.getDimensionData(link.link.point.getDimension());
ArrayList<NewDimData> roots = PocketManager.getRootDimensions(); ArrayList<NewDimData> roots = PocketManager.getRootDimensions();
int shiftChance = START_ROOT_SHIFT_CHANCE + ROOT_SHIFT_CHANCE_PER_LEVEL * (current.packDepth() - 1); int shiftChance = START_ROOT_SHIFT_CHANCE + ROOT_SHIFT_CHANCE_PER_LEVEL * (current.packDepth() - 1);

View file

@ -4,46 +4,56 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import StevenDimDoors.mod_pocketDim.util.Point4D; import StevenDimDoors.mod_pocketDim.util.Point4D;
import StevenDimDoors.mod_pocketDim.watcher.ClientLinkData;
public abstract class DimLink public abstract class DimLink
{ {
protected Point4D source; protected ClientLinkData link;
protected DimLink parent; protected DimLink parent;
protected LinkTail tail; protected LinkTail tail;
protected int orientation;
protected List<DimLink> children; protected List<DimLink> children;
protected DimLink(Point4D source, DimLink parent, int orientation) protected DimLink(ClientLinkData link, DimLink parent)
{ {
if (parent.source.getDimension() != source.getDimension()) if (parent.link.point.getDimension() != link.point.getDimension())
{ {
// Ban having children in other dimensions to avoid serialization issues with cross-dimensional tails // Ban having children in other dimensions to avoid serialization issues with cross-dimensional tails
throw new IllegalArgumentException("source and parent.source must have the same dimension."); throw new IllegalArgumentException("source and parent.source must have the same dimension.");
} }
this.orientation=orientation;
this.parent = parent; this.parent = parent;
this.source = source; this.link = link;
this.tail = parent.tail; this.tail = parent.tail;
this.children = new LinkedList<DimLink>(); this.children = new LinkedList<DimLink>();
parent.children.add(this); parent.children.add(this);
} }
protected DimLink(Point4D source, int linkType, int orientation) protected DimLink(ClientLinkData link, int linkType)
{ {
if ((linkType < LinkTypes.ENUM_MIN || linkType > LinkTypes.ENUM_MAX) && linkType != LinkTypes.CLIENT_SIDE) if ((linkType < LinkTypes.ENUM_MIN || linkType > LinkTypes.ENUM_MAX) && linkType != LinkTypes.CLIENT_SIDE)
{ {
throw new IllegalArgumentException("The specified link type is invalid."); throw new IllegalArgumentException("The specified link type is invalid.");
} }
this.orientation = orientation;
this.parent = null; this.parent = null;
this.source = source; this.link = link;
this.tail = new LinkTail(linkType, null); this.tail = new LinkTail(linkType, null);
this.children = new LinkedList<DimLink>(); this.children = new LinkedList<DimLink>();
} }
public Point4D source() public Point4D source()
{ {
return source; return link.point;
}
public int orientation()
{
return link.orientation;
}
public ClientLinkData link()
{
return link;
} }
public Point4D destination() public Point4D destination()
@ -52,7 +62,7 @@ public abstract class DimLink
} }
public int getDestinationOrientation() public int getDestinationOrientation()
{ {
return PocketManager.getLink(source.getX(), source.getY(), source.getZ(), source.getDimension()).orientation(); return PocketManager.getLink(link.point.getX(), link.point.getY(), link.point.getZ(), link.point.getDimension()).link().orientation;
} }
public boolean hasDestination() public boolean hasDestination()
{ {
@ -78,13 +88,9 @@ public abstract class DimLink
{ {
return tail.getLinkType(); return tail.getLinkType();
} }
public int orientation()
{
return orientation;
}
public String toString() public String toString()
{ {
return source + " -> " + (hasDestination() ? destination() : ""); return link.point + " -> " + (hasDestination() ? destination() : "");
} }
} }

View file

@ -6,6 +6,7 @@ import java.util.Map;
import java.util.Random; import java.util.Random;
import java.util.TreeMap; import java.util.TreeMap;
import StevenDimDoors.mod_pocketDim.watcher.ClientLinkData;
import net.minecraft.world.World; import net.minecraft.world.World;
import StevenDimDoors.mod_pocketDim.DDProperties; import StevenDimDoors.mod_pocketDim.DDProperties;
import StevenDimDoors.mod_pocketDim.Point3D; import StevenDimDoors.mod_pocketDim.Point3D;
@ -21,12 +22,12 @@ public abstract class NewDimData
{ {
public InnerDimLink(Point4D source, DimLink parent,int orientation) public InnerDimLink(Point4D source, DimLink parent,int orientation)
{ {
super(source, parent,orientation); super(new ClientLinkData(source, orientation), parent);
} }
public InnerDimLink(Point4D source, int linkType, int orientation) public InnerDimLink(Point4D source, int linkType, int orientation)
{ {
super(source, linkType,orientation); super(new ClientLinkData(source, orientation), linkType);
} }
public void setDestination(int x, int y, int z, NewDimData dimension) public void setDestination(int x, int y, int z, NewDimData dimension)
@ -50,7 +51,7 @@ public abstract class NewDimData
} }
parent = null; parent = null;
source = null; link = null;
tail = new LinkTail(0, null); tail = new LinkTail(0, null);
} }
@ -65,7 +66,7 @@ public abstract class NewDimData
//Ignore this request silently //Ignore this request silently
return false; return false;
} }
if (nextParent.source.getDimension() != source.getDimension()) if (nextParent.link.point.getDimension() != link.point.getDimension())
{ {
// Ban having children in other dimensions to avoid serialization issues with cross-dimensional tails // Ban having children in other dimensions to avoid serialization issues with cross-dimensional tails
throw new IllegalArgumentException("source and parent.source must have the same dimension."); throw new IllegalArgumentException("source and parent.source must have the same dimension.");
@ -88,7 +89,7 @@ public abstract class NewDimData
parent = nextParent; parent = nextParent;
tail = nextParent.tail; tail = nextParent.tail;
nextParent.children.add(this); nextParent.children.add(this);
this.orientation=orientation; this.link.orientation=orientation;
return true; return true;
} }
@ -111,7 +112,7 @@ public abstract class NewDimData
parent = null; parent = null;
tail = new LinkTail(linkType, null); tail = new LinkTail(linkType, null);
//Set new orientation //Set new orientation
this.orientation=orientation; this.link.orientation=orientation;
} }
} }
@ -130,10 +131,10 @@ public abstract class NewDimData
protected Point4D origin; protected Point4D origin;
protected int orientation; protected int orientation;
protected DungeonData dungeon; protected DungeonData dungeon;
protected IUpdateWatcher<Point4D> linkWatcher; protected IUpdateWatcher<ClientLinkData> linkWatcher;
protected NewDimData(int id, NewDimData parent, boolean isPocket, boolean isDungeon, protected NewDimData(int id, NewDimData parent, boolean isPocket, boolean isDungeon,
IUpdateWatcher<Point4D> linkWatcher) IUpdateWatcher<ClientLinkData> linkWatcher)
{ {
// The isPocket flag is redundant. It's meant as an integrity safeguard. // The isPocket flag is redundant. It's meant as an integrity safeguard.
if (isPocket && (parent == null)) if (isPocket && (parent == null))
@ -267,7 +268,7 @@ public abstract class NewDimData
//Link created! //Link created!
if(linkType!=LinkTypes.CLIENT_SIDE) if(linkType!=LinkTypes.CLIENT_SIDE)
{ {
linkWatcher.onCreated(link.source); linkWatcher.onCreated(link.link);
} }
return link; return link;
} }
@ -290,19 +291,19 @@ public abstract class NewDimData
InnerDimLink link = linkMapping.get(source); InnerDimLink link = linkMapping.get(source);
if (link == null) if (link == null)
{ {
link = new InnerDimLink(source, parent, parent.orientation); link = new InnerDimLink(source, parent, parent.link.orientation);
linkMapping.put(source, link); linkMapping.put(source, link);
linkList.add(link); linkList.add(link);
//Link created! //Link created!
linkWatcher.onCreated(link.source); linkWatcher.onCreated(link.link);
} }
else else
{ {
if (link.overwrite(parent, parent.orientation)) if (link.overwrite(parent, parent.link.orientation))
{ {
//Link created! //Link created!
linkWatcher.onCreated(link.source); linkWatcher.onCreated(link.link);
} }
} }
return link; return link;
@ -319,7 +320,7 @@ public abstract class NewDimData
{ {
linkList.remove(target); linkList.remove(target);
//Raise deletion event //Raise deletion event
linkWatcher.onDeleted(target.source); linkWatcher.onDeleted(target.link);
target.clear(); target.clear();
} }
return (target != null); return (target != null);
@ -334,9 +335,9 @@ public abstract class NewDimData
linkList.remove(target); linkList.remove(target);
//Raise deletion event //Raise deletion event
//TODO why is source null here? //TODO why is source null here?
if(target.source!=null) if(target.link!=null)
{ {
linkWatcher.onDeleted(target.source); linkWatcher.onDeleted(target.link);
} }
target.clear(); target.clear();
} }

View file

@ -47,7 +47,7 @@ public class PocketManager
// that any link destinations must be real dimensions controlled by PocketManager. // that any link destinations must be real dimensions controlled by PocketManager.
public InnerDimData(int id, InnerDimData parent, boolean isPocket, boolean isDungeon, public InnerDimData(int id, InnerDimData parent, boolean isPocket, boolean isDungeon,
IUpdateWatcher<Point4D> linkWatcher) IUpdateWatcher<ClientLinkData> linkWatcher)
{ {
super(id, parent, isPocket, isDungeon, linkWatcher); super(id, parent, isPocket, isDungeon, linkWatcher);
} }
@ -119,7 +119,7 @@ public class PocketManager
Point3D parentPoint = new Point3D(-1,-1,-1); Point3D parentPoint = new Point3D(-1,-1,-1);
if(link.parent!=null) if(link.parent!=null)
{ {
parentPoint=link.parent.source.toPoint3D(); parentPoint=link.parent.link.point.toPoint3D();
} }
for(DimLink childLink : link.children) for(DimLink childLink : link.children)
@ -127,7 +127,7 @@ public class PocketManager
children.add(childLink.source().toPoint3D()); children.add(childLink.source().toPoint3D());
} }
PackedLinkTail tail = new PackedLinkTail(link.tail.getDestination(),link.tail.getLinkType()); PackedLinkTail tail = new PackedLinkTail(link.tail.getDestination(),link.tail.getLinkType());
Links.add(new PackedLinkData(link.source,parentPoint,tail,link.orientation,children)); Links.add(new PackedLinkData(link.link.point,parentPoint,tail,link.link.orientation,children));
PackedLinkTail tempTail = new PackedLinkTail(link.tail.getDestination(),link.tail.getLinkType()); PackedLinkTail tempTail = new PackedLinkTail(link.tail.getDestination(),link.tail.getLinkType());
if(Tails.contains(tempTail)) if(Tails.contains(tempTail))
@ -212,7 +212,7 @@ public class PocketManager
* Set as true if we are a client that has connected to a dedicated server * Set as true if we are a client that has connected to a dedicated server
*/ */
public static volatile boolean isConnected = false; public static volatile boolean isConnected = false;
private static final UpdateWatcherProxy<Point4D> linkWatcher = new UpdateWatcherProxy<Point4D>(); private static final UpdateWatcherProxy<ClientLinkData> linkWatcher = new UpdateWatcherProxy<ClientLinkData>();
private static final UpdateWatcherProxy<ClientDimData> dimWatcher = new UpdateWatcherProxy<ClientDimData>(); private static final UpdateWatcherProxy<ClientDimData> dimWatcher = new UpdateWatcherProxy<ClientDimData>();
private static ArrayList<NewDimData> rootDimensions = null; private static ArrayList<NewDimData> rootDimensions = null;
@ -650,12 +650,12 @@ public class PocketManager
return dimWatcher.unregisterReceiver(watcher); return dimWatcher.unregisterReceiver(watcher);
} }
public static void registerLinkWatcher(IUpdateWatcher<Point4D> watcher) public static void registerLinkWatcher(IUpdateWatcher<ClientLinkData> watcher)
{ {
linkWatcher.registerReceiver(watcher); linkWatcher.registerReceiver(watcher);
} }
public static boolean unregisterLinkWatcher(IUpdateWatcher<Point4D> watcher) public static boolean unregisterLinkWatcher(IUpdateWatcher<ClientLinkData> watcher)
{ {
return linkWatcher.unregisterReceiver(watcher); return linkWatcher.unregisterReceiver(watcher);
} }

View file

@ -32,6 +32,8 @@ public class ClientDimData
public static ClientDimData read(DataInputStream input) throws IOException public static ClientDimData read(DataInputStream input) throws IOException
{ {
return new ClientDimData(input.readInt(), input.readInt()); int id = input.readInt();
int rootId = input.readInt();
return new ClientDimData(id, rootId);
} }
} }

View file

@ -32,7 +32,9 @@ public class ClientLinkData
public static ClientLinkData read(DataInputStream input) throws IOException public static ClientLinkData read(DataInputStream input) throws IOException
{ {
return new ClientLinkData(Point4D.read(input), input.readInt()); Point4D point = Point4D.read(input);
int orientation = input.readInt();
return new ClientLinkData(point, orientation);
} }
} }