From 56ecb0cd9ea7c9fbdacf76e7f0bb4b840275b42d Mon Sep 17 00:00:00 2001 From: SenseiKiwi Date: Mon, 2 Sep 2013 11:47:12 -0400 Subject: [PATCH 1/8] Reorganized DimLink Code Moved the DimLink code out of NewDimData in order to reduce clutter inside that class and made it a separate class, except for functions that should only be available for NewDimData. Deleted IDimLink and changed all references to it to use DimLink instead. DimLink is now an abstract class, which achieves the same encapsulation and protection we had before by having DimLink implement IDimLink from within NewDimData. NewDimData has a new class inside, InnerDimLink, which provides it access to special functions that would be dangerous to expose. This is the same mechanism used to protect NewDimData's dangerous functions. These changes are in preparation for adding more code for packet handling. --- .../mod_pocketDim/DDTeleporter.java | 13 +- .../mod_pocketDim/blocks/DimensionalDoor.java | 9 +- .../mod_pocketDim/blocks/TransientDoor.java | 4 +- .../mod_pocketDim/blocks/UnstableDoor.java | 4 +- .../mod_pocketDim/blocks/WarpDoor.java | 7 +- .../commands/CommandCreateDungeonRift.java | 4 +- .../mod_pocketDim/core/DimLink.java | 77 ++++++++ .../mod_pocketDim/core/IDimLink.java | 27 --- .../mod_pocketDim/core/LinkTypes.java | 19 ++ .../mod_pocketDim/core/NewDimData.java | 164 ++++++------------ .../mod_pocketDim/core/PocketManager.java | 6 +- .../dungeon/DungeonSchematic.java | 15 +- .../mod_pocketDim/helpers/DungeonHelper.java | 7 +- .../mod_pocketDim/items/BaseItemDoor.java | 4 +- .../mod_pocketDim/items/ItemRiftBlade.java | 4 +- .../items/ItemRiftSignature.java | 7 +- .../ticking/RiftRegenerator.java | 4 +- .../tileentities/TileEntityRift.java | 6 +- .../mod_pocketDim/world/GatewayGenerator.java | 9 +- .../mod_pocketDim/world/PocketBuilder.java | 8 +- 20 files changed, 206 insertions(+), 192 deletions(-) create mode 100644 StevenDimDoors/mod_pocketDim/core/DimLink.java delete mode 100644 StevenDimDoors/mod_pocketDim/core/IDimLink.java create mode 100644 StevenDimDoors/mod_pocketDim/core/LinkTypes.java diff --git a/StevenDimDoors/mod_pocketDim/DDTeleporter.java b/StevenDimDoors/mod_pocketDim/DDTeleporter.java index e390ca7..06b7289 100644 --- a/StevenDimDoors/mod_pocketDim/DDTeleporter.java +++ b/StevenDimDoors/mod_pocketDim/DDTeleporter.java @@ -16,7 +16,8 @@ import net.minecraft.util.MathHelper; import net.minecraft.world.World; import net.minecraft.world.WorldServer; import net.minecraftforge.common.DimensionManager; -import StevenDimDoors.mod_pocketDim.core.IDimLink; +import StevenDimDoors.mod_pocketDim.core.DimLink; +import StevenDimDoors.mod_pocketDim.core.LinkTypes; import StevenDimDoors.mod_pocketDim.core.PocketManager; import StevenDimDoors.mod_pocketDim.util.Point4D; import StevenDimDoors.mod_pocketDim.world.PocketBuilder; @@ -312,7 +313,7 @@ public class DDTeleporter * @param link - the link the player is using to teleport; sends the player to its destination * @param player - the instance of the player to be teleported */ - public static void traverseDimDoor(World world, IDimLink link, Entity entity) + public static void traverseDimDoor(World world, DimLink link, Entity entity) { if (world == null) { @@ -349,7 +350,7 @@ public class DDTeleporter entity.worldObj.playSoundEffect(entity.posX, entity.posY, entity.posZ, "mob.endermen.portal", 1.0F, 1.0F); } - private static boolean initializeDestination(IDimLink link, DDProperties properties) + private static boolean initializeDestination(DimLink link, DDProperties properties) { //FIXME: Change this later to support rooms that have been wiped and must be regenerated. if (link.hasDestination()) @@ -362,11 +363,11 @@ public class DDTeleporter //FIXME: Add code for restoring the destination-side door. switch (link.linkType()) { - case IDimLink.TYPE_DUNGEON: + case LinkTypes.DUNGEON: return PocketBuilder.generateNewDungeonPocket(link, properties); - case IDimLink.TYPE_POCKET: + case LinkTypes.POCKET: return PocketBuilder.generateNewPocket(link, properties); - case IDimLink.TYPE_NORMAL: + case LinkTypes.NORMAL: return true; default: throw new IllegalArgumentException("link has an unrecognized link type."); diff --git a/StevenDimDoors/mod_pocketDim/blocks/DimensionalDoor.java b/StevenDimDoors/mod_pocketDim/blocks/DimensionalDoor.java index fbd0281..95de79d 100644 --- a/StevenDimDoors/mod_pocketDim/blocks/DimensionalDoor.java +++ b/StevenDimDoors/mod_pocketDim/blocks/DimensionalDoor.java @@ -20,7 +20,8 @@ import net.minecraft.world.World; import StevenDimDoors.mod_pocketDim.DDProperties; import StevenDimDoors.mod_pocketDim.DDTeleporter; import StevenDimDoors.mod_pocketDim.mod_pocketDim; -import StevenDimDoors.mod_pocketDim.core.IDimLink; +import StevenDimDoors.mod_pocketDim.core.DimLink; +import StevenDimDoors.mod_pocketDim.core.LinkTypes; import StevenDimDoors.mod_pocketDim.core.NewDimData; import StevenDimDoors.mod_pocketDim.core.PocketManager; import StevenDimDoors.mod_pocketDim.schematic.BlockRotator; @@ -59,7 +60,7 @@ public class DimensionalDoor extends BlockContainer { this.onPoweredBlockChange(world, x, y, z, false); - IDimLink link = PocketManager.getLink(x, y, z, world.provider.dimensionId); + DimLink link = PocketManager.getLink(x, y, z, world.provider.dimensionId); if (link != null) { DDTeleporter.traverseDimDoor(world, link, entity); @@ -169,10 +170,10 @@ public class DimensionalDoor extends BlockContainer if (!world.isRemote && world.getBlockId(x, y - 1, z) == this.blockID) { NewDimData dimension = PocketManager.getDimensionData(world); - IDimLink link = dimension.getLink(x, y, z); + DimLink link = dimension.getLink(x, y, z); if (link == null) { - dimension.createLink(x, y, z, IDimLink.TYPE_POCKET); + dimension.createLink(x, y, z, LinkTypes.POCKET); } } world.setBlockTileEntity(x, y, z, this.createNewTileEntity(world)); diff --git a/StevenDimDoors/mod_pocketDim/blocks/TransientDoor.java b/StevenDimDoors/mod_pocketDim/blocks/TransientDoor.java index 6fbbc93..48ce75e 100644 --- a/StevenDimDoors/mod_pocketDim/blocks/TransientDoor.java +++ b/StevenDimDoors/mod_pocketDim/blocks/TransientDoor.java @@ -12,7 +12,7 @@ import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import StevenDimDoors.mod_pocketDim.DDTeleporter; import StevenDimDoors.mod_pocketDim.mod_pocketDim; -import StevenDimDoors.mod_pocketDim.core.IDimLink; +import StevenDimDoors.mod_pocketDim.core.DimLink; import StevenDimDoors.mod_pocketDim.core.PocketManager; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; @@ -66,7 +66,7 @@ public class TransientDoor extends WarpDoor { this.onPoweredBlockChange(world, x, y, z, false); - IDimLink link = PocketManager.getLink(x, y, z, world.provider.dimensionId); + DimLink link = PocketManager.getLink(x, y, z, world.provider.dimensionId); if (link != null) { DDTeleporter.traverseDimDoor(world, link, entity); diff --git a/StevenDimDoors/mod_pocketDim/blocks/UnstableDoor.java b/StevenDimDoors/mod_pocketDim/blocks/UnstableDoor.java index d53e576..c4875ed 100644 --- a/StevenDimDoors/mod_pocketDim/blocks/UnstableDoor.java +++ b/StevenDimDoors/mod_pocketDim/blocks/UnstableDoor.java @@ -7,7 +7,7 @@ import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import StevenDimDoors.mod_pocketDim.DDProperties; import StevenDimDoors.mod_pocketDim.mod_pocketDim; -import StevenDimDoors.mod_pocketDim.core.IDimLink; +import StevenDimDoors.mod_pocketDim.core.LinkTypes; import StevenDimDoors.mod_pocketDim.core.NewDimData; import StevenDimDoors.mod_pocketDim.core.PocketManager; import cpw.mods.fml.relauncher.Side; @@ -54,7 +54,7 @@ public class UnstableDoor extends DimensionalDoor if (!world.isRemote && world.getBlockId(x, y - 1, z) == this.blockID) { NewDimData dimension = PocketManager.getDimensionData(world); - dimension.createLink(x, y, z, IDimLink.TYPE_RANDOM); + dimension.createLink(x, y, z, LinkTypes.RANDOM); } } } \ No newline at end of file diff --git a/StevenDimDoors/mod_pocketDim/blocks/WarpDoor.java b/StevenDimDoors/mod_pocketDim/blocks/WarpDoor.java index 410a052..445d8d9 100644 --- a/StevenDimDoors/mod_pocketDim/blocks/WarpDoor.java +++ b/StevenDimDoors/mod_pocketDim/blocks/WarpDoor.java @@ -9,7 +9,8 @@ import net.minecraft.util.Icon; import net.minecraft.world.IBlockAccess; import net.minecraft.world.World; import StevenDimDoors.mod_pocketDim.mod_pocketDim; -import StevenDimDoors.mod_pocketDim.core.IDimLink; +import StevenDimDoors.mod_pocketDim.core.DimLink; +import StevenDimDoors.mod_pocketDim.core.LinkTypes; import StevenDimDoors.mod_pocketDim.core.NewDimData; import StevenDimDoors.mod_pocketDim.core.PocketManager; import cpw.mods.fml.relauncher.Side; @@ -38,10 +39,10 @@ public class WarpDoor extends DimensionalDoor if (!world.isRemote && world.getBlockId(x, y - 1, z) == this.blockID) { NewDimData dimension = PocketManager.getDimensionData(world); - IDimLink link = dimension.getLink(x, y, z); + DimLink link = dimension.getLink(x, y, z); if (link == null) { - dimension.createLink(x, y, z, IDimLink.TYPE_SAFE_EXIT); + dimension.createLink(x, y, z, LinkTypes.SAFE_EXIT); } } world.setBlockTileEntity(x, y, z, this.createNewTileEntity(world)); diff --git a/StevenDimDoors/mod_pocketDim/commands/CommandCreateDungeonRift.java b/StevenDimDoors/mod_pocketDim/commands/CommandCreateDungeonRift.java index 8a055f8..c15b458 100644 --- a/StevenDimDoors/mod_pocketDim/commands/CommandCreateDungeonRift.java +++ b/StevenDimDoors/mod_pocketDim/commands/CommandCreateDungeonRift.java @@ -4,7 +4,7 @@ import java.util.Collection; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.util.MathHelper; -import StevenDimDoors.mod_pocketDim.core.IDimLink; +import StevenDimDoors.mod_pocketDim.core.DimLink; import StevenDimDoors.mod_pocketDim.core.PocketManager; import StevenDimDoors.mod_pocketDim.dungeon.DungeonData; import StevenDimDoors.mod_pocketDim.helpers.DungeonHelper; @@ -55,7 +55,7 @@ public class CommandCreateDungeonRift extends DDCommandBase } else { - IDimLink link; + DimLink link; DungeonData result; int x = MathHelper.floor_double(sender.posX); int y = MathHelper.floor_double(sender.posY); diff --git a/StevenDimDoors/mod_pocketDim/core/DimLink.java b/StevenDimDoors/mod_pocketDim/core/DimLink.java new file mode 100644 index 0000000..c0cf6de --- /dev/null +++ b/StevenDimDoors/mod_pocketDim/core/DimLink.java @@ -0,0 +1,77 @@ +package StevenDimDoors.mod_pocketDim.core; + +import java.util.ArrayList; + +import StevenDimDoors.mod_pocketDim.util.Point4D; + +public abstract class DimLink +{ + private static final int EXPECTED_CHILDREN = 2; + + protected Point4D source; + protected DimLink parent; + protected LinkTail tail; + protected ArrayList children; + + public DimLink(Point4D source, DimLink parent) + { + this.parent = parent; + this.source = source; + this.tail = parent.tail; + this.children = new ArrayList(EXPECTED_CHILDREN); + parent.children.add(this); + } + + public DimLink(Point4D source, int linkType) + { + if (linkType < LinkTypes.ENUM_MIN || linkType > LinkTypes.ENUM_MAX) + { + throw new IllegalArgumentException("The specified link type is invalid."); + } + + this.parent = null; + this.source = source; + this.tail = new LinkTail(linkType, null); + this.children = new ArrayList(EXPECTED_CHILDREN); + } + + public Point4D source() + { + return source; + } + + public Point4D destination() + { + return tail.getDestination(); + } + + public boolean hasDestination() + { + return (tail.getDestination() != null); + } + + public Iterable children() + { + return children; + } + + public int childCount() + { + return children.size(); + } + + public DimLink parent() + { + return parent; + } + + public int linkType() + { + return tail.getLinkType(); + } + + public String toString() + { + return source + " -> " + (hasDestination() ? destination() : ""); + } +} diff --git a/StevenDimDoors/mod_pocketDim/core/IDimLink.java b/StevenDimDoors/mod_pocketDim/core/IDimLink.java deleted file mode 100644 index 4ed15b0..0000000 --- a/StevenDimDoors/mod_pocketDim/core/IDimLink.java +++ /dev/null @@ -1,27 +0,0 @@ -package StevenDimDoors.mod_pocketDim.core; - -import StevenDimDoors.mod_pocketDim.util.Point4D; - -public interface IDimLink -{ - public final int TYPE_ENUM_MIN = 0; - public final int TYPE_ENUM_MAX = 8; - - public final int TYPE_NORMAL = 0; - public final int TYPE_LIMBO = 1; - public final int TYPE_POCKET = 2; - public final int TYPE_DUNGEON = 3; - public final int TYPE_RANDOM = 4; - public final int TYPE_DUNGEON_EXIT = 5; - public final int TYPE_SAFE_EXIT = 6; - public final int TYPE_UNSAFE_EXIT = 7; - public final int TYPE_RANDOM_DUNGEON = 8; - - public Point4D source(); - public Point4D destination(); - public boolean hasDestination(); - public Iterable children(); - public int childCount(); - public IDimLink parent(); - public int linkType(); -} \ No newline at end of file diff --git a/StevenDimDoors/mod_pocketDim/core/LinkTypes.java b/StevenDimDoors/mod_pocketDim/core/LinkTypes.java new file mode 100644 index 0000000..b493e6c --- /dev/null +++ b/StevenDimDoors/mod_pocketDim/core/LinkTypes.java @@ -0,0 +1,19 @@ +package StevenDimDoors.mod_pocketDim.core; + +public class LinkTypes +{ + private LinkTypes() { } + + public static final int ENUM_MIN = 0; + public static final int ENUM_MAX = 8; + + public static final int NORMAL = 0; + public static final int LIMBO = 1; + public static final int POCKET = 2; + public static final int DUNGEON = 3; + public static final int RANDOM = 4; + public static final int DUNGEON_EXIT = 5; + public static final int SAFE_EXIT = 6; + public static final int UNSAFE_EXIT = 7; + public static final int RANDOM_DUNGEON = 8; +} diff --git a/StevenDimDoors/mod_pocketDim/core/NewDimData.java b/StevenDimDoors/mod_pocketDim/core/NewDimData.java index c7e0c2e..02ba5c4 100644 --- a/StevenDimDoors/mod_pocketDim/core/NewDimData.java +++ b/StevenDimDoors/mod_pocketDim/core/NewDimData.java @@ -1,4 +1,5 @@ package StevenDimDoors.mod_pocketDim.core; + import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -15,92 +16,29 @@ import StevenDimDoors.mod_pocketDim.watcher.IUpdateWatcher; public abstract class NewDimData { - private static class DimLink implements IDimLink + private static class InnerDimLink extends DimLink { - //DimLink is an inner class here to make it immutable to code outside NewDimData - - private static final int EXPECTED_CHILDREN = 2; - - private Point4D source; - private DimLink parent; - private LinkTail tail; - private ArrayList children; - - public DimLink(Point4D source, DimLink parent) + public InnerDimLink(Point4D source, DimLink parent) { - this.parent = parent; - this.source = source; - this.tail = parent.tail; - this.children = new ArrayList(EXPECTED_CHILDREN); - parent.children.add(this); + super(source, parent); } - public DimLink(Point4D source, int linkType) + public InnerDimLink(Point4D source, int linkType) { - if (linkType < IDimLink.TYPE_ENUM_MIN || linkType > IDimLink.TYPE_ENUM_MAX) - { - throw new IllegalArgumentException("The specified link type is invalid."); - } - - this.parent = null; - this.source = source; - this.tail = new LinkTail(linkType, null); - this.children = new ArrayList(EXPECTED_CHILDREN); + super(source, linkType); } - @Override - public Point4D source() - { - return source; - } - - @Override - public Point4D destination() - { - return tail.getDestination(); - } - - @Override - public boolean hasDestination() - { - return (tail.getDestination() != null); - } - public void setDestination(int x, int y, int z, NewDimData dimension) { tail.setDestination(new Point4D(x, y, z, dimension.id())); } - - @Override - public Iterable children() - { - return children; - } - - @Override - public int childCount() - { - return children.size(); - } - - @Override - public IDimLink parent() - { - return parent; - } - - @Override - public int linkType() - { - return tail.getLinkType(); - } public void clear() { //Release children - for (IDimLink child : children) + for (DimLink child : children) { - ((DimLink) child).parent = null; + ((InnerDimLink) child).parent = null; } children.clear(); @@ -115,7 +53,7 @@ public abstract class NewDimData tail = new LinkTail(0, null); } - public void overwrite(DimLink nextParent) + public boolean overwrite(InnerDimLink nextParent) { if (nextParent == null) { @@ -125,13 +63,13 @@ public abstract class NewDimData if (this == nextParent) { //Ignore this request silently - return; + return false; } //Release children - for (IDimLink child : children) + for (DimLink child : children) { - ((DimLink) child).parent = null; + ((InnerDimLink) child).parent = null; } children.clear(); @@ -145,14 +83,15 @@ public abstract class NewDimData parent = nextParent; tail = nextParent.tail; nextParent.children.add(this); + return true; } public void overwrite(int linkType) { //Release children - for (IDimLink child : children) + for (DimLink child : children) { - ((DimLink) child).parent = null; + ((InnerDimLink) child).parent = null; } children.clear(); @@ -166,12 +105,6 @@ public abstract class NewDimData parent = null; tail = new LinkTail(linkType, null); } - - @Override - public String toString() - { - return source + " -> " + (hasDestination() ? destination() : ""); - } public IOpaqueMessage toMessage() { @@ -187,8 +120,8 @@ public abstract class NewDimData private static Random random = new Random(); private final int id; - private final Map linkMapping; - private final List linkList; + private final Map linkMapping; + private final List linkList; private final boolean isDungeon; private boolean isFilled; private final int depth; @@ -216,8 +149,8 @@ public abstract class NewDimData } this.id = id; - this.linkMapping = new TreeMap(); //Should be stored in oct tree -- temporary solution - this.linkList = new ArrayList(); //Should be stored in oct tree -- temporary solution + this.linkMapping = new TreeMap(); //Should be stored in oct tree -- temporary solution + this.linkList = new ArrayList(); //Should be stored in oct tree -- temporary solution this.children = new ArrayList(); this.parent = parent; this.packDepth = 0; @@ -247,7 +180,7 @@ public abstract class NewDimData protected abstract IOpaqueMessage toMessage(); protected abstract IOpaqueMessage toKey(); - public IDimLink findNearestRift(World world, int range, int x, int y, int z) + public DimLink findNearestRift(World world, int range, int x, int y, int z) { //TODO: Rewrite this later to use an octtree @@ -260,8 +193,8 @@ public abstract class NewDimData //Note: Only detect rifts at a distance > 1, so we ignore the rift //that called this function and any adjacent rifts. - IDimLink nearest = null; - IDimLink link; + DimLink nearest = null; + DimLink link; int distance; int minDistance = Integer.MAX_VALUE; @@ -296,18 +229,18 @@ public abstract class NewDimData return Math.abs(i) + Math.abs(j) + Math.abs(k); } - public IDimLink createLink(int x, int y, int z, int linkType) + public DimLink createLink(int x, int y, int z, int linkType) { return createLink(new Point4D(x, y, z, id), linkType); } - private IDimLink createLink(Point4D source, int linkType) + private DimLink createLink(Point4D source, int linkType) { //Return an existing link if there is one to avoid creating multiple links starting at the same point. - DimLink link = linkMapping.get(source); + InnerDimLink link = linkMapping.get(source); if (link == null) { - link = new DimLink(source, linkType); + link = new InnerDimLink(source, linkType); linkMapping.put(source, link); linkList.add(link); } @@ -320,44 +253,49 @@ public abstract class NewDimData return link; } - public IDimLink createChildLink(int x, int y, int z, IDimLink parent) + public DimLink createChildLink(int x, int y, int z, DimLink parent) { if (parent == null) { throw new IllegalArgumentException("parent cannot be null."); } - return createChildLink(new Point4D(x, y, z, id), (DimLink) parent); + return createChildLink(new Point4D(x, y, z, id), (InnerDimLink) parent); } - private IDimLink createChildLink(Point4D source, DimLink parent) + private DimLink createChildLink(Point4D source, InnerDimLink parent) { //To avoid having multiple links at a single point, if we find an existing link then we overwrite //its destination data instead of creating a new instance. - DimLink link = linkMapping.get(source); + InnerDimLink link = linkMapping.get(source); if (link == null) { - link = new DimLink(source, parent); + link = new InnerDimLink(source, parent); linkMapping.put(source, link); linkList.add(link); + + //Link created! + linkWatcher.onCreated(link.toMessage()); } else { - link.overwrite(parent); + if (link.overwrite(parent)) + { + //Link created! + linkWatcher.onCreated(link.toMessage()); + } } - //Link created! - linkWatcher.onCreated(link.toMessage()); return link; } - public boolean deleteLink(IDimLink link) + public boolean deleteLink(DimLink link) { if (link.source().getDimension() != id) { throw new IllegalArgumentException("Attempted to delete a link from another dimension."); } - DimLink target = linkMapping.remove(link.source()); + InnerDimLink target = linkMapping.remove(link.source()); if (target != null) { linkList.remove(target); @@ -371,7 +309,7 @@ public abstract class NewDimData public boolean deleteLink(int x, int y, int z) { Point4D location = new Point4D(x, y, z, id); - DimLink target = linkMapping.remove(location); + InnerDimLink target = linkMapping.remove(location); if (target != null) { linkList.remove(target); @@ -382,13 +320,13 @@ public abstract class NewDimData return (target != null); } - public IDimLink getLink(int x, int y, int z) + public DimLink getLink(int x, int y, int z) { Point4D location = new Point4D(x, y, z, id); return linkMapping.get(location); } - public IDimLink getLink(Point4D location) + public DimLink getLink(Point4D location) { if (location.getDimension() != id) return null; @@ -396,9 +334,9 @@ public abstract class NewDimData return linkMapping.get(location); } - public ArrayList getAllLinks() + public ArrayList getAllLinks() { - ArrayList results = new ArrayList(linkMapping.size()); + ArrayList results = new ArrayList(linkMapping.size()); results.addAll(linkMapping.values()); return results; } @@ -480,7 +418,7 @@ public abstract class NewDimData return children; } - public void initializeDungeon(int originX, int originY, int originZ, int orientation, IDimLink incoming, DungeonData dungeon) + public void initializeDungeon(int originX, int originY, int originZ, int orientation, DimLink incoming, DungeonData dungeon) { if (!isDungeon) { @@ -532,7 +470,7 @@ public abstract class NewDimData } } - public void initializePocket(int originX, int originY, int originZ, int orientation, IDimLink incoming) + public void initializePocket(int originX, int originY, int originZ, int orientation, DimLink incoming) { if (!isPocketDimension()) { @@ -550,15 +488,15 @@ public abstract class NewDimData dimWatcher.onUpdated(this.toMessage()); } - public void setDestination(IDimLink incoming, int x, int y, int z) + public void setDestination(DimLink incoming, int x, int y, int z) { - DimLink link = (DimLink) incoming; + InnerDimLink link = (InnerDimLink) incoming; link.setDestination(x, y, z, this); //Raise update event linkWatcher.onUpdated(link.toMessage()); } - public IDimLink getRandomLink() + public DimLink getRandomLink() { if (linkMapping.isEmpty()) { diff --git a/StevenDimDoors/mod_pocketDim/core/PocketManager.java b/StevenDimDoors/mod_pocketDim/core/PocketManager.java index 3555915..92ff1e2 100644 --- a/StevenDimDoors/mod_pocketDim/core/PocketManager.java +++ b/StevenDimDoors/mod_pocketDim/core/PocketManager.java @@ -259,7 +259,7 @@ public class PocketManager //Does not actually unregister the rift data, see deleteRift for that. NewDimData dimension = getDimensionData(world); - IDimLink nearest = dimension.findNearestRift(world, range, x, y, z); + DimLink nearest = dimension.findNearestRift(world, range, x, y, z); if (nearest != null) { @@ -339,12 +339,12 @@ public class PocketManager return dimensionData.values(); } - public static IDimLink getLink(int x, int y, int z, World world) + public static DimLink getLink(int x, int y, int z, World world) { return getLink(x, y, z, world.provider.dimensionId); } - public static IDimLink getLink(int x, int y, int z, int dimensionID) + public static DimLink getLink(int x, int y, int z, int dimensionID) { NewDimData dimension = dimensionData.get(dimensionID); if (dimension != null) diff --git a/StevenDimDoors/mod_pocketDim/dungeon/DungeonSchematic.java b/StevenDimDoors/mod_pocketDim/dungeon/DungeonSchematic.java index 9426174..c118c17 100644 --- a/StevenDimDoors/mod_pocketDim/dungeon/DungeonSchematic.java +++ b/StevenDimDoors/mod_pocketDim/dungeon/DungeonSchematic.java @@ -17,7 +17,8 @@ import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import StevenDimDoors.mod_pocketDim.DDProperties; import StevenDimDoors.mod_pocketDim.Point3D; -import StevenDimDoors.mod_pocketDim.core.IDimLink; +import StevenDimDoors.mod_pocketDim.core.DimLink; +import StevenDimDoors.mod_pocketDim.core.LinkTypes; import StevenDimDoors.mod_pocketDim.core.NewDimData; import StevenDimDoors.mod_pocketDim.core.PocketManager; import StevenDimDoors.mod_pocketDim.schematic.BlockRotator; @@ -167,7 +168,7 @@ public class DungeonSchematic extends Schematic { return new DungeonSchematic(Schematic.copyFromWorld(world, x, y, z, width, height, length, doCompactBounds)); } - public void copyToWorld(World world, Point3D pocketCenter, int dungeonOrientation, IDimLink entryLink, Random random) + public void copyToWorld(World world, Point3D pocketCenter, int dungeonOrientation, DimLink entryLink, Random random) { //TODO: This function is an improvised solution so we can get the release moving. In the future, //we should generalize block transformations and implement support for them at the level of Schematic, @@ -224,7 +225,7 @@ public class DungeonSchematic extends Schematic { setUpDungeon(PocketManager.getDimensionData(world), world, pocketCenter, turnAngle, entryLink, random); } - private void setUpDungeon(NewDimData dimension, World world, Point3D pocketCenter, int turnAngle, IDimLink entryLink, Random random) + private void setUpDungeon(NewDimData dimension, World world, Point3D pocketCenter, int turnAngle, DimLink entryLink, Random random) { //Transform dungeon corners Point3D minCorner = new Point3D(0, 0, 0); @@ -282,9 +283,9 @@ public class DungeonSchematic extends Schematic { } } - private static void createEntranceReverseLink(NewDimData dimension, Point3D pocketCenter, IDimLink entryLink) + private static void createEntranceReverseLink(NewDimData dimension, Point3D pocketCenter, DimLink entryLink) { - IDimLink link = dimension.createLink(pocketCenter.getX(), pocketCenter.getY(), pocketCenter.getZ(), IDimLink.TYPE_NORMAL); + DimLink link = dimension.createLink(pocketCenter.getX(), pocketCenter.getY(), pocketCenter.getZ(), LinkTypes.NORMAL); Point4D destination = link.source(); NewDimData prevDim = PocketManager.getDimensionData(destination.getDimension()); prevDim.setDestination(link, destination.getX(), destination.getY(), destination.getZ()); @@ -295,7 +296,7 @@ public class DungeonSchematic extends Schematic { //Transform the door's location to the pocket coordinate system Point3D location = point.clone(); BlockRotator.transformPoint(location, entrance, rotation, pocketCenter); - dimension.createLink(location.getX(), location.getY(), location.getZ(), IDimLink.TYPE_DUNGEON_EXIT); + dimension.createLink(location.getX(), location.getY(), location.getZ(), LinkTypes.DUNGEON_EXIT); } private static void createDimensionalDoorLink(NewDimData dimension, Point3D point, Point3D entrance, int rotation, Point3D pocketCenter) @@ -303,7 +304,7 @@ public class DungeonSchematic extends Schematic { //Transform the door's location to the pocket coordinate system Point3D location = point.clone(); BlockRotator.transformPoint(location, entrance, rotation, pocketCenter); - dimension.createLink(location.getX(), location.getY(), location.getZ(), IDimLink.TYPE_DUNGEON); + dimension.createLink(location.getX(), location.getY(), location.getZ(), LinkTypes.DUNGEON); } private static void spawnMonolith(World world, Point3D point, Point3D entrance, int rotation, Point3D pocketCenter) diff --git a/StevenDimDoors/mod_pocketDim/helpers/DungeonHelper.java b/StevenDimDoors/mod_pocketDim/helpers/DungeonHelper.java index cc9cfdb..cebfefb 100644 --- a/StevenDimDoors/mod_pocketDim/helpers/DungeonHelper.java +++ b/StevenDimDoors/mod_pocketDim/helpers/DungeonHelper.java @@ -21,7 +21,8 @@ import net.minecraft.util.WeightedRandom; import net.minecraft.world.World; import StevenDimDoors.mod_pocketDim.DDProperties; import StevenDimDoors.mod_pocketDim.mod_pocketDim; -import StevenDimDoors.mod_pocketDim.core.IDimLink; +import StevenDimDoors.mod_pocketDim.core.DimLink; +import StevenDimDoors.mod_pocketDim.core.LinkTypes; import StevenDimDoors.mod_pocketDim.core.NewDimData; import StevenDimDoors.mod_pocketDim.core.PocketManager; import StevenDimDoors.mod_pocketDim.dungeon.DungeonData; @@ -274,11 +275,11 @@ public class DungeonHelper return pack; } - public IDimLink createCustomDungeonDoor(World world, int x, int y, int z) + public DimLink createCustomDungeonDoor(World world, int x, int y, int z) { //Create a link above the specified position. Link to a new pocket dimension. NewDimData dimension = PocketManager.getDimensionData(world); - IDimLink link = dimension.createLink(x, y + 1, z, IDimLink.TYPE_POCKET); + DimLink link = dimension.createLink(x, y + 1, z, LinkTypes.POCKET); //Place a Warp Door linked to that pocket ItemDimensionalDoor.placeDoorBlock(world, x, y, z, 3, mod_pocketDim.warpDoor); diff --git a/StevenDimDoors/mod_pocketDim/items/BaseItemDoor.java b/StevenDimDoors/mod_pocketDim/items/BaseItemDoor.java index 88600bc..9517b95 100644 --- a/StevenDimDoors/mod_pocketDim/items/BaseItemDoor.java +++ b/StevenDimDoors/mod_pocketDim/items/BaseItemDoor.java @@ -15,7 +15,7 @@ import net.minecraft.util.Vec3; import net.minecraft.world.World; import StevenDimDoors.mod_pocketDim.DDProperties; import StevenDimDoors.mod_pocketDim.mod_pocketDim; -import StevenDimDoors.mod_pocketDim.core.IDimLink; +import StevenDimDoors.mod_pocketDim.core.DimLink; import StevenDimDoors.mod_pocketDim.core.PocketManager; public abstract class BaseItemDoor extends ItemDoor @@ -114,7 +114,7 @@ public abstract class BaseItemDoor extends ItemDoor { if (world.getBlockId(hit.blockX, hit.blockY, hit.blockZ) == properties.RiftBlockID) { - IDimLink link = PocketManager.getLink(hit.blockX, hit.blockY, hit.blockZ, world.provider.dimensionId); + DimLink link = PocketManager.getLink(hit.blockX, hit.blockY, hit.blockZ, world.provider.dimensionId); if (link != null) { int x = hit.blockX; diff --git a/StevenDimDoors/mod_pocketDim/items/ItemRiftBlade.java b/StevenDimDoors/mod_pocketDim/items/ItemRiftBlade.java index c6eec69..ba49480 100644 --- a/StevenDimDoors/mod_pocketDim/items/ItemRiftBlade.java +++ b/StevenDimDoors/mod_pocketDim/items/ItemRiftBlade.java @@ -20,7 +20,7 @@ import net.minecraft.util.Vec3; import net.minecraft.world.World; import StevenDimDoors.mod_pocketDim.DDProperties; import StevenDimDoors.mod_pocketDim.mod_pocketDim; -import StevenDimDoors.mod_pocketDim.core.IDimLink; +import StevenDimDoors.mod_pocketDim.core.LinkTypes; import StevenDimDoors.mod_pocketDim.core.NewDimData; import StevenDimDoors.mod_pocketDim.core.PocketManager; import cpw.mods.fml.relauncher.Side; @@ -187,7 +187,7 @@ public class ItemRiftBlade extends ItemSword NewDimData dimension = PocketManager.getDimensionData(world); if (!dimension.isPocketDimension() && dimension.getLink(x, y + 1, z) == null) { - dimension.createLink(x, y + 1, z, IDimLink.TYPE_POCKET); + dimension.createLink(x, y + 1, z, LinkTypes.POCKET); player.worldObj.playSoundAtEntity(player,"mods.DimDoors.sfx.riftDoor", 0.6f, 1); ItemDimensionalDoor.placeDoorBlock(world, x, y, z, orientation, mod_pocketDim.transientDoor); } diff --git a/StevenDimDoors/mod_pocketDim/items/ItemRiftSignature.java b/StevenDimDoors/mod_pocketDim/items/ItemRiftSignature.java index 36c2757..088b93d 100644 --- a/StevenDimDoors/mod_pocketDim/items/ItemRiftSignature.java +++ b/StevenDimDoors/mod_pocketDim/items/ItemRiftSignature.java @@ -10,7 +10,8 @@ import net.minecraft.nbt.NBTTagCompound; import net.minecraft.world.World; import net.minecraftforge.common.DimensionManager; import StevenDimDoors.mod_pocketDim.mod_pocketDim; -import StevenDimDoors.mod_pocketDim.core.IDimLink; +import StevenDimDoors.mod_pocketDim.core.DimLink; +import StevenDimDoors.mod_pocketDim.core.LinkTypes; import StevenDimDoors.mod_pocketDim.core.NewDimData; import StevenDimDoors.mod_pocketDim.core.PocketManager; import StevenDimDoors.mod_pocketDim.util.Point4D; @@ -66,8 +67,8 @@ public class ItemRiftSignature extends Item //The link was used before and already has an endpoint stored. Create links connecting the two endpoints. NewDimData sourceDimension = PocketManager.getDimensionData(source.getDimension()); NewDimData destinationDimension = PocketManager.getDimensionData(world); - IDimLink link = sourceDimension.createLink(source.getX(), source.getY(), source.getZ(), IDimLink.TYPE_NORMAL); - IDimLink reverse = destinationDimension.createLink(x, y, z, IDimLink.TYPE_NORMAL); + DimLink link = sourceDimension.createLink(source.getX(), source.getY(), source.getZ(), LinkTypes.NORMAL); + DimLink reverse = destinationDimension.createLink(x, y, z, LinkTypes.NORMAL); destinationDimension.setDestination(link, x, y, z); sourceDimension.setDestination(reverse, source.getX(), source.getY(), source.getZ()); diff --git a/StevenDimDoors/mod_pocketDim/ticking/RiftRegenerator.java b/StevenDimDoors/mod_pocketDim/ticking/RiftRegenerator.java index 950c505..4c82dc1 100644 --- a/StevenDimDoors/mod_pocketDim/ticking/RiftRegenerator.java +++ b/StevenDimDoors/mod_pocketDim/ticking/RiftRegenerator.java @@ -4,7 +4,7 @@ import net.minecraft.world.World; import net.minecraftforge.common.DimensionManager; import StevenDimDoors.mod_pocketDim.DDProperties; import StevenDimDoors.mod_pocketDim.mod_pocketDim; -import StevenDimDoors.mod_pocketDim.core.IDimLink; +import StevenDimDoors.mod_pocketDim.core.DimLink; import StevenDimDoors.mod_pocketDim.core.NewDimData; import StevenDimDoors.mod_pocketDim.core.PocketManager; import StevenDimDoors.mod_pocketDim.tileentities.TileEntityRift; @@ -46,7 +46,7 @@ public class RiftRegenerator implements IRegularTickReceiver { { for (int count = 0; count < RIFTS_REGENERATED_PER_DIMENSION; count++) { - IDimLink link = dimension.getRandomLink(); + DimLink link = dimension.getRandomLink(); Point4D source = link.source(); if (!mod_pocketDim.blockRift.isBlockImmune(world, source.getX(), source.getY(), source.getZ())) { diff --git a/StevenDimDoors/mod_pocketDim/tileentities/TileEntityRift.java b/StevenDimDoors/mod_pocketDim/tileentities/TileEntityRift.java index dfa27b5..fc74f69 100644 --- a/StevenDimDoors/mod_pocketDim/tileentities/TileEntityRift.java +++ b/StevenDimDoors/mod_pocketDim/tileentities/TileEntityRift.java @@ -17,7 +17,7 @@ import net.minecraft.tileentity.TileEntity; import net.minecraft.util.AxisAlignedBB; import net.minecraft.util.MathHelper; import StevenDimDoors.mod_pocketDim.mod_pocketDim; -import StevenDimDoors.mod_pocketDim.core.IDimLink; +import StevenDimDoors.mod_pocketDim.core.DimLink; import StevenDimDoors.mod_pocketDim.core.NewDimData; import StevenDimDoors.mod_pocketDim.core.PocketManager; import StevenDimDoors.mod_pocketDim.util.Point4D; @@ -38,7 +38,7 @@ public class TileEntityRift extends TileEntity public int age = 0; public HashMap renderingCenters = new HashMap(); - public IDimLink nearestRiftData; + public DimLink nearestRiftData; public int spawnedEndermenID=0; DataWatcher watcher = new DataWatcher(); @@ -240,7 +240,7 @@ public class TileEntityRift extends TileEntity if (growCount < 100) { NewDimData dimension = PocketManager.getDimensionData(worldObj); - IDimLink link = dimension.getLink(xCoord, yCoord, zCoord); + DimLink link = dimension.getLink(xCoord, yCoord, zCoord); if (link != null) { if (!this.hasGrownRifts && random.nextInt(3) == 0) diff --git a/StevenDimDoors/mod_pocketDim/world/GatewayGenerator.java b/StevenDimDoors/mod_pocketDim/world/GatewayGenerator.java index 2a2e0f0..bc52bf7 100644 --- a/StevenDimDoors/mod_pocketDim/world/GatewayGenerator.java +++ b/StevenDimDoors/mod_pocketDim/world/GatewayGenerator.java @@ -9,7 +9,8 @@ import net.minecraft.world.chunk.IChunkProvider; import net.minecraftforge.common.DimensionManager; import StevenDimDoors.mod_pocketDim.DDProperties; import StevenDimDoors.mod_pocketDim.mod_pocketDim; -import StevenDimDoors.mod_pocketDim.core.IDimLink; +import StevenDimDoors.mod_pocketDim.core.DimLink; +import StevenDimDoors.mod_pocketDim.core.LinkTypes; import StevenDimDoors.mod_pocketDim.core.NewDimData; import StevenDimDoors.mod_pocketDim.core.PocketManager; import StevenDimDoors.mod_pocketDim.items.ItemDimensionalDoor; @@ -57,7 +58,7 @@ public class GatewayGenerator implements IWorldGenerator int attempts; int correction; boolean valid; - IDimLink link; + DimLink link; NewDimData dimension; //Check if we're generating things in the Nether @@ -97,7 +98,7 @@ public class GatewayGenerator implements IWorldGenerator if (link == null) { dimension = PocketManager.getDimensionData(world); - link = dimension.createLink(x, y + 1, z, IDimLink.TYPE_POCKET); + link = dimension.createLink(x, y + 1, z, LinkTypes.POCKET); } else { @@ -132,7 +133,7 @@ public class GatewayGenerator implements IWorldGenerator { //Create a partial link to a dungeon. dimension = PocketManager.getDimensionData(world); - link = dimension.createLink(x, y + 1, z, IDimLink.TYPE_DUNGEON); + link = dimension.createLink(x, y + 1, z, LinkTypes.DUNGEON); //If the current dimension isn't Limbo, build a Rift Gateway out of Stone Bricks if (dimension.id() != properties.LimboDimensionID) diff --git a/StevenDimDoors/mod_pocketDim/world/PocketBuilder.java b/StevenDimDoors/mod_pocketDim/world/PocketBuilder.java index cc524fd..1f9aa7b 100644 --- a/StevenDimDoors/mod_pocketDim/world/PocketBuilder.java +++ b/StevenDimDoors/mod_pocketDim/world/PocketBuilder.java @@ -10,7 +10,7 @@ import net.minecraft.world.chunk.storage.ExtendedBlockStorage; import net.minecraftforge.common.DimensionManager; import StevenDimDoors.mod_pocketDim.DDProperties; import StevenDimDoors.mod_pocketDim.Point3D; -import StevenDimDoors.mod_pocketDim.core.IDimLink; +import StevenDimDoors.mod_pocketDim.core.DimLink; import StevenDimDoors.mod_pocketDim.core.NewDimData; import StevenDimDoors.mod_pocketDim.core.PocketManager; import StevenDimDoors.mod_pocketDim.dungeon.DungeonData; @@ -36,7 +36,7 @@ public class PocketBuilder private PocketBuilder() { } - public static boolean generateNewDungeonPocket(IDimLink link, DDProperties properties) + public static boolean generateNewDungeonPocket(DimLink link, DDProperties properties) { if (link == null) { @@ -214,7 +214,7 @@ public class PocketBuilder schematic.getLength() <= DungeonHelper.MAX_DUNGEON_LENGTH); } - public static boolean generateNewPocket(IDimLink link, DDProperties properties) + public static boolean generateNewPocket(DimLink link, DDProperties properties) { return generateNewPocket(link, DEFAULT_POCKET_SIZE, DEFAULT_POCKET_WALL_THICKNESS, properties); } @@ -240,7 +240,7 @@ public class PocketBuilder return orientation; } - public static boolean generateNewPocket(IDimLink link, int size, int wallThickness, DDProperties properties) + public static boolean generateNewPocket(DimLink link, int size, int wallThickness, DDProperties properties) { if (link == null) { -- 2.39.5 From 307d2258d14f4ca2f51e28db403ec9a7fac6db8b Mon Sep 17 00:00:00 2001 From: SenseiKiwi Date: Mon, 2 Sep 2013 16:51:20 -0400 Subject: [PATCH 2/8] Progress on Rewriting Packet Handling Continued building a system for transferring the complete internal states of our dimensions from the server to the client. However, Steven suggested that clients only need minimal data to operate properly, as opposed to the server. My motivation for this more complicated system was the concern that minimal information wouldn't be enough. I'm going to commit my progress, then tear it down and write a much simpler version. --- .../mod_pocketDim/ConnectionHandler.java | 4 +- .../mod_pocketDim/ServerPacketHandler.java | 18 +-- .../mod_pocketDim/core/DimLink.java | 4 +- .../mod_pocketDim/core/NewDimData.java | 18 +-- .../mod_pocketDim/core/PocketManager.java | 30 ++-- .../messages/DimMessageBuilder.java | 116 +++++++++++++++ .../mod_pocketDim/messages/IDataMessage.java | 9 ++ .../messages/IMessageBuilder.java | 11 ++ .../messages/IUpdateWatcher.java | 8 ++ .../messages/LinkMessageBuilder.java | 134 ++++++++++++++++++ .../UpdateWatcherProxy.java | 8 +- .../mod_pocketDim/util/Point4D.java | 28 ++++ .../mod_pocketDim/watcher/IOpaqueMessage.java | 8 -- .../mod_pocketDim/watcher/IOpaqueReader.java | 8 -- .../mod_pocketDim/watcher/IUpdateWatcher.java | 8 -- 15 files changed, 348 insertions(+), 64 deletions(-) create mode 100644 StevenDimDoors/mod_pocketDim/messages/DimMessageBuilder.java create mode 100644 StevenDimDoors/mod_pocketDim/messages/IDataMessage.java create mode 100644 StevenDimDoors/mod_pocketDim/messages/IMessageBuilder.java create mode 100644 StevenDimDoors/mod_pocketDim/messages/IUpdateWatcher.java create mode 100644 StevenDimDoors/mod_pocketDim/messages/LinkMessageBuilder.java rename StevenDimDoors/mod_pocketDim/{watcher => messages}/UpdateWatcherProxy.java (79%) delete mode 100644 StevenDimDoors/mod_pocketDim/watcher/IOpaqueMessage.java delete mode 100644 StevenDimDoors/mod_pocketDim/watcher/IOpaqueReader.java delete mode 100644 StevenDimDoors/mod_pocketDim/watcher/IUpdateWatcher.java diff --git a/StevenDimDoors/mod_pocketDim/ConnectionHandler.java b/StevenDimDoors/mod_pocketDim/ConnectionHandler.java index 0b13248..2abf8d3 100644 --- a/StevenDimDoors/mod_pocketDim/ConnectionHandler.java +++ b/StevenDimDoors/mod_pocketDim/ConnectionHandler.java @@ -11,7 +11,7 @@ import net.minecraft.network.packet.Packet1Login; import net.minecraft.network.packet.Packet250CustomPayload; import net.minecraft.server.MinecraftServer; import StevenDimDoors.mod_pocketDim.core.PocketManager; -import StevenDimDoors.mod_pocketDim.watcher.IOpaqueMessage; +import StevenDimDoors.mod_pocketDim.messages.IDataMessage; import cpw.mods.fml.common.network.IConnectionHandler; import cpw.mods.fml.common.network.Player; @@ -41,7 +41,7 @@ public class ConnectionHandler implements IConnectionHandler //Send information about all the registered dimensions and links to the client try { - IOpaqueMessage message = PocketManager.getState(); + IDataMessage message = PocketManager.getState(); Packet250CustomPayload packet = new Packet250CustomPayload(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); DataOutputStream writer = new DataOutputStream(buffer); diff --git a/StevenDimDoors/mod_pocketDim/ServerPacketHandler.java b/StevenDimDoors/mod_pocketDim/ServerPacketHandler.java index 310b84a..d33c613 100644 --- a/StevenDimDoors/mod_pocketDim/ServerPacketHandler.java +++ b/StevenDimDoors/mod_pocketDim/ServerPacketHandler.java @@ -7,8 +7,8 @@ import java.io.IOException; import net.minecraft.network.INetworkManager; import net.minecraft.network.packet.Packet250CustomPayload; import StevenDimDoors.mod_pocketDim.core.PocketManager; -import StevenDimDoors.mod_pocketDim.watcher.IOpaqueMessage; -import StevenDimDoors.mod_pocketDim.watcher.IUpdateWatcher; +import StevenDimDoors.mod_pocketDim.messages.IDataMessage; +import StevenDimDoors.mod_pocketDim.messages.IUpdateWatcher; import cpw.mods.fml.common.network.IPacketHandler; import cpw.mods.fml.common.network.PacketDispatcher; import cpw.mods.fml.common.network.Player; @@ -30,19 +30,19 @@ public class ServerPacketHandler implements IPacketHandler private class DimWatcher implements IUpdateWatcher { @Override - public void onCreated(IOpaqueMessage message) + public void onCreated(IDataMessage message) { sendMessageToAllPlayers(PacketConstants.CREATE_DIM_PACKET_ID, message); } @Override - public void onUpdated(IOpaqueMessage message) + public void onUpdated(IDataMessage message) { sendMessageToAllPlayers(PacketConstants.UPDATE_DIM_PACKET_ID, message); } @Override - public void onDeleted(IOpaqueMessage message) + public void onDeleted(IDataMessage message) { sendMessageToAllPlayers(PacketConstants.DELETE_DIM_PACKET_ID, message); } @@ -51,25 +51,25 @@ public class ServerPacketHandler implements IPacketHandler private class LinkWatcher implements IUpdateWatcher { @Override - public void onCreated(IOpaqueMessage message) + public void onCreated(IDataMessage message) { sendMessageToAllPlayers(PacketConstants.CREATE_LINK_PACKET_ID, message); } @Override - public void onUpdated(IOpaqueMessage message) + public void onUpdated(IDataMessage message) { sendMessageToAllPlayers(PacketConstants.UPDATE_LINK_PACKET_ID, message); } @Override - public void onDeleted(IOpaqueMessage message) + public void onDeleted(IDataMessage message) { sendMessageToAllPlayers(PacketConstants.DELETE_LINK_PACKET_ID, message); } } - private static void sendMessageToAllPlayers(byte id, IOpaqueMessage message) + private static void sendMessageToAllPlayers(byte id, IDataMessage message) { try { diff --git a/StevenDimDoors/mod_pocketDim/core/DimLink.java b/StevenDimDoors/mod_pocketDim/core/DimLink.java index c0cf6de..a8c296b 100644 --- a/StevenDimDoors/mod_pocketDim/core/DimLink.java +++ b/StevenDimDoors/mod_pocketDim/core/DimLink.java @@ -13,7 +13,7 @@ public abstract class DimLink protected LinkTail tail; protected ArrayList children; - public DimLink(Point4D source, DimLink parent) + protected DimLink(Point4D source, DimLink parent) { this.parent = parent; this.source = source; @@ -22,7 +22,7 @@ public abstract class DimLink parent.children.add(this); } - public DimLink(Point4D source, int linkType) + protected DimLink(Point4D source, int linkType) { if (linkType < LinkTypes.ENUM_MIN || linkType > LinkTypes.ENUM_MAX) { diff --git a/StevenDimDoors/mod_pocketDim/core/NewDimData.java b/StevenDimDoors/mod_pocketDim/core/NewDimData.java index 02ba5c4..7ff3160 100644 --- a/StevenDimDoors/mod_pocketDim/core/NewDimData.java +++ b/StevenDimDoors/mod_pocketDim/core/NewDimData.java @@ -10,9 +10,10 @@ import net.minecraft.world.World; import StevenDimDoors.mod_pocketDim.DDProperties; import StevenDimDoors.mod_pocketDim.dungeon.DungeonData; import StevenDimDoors.mod_pocketDim.dungeon.pack.DungeonPack; +import StevenDimDoors.mod_pocketDim.messages.IDataMessage; +import StevenDimDoors.mod_pocketDim.messages.IUpdateWatcher; +import StevenDimDoors.mod_pocketDim.messages.LinkMessageBuilder; import StevenDimDoors.mod_pocketDim.util.Point4D; -import StevenDimDoors.mod_pocketDim.watcher.IOpaqueMessage; -import StevenDimDoors.mod_pocketDim.watcher.IUpdateWatcher; public abstract class NewDimData { @@ -106,17 +107,18 @@ public abstract class NewDimData tail = new LinkTail(linkType, null); } - public IOpaqueMessage toMessage() + public IDataMessage toMessage() { - return null; + return linkMessageBuilder.createMessage(this); } - public IOpaqueMessage toKey() + public IDataMessage toKey() { - return null; + return linkMessageBuilder.createKey(this); } } + private static LinkMessageBuilder linkMessageBuilder = new LinkMessageBuilder(); private static Random random = new Random(); private final int id; @@ -177,8 +179,8 @@ public abstract class NewDimData } } - protected abstract IOpaqueMessage toMessage(); - protected abstract IOpaqueMessage toKey(); + protected abstract IDataMessage toMessage(); + protected abstract IDataMessage toKey(); public DimLink findNearestRift(World world, int range, int x, int y, int z) { diff --git a/StevenDimDoors/mod_pocketDim/core/PocketManager.java b/StevenDimDoors/mod_pocketDim/core/PocketManager.java index 92ff1e2..91f90ca 100644 --- a/StevenDimDoors/mod_pocketDim/core/PocketManager.java +++ b/StevenDimDoors/mod_pocketDim/core/PocketManager.java @@ -14,18 +14,19 @@ import net.minecraft.world.World; import net.minecraftforge.common.DimensionManager; import StevenDimDoors.mod_pocketDim.DDProperties; import StevenDimDoors.mod_pocketDim.helpers.DeleteFolder; +import StevenDimDoors.mod_pocketDim.messages.DimMessageBuilder; +import StevenDimDoors.mod_pocketDim.messages.IDataMessage; +import StevenDimDoors.mod_pocketDim.messages.IUpdateWatcher; +import StevenDimDoors.mod_pocketDim.messages.UpdateWatcherProxy; import StevenDimDoors.mod_pocketDim.tileentities.TileEntityRift; import StevenDimDoors.mod_pocketDim.util.Point4D; -import StevenDimDoors.mod_pocketDim.watcher.IOpaqueMessage; -import StevenDimDoors.mod_pocketDim.watcher.IUpdateWatcher; -import StevenDimDoors.mod_pocketDim.watcher.UpdateWatcherProxy; /** * This class regulates all the operations involving the storage and manipulation of dimensions. It handles saving dim data, teleporting the player, and * creating/registering new dimensions as well as loading old dimensions on startup */ public class PocketManager -{ +{ private static class InnerDimData extends NewDimData { //This inner class allows us to instantiate NewDimData indirectly without exposing @@ -40,22 +41,21 @@ public class PocketManager } @Override - protected IOpaqueMessage toMessage() + protected IDataMessage toMessage() { - // TODO Auto-generated method stub - return null; + return dimMessageBuilder.createMessage(this); } @Override - protected IOpaqueMessage toKey() + protected IDataMessage toKey() { - // TODO Auto-generated method stub - return null; + return dimMessageBuilder.createKey(this); } } - + + private static DimMessageBuilder dimMessageBuilder = new DimMessageBuilder(); private static int OVERWORLD_DIMENSION_ID = 0; - + private static volatile boolean isLoading = false; private static volatile boolean isLoaded = false; private static volatile boolean isSaving = false; @@ -198,7 +198,7 @@ public class PocketManager System.out.println("Loading Dimensional Doors save data..."); File saveFile = new File(DimensionManager.getCurrentSaveRootDirectory() + "/dimdoors.dat"); //Missing code for converting the binary data in the file into an IOpaqueMessage - IOpaqueMessage saveData; + IDataMessage saveData; setState(saveData); System.out.println("Loaded successfully!"); } @@ -377,12 +377,12 @@ public class PocketManager return linkWatcher.unregisterReceiver(watcher); } - public static IOpaqueMessage getState() + public static IDataMessage getState() { } - public static void setState(IOpaqueMessage state) + public static void setState(IDataMessage state) { if (isLoaded) { diff --git a/StevenDimDoors/mod_pocketDim/messages/DimMessageBuilder.java b/StevenDimDoors/mod_pocketDim/messages/DimMessageBuilder.java new file mode 100644 index 0000000..cb46451 --- /dev/null +++ b/StevenDimDoors/mod_pocketDim/messages/DimMessageBuilder.java @@ -0,0 +1,116 @@ +package StevenDimDoors.mod_pocketDim.messages; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.List; +import java.util.Map; + +import StevenDimDoors.mod_pocketDim.core.DimLink; +import StevenDimDoors.mod_pocketDim.core.NewDimData; +import StevenDimDoors.mod_pocketDim.core.NewDimData.InnerDimLink; +import StevenDimDoors.mod_pocketDim.dungeon.DungeonData; +import StevenDimDoors.mod_pocketDim.util.Point4D; + +import com.google.common.collect.ImmutableList; + +public class DimMessageBuilder implements IMessageBuilder +{ + public static class DimMessage implements IDataMessage + { + //We'll use public fields here since this is a data container object and all the fields are immutable + //We will not transfer dungeon, link data, or any data on child dimensions + //As far as I can tell, the children will handle updating their parents anyway + + public final int ID; + public final boolean IsDungeon; + public final boolean IsFilled; + public final int Depth; + public final int PackDepth; + public final Integer ParentID; + public final int RootID; + public final Point4D Origin; + public final int Orientation; + + private DimMessage(NewDimData dimension) + { + ID = dimension.id(); + IsDungeon = dimension.isDungeon(); + IsFilled = dimension.isFilled(); + Depth = dimension.depth(); + PackDepth = dimension.packDepth(); + ParentID = (dimension.parent() != null) ? dimension.parent().id() : null; + RootID = dimension.root().id(); + Origin = dimension.origin(); + Orientation = dimension.orientation(); + } + + private DimMessage(DataInputStream stream) throws IOException + { + ID = stream.readInt(); + IsDungeon = stream.readBoolean(); + IsFilled = stream.readBoolean(); + Depth = stream.readInt(); + PackDepth = stream.readInt(); + ParentID = stream. + } + + @Override + public void writeToStream(DataOutputStream stream) throws IOException + { + //Write a flag indicating that this is a full message and not a key + stream.writeBoolean(true); + + } + } + + public static class DimKeyMessage implements IDataMessage + { + //We'll use public fields here since this is a data container object and all the fields are immutable + public final int ID; + + private DimKeyMessage(NewDimData dimension) + { + ID = dimension.id(); + } + + private DimKeyMessage(DataInputStream stream) throws IOException + { + ID = stream.readInt(); + } + + @Override + public void writeToStream(DataOutputStream stream) throws IOException + { + //Write a flag indicating that this is a key + stream.writeBoolean(false); + stream.writeInt(ID); + } + } + + @Override + public IDataMessage createKey(NewDimData target) + { + return new DimKeyMessage(target); + } + + @Override + public IDataMessage createMessage(NewDimData target) + { + return new DimMessage(target); + } + + @Override + public IDataMessage read(DataInputStream source) throws IOException + { + //Check whether the message is a full message or just a key + if (source.readBoolean()) + { + return new DimMessage(source); + } + else + { + return new DimKeyMessage(source); + } + } +} diff --git a/StevenDimDoors/mod_pocketDim/messages/IDataMessage.java b/StevenDimDoors/mod_pocketDim/messages/IDataMessage.java new file mode 100644 index 0000000..8ee841d --- /dev/null +++ b/StevenDimDoors/mod_pocketDim/messages/IDataMessage.java @@ -0,0 +1,9 @@ +package StevenDimDoors.mod_pocketDim.messages; + +import java.io.DataOutputStream; +import java.io.IOException; + +public interface IDataMessage +{ + public void writeToStream(DataOutputStream stream) throws IOException; +} diff --git a/StevenDimDoors/mod_pocketDim/messages/IMessageBuilder.java b/StevenDimDoors/mod_pocketDim/messages/IMessageBuilder.java new file mode 100644 index 0000000..297f8e5 --- /dev/null +++ b/StevenDimDoors/mod_pocketDim/messages/IMessageBuilder.java @@ -0,0 +1,11 @@ +package StevenDimDoors.mod_pocketDim.messages; + +import java.io.DataInputStream; +import java.io.IOException; + +public interface IMessageBuilder +{ + public IDataMessage createKey(T target); + public IDataMessage createMessage(T target); + public IDataMessage read(DataInputStream source) throws IOException; +} diff --git a/StevenDimDoors/mod_pocketDim/messages/IUpdateWatcher.java b/StevenDimDoors/mod_pocketDim/messages/IUpdateWatcher.java new file mode 100644 index 0000000..3bc24fd --- /dev/null +++ b/StevenDimDoors/mod_pocketDim/messages/IUpdateWatcher.java @@ -0,0 +1,8 @@ +package StevenDimDoors.mod_pocketDim.messages; + +public interface IUpdateWatcher +{ + public void onCreated(IDataMessage message); + public void onUpdated(IDataMessage message); + public void onDeleted(IDataMessage message); +} diff --git a/StevenDimDoors/mod_pocketDim/messages/LinkMessageBuilder.java b/StevenDimDoors/mod_pocketDim/messages/LinkMessageBuilder.java new file mode 100644 index 0000000..d9d21bc --- /dev/null +++ b/StevenDimDoors/mod_pocketDim/messages/LinkMessageBuilder.java @@ -0,0 +1,134 @@ +package StevenDimDoors.mod_pocketDim.messages; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import StevenDimDoors.mod_pocketDim.core.DimLink; +import StevenDimDoors.mod_pocketDim.util.Point4D; + +import com.google.common.collect.ImmutableList; + +public class LinkMessageBuilder implements IMessageBuilder +{ + public static class LinkMessage implements IDataMessage + { + //We'll use public fields here since this is a data container object and all the fields are immutable + public final Point4D Source; + public final Point4D Destination; + public final int LinkType; + public final Point4D Parent; + public final ImmutableList Children; + + private LinkMessage(DimLink link) + { + // TODO: In the case that a child's parent has been removed but the rest of the group still exists, + // this group bond will be lost to this link on the client side. Currently, that's not a problem since + // destination data and groups don't matter to the client, but it's something to think about later. + + Source = link.source(); + Destination = link.destination(); + LinkType = link.linkType(); + Parent = link.parent().source(); + ImmutableList.Builder builder = new ImmutableList.Builder(); + for (DimLink child : link.children()) + { + builder.add(child.source()); + } + Children = builder.build(); + } + + private LinkMessage(DataInputStream stream) throws IOException + { + Source = Point4D.read(stream); + Parent = Point4D.read(stream); + if (Parent == null) + { + Destination = Point4D.read(stream); + LinkType = stream.readInt(); + } + else + { + Destination = null; + LinkType = -1; + } + int childCount = stream.readInt(); + ImmutableList.Builder builder = new ImmutableList.Builder(); + for (int k = 0; k < childCount; k++) + { + builder.add(Point4D.read(stream)); + } + Children = builder.build(); + } + + @Override + public void writeToStream(DataOutputStream stream) throws IOException + { + //Write a flag indicating that this is a full message and not a key + stream.writeBoolean(true); + Point4D.write(Source, stream); + Point4D.write(Parent, stream); + //A link only has its own destination information if it has no parent to provide it + if (Parent == null) + { + Point4D.write(Destination, stream); + stream.writeInt(LinkType); + } + stream.writeInt(Children.size()); + for (Point4D child : Children) + { + Point4D.write(child, stream); + } + } + } + + public static class LinkKeyMessage implements IDataMessage + { + //We'll use public fields here since this is a data container object and all the fields are immutable + public final Point4D Source; + + private LinkKeyMessage(DimLink link) + { + Source = link.source(); + } + + private LinkKeyMessage(DataInputStream stream) throws IOException + { + Source = Point4D.read(stream); + } + + @Override + public void writeToStream(DataOutputStream stream) throws IOException + { + //Write a flag indicating that this is a key + stream.writeBoolean(false); + Point4D.write(Source, stream); + } + } + + @Override + public IDataMessage createKey(DimLink target) + { + return new LinkKeyMessage(target); + } + + @Override + public IDataMessage createMessage(DimLink target) + { + return new LinkMessage(target); + } + + @Override + public IDataMessage read(DataInputStream source) throws IOException + { + //Check whether the message is a full message or just a key + if (source.readBoolean()) + { + return new LinkMessage(source); + } + else + { + return new LinkKeyMessage(source); + } + } +} diff --git a/StevenDimDoors/mod_pocketDim/watcher/UpdateWatcherProxy.java b/StevenDimDoors/mod_pocketDim/messages/UpdateWatcherProxy.java similarity index 79% rename from StevenDimDoors/mod_pocketDim/watcher/UpdateWatcherProxy.java rename to StevenDimDoors/mod_pocketDim/messages/UpdateWatcherProxy.java index 49874f2..1326151 100644 --- a/StevenDimDoors/mod_pocketDim/watcher/UpdateWatcherProxy.java +++ b/StevenDimDoors/mod_pocketDim/messages/UpdateWatcherProxy.java @@ -1,4 +1,4 @@ -package StevenDimDoors.mod_pocketDim.watcher; +package StevenDimDoors.mod_pocketDim.messages; import java.util.ArrayList; import java.util.List; @@ -13,7 +13,7 @@ public class UpdateWatcherProxy implements IUpdateWatcher } @Override - public void onCreated(IOpaqueMessage message) + public void onCreated(IDataMessage message) { for (IUpdateWatcher receiver : watchers) { @@ -22,7 +22,7 @@ public class UpdateWatcherProxy implements IUpdateWatcher } @Override - public void onUpdated(IOpaqueMessage message) + public void onUpdated(IDataMessage message) { for (IUpdateWatcher receiver : watchers) { @@ -31,7 +31,7 @@ public class UpdateWatcherProxy implements IUpdateWatcher } @Override - public void onDeleted(IOpaqueMessage message) + public void onDeleted(IDataMessage message) { for (IUpdateWatcher receiver : watchers) { diff --git a/StevenDimDoors/mod_pocketDim/util/Point4D.java b/StevenDimDoors/mod_pocketDim/util/Point4D.java index e8c6214..08164a8 100644 --- a/StevenDimDoors/mod_pocketDim/util/Point4D.java +++ b/StevenDimDoors/mod_pocketDim/util/Point4D.java @@ -1,5 +1,9 @@ package StevenDimDoors.mod_pocketDim.util; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + public final class Point4D implements Comparable { @@ -155,4 +159,28 @@ public final class Point4D implements Comparable { return "(" + x + ", " + y + ", " + z + ", " + dimension + ")"; } + + public static void write(Point4D point, DataOutputStream stream) throws IOException + { + stream.writeBoolean(point != null); + if (point != null) + { + stream.writeInt(point.x); + stream.writeInt(point.y); + stream.writeInt(point.z); + stream.writeInt(point.dimension); + } + } + + public static Point4D read(DataInputStream stream) throws IOException + { + if (stream.readBoolean()) + { + return new Point4D( stream.readInt(), stream.readInt(), stream.readInt(), stream.readInt() ); + } + else + { + return null; + } + } } diff --git a/StevenDimDoors/mod_pocketDim/watcher/IOpaqueMessage.java b/StevenDimDoors/mod_pocketDim/watcher/IOpaqueMessage.java deleted file mode 100644 index a3add2e..0000000 --- a/StevenDimDoors/mod_pocketDim/watcher/IOpaqueMessage.java +++ /dev/null @@ -1,8 +0,0 @@ -package StevenDimDoors.mod_pocketDim.watcher; - -import java.io.DataOutputStream; - -public interface IOpaqueMessage -{ - void writeToStream(DataOutputStream stream); -} diff --git a/StevenDimDoors/mod_pocketDim/watcher/IOpaqueReader.java b/StevenDimDoors/mod_pocketDim/watcher/IOpaqueReader.java deleted file mode 100644 index 8250094..0000000 --- a/StevenDimDoors/mod_pocketDim/watcher/IOpaqueReader.java +++ /dev/null @@ -1,8 +0,0 @@ -package StevenDimDoors.mod_pocketDim.watcher; - -import com.google.common.io.ByteArrayDataInput; - -public interface IOpaqueReader -{ - IOpaqueMessage read(ByteArrayDataInput source); -} diff --git a/StevenDimDoors/mod_pocketDim/watcher/IUpdateWatcher.java b/StevenDimDoors/mod_pocketDim/watcher/IUpdateWatcher.java deleted file mode 100644 index 31f5a85..0000000 --- a/StevenDimDoors/mod_pocketDim/watcher/IUpdateWatcher.java +++ /dev/null @@ -1,8 +0,0 @@ -package StevenDimDoors.mod_pocketDim.watcher; - -public interface IUpdateWatcher -{ - public void onCreated(IOpaqueMessage message); - public void onUpdated(IOpaqueMessage message); - public void onDeleted(IOpaqueMessage message); -} -- 2.39.5 From 3568d223ffedcb99875ca3e739e5fbcb0ff50e3f Mon Sep 17 00:00:00 2001 From: SenseiKiwi Date: Tue, 3 Sep 2013 15:33:09 -0400 Subject: [PATCH 3/8] Overhauled Server-Side Packet Handling Threw out the complicated architecture that I'd made for synchronizing server and client data perfectly. Instead, we now send just enough data to the client and the resulting code is simpler. Some of the client-side code is also done so all packet handling should be finished soon. --- .../mod_pocketDim/ConnectionHandler.java | 4 +- .../mod_pocketDim/ServerPacketHandler.java | 68 ++++---- .../mod_pocketDim/core/DimLink.java | 15 +- .../core/IDimRegistrationCallback.java | 6 + .../mod_pocketDim/core/LinkTypes.java | 2 + .../mod_pocketDim/core/NewDimData.java | 78 +++++---- .../mod_pocketDim/core/PocketManager.java | 165 ++++++++++++------ .../mod_pocketDim/helpers/Compactor.java | 83 +++++++++ .../messages/DimMessageBuilder.java | 116 ------------ .../mod_pocketDim/messages/IDataMessage.java | 9 - .../messages/IMessageBuilder.java | 11 -- .../messages/IUpdateWatcher.java | 8 - .../messages/LinkMessageBuilder.java | 134 -------------- .../messages/UpdateWatcherProxy.java | 51 ------ .../mod_pocketDim/watcher/ClientDimData.java | 37 ++++ .../mod_pocketDim/watcher/IUpdateWatcher.java | 7 + .../watcher/UpdateWatcherProxy.java | 42 +++++ 17 files changed, 377 insertions(+), 459 deletions(-) create mode 100644 StevenDimDoors/mod_pocketDim/core/IDimRegistrationCallback.java create mode 100644 StevenDimDoors/mod_pocketDim/helpers/Compactor.java delete mode 100644 StevenDimDoors/mod_pocketDim/messages/DimMessageBuilder.java delete mode 100644 StevenDimDoors/mod_pocketDim/messages/IDataMessage.java delete mode 100644 StevenDimDoors/mod_pocketDim/messages/IMessageBuilder.java delete mode 100644 StevenDimDoors/mod_pocketDim/messages/IUpdateWatcher.java delete mode 100644 StevenDimDoors/mod_pocketDim/messages/LinkMessageBuilder.java delete mode 100644 StevenDimDoors/mod_pocketDim/messages/UpdateWatcherProxy.java create mode 100644 StevenDimDoors/mod_pocketDim/watcher/ClientDimData.java create mode 100644 StevenDimDoors/mod_pocketDim/watcher/IUpdateWatcher.java create mode 100644 StevenDimDoors/mod_pocketDim/watcher/UpdateWatcherProxy.java diff --git a/StevenDimDoors/mod_pocketDim/ConnectionHandler.java b/StevenDimDoors/mod_pocketDim/ConnectionHandler.java index 2abf8d3..07a0b77 100644 --- a/StevenDimDoors/mod_pocketDim/ConnectionHandler.java +++ b/StevenDimDoors/mod_pocketDim/ConnectionHandler.java @@ -11,7 +11,6 @@ import net.minecraft.network.packet.Packet1Login; import net.minecraft.network.packet.Packet250CustomPayload; import net.minecraft.server.MinecraftServer; import StevenDimDoors.mod_pocketDim.core.PocketManager; -import StevenDimDoors.mod_pocketDim.messages.IDataMessage; import cpw.mods.fml.common.network.IConnectionHandler; import cpw.mods.fml.common.network.Player; @@ -41,12 +40,11 @@ public class ConnectionHandler implements IConnectionHandler //Send information about all the registered dimensions and links to the client try { - IDataMessage message = PocketManager.getState(); Packet250CustomPayload packet = new Packet250CustomPayload(); ByteArrayOutputStream buffer = new ByteArrayOutputStream(); DataOutputStream writer = new DataOutputStream(buffer); writer.writeByte(PacketConstants.CLIENT_JOIN_PACKET_ID); - message.writeToStream(writer); + PocketManager.writePacket(writer); writer.close(); packet.channel = PacketConstants.CHANNEL_NAME; packet.data = buffer.toByteArray(); diff --git a/StevenDimDoors/mod_pocketDim/ServerPacketHandler.java b/StevenDimDoors/mod_pocketDim/ServerPacketHandler.java index d33c613..e8eb51a 100644 --- a/StevenDimDoors/mod_pocketDim/ServerPacketHandler.java +++ b/StevenDimDoors/mod_pocketDim/ServerPacketHandler.java @@ -7,8 +7,9 @@ import java.io.IOException; import net.minecraft.network.INetworkManager; import net.minecraft.network.packet.Packet250CustomPayload; import StevenDimDoors.mod_pocketDim.core.PocketManager; -import StevenDimDoors.mod_pocketDim.messages.IDataMessage; -import StevenDimDoors.mod_pocketDim.messages.IUpdateWatcher; +import StevenDimDoors.mod_pocketDim.util.Point4D; +import StevenDimDoors.mod_pocketDim.watcher.ClientDimData; +import StevenDimDoors.mod_pocketDim.watcher.IUpdateWatcher; import cpw.mods.fml.common.network.IPacketHandler; import cpw.mods.fml.common.network.PacketDispatcher; import cpw.mods.fml.common.network.Player; @@ -22,54 +23,39 @@ public class ServerPacketHandler implements IPacketHandler } @Override - public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) - { - - } + public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) { } - private class DimWatcher implements IUpdateWatcher + private static class DimWatcher implements IUpdateWatcher { @Override - public void onCreated(IDataMessage message) + public void onCreated(ClientDimData message) { - sendMessageToAllPlayers(PacketConstants.CREATE_DIM_PACKET_ID, message); + sendDimPacket(PacketConstants.CREATE_DIM_PACKET_ID, message); } @Override - public void onUpdated(IDataMessage message) + public void onDeleted(ClientDimData message) { - sendMessageToAllPlayers(PacketConstants.UPDATE_DIM_PACKET_ID, message); - } - - @Override - public void onDeleted(IDataMessage message) - { - sendMessageToAllPlayers(PacketConstants.DELETE_DIM_PACKET_ID, message); + sendDimPacket(PacketConstants.DELETE_DIM_PACKET_ID, message); } } - private class LinkWatcher implements IUpdateWatcher + private static class LinkWatcher implements IUpdateWatcher { @Override - public void onCreated(IDataMessage message) + public void onCreated(Point4D message) { - sendMessageToAllPlayers(PacketConstants.CREATE_LINK_PACKET_ID, message); + sendLinkPacket(PacketConstants.CREATE_LINK_PACKET_ID, message); } @Override - public void onUpdated(IDataMessage message) + public void onDeleted(Point4D message) { - sendMessageToAllPlayers(PacketConstants.UPDATE_LINK_PACKET_ID, message); - } - - @Override - public void onDeleted(IDataMessage message) - { - sendMessageToAllPlayers(PacketConstants.DELETE_LINK_PACKET_ID, message); + sendLinkPacket(PacketConstants.DELETE_LINK_PACKET_ID, message); } } - private static void sendMessageToAllPlayers(byte id, IDataMessage message) + private static void sendDimPacket(byte id, ClientDimData data) { try { @@ -77,7 +63,29 @@ public class ServerPacketHandler implements IPacketHandler ByteArrayOutputStream buffer = new ByteArrayOutputStream(); DataOutputStream writer = new DataOutputStream(buffer); writer.writeByte(id); - message.writeToStream(writer); + data.write(writer); + writer.close(); + packet.channel = PacketConstants.CHANNEL_NAME; + packet.data = buffer.toByteArray(); + packet.length = packet.data.length; + PacketDispatcher.sendPacketToAllPlayers(packet); + } + catch (IOException e) + { + //This shouldn't happen... + e.printStackTrace(); + } + } + + private static void sendLinkPacket(byte id, Point4D data) + { + try + { + Packet250CustomPayload packet = new Packet250CustomPayload(); + ByteArrayOutputStream buffer = new ByteArrayOutputStream(); + DataOutputStream writer = new DataOutputStream(buffer); + writer.writeByte(id); + Point4D.write(data, writer); writer.close(); packet.channel = PacketConstants.CHANNEL_NAME; packet.data = buffer.toByteArray(); diff --git a/StevenDimDoors/mod_pocketDim/core/DimLink.java b/StevenDimDoors/mod_pocketDim/core/DimLink.java index a8c296b..af8c7aa 100644 --- a/StevenDimDoors/mod_pocketDim/core/DimLink.java +++ b/StevenDimDoors/mod_pocketDim/core/DimLink.java @@ -1,30 +1,29 @@ package StevenDimDoors.mod_pocketDim.core; -import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; import StevenDimDoors.mod_pocketDim.util.Point4D; public abstract class DimLink -{ - private static final int EXPECTED_CHILDREN = 2; - +{ protected Point4D source; protected DimLink parent; protected LinkTail tail; - protected ArrayList children; + protected List children; protected DimLink(Point4D source, DimLink parent) { this.parent = parent; this.source = source; this.tail = parent.tail; - this.children = new ArrayList(EXPECTED_CHILDREN); + this.children = new LinkedList(); parent.children.add(this); } protected DimLink(Point4D source, int linkType) { - if (linkType < LinkTypes.ENUM_MIN || linkType > LinkTypes.ENUM_MAX) + if (linkType < LinkTypes.ENUM_MIN || linkType > LinkTypes.ENUM_MAX && linkType != LinkTypes.UNKNOWN) { throw new IllegalArgumentException("The specified link type is invalid."); } @@ -32,7 +31,7 @@ public abstract class DimLink this.parent = null; this.source = source; this.tail = new LinkTail(linkType, null); - this.children = new ArrayList(EXPECTED_CHILDREN); + this.children = new LinkedList(); } public Point4D source() diff --git a/StevenDimDoors/mod_pocketDim/core/IDimRegistrationCallback.java b/StevenDimDoors/mod_pocketDim/core/IDimRegistrationCallback.java new file mode 100644 index 0000000..04a0c38 --- /dev/null +++ b/StevenDimDoors/mod_pocketDim/core/IDimRegistrationCallback.java @@ -0,0 +1,6 @@ +package StevenDimDoors.mod_pocketDim.core; + +public interface IDimRegistrationCallback +{ + public NewDimData registerDimension(int dimensionID, int rootID); +} diff --git a/StevenDimDoors/mod_pocketDim/core/LinkTypes.java b/StevenDimDoors/mod_pocketDim/core/LinkTypes.java index b493e6c..f5fe139 100644 --- a/StevenDimDoors/mod_pocketDim/core/LinkTypes.java +++ b/StevenDimDoors/mod_pocketDim/core/LinkTypes.java @@ -7,6 +7,8 @@ public class LinkTypes public static final int ENUM_MIN = 0; public static final int ENUM_MAX = 8; + public static final int UNKNOWN = -1337; + public static final int NORMAL = 0; public static final int LIMBO = 1; public static final int POCKET = 2; diff --git a/StevenDimDoors/mod_pocketDim/core/NewDimData.java b/StevenDimDoors/mod_pocketDim/core/NewDimData.java index 7ff3160..de313b9 100644 --- a/StevenDimDoors/mod_pocketDim/core/NewDimData.java +++ b/StevenDimDoors/mod_pocketDim/core/NewDimData.java @@ -10,10 +10,8 @@ import net.minecraft.world.World; import StevenDimDoors.mod_pocketDim.DDProperties; import StevenDimDoors.mod_pocketDim.dungeon.DungeonData; import StevenDimDoors.mod_pocketDim.dungeon.pack.DungeonPack; -import StevenDimDoors.mod_pocketDim.messages.IDataMessage; -import StevenDimDoors.mod_pocketDim.messages.IUpdateWatcher; -import StevenDimDoors.mod_pocketDim.messages.LinkMessageBuilder; import StevenDimDoors.mod_pocketDim.util.Point4D; +import StevenDimDoors.mod_pocketDim.watcher.IUpdateWatcher; public abstract class NewDimData { @@ -106,19 +104,8 @@ public abstract class NewDimData parent = null; tail = new LinkTail(linkType, null); } - - public IDataMessage toMessage() - { - return linkMessageBuilder.createMessage(this); - } - - public IDataMessage toKey() - { - return linkMessageBuilder.createKey(this); - } } - private static LinkMessageBuilder linkMessageBuilder = new LinkMessageBuilder(); private static Random random = new Random(); private final int id; @@ -134,13 +121,12 @@ public abstract class NewDimData private Point4D origin; private int orientation; private DungeonData dungeon; - private final IUpdateWatcher dimWatcher; - private final IUpdateWatcher linkWatcher; + private final IUpdateWatcher linkWatcher; protected NewDimData(int id, NewDimData parent, boolean isPocket, boolean isDungeon, - IUpdateWatcher dimWatcher, IUpdateWatcher linkWatcher) + IUpdateWatcher 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)) { throw new NullPointerException("Dimensions can be pocket dimensions if and only if they have a parent dimension."); @@ -161,7 +147,6 @@ public abstract class NewDimData this.orientation = 0; this.origin = null; this.dungeon = null; - this.dimWatcher = dimWatcher; this.linkWatcher = linkWatcher; //Register with parent @@ -179,8 +164,32 @@ public abstract class NewDimData } } - protected abstract IDataMessage toMessage(); - protected abstract IDataMessage toKey(); + protected NewDimData(int id, NewDimData root) + { + // This constructor is meant for client-side code only + this.id = id; + this.linkMapping = new TreeMap(); //Should be stored in oct tree -- temporary solution + this.linkList = new ArrayList(); //Should be stored in oct tree -- temporary solution + this.children = new ArrayList(); + this.parent = null; + this.packDepth = 0; + this.isDungeon = false; + this.isFilled = false; + this.orientation = 0; + this.origin = null; + this.dungeon = null; + this.linkWatcher = null; + this.depth = 0; + if (root != null) + { + this.root = root; + } + else + { + this.root = this; + } + + } public DimLink findNearestRift(World world, int range, int x, int y, int z) { @@ -251,7 +260,7 @@ public abstract class NewDimData link.overwrite(linkType); } //Link created! - linkWatcher.onCreated(link.toMessage()); + linkWatcher.onCreated(link.source); return link; } @@ -278,14 +287,14 @@ public abstract class NewDimData linkList.add(link); //Link created! - linkWatcher.onCreated(link.toMessage()); + linkWatcher.onCreated(link.source); } else { if (link.overwrite(parent)) { //Link created! - linkWatcher.onCreated(link.toMessage()); + linkWatcher.onCreated(link.source); } } return link; @@ -302,7 +311,7 @@ public abstract class NewDimData { linkList.remove(target); //Raise deletion event - linkWatcher.onDeleted(target.toKey()); + linkWatcher.onDeleted(target.source); target.clear(); } return (target != null); @@ -316,7 +325,7 @@ public abstract class NewDimData { linkList.remove(target); //Raise deletion event - linkWatcher.onDeleted(target.toKey()); + linkWatcher.onDeleted(target.source); target.clear(); } return (target != null); @@ -345,7 +354,7 @@ public abstract class NewDimData public boolean isPocketDimension() { - return (parent != null); + return (root != this); } public boolean isDungeon() @@ -361,8 +370,6 @@ public abstract class NewDimData public void setFilled(boolean isFilled) { this.isFilled = isFilled; - //Raise the dim update event - dimWatcher.onUpdated(this.toMessage()); } public int id() @@ -412,7 +419,7 @@ public abstract class NewDimData public int linkCount() { - return linkMapping.size(); + return linkList.size(); } public Iterable children() @@ -420,6 +427,11 @@ public abstract class NewDimData return children; } + public Iterable links() + { + return linkList; + } + public void initializeDungeon(int originX, int originY, int originZ, int orientation, DimLink incoming, DungeonData dungeon) { if (!isDungeon) @@ -436,8 +448,6 @@ public abstract class NewDimData this.orientation = orientation; this.dungeon = dungeon; this.packDepth = calculatePackDepth(parent, dungeon); - //Raise the dim update event - dimWatcher.onUpdated(this.toMessage()); } private static int calculatePackDepth(NewDimData parent, DungeonData current) @@ -486,16 +496,12 @@ public abstract class NewDimData setDestination(incoming, originX, originY, originZ); this.origin = incoming.destination(); this.orientation = orientation; - //Raise the dim update event - dimWatcher.onUpdated(this.toMessage()); } public void setDestination(DimLink incoming, int x, int y, int z) { InnerDimLink link = (InnerDimLink) incoming; link.setDestination(x, y, z, this); - //Raise update event - linkWatcher.onUpdated(link.toMessage()); } public DimLink getRandomLink() diff --git a/StevenDimDoors/mod_pocketDim/core/PocketManager.java b/StevenDimDoors/mod_pocketDim/core/PocketManager.java index 91f90ca..fc1f854 100644 --- a/StevenDimDoors/mod_pocketDim/core/PocketManager.java +++ b/StevenDimDoors/mod_pocketDim/core/PocketManager.java @@ -1,9 +1,8 @@ package StevenDimDoors.mod_pocketDim.core; +import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; import java.io.IOException; import java.util.HashMap; @@ -13,13 +12,13 @@ import net.minecraft.tileentity.TileEntity; import net.minecraft.world.World; import net.minecraftforge.common.DimensionManager; import StevenDimDoors.mod_pocketDim.DDProperties; +import StevenDimDoors.mod_pocketDim.helpers.Compactor; import StevenDimDoors.mod_pocketDim.helpers.DeleteFolder; -import StevenDimDoors.mod_pocketDim.messages.DimMessageBuilder; -import StevenDimDoors.mod_pocketDim.messages.IDataMessage; -import StevenDimDoors.mod_pocketDim.messages.IUpdateWatcher; -import StevenDimDoors.mod_pocketDim.messages.UpdateWatcherProxy; import StevenDimDoors.mod_pocketDim.tileentities.TileEntityRift; import StevenDimDoors.mod_pocketDim.util.Point4D; +import StevenDimDoors.mod_pocketDim.watcher.ClientDimData; +import StevenDimDoors.mod_pocketDim.watcher.IUpdateWatcher; +import StevenDimDoors.mod_pocketDim.watcher.UpdateWatcherProxy; /** * This class regulates all the operations involving the storage and manipulation of dimensions. It handles saving dim data, teleporting the player, and @@ -29,41 +28,40 @@ public class PocketManager { private static class InnerDimData extends NewDimData { - //This inner class allows us to instantiate NewDimData indirectly without exposing - //a public constructor from NewDimData. It's meant to stop us from constructing instances - //of NewDimData without using PocketManager's functions. In turn, that enforces that any - //link destinations must be real dimensions controlled by PocketManager. + // This class allows us to instantiate NewDimData indirectly without exposing + // a public constructor from NewDimData. It's meant to stop us from constructing + // instances of NewDimData going through PocketManager. In turn, that enforces + // that any link destinations must be real dimensions controlled by PocketManager. - public InnerDimData(int id, NewDimData parent, boolean isPocket, boolean isDungeon, - IUpdateWatcher dimWatcher, IUpdateWatcher linkWatcher) + public InnerDimData(int id, InnerDimData parent, boolean isPocket, boolean isDungeon, + IUpdateWatcher linkWatcher) { - super(id, parent, isPocket, isDungeon, dimWatcher, linkWatcher); + super(id, parent, isPocket, isDungeon, linkWatcher); } - - @Override - protected IDataMessage toMessage() + + public InnerDimData(int id, InnerDimData root) { - return dimMessageBuilder.createMessage(this); + // This constructor is meant for client-side code only + super(id, root); } - - @Override - protected IDataMessage toKey() + + public InnerDimData(int id) { - return dimMessageBuilder.createKey(this); + // This constructor is meant for client-side code only + super(id, null); } } - private static DimMessageBuilder dimMessageBuilder = new DimMessageBuilder(); private static int OVERWORLD_DIMENSION_ID = 0; private static volatile boolean isLoading = false; private static volatile boolean isLoaded = false; private static volatile boolean isSaving = false; - private static UpdateWatcherProxy linkWatcher = null; - private static UpdateWatcherProxy dimWatcher = null; + private static UpdateWatcherProxy linkWatcher = null; + private static UpdateWatcherProxy dimWatcher = null; //HashMap that maps all the dimension IDs registered with DimDoors to their DD data. - private static HashMap dimensionData = new HashMap(); + private static HashMap dimensionData = null; public static boolean isLoaded() { @@ -87,16 +85,17 @@ public class PocketManager isLoading = true; - //Set up watcher proxies - dimWatcher = new UpdateWatcherProxy(); - linkWatcher = new UpdateWatcherProxy(); - - loadInternal(); + //Set up fields + dimensionData = new HashMap(); + dimWatcher = new UpdateWatcherProxy(); + linkWatcher = new UpdateWatcherProxy(); //Register Limbo DDProperties properties = DDProperties.instance(); registerDimension(properties.LimboDimensionID, null, false, false); + loadInternal(); + //Register pocket dimensions registerPockets(properties); @@ -134,7 +133,7 @@ public class PocketManager DeleteFolder.deleteFolder(save); } //Raise the dim deleted event - dimWatcher.onDeleted(dimension.toKey()); + dimWatcher.onDeleted(new ClientDimData(dimension)); //dimension.implode()??? -- more like delete, but yeah return true; } @@ -184,9 +183,7 @@ public class PocketManager /** * loads the dim data from the saved hashMap. Also handles compatibility with old saves, see OldSaveHandler - * @return */ - @SuppressWarnings("unchecked") private static void loadInternal() { // SenseiKiwi: This is a temporary function for testing purposes. @@ -196,10 +193,9 @@ public class PocketManager DimensionManager.getCurrentSaveRootDirectory() != null) { System.out.println("Loading Dimensional Doors save data..."); - File saveFile = new File(DimensionManager.getCurrentSaveRootDirectory() + "/dimdoors.dat"); - //Missing code for converting the binary data in the file into an IOpaqueMessage - IDataMessage saveData; - setState(saveData); + /*File saveFile = new File(DimensionManager.getCurrentSaveRootDirectory() + "/dimdoors.dat"); + + setState(saveData);*/ System.out.println("Loaded successfully!"); } } @@ -228,7 +224,7 @@ public class PocketManager try { System.out.println("Writing Dimensional Doors save data..."); - String tempPath = DimensionManager.getCurrentSaveRootDirectory() + "/dimdoors.tmp"; + /*String tempPath = DimensionManager.getCurrentSaveRootDirectory() + "/dimdoors.tmp"; String savePath = DimensionManager.getCurrentSaveRootDirectory() + "/dimdoors.dat"; File tempFile = new File(tempPath); File saveFile = new File(savePath); @@ -236,17 +232,17 @@ public class PocketManager getState().writeToStream(writer); writer.close(); saveFile.delete(); - tempFile.renameTo(saveFile); + tempFile.renameTo(saveFile);*/ System.out.println("Saved successfully!"); } - catch (FileNotFoundException e) + /*catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); - } + }*/ finally { isSaving = false; @@ -291,17 +287,17 @@ public class PocketManager DDProperties properties = DDProperties.instance(); int dimensionID = DimensionManager.getNextFreeDimId(); DimensionManager.registerDimension(dimensionID, properties.PocketProviderID); - return registerDimension(dimensionID, parent, true, isDungeon); + return registerDimension(dimensionID, (InnerDimData) parent, true, isDungeon); } - private static NewDimData registerDimension(int dimensionID, NewDimData parent, boolean isPocket, boolean isDungeon) + private static NewDimData registerDimension(int dimensionID, InnerDimData parent, boolean isPocket, boolean isDungeon) { if (dimensionData.containsKey(dimensionID)) { throw new IllegalArgumentException("Cannot register a dimension with ID = " + dimensionID + " because it has already been registered."); } - NewDimData dimension = new InnerDimData(dimensionID, parent, isPocket, isDungeon, dimWatcher, linkWatcher); + InnerDimData dimension = new InnerDimData(dimensionID, parent, isPocket, isDungeon, linkWatcher); dimensionData.put(dimensionID, dimension); return dimension; } @@ -330,14 +326,18 @@ public class PocketManager public static void unload() { save(); + dimWatcher = null; + linkWatcher = null; + dimensionData = null; unregisterPockets(); - dimensionData.clear(); } + /* + * This isn't needed right now and it's causing me problems due to the iterator's generic type -_- public static Iterable getDimensions() { return dimensionData.values(); - } + }*/ public static DimLink getLink(int x, int y, int z, World world) { @@ -357,37 +357,96 @@ public class PocketManager } } - public static void registerDimWatcher(IUpdateWatcher watcher) + public static void registerDimWatcher(IUpdateWatcher watcher) { dimWatcher.registerReceiver(watcher); } - public static boolean unregisterDimWatcher(IUpdateWatcher watcher) + public static boolean unregisterDimWatcher(IUpdateWatcher watcher) { return dimWatcher.unregisterReceiver(watcher); } - public static void registerLinkWatcher(IUpdateWatcher watcher) + public static void registerLinkWatcher(IUpdateWatcher watcher) { linkWatcher.registerReceiver(watcher); } - public static boolean unregisterLinkWatcher(IUpdateWatcher watcher) + public static boolean unregisterLinkWatcher(IUpdateWatcher watcher) { return linkWatcher.unregisterReceiver(watcher); } - public static IDataMessage getState() + public static void writePacket(DataOutputStream output) throws IOException { - + // Write a very compact description of our dimensions and links to be sent to a client + Compactor.write(dimensionData.values(), output); } - public static void setState(IDataMessage state) + public static void readPacket(DataInputStream input) throws IOException { if (isLoaded) { throw new IllegalStateException("Pocket dimensions have already been loaded!"); } + if (isLoading) + { + throw new IllegalStateException("Pocket dimensions are already loading!"); + } + + isLoading = true; + // Set up fields + dimensionData = new HashMap(); + dimWatcher = new UpdateWatcherProxy(); + linkWatcher = new UpdateWatcherProxy(); + + // Load compacted client-side dimension data + Compactor.readDimensions(input, new DimRegistrationCallback()); + + // Register pocket dimensions + DDProperties properties = DDProperties.instance(); + registerPockets(properties); + + isLoaded = true; + isLoading = false; + } + + private static class DimRegistrationCallback implements IDimRegistrationCallback + { + // We use this class to provide Compactor with the ability to send us dim data without + // having to instantiate a bunch of data containers and without exposing an "unsafe" + // creation method for anyone to call. Integrity protection for the win! It's like + // exposing a private constructor ONLY to a very specific trusted class. + + @Override + public NewDimData registerDimension(int dimensionID, int rootID) + { + // No need to raise events here since this code should only run on the client side + // We assume that the root dimension has already been registered to avoid dependency issues + + InnerDimData root = dimensionData.get(rootID); + InnerDimData dimension; + + if (rootID != dimensionID) + { + dimension = dimensionData.get(dimensionID); + if (dimension == null) + { + dimension = new InnerDimData(dimensionID, root); + dimensionData.put(dimension.id(), dimension); + } + } + else + { + if (root == null) + { + root = new InnerDimData(rootID); + dimensionData.put(root.id(), root); + } + dimension = root; + } + return dimension; + } } } diff --git a/StevenDimDoors/mod_pocketDim/helpers/Compactor.java b/StevenDimDoors/mod_pocketDim/helpers/Compactor.java new file mode 100644 index 0000000..6d5f5d5 --- /dev/null +++ b/StevenDimDoors/mod_pocketDim/helpers/Compactor.java @@ -0,0 +1,83 @@ +package StevenDimDoors.mod_pocketDim.helpers; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.util.Collection; +import java.util.Comparator; +import java.util.HashSet; + +import StevenDimDoors.mod_pocketDim.core.DimLink; +import StevenDimDoors.mod_pocketDim.core.IDimRegistrationCallback; +import StevenDimDoors.mod_pocketDim.core.LinkTypes; +import StevenDimDoors.mod_pocketDim.core.NewDimData; +import StevenDimDoors.mod_pocketDim.util.Point4D; + +public class Compactor +{ + + private static class DimComparator implements Comparator + { + @Override + public int compare(NewDimData a, NewDimData b) + { + return a.id() - b.id(); + } + } + + public static void write(Collection values, DataOutputStream output) throws IOException + { + // SenseiKiwi: Just encode the data straight up for now. I'll implement fancier compression later. + output.writeInt(values.size()); + for (NewDimData dimension : values) + { + output.writeInt(dimension.id()); + output.writeInt(dimension.root().id()); + output.writeInt(dimension.linkCount()); + for (DimLink link : dimension.links()) + { + Point4D.write(link.source(), output); + } + } + + + // Note to self: the root ID can be "compressed" by grouping + // dimensions by their root ID and then only sending it once + + /* + // To compress the dimension IDs, we'll sort them by ID + // and write the _difference_ between their ID numbers. + NewDimData[] dimensions = new NewDimData[values.size()]; + dimensions = values.toArray(dimensions); + Arrays.sort(dimensions, new DimComparator()); + */ + } + + public static void readDimensions(DataInputStream input, IDimRegistrationCallback callback) throws IOException + { + // Read in the dimensions one by one. Make sure we register root dimensions before + // attempting to register the dimensions under them. + + HashSet rootIDs = new HashSet(); + + int dimCount = input.readInt(); + for (int k = 0; k < dimCount; k++) + { + int id = input.readInt(); + int rootID = input.readInt(); + + if (rootIDs.add(rootID)) + { + callback.registerDimension(rootID, rootID); + } + // Don't check if (id != rootID) - we want to retrieve the reference anyway + NewDimData dimension = callback.registerDimension(id, rootID); + int linkCount = input.readInt(); + for (int h = 0; h < linkCount; h++) + { + Point4D source = Point4D.read(input); + dimension.createLink(source.getX(), source.getY(), source.getZ(), LinkTypes.UNKNOWN); + } + } + } +} diff --git a/StevenDimDoors/mod_pocketDim/messages/DimMessageBuilder.java b/StevenDimDoors/mod_pocketDim/messages/DimMessageBuilder.java deleted file mode 100644 index cb46451..0000000 --- a/StevenDimDoors/mod_pocketDim/messages/DimMessageBuilder.java +++ /dev/null @@ -1,116 +0,0 @@ -package StevenDimDoors.mod_pocketDim.messages; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; -import java.util.List; -import java.util.Map; - -import StevenDimDoors.mod_pocketDim.core.DimLink; -import StevenDimDoors.mod_pocketDim.core.NewDimData; -import StevenDimDoors.mod_pocketDim.core.NewDimData.InnerDimLink; -import StevenDimDoors.mod_pocketDim.dungeon.DungeonData; -import StevenDimDoors.mod_pocketDim.util.Point4D; - -import com.google.common.collect.ImmutableList; - -public class DimMessageBuilder implements IMessageBuilder -{ - public static class DimMessage implements IDataMessage - { - //We'll use public fields here since this is a data container object and all the fields are immutable - //We will not transfer dungeon, link data, or any data on child dimensions - //As far as I can tell, the children will handle updating their parents anyway - - public final int ID; - public final boolean IsDungeon; - public final boolean IsFilled; - public final int Depth; - public final int PackDepth; - public final Integer ParentID; - public final int RootID; - public final Point4D Origin; - public final int Orientation; - - private DimMessage(NewDimData dimension) - { - ID = dimension.id(); - IsDungeon = dimension.isDungeon(); - IsFilled = dimension.isFilled(); - Depth = dimension.depth(); - PackDepth = dimension.packDepth(); - ParentID = (dimension.parent() != null) ? dimension.parent().id() : null; - RootID = dimension.root().id(); - Origin = dimension.origin(); - Orientation = dimension.orientation(); - } - - private DimMessage(DataInputStream stream) throws IOException - { - ID = stream.readInt(); - IsDungeon = stream.readBoolean(); - IsFilled = stream.readBoolean(); - Depth = stream.readInt(); - PackDepth = stream.readInt(); - ParentID = stream. - } - - @Override - public void writeToStream(DataOutputStream stream) throws IOException - { - //Write a flag indicating that this is a full message and not a key - stream.writeBoolean(true); - - } - } - - public static class DimKeyMessage implements IDataMessage - { - //We'll use public fields here since this is a data container object and all the fields are immutable - public final int ID; - - private DimKeyMessage(NewDimData dimension) - { - ID = dimension.id(); - } - - private DimKeyMessage(DataInputStream stream) throws IOException - { - ID = stream.readInt(); - } - - @Override - public void writeToStream(DataOutputStream stream) throws IOException - { - //Write a flag indicating that this is a key - stream.writeBoolean(false); - stream.writeInt(ID); - } - } - - @Override - public IDataMessage createKey(NewDimData target) - { - return new DimKeyMessage(target); - } - - @Override - public IDataMessage createMessage(NewDimData target) - { - return new DimMessage(target); - } - - @Override - public IDataMessage read(DataInputStream source) throws IOException - { - //Check whether the message is a full message or just a key - if (source.readBoolean()) - { - return new DimMessage(source); - } - else - { - return new DimKeyMessage(source); - } - } -} diff --git a/StevenDimDoors/mod_pocketDim/messages/IDataMessage.java b/StevenDimDoors/mod_pocketDim/messages/IDataMessage.java deleted file mode 100644 index 8ee841d..0000000 --- a/StevenDimDoors/mod_pocketDim/messages/IDataMessage.java +++ /dev/null @@ -1,9 +0,0 @@ -package StevenDimDoors.mod_pocketDim.messages; - -import java.io.DataOutputStream; -import java.io.IOException; - -public interface IDataMessage -{ - public void writeToStream(DataOutputStream stream) throws IOException; -} diff --git a/StevenDimDoors/mod_pocketDim/messages/IMessageBuilder.java b/StevenDimDoors/mod_pocketDim/messages/IMessageBuilder.java deleted file mode 100644 index 297f8e5..0000000 --- a/StevenDimDoors/mod_pocketDim/messages/IMessageBuilder.java +++ /dev/null @@ -1,11 +0,0 @@ -package StevenDimDoors.mod_pocketDim.messages; - -import java.io.DataInputStream; -import java.io.IOException; - -public interface IMessageBuilder -{ - public IDataMessage createKey(T target); - public IDataMessage createMessage(T target); - public IDataMessage read(DataInputStream source) throws IOException; -} diff --git a/StevenDimDoors/mod_pocketDim/messages/IUpdateWatcher.java b/StevenDimDoors/mod_pocketDim/messages/IUpdateWatcher.java deleted file mode 100644 index 3bc24fd..0000000 --- a/StevenDimDoors/mod_pocketDim/messages/IUpdateWatcher.java +++ /dev/null @@ -1,8 +0,0 @@ -package StevenDimDoors.mod_pocketDim.messages; - -public interface IUpdateWatcher -{ - public void onCreated(IDataMessage message); - public void onUpdated(IDataMessage message); - public void onDeleted(IDataMessage message); -} diff --git a/StevenDimDoors/mod_pocketDim/messages/LinkMessageBuilder.java b/StevenDimDoors/mod_pocketDim/messages/LinkMessageBuilder.java deleted file mode 100644 index d9d21bc..0000000 --- a/StevenDimDoors/mod_pocketDim/messages/LinkMessageBuilder.java +++ /dev/null @@ -1,134 +0,0 @@ -package StevenDimDoors.mod_pocketDim.messages; - -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.IOException; - -import StevenDimDoors.mod_pocketDim.core.DimLink; -import StevenDimDoors.mod_pocketDim.util.Point4D; - -import com.google.common.collect.ImmutableList; - -public class LinkMessageBuilder implements IMessageBuilder -{ - public static class LinkMessage implements IDataMessage - { - //We'll use public fields here since this is a data container object and all the fields are immutable - public final Point4D Source; - public final Point4D Destination; - public final int LinkType; - public final Point4D Parent; - public final ImmutableList Children; - - private LinkMessage(DimLink link) - { - // TODO: In the case that a child's parent has been removed but the rest of the group still exists, - // this group bond will be lost to this link on the client side. Currently, that's not a problem since - // destination data and groups don't matter to the client, but it's something to think about later. - - Source = link.source(); - Destination = link.destination(); - LinkType = link.linkType(); - Parent = link.parent().source(); - ImmutableList.Builder builder = new ImmutableList.Builder(); - for (DimLink child : link.children()) - { - builder.add(child.source()); - } - Children = builder.build(); - } - - private LinkMessage(DataInputStream stream) throws IOException - { - Source = Point4D.read(stream); - Parent = Point4D.read(stream); - if (Parent == null) - { - Destination = Point4D.read(stream); - LinkType = stream.readInt(); - } - else - { - Destination = null; - LinkType = -1; - } - int childCount = stream.readInt(); - ImmutableList.Builder builder = new ImmutableList.Builder(); - for (int k = 0; k < childCount; k++) - { - builder.add(Point4D.read(stream)); - } - Children = builder.build(); - } - - @Override - public void writeToStream(DataOutputStream stream) throws IOException - { - //Write a flag indicating that this is a full message and not a key - stream.writeBoolean(true); - Point4D.write(Source, stream); - Point4D.write(Parent, stream); - //A link only has its own destination information if it has no parent to provide it - if (Parent == null) - { - Point4D.write(Destination, stream); - stream.writeInt(LinkType); - } - stream.writeInt(Children.size()); - for (Point4D child : Children) - { - Point4D.write(child, stream); - } - } - } - - public static class LinkKeyMessage implements IDataMessage - { - //We'll use public fields here since this is a data container object and all the fields are immutable - public final Point4D Source; - - private LinkKeyMessage(DimLink link) - { - Source = link.source(); - } - - private LinkKeyMessage(DataInputStream stream) throws IOException - { - Source = Point4D.read(stream); - } - - @Override - public void writeToStream(DataOutputStream stream) throws IOException - { - //Write a flag indicating that this is a key - stream.writeBoolean(false); - Point4D.write(Source, stream); - } - } - - @Override - public IDataMessage createKey(DimLink target) - { - return new LinkKeyMessage(target); - } - - @Override - public IDataMessage createMessage(DimLink target) - { - return new LinkMessage(target); - } - - @Override - public IDataMessage read(DataInputStream source) throws IOException - { - //Check whether the message is a full message or just a key - if (source.readBoolean()) - { - return new LinkMessage(source); - } - else - { - return new LinkKeyMessage(source); - } - } -} diff --git a/StevenDimDoors/mod_pocketDim/messages/UpdateWatcherProxy.java b/StevenDimDoors/mod_pocketDim/messages/UpdateWatcherProxy.java deleted file mode 100644 index 1326151..0000000 --- a/StevenDimDoors/mod_pocketDim/messages/UpdateWatcherProxy.java +++ /dev/null @@ -1,51 +0,0 @@ -package StevenDimDoors.mod_pocketDim.messages; - -import java.util.ArrayList; -import java.util.List; - -public class UpdateWatcherProxy implements IUpdateWatcher -{ - private List watchers; - - public UpdateWatcherProxy() - { - watchers = new ArrayList(); - } - - @Override - public void onCreated(IDataMessage message) - { - for (IUpdateWatcher receiver : watchers) - { - receiver.onCreated(message); - } - } - - @Override - public void onUpdated(IDataMessage message) - { - for (IUpdateWatcher receiver : watchers) - { - receiver.onUpdated(message); - } - } - - @Override - public void onDeleted(IDataMessage message) - { - for (IUpdateWatcher receiver : watchers) - { - receiver.onDeleted(message); - } - } - - public void registerReceiver(IUpdateWatcher receiver) - { - watchers.add(receiver); - } - - public boolean unregisterReceiver(IUpdateWatcher receiver) - { - return watchers.remove(receiver); - } -} diff --git a/StevenDimDoors/mod_pocketDim/watcher/ClientDimData.java b/StevenDimDoors/mod_pocketDim/watcher/ClientDimData.java new file mode 100644 index 0000000..7614a10 --- /dev/null +++ b/StevenDimDoors/mod_pocketDim/watcher/ClientDimData.java @@ -0,0 +1,37 @@ +package StevenDimDoors.mod_pocketDim.watcher; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; + +import StevenDimDoors.mod_pocketDim.core.NewDimData; + +public class ClientDimData +{ + //We'll use public fields since this is just a data container and it's immutable + public final int ID; + public final int RootID; + + public ClientDimData(int id, int rootID) + { + ID = id; + RootID = rootID; + } + + public ClientDimData(NewDimData dimension) + { + ID = dimension.id(); + RootID = dimension.root().id(); + } + + public void write(DataOutputStream output) throws IOException + { + output.writeInt(ID); + output.writeInt(RootID); + } + + public static ClientDimData read(DataInputStream input) throws IOException + { + return new ClientDimData(input.readInt(), input.readInt()); + } +} diff --git a/StevenDimDoors/mod_pocketDim/watcher/IUpdateWatcher.java b/StevenDimDoors/mod_pocketDim/watcher/IUpdateWatcher.java new file mode 100644 index 0000000..eb8f920 --- /dev/null +++ b/StevenDimDoors/mod_pocketDim/watcher/IUpdateWatcher.java @@ -0,0 +1,7 @@ +package StevenDimDoors.mod_pocketDim.watcher; + +public interface IUpdateWatcher +{ + public void onCreated(T message); + public void onDeleted(T message); +} diff --git a/StevenDimDoors/mod_pocketDim/watcher/UpdateWatcherProxy.java b/StevenDimDoors/mod_pocketDim/watcher/UpdateWatcherProxy.java new file mode 100644 index 0000000..e105476 --- /dev/null +++ b/StevenDimDoors/mod_pocketDim/watcher/UpdateWatcherProxy.java @@ -0,0 +1,42 @@ +package StevenDimDoors.mod_pocketDim.watcher; + +import java.util.ArrayList; +import java.util.List; + +public class UpdateWatcherProxy implements IUpdateWatcher +{ + private List> watchers; + + public UpdateWatcherProxy() + { + watchers = new ArrayList>(); + } + + @Override + public void onCreated(T message) + { + for (IUpdateWatcher receiver : watchers) + { + receiver.onCreated(message); + } + } + + @Override + public void onDeleted(T message) + { + for (IUpdateWatcher receiver : watchers) + { + receiver.onDeleted(message); + } + } + + public void registerReceiver(IUpdateWatcher receiver) + { + watchers.add(receiver); + } + + public boolean unregisterReceiver(IUpdateWatcher receiver) + { + return watchers.remove(receiver); + } +} -- 2.39.5 From 4cd7d3c0ae334122d4ad6a9b77dbe65ca1c68b59 Mon Sep 17 00:00:00 2001 From: SenseiKiwi Date: Tue, 3 Sep 2013 17:25:58 -0400 Subject: [PATCH 4/8] Completed Packet Handling Code Finished implementing all the packet handling code. It could be improved in the future to compress the initial packet sent to clients. With this, the code is complete enough to run! Commands have not been fixed yet but that will come in the future. --- .../mod_pocketDim/PacketConstants.java | 11 +- .../mod_pocketDim/core/DimLink.java | 2 +- .../mod_pocketDim/core/LinkTypes.java | 2 +- .../mod_pocketDim/core/NewDimData.java | 15 +-- .../mod_pocketDim/core/PocketManager.java | 125 ++++++++++++------ .../mod_pocketDim/helpers/Compactor.java | 2 +- .../mod_pocketDim/mod_pocketDim.java | 2 +- .../ticking/RiftRegenerator.java | 8 +- .../mod_pocketDim/watcher/IUpdateSource.java | 8 ++ .../ClientPacketHandler.java | 59 ++++++++- 10 files changed, 162 insertions(+), 72 deletions(-) create mode 100644 StevenDimDoors/mod_pocketDim/watcher/IUpdateSource.java diff --git a/StevenDimDoors/mod_pocketDim/PacketConstants.java b/StevenDimDoors/mod_pocketDim/PacketConstants.java index 67d4e60..847a5b0 100644 --- a/StevenDimDoors/mod_pocketDim/PacketConstants.java +++ b/StevenDimDoors/mod_pocketDim/PacketConstants.java @@ -3,13 +3,12 @@ package StevenDimDoors.mod_pocketDim; public class PacketConstants { private PacketConstants() { } + + public static final String CHANNEL_NAME = "DimDoorsPackets"; public static final byte CLIENT_JOIN_PACKET_ID = 1; public static final byte CREATE_DIM_PACKET_ID = 2; - public static final byte UPDATE_DIM_PACKET_ID = 3; - public static final byte DELETE_DIM_PACKET_ID = 4; - public static final byte CREATE_LINK_PACKET_ID = 5; - public static final byte UPDATE_LINK_PACKET_ID = 6; - public static final byte DELETE_LINK_PACKET_ID = 7; - public static final String CHANNEL_NAME = "DimDoorsPackets"; + public static final byte DELETE_DIM_PACKET_ID = 3; + public static final byte CREATE_LINK_PACKET_ID = 4; + public static final byte DELETE_LINK_PACKET_ID = 5; } diff --git a/StevenDimDoors/mod_pocketDim/core/DimLink.java b/StevenDimDoors/mod_pocketDim/core/DimLink.java index af8c7aa..cdf25a6 100644 --- a/StevenDimDoors/mod_pocketDim/core/DimLink.java +++ b/StevenDimDoors/mod_pocketDim/core/DimLink.java @@ -23,7 +23,7 @@ public abstract class DimLink protected DimLink(Point4D source, int linkType) { - if (linkType < LinkTypes.ENUM_MIN || linkType > LinkTypes.ENUM_MAX && linkType != LinkTypes.UNKNOWN) + if (linkType < LinkTypes.ENUM_MIN || linkType > LinkTypes.ENUM_MAX && linkType != LinkTypes.CLIENT_SIDE) { throw new IllegalArgumentException("The specified link type is invalid."); } diff --git a/StevenDimDoors/mod_pocketDim/core/LinkTypes.java b/StevenDimDoors/mod_pocketDim/core/LinkTypes.java index f5fe139..5e64aff 100644 --- a/StevenDimDoors/mod_pocketDim/core/LinkTypes.java +++ b/StevenDimDoors/mod_pocketDim/core/LinkTypes.java @@ -7,7 +7,7 @@ public class LinkTypes public static final int ENUM_MIN = 0; public static final int ENUM_MAX = 8; - public static final int UNKNOWN = -1337; + public static final int CLIENT_SIDE = -1337; public static final int NORMAL = 0; public static final int LIMBO = 1; diff --git a/StevenDimDoors/mod_pocketDim/core/NewDimData.java b/StevenDimDoors/mod_pocketDim/core/NewDimData.java index de313b9..0be6468 100644 --- a/StevenDimDoors/mod_pocketDim/core/NewDimData.java +++ b/StevenDimDoors/mod_pocketDim/core/NewDimData.java @@ -167,6 +167,11 @@ public abstract class NewDimData protected NewDimData(int id, NewDimData root) { // This constructor is meant for client-side code only + if (root == null) + { + throw new IllegalArgumentException("root cannot be null."); + } + this.id = id; this.linkMapping = new TreeMap(); //Should be stored in oct tree -- temporary solution this.linkList = new ArrayList(); //Should be stored in oct tree -- temporary solution @@ -180,15 +185,7 @@ public abstract class NewDimData this.dungeon = null; this.linkWatcher = null; this.depth = 0; - if (root != null) - { - this.root = root; - } - else - { - this.root = this; - } - + this.root = root; } public DimLink findNearestRift(World world, int range, int x, int y, int z) diff --git a/StevenDimDoors/mod_pocketDim/core/PocketManager.java b/StevenDimDoors/mod_pocketDim/core/PocketManager.java index fc1f854..d590930 100644 --- a/StevenDimDoors/mod_pocketDim/core/PocketManager.java +++ b/StevenDimDoors/mod_pocketDim/core/PocketManager.java @@ -17,6 +17,7 @@ import StevenDimDoors.mod_pocketDim.helpers.DeleteFolder; import StevenDimDoors.mod_pocketDim.tileentities.TileEntityRift; import StevenDimDoors.mod_pocketDim.util.Point4D; import StevenDimDoors.mod_pocketDim.watcher.ClientDimData; +import StevenDimDoors.mod_pocketDim.watcher.IUpdateSource; import StevenDimDoors.mod_pocketDim.watcher.IUpdateWatcher; import StevenDimDoors.mod_pocketDim.watcher.UpdateWatcherProxy; @@ -44,14 +45,54 @@ public class PocketManager // This constructor is meant for client-side code only super(id, root); } - - public InnerDimData(int id) + } + + private static class ClientLinkWatcher implements IUpdateWatcher + { + @Override + public void onCreated(Point4D source) { - // This constructor is meant for client-side code only - super(id, null); + NewDimData dimension = getDimensionData(source.getDimension()); + dimension.createLink(source.getX(), source.getY(), source.getZ(), LinkTypes.CLIENT_SIDE); + } + + @Override + public void onDeleted(Point4D source) + { + NewDimData dimension = getDimensionData(source.getDimension()); + dimension.deleteLink(source.getX(), source.getY(), source.getZ()); + } + } + + private static class ClientDimWatcher implements IUpdateWatcher + { + @Override + public void onCreated(ClientDimData data) + { + registerClientDimension(data.ID, data.RootID); + } + + @Override + public void onDeleted(ClientDimData data) + { + deletePocket(getDimensionData(data.ID), false); } } + private static class DimRegistrationCallback implements IDimRegistrationCallback + { + // We use this class to provide Compactor with the ability to send us dim data without + // having to instantiate a bunch of data containers and without exposing an "unsafe" + // creation method for anyone to call. Integrity protection for the win! It's like + // exposing a private constructor ONLY to a very specific trusted class. + + @Override + public NewDimData registerDimension(int dimensionID, int rootID) + { + return registerClientDimension(dimensionID, rootID); + } + } + private static int OVERWORLD_DIMENSION_ID = 0; private static volatile boolean isLoading = false; @@ -301,6 +342,30 @@ public class PocketManager dimensionData.put(dimensionID, dimension); return dimension; } + + private static NewDimData registerClientDimension(int dimensionID, int rootID) + { + // No need to raise events here since this code should only run on the client side + // getDimensionData() always handles root dimensions properly, even if the weren't defined before + + InnerDimData root = (InnerDimData) getDimensionData(rootID); + InnerDimData dimension; + + if (rootID != dimensionID) + { + dimension = dimensionData.get(dimensionID); + if (dimension == null) + { + dimension = new InnerDimData(dimensionID, root); + dimensionData.put(dimension.id(), dimension); + } + } + else + { + dimension = root; + } + return dimension; + } public static NewDimData getDimensionData(World world) { @@ -323,6 +388,11 @@ public class PocketManager return dimension; } + public static Iterable getDimensions() + { + return dimensionData.values(); + } + public static void unload() { save(); @@ -376,6 +446,11 @@ public class PocketManager { return linkWatcher.unregisterReceiver(watcher); } + + public static void getWatchers(IUpdateSource updateSource) + { + updateSource.registerWatchers(new ClientDimWatcher(), new ClientLinkWatcher()); + } public static void writePacket(DataOutputStream output) throws IOException { @@ -398,8 +473,8 @@ public class PocketManager // Set up fields dimensionData = new HashMap(); - dimWatcher = new UpdateWatcherProxy(); - linkWatcher = new UpdateWatcherProxy(); + dimWatcher = null; // Clients shouldn't need to watch dims + linkWatcher = null; // Clients shouldn't need to watch links // Load compacted client-side dimension data Compactor.readDimensions(input, new DimRegistrationCallback()); @@ -411,42 +486,4 @@ public class PocketManager isLoaded = true; isLoading = false; } - - private static class DimRegistrationCallback implements IDimRegistrationCallback - { - // We use this class to provide Compactor with the ability to send us dim data without - // having to instantiate a bunch of data containers and without exposing an "unsafe" - // creation method for anyone to call. Integrity protection for the win! It's like - // exposing a private constructor ONLY to a very specific trusted class. - - @Override - public NewDimData registerDimension(int dimensionID, int rootID) - { - // No need to raise events here since this code should only run on the client side - // We assume that the root dimension has already been registered to avoid dependency issues - - InnerDimData root = dimensionData.get(rootID); - InnerDimData dimension; - - if (rootID != dimensionID) - { - dimension = dimensionData.get(dimensionID); - if (dimension == null) - { - dimension = new InnerDimData(dimensionID, root); - dimensionData.put(dimension.id(), dimension); - } - } - else - { - if (root == null) - { - root = new InnerDimData(rootID); - dimensionData.put(root.id(), root); - } - dimension = root; - } - return dimension; - } - } } diff --git a/StevenDimDoors/mod_pocketDim/helpers/Compactor.java b/StevenDimDoors/mod_pocketDim/helpers/Compactor.java index 6d5f5d5..e13d902 100644 --- a/StevenDimDoors/mod_pocketDim/helpers/Compactor.java +++ b/StevenDimDoors/mod_pocketDim/helpers/Compactor.java @@ -76,7 +76,7 @@ public class Compactor for (int h = 0; h < linkCount; h++) { Point4D source = Point4D.read(input); - dimension.createLink(source.getX(), source.getY(), source.getZ(), LinkTypes.UNKNOWN); + dimension.createLink(source.getX(), source.getY(), source.getZ(), LinkTypes.CLIENT_SIDE); } } } diff --git a/StevenDimDoors/mod_pocketDim/mod_pocketDim.java b/StevenDimDoors/mod_pocketDim/mod_pocketDim.java index cde2099..c7cd467 100644 --- a/StevenDimDoors/mod_pocketDim/mod_pocketDim.java +++ b/StevenDimDoors/mod_pocketDim/mod_pocketDim.java @@ -164,7 +164,7 @@ public class mod_pocketDim //MonolithSpawner should be initialized before any provider instances are created //Register the other regular tick receivers as well spawner = new MonolithSpawner(commonTickHandler, properties); - new RiftRegenerator(commonTickHandler, properties); //No need to store the reference + new RiftRegenerator(commonTickHandler); //No need to store the reference LimboDecay decay = new LimboDecay(commonTickHandler, properties); transientDoor = (new TransientDoor(properties.TransientDoorID, Material.iron)).setHardness(1.0F) .setUnlocalizedName("transientDoor"); diff --git a/StevenDimDoors/mod_pocketDim/ticking/RiftRegenerator.java b/StevenDimDoors/mod_pocketDim/ticking/RiftRegenerator.java index 4c82dc1..114fa35 100644 --- a/StevenDimDoors/mod_pocketDim/ticking/RiftRegenerator.java +++ b/StevenDimDoors/mod_pocketDim/ticking/RiftRegenerator.java @@ -7,22 +7,16 @@ import StevenDimDoors.mod_pocketDim.mod_pocketDim; import StevenDimDoors.mod_pocketDim.core.DimLink; import StevenDimDoors.mod_pocketDim.core.NewDimData; import StevenDimDoors.mod_pocketDim.core.PocketManager; -import StevenDimDoors.mod_pocketDim.tileentities.TileEntityRift; import StevenDimDoors.mod_pocketDim.util.Point4D; -import cpw.mods.fml.common.FMLCommonHandler; -import cpw.mods.fml.relauncher.Side; public class RiftRegenerator implements IRegularTickReceiver { private static final int RIFT_REGENERATION_INTERVAL = 200; //Regenerate random rifts every 200 ticks private static final int RIFTS_REGENERATED_PER_DIMENSION = 5; - private DDProperties properties; - - public RiftRegenerator(IRegularTickSender sender, DDProperties properties) + public RiftRegenerator(IRegularTickSender sender) { sender.registerForTicking(this, RIFT_REGENERATION_INTERVAL, false); - this.properties = properties; } @Override diff --git a/StevenDimDoors/mod_pocketDim/watcher/IUpdateSource.java b/StevenDimDoors/mod_pocketDim/watcher/IUpdateSource.java new file mode 100644 index 0000000..98e260e --- /dev/null +++ b/StevenDimDoors/mod_pocketDim/watcher/IUpdateSource.java @@ -0,0 +1,8 @@ +package StevenDimDoors.mod_pocketDim.watcher; + +import StevenDimDoors.mod_pocketDim.util.Point4D; + +public interface IUpdateSource +{ + public void registerWatchers(IUpdateWatcher dimWatcher, IUpdateWatcher linkWatcher); +} diff --git a/StevenDimDoors/mod_pocketDimClient/ClientPacketHandler.java b/StevenDimDoors/mod_pocketDimClient/ClientPacketHandler.java index 9100b78..a98f115 100644 --- a/StevenDimDoors/mod_pocketDimClient/ClientPacketHandler.java +++ b/StevenDimDoors/mod_pocketDimClient/ClientPacketHandler.java @@ -1,15 +1,70 @@ package StevenDimDoors.mod_pocketDimClient; +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; + import net.minecraft.network.INetworkManager; import net.minecraft.network.packet.Packet250CustomPayload; +import StevenDimDoors.mod_pocketDim.PacketConstants; +import StevenDimDoors.mod_pocketDim.core.PocketManager; +import StevenDimDoors.mod_pocketDim.util.Point4D; +import StevenDimDoors.mod_pocketDim.watcher.ClientDimData; +import StevenDimDoors.mod_pocketDim.watcher.IUpdateSource; +import StevenDimDoors.mod_pocketDim.watcher.IUpdateWatcher; import cpw.mods.fml.common.network.IPacketHandler; import cpw.mods.fml.common.network.Player; -public class ClientPacketHandler implements IPacketHandler +public class ClientPacketHandler implements IPacketHandler, IUpdateSource { + private IUpdateWatcher linkWatcher; + private IUpdateWatcher dimWatcher; + + public ClientPacketHandler() + { + PocketManager.getWatchers(this); + } + + @Override + public void registerWatchers(IUpdateWatcher dimWatcher, IUpdateWatcher linkWatcher) + { + this.dimWatcher = dimWatcher; + this.linkWatcher = linkWatcher; + } + @Override public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player) { - + // TODO: Is this even necessary? I'm not convinced we can receive packets from other channels anyway! + if (!packet.channel.equals(PacketConstants.CHANNEL_NAME)) + return; + + try + { + DataInputStream input = new DataInputStream(new ByteArrayInputStream(packet.data)); + byte packetID = input.readByte(); + switch (packetID) + { + case PacketConstants.CLIENT_JOIN_PACKET_ID: + PocketManager.readPacket(input); + break; + case PacketConstants.CREATE_DIM_PACKET_ID: + dimWatcher.onCreated( ClientDimData.read(input) ); + break; + case PacketConstants.CREATE_LINK_PACKET_ID: + linkWatcher.onCreated( Point4D.read(input) ); + break; + case PacketConstants.DELETE_DIM_PACKET_ID: + dimWatcher.onDeleted( ClientDimData.read(input) ); + break; + case PacketConstants.DELETE_LINK_PACKET_ID: + linkWatcher.onDeleted( Point4D.read(input) ); + break; + } + } + catch (Exception e) + { + System.err.println("An exception occurred while processing a data packet:"); + e.printStackTrace(); + } } } -- 2.39.5 From 77bc0e833f69092971f66adb6774d93de4d86aa9 Mon Sep 17 00:00:00 2001 From: SenseiKiwi Date: Tue, 3 Sep 2013 17:47:02 -0400 Subject: [PATCH 5/8] Fixed Bugs Fixed bugs that would cause Minecraft to crash on startup. At least I can get to the main menu now. --- .../mod_pocketDim/core/PocketManager.java | 16 +++------------- .../mod_pocketDim/dungeon/DungeonData.java | 2 +- 2 files changed, 4 insertions(+), 14 deletions(-) diff --git a/StevenDimDoors/mod_pocketDim/core/PocketManager.java b/StevenDimDoors/mod_pocketDim/core/PocketManager.java index d590930..b79ee2b 100644 --- a/StevenDimDoors/mod_pocketDim/core/PocketManager.java +++ b/StevenDimDoors/mod_pocketDim/core/PocketManager.java @@ -98,8 +98,8 @@ public class PocketManager private static volatile boolean isLoading = false; private static volatile boolean isLoaded = false; private static volatile boolean isSaving = false; - private static UpdateWatcherProxy linkWatcher = null; - private static UpdateWatcherProxy dimWatcher = null; + private static final UpdateWatcherProxy linkWatcher = new UpdateWatcherProxy(); + private static final UpdateWatcherProxy dimWatcher = new UpdateWatcherProxy(); //HashMap that maps all the dimension IDs registered with DimDoors to their DD data. private static HashMap dimensionData = null; @@ -125,11 +125,7 @@ public class PocketManager } isLoading = true; - - //Set up fields dimensionData = new HashMap(); - dimWatcher = new UpdateWatcherProxy(); - linkWatcher = new UpdateWatcherProxy(); //Register Limbo DDProperties properties = DDProperties.instance(); @@ -396,8 +392,6 @@ public class PocketManager public static void unload() { save(); - dimWatcher = null; - linkWatcher = null; dimensionData = null; unregisterPockets(); } @@ -470,12 +464,8 @@ public class PocketManager } isLoading = true; - - // Set up fields dimensionData = new HashMap(); - dimWatcher = null; // Clients shouldn't need to watch dims - linkWatcher = null; // Clients shouldn't need to watch links - + // Load compacted client-side dimension data Compactor.readDimensions(input, new DimRegistrationCallback()); diff --git a/StevenDimDoors/mod_pocketDim/dungeon/DungeonData.java b/StevenDimDoors/mod_pocketDim/dungeon/DungeonData.java index 7c298df..aa4bfa3 100644 --- a/StevenDimDoors/mod_pocketDim/dungeon/DungeonData.java +++ b/StevenDimDoors/mod_pocketDim/dungeon/DungeonData.java @@ -31,7 +31,7 @@ public class DungeonData int indexB = schematicPath.lastIndexOf('/'); indexA = Math.max(indexA, indexB) + 1; - return schematicPath.substring(indexA, schematicPath.length() - DungeonHelper.SCHEMATIC_FILE_EXTENSION.length() - indexA); + return schematicPath.substring(indexA, schematicPath.length() - DungeonHelper.SCHEMATIC_FILE_EXTENSION.length()); } public int weight() -- 2.39.5 From 8bfe9dc22ecd56b785e5c2c830dac55271a50f40 Mon Sep 17 00:00:00 2001 From: SenseiKiwi Date: Tue, 3 Sep 2013 18:28:42 -0400 Subject: [PATCH 6/8] Fixed More Bugs Fixed more bugs. Now it's possible to generate a world without crashing. Unfortunately, it's clear that there is a packet-sending loop going on. I'll have to add a check to prevent integrated servers from spamming themselves. --- .../mod_pocketDim/EventHookContainer.java | 13 ++++++++++++- StevenDimDoors/mod_pocketDim/core/NewDimData.java | 2 +- StevenDimDoors/mod_pocketDim/mod_pocketDim.java | 1 - 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/StevenDimDoors/mod_pocketDim/EventHookContainer.java b/StevenDimDoors/mod_pocketDim/EventHookContainer.java index 025a24f..2e2756d 100644 --- a/StevenDimDoors/mod_pocketDim/EventHookContainer.java +++ b/StevenDimDoors/mod_pocketDim/EventHookContainer.java @@ -36,7 +36,18 @@ public class EventHookContainer @ForgeSubscribe public void onWorldLoad(WorldEvent.Load event) { - RiftRegenerator.regenerateRiftsInAllWorlds(); + // We need to initialize PocketManager here because onServerAboutToStart fires before we can + // use DimensionManager and onServerStarting fires after the game tries to generate terrain. + // If a gateway tries to generate before PocketManager has initialized, we get a crash. + if (!PocketManager.isLoaded()) + { + PocketManager.load(); + } + + if (PocketManager.isLoaded()) + { + RiftRegenerator.regenerateRiftsInAllWorlds(); + } } @ForgeSubscribe diff --git a/StevenDimDoors/mod_pocketDim/core/NewDimData.java b/StevenDimDoors/mod_pocketDim/core/NewDimData.java index 0be6468..40fe18d 100644 --- a/StevenDimDoors/mod_pocketDim/core/NewDimData.java +++ b/StevenDimDoors/mod_pocketDim/core/NewDimData.java @@ -127,7 +127,7 @@ public abstract class NewDimData IUpdateWatcher linkWatcher) { // The isPocket flag is redundant. It's meant as an integrity safeguard. - if (isPocket == (parent != null)) + if (isPocket != (parent != null)) { throw new NullPointerException("Dimensions can be pocket dimensions if and only if they have a parent dimension."); } diff --git a/StevenDimDoors/mod_pocketDim/mod_pocketDim.java b/StevenDimDoors/mod_pocketDim/mod_pocketDim.java index c7cd467..4ab5058 100644 --- a/StevenDimDoors/mod_pocketDim/mod_pocketDim.java +++ b/StevenDimDoors/mod_pocketDim/mod_pocketDim.java @@ -384,6 +384,5 @@ public class mod_pocketDim CommandCreatePocket.instance().register(event); CommandTeleportPlayer.instance().register(event); */ - PocketManager.load(); } } -- 2.39.5 From 1a50aa2290a540905c336feae6b5d61a54b291cc Mon Sep 17 00:00:00 2001 From: SenseiKiwi Date: Tue, 3 Sep 2013 19:31:19 -0400 Subject: [PATCH 7/8] Minor Changes Minor changes: removed unused imports and random blocks of whitespace --- .../mod_pocketDimClient/ClientProxy.java | 36 +++---------------- .../ClientTickHandler.java | 3 -- .../mod_pocketDimClient/RenderDimDoor.java | 3 -- .../mod_pocketDimClient/RenderMobObelisk.java | 3 -- .../mod_pocketDimClient/RenderRift.java | 21 +++++------ 5 files changed, 13 insertions(+), 53 deletions(-) diff --git a/StevenDimDoors/mod_pocketDimClient/ClientProxy.java b/StevenDimDoors/mod_pocketDimClient/ClientProxy.java index b1428c3..7cb1469 100644 --- a/StevenDimDoors/mod_pocketDimClient/ClientProxy.java +++ b/StevenDimDoors/mod_pocketDimClient/ClientProxy.java @@ -1,19 +1,10 @@ package StevenDimDoors.mod_pocketDimClient; -import java.io.File; - -import cpw.mods.fml.client.FMLClientHandler; -import cpw.mods.fml.client.registry.ClientRegistry; -import cpw.mods.fml.client.registry.RenderingRegistry; -import net.minecraft.entity.player.EntityPlayer; import net.minecraft.src.ModLoader; -import net.minecraftforge.client.MinecraftForgeClient; import StevenDimDoors.mod_pocketDim.CommonProxy; -import StevenDimDoors.mod_pocketDim.Spells; -import StevenDimDoors.mod_pocketDim.mod_pocketDim; -import StevenDimDoors.mod_pocketDim.ticking.CommonTickHandler; import StevenDimDoors.mod_pocketDim.ticking.MobMonolith; import StevenDimDoors.mod_pocketDim.tileentities.TileEntityDimDoor; -import StevenDimDoors.mod_pocketDim.tileentities.TileEntityRift; +import cpw.mods.fml.client.registry.ClientRegistry; +import cpw.mods.fml.client.registry.RenderingRegistry; public class ClientProxy extends CommonProxy @@ -22,41 +13,24 @@ public class ClientProxy extends CommonProxy @Override public void registerRenderers() { - //MinecraftForgeClient.preloadTexture(BLOCK_PNG); ClientRegistry.bindTileEntitySpecialRenderer(TileEntityDimDoor.class, new RenderDimDoor()); //This code activates the new rift rendering, as well as a bit of code in TileEntityRift //ClientRegistry.bindTileEntitySpecialRenderer(TileEntityRift.class, new RenderRift()); - //MinecraftForgeClient.preloadTexture(RIFT2_PNG); - RenderingRegistry.registerEntityRenderingHandler(MobMonolith.class, new RenderMobObelisk(.5F)); - - - - + RenderingRegistry.registerEntityRenderingHandler(MobMonolith.class, new RenderMobObelisk(.5F)); } - - - @Override public void loadTextures() { - - - - - - - - } + @Override public void printStringClient(String string) - { - + { ModLoader.getMinecraftInstance().ingameGUI.getChatGUI().printChatMessage(string); } diff --git a/StevenDimDoors/mod_pocketDimClient/ClientTickHandler.java b/StevenDimDoors/mod_pocketDimClient/ClientTickHandler.java index 1b9ad55..cece273 100644 --- a/StevenDimDoors/mod_pocketDimClient/ClientTickHandler.java +++ b/StevenDimDoors/mod_pocketDimClient/ClientTickHandler.java @@ -1,9 +1,6 @@ package StevenDimDoors.mod_pocketDimClient; import java.util.EnumSet; -import StevenDimDoors.mod_pocketDim.Spells; -import StevenDimDoors.mod_pocketDim.mod_pocketDim; - import net.minecraft.client.Minecraft; import net.minecraft.client.gui.GuiScreen; import cpw.mods.fml.common.ITickHandler; diff --git a/StevenDimDoors/mod_pocketDimClient/RenderDimDoor.java b/StevenDimDoors/mod_pocketDimClient/RenderDimDoor.java index 6239fc8..384ec97 100644 --- a/StevenDimDoors/mod_pocketDimClient/RenderDimDoor.java +++ b/StevenDimDoors/mod_pocketDimClient/RenderDimDoor.java @@ -3,11 +3,9 @@ package StevenDimDoors.mod_pocketDimClient; import java.nio.FloatBuffer; import java.util.Random; -import net.minecraft.block.Block; import net.minecraft.client.Minecraft; import net.minecraft.client.renderer.ActiveRenderInfo; import net.minecraft.client.renderer.GLAllocation; -import net.minecraft.client.renderer.Tessellator; import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer; import net.minecraft.tileentity.TileEntity; @@ -15,7 +13,6 @@ import org.lwjgl.opengl.GL11; import StevenDimDoors.mod_pocketDim.DDProperties; import StevenDimDoors.mod_pocketDim.mod_pocketDim; -import StevenDimDoors.mod_pocketDim.blocks.DimensionalDoor; import StevenDimDoors.mod_pocketDim.tileentities.TileEntityDimDoor; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; diff --git a/StevenDimDoors/mod_pocketDimClient/RenderMobObelisk.java b/StevenDimDoors/mod_pocketDimClient/RenderMobObelisk.java index e1060a9..2bbdbad 100644 --- a/StevenDimDoors/mod_pocketDimClient/RenderMobObelisk.java +++ b/StevenDimDoors/mod_pocketDimClient/RenderMobObelisk.java @@ -1,9 +1,6 @@ package StevenDimDoors.mod_pocketDimClient; -import StevenDimDoors.mod_pocketDim.ticking.MobMonolith; import net.minecraft.client.renderer.entity.RenderLiving; -import net.minecraft.entity.Entity; -import net.minecraft.entity.EntityLiving; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; diff --git a/StevenDimDoors/mod_pocketDimClient/RenderRift.java b/StevenDimDoors/mod_pocketDimClient/RenderRift.java index adb4f13..07c1cf9 100644 --- a/StevenDimDoors/mod_pocketDimClient/RenderRift.java +++ b/StevenDimDoors/mod_pocketDimClient/RenderRift.java @@ -1,24 +1,19 @@ package StevenDimDoors.mod_pocketDimClient; -import java.nio.FloatBuffer; -import java.util.HashMap; -import java.util.Random; +import static org.lwjgl.opengl.GL11.GL_BLEND; +import static org.lwjgl.opengl.GL11.GL_LIGHTING; +import static org.lwjgl.opengl.GL11.GL_ONE_MINUS_DST_COLOR; +import static org.lwjgl.opengl.GL11.GL_TEXTURE_2D; +import static org.lwjgl.opengl.GL11.GL_ZERO; +import static org.lwjgl.opengl.GL11.glBlendFunc; + +import java.util.HashMap; -import net.minecraft.block.Block; -import net.minecraft.client.Minecraft; -import net.minecraft.client.renderer.ActiveRenderInfo; -import net.minecraft.client.renderer.GLAllocation; -import net.minecraft.client.renderer.Tessellator; import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer; import net.minecraft.tileentity.TileEntity; import org.lwjgl.opengl.GL11; -import static org.lwjgl.opengl.GL11.*; -import StevenDimDoors.mod_pocketDim.DDProperties; -import StevenDimDoors.mod_pocketDim.mod_pocketDim; -import StevenDimDoors.mod_pocketDim.blocks.DimensionalDoor; -import StevenDimDoors.mod_pocketDim.tileentities.TileEntityDimDoor; import StevenDimDoors.mod_pocketDim.tileentities.TileEntityRift; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; -- 2.39.5 From 549ee548527db96deed0a49f3a1be9a7cb2ce630 Mon Sep 17 00:00:00 2001 From: SenseiKiwi Date: Tue, 3 Sep 2013 21:41:54 -0400 Subject: [PATCH 8/8] Fixed More Bugs Fixed the issue with integrated servers causing circular updates between the combined server and client. We now check on the client side whether the connection we're receiving data from is a memory connection (which is presumably only used by integrated servers). If so, the client ignores any incoming packets. We don't just disable update events altogether because LAN games will require updating remote clients. Also fixed a bug in PocketManager.unload() - we weren't setting isLoaded to false after unloading everything and unregisterPockets() had to be called before setting dimensionData to null. --- .../mod_pocketDim/core/PocketManager.java | 15 +++++++-------- .../mod_pocketDimClient/ClientPacketHandler.java | 5 +++++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/StevenDimDoors/mod_pocketDim/core/PocketManager.java b/StevenDimDoors/mod_pocketDim/core/PocketManager.java index b79ee2b..a03a7b9 100644 --- a/StevenDimDoors/mod_pocketDim/core/PocketManager.java +++ b/StevenDimDoors/mod_pocketDim/core/PocketManager.java @@ -391,17 +391,16 @@ public class PocketManager public static void unload() { + if (!isLoaded) + { + throw new IllegalStateException("Pocket dimensions have already been unloaded!"); + } + save(); - dimensionData = null; unregisterPockets(); + dimensionData = null; + isLoaded = false; } - - /* - * This isn't needed right now and it's causing me problems due to the iterator's generic type -_- - public static Iterable getDimensions() - { - return dimensionData.values(); - }*/ public static DimLink getLink(int x, int y, int z, World world) { diff --git a/StevenDimDoors/mod_pocketDimClient/ClientPacketHandler.java b/StevenDimDoors/mod_pocketDimClient/ClientPacketHandler.java index a98f115..a969a92 100644 --- a/StevenDimDoors/mod_pocketDimClient/ClientPacketHandler.java +++ b/StevenDimDoors/mod_pocketDimClient/ClientPacketHandler.java @@ -38,6 +38,11 @@ public class ClientPacketHandler implements IPacketHandler, IUpdateSource if (!packet.channel.equals(PacketConstants.CHANNEL_NAME)) return; + // If this is a memory connection, then our client is running an integrated server. + // We can tell by checking if packet size is 0. + if (manager.packetSize() == 0) + return; + try { DataInputStream input = new DataInputStream(new ByteArrayInputStream(packet.data)); -- 2.39.5