Connected Rift-Link system to rift registry- and saving- system

Merged functionality of "RiftHandler" class into "RiftRegistry" class
Moved the "Location" class

Why the RiftRegistry couples the Rift ID to the Rift **location** and
the PocketRegistry couples the Pocket ID to the actual Pocket
**instance**:
-Each Rift is a Tile-Entity, which means that it has its infromation can
be stored using the Tile-Entitiy's information saving system.
-Each Pocket is barely more than an information storage container, which
depends on having an external information saving system, which the
PocketRegistry provides for it.
This commit is contained in:
Mathijs Riezebos 2017-01-13 01:08:21 +01:00
parent 5c6fd1d1fa
commit f68c9ec4a2
8 changed files with 117 additions and 121 deletions

View file

@ -2,6 +2,20 @@
<gradle-project-properties>
<!--DO NOT EDIT THIS FILE! - Used by the Gradle plugin of NetBeans.-->
<auxiliary>
<editor-bookmarks xmlns="http://www.netbeans.org/ns/editor-bookmarks/2" lastBookmarkId="0"/>
<editor-bookmarks lastBookmarkId="0" xmlns="http://www.netbeans.org/ns/editor-bookmarks/2"/>
<open-files xmlns="http://www.netbeans.org/ns/projectui-open-files/2">
<group>
<file>file:/C:/Users/Gebruiker/Documents/GitHub/DimDoors/src/main/java/com/zixiken/dimdoors/shared/RiftRegistry.java</file>
<file>file:/C:/Users/Gebruiker/Documents/GitHub/DimDoors/src/main/java/com/zixiken/dimdoors/tileentities/DDTileEntityBase.java</file>
<file>file:/C:/Users/Gebruiker/Documents/GitHub/DimDoors/src/main/java/com/zixiken/dimdoors/shared/DDSavedData.java</file>
<file>file:/C:/Users/Gebruiker/Documents/GitHub/DimDoors/src/main/java/com/zixiken/dimdoors/DimDoors.java</file>
<file>file:/C:/Users/Gebruiker/Documents/GitHub/DimDoors/src/main/java/com/zixiken/dimdoors/server/DDProxyServer.java</file>
<file>file:/C:/Users/Gebruiker/Documents/GitHub/DimDoors/src/main/java/com/zixiken/dimdoors/shared/Pocket.java</file>
<file>file:/C:/Users/Gebruiker/Documents/GitHub/DimDoors/src/main/java/com/zixiken/dimdoors/DDProxyCommon.java</file>
<file>file:/C:/Users/Gebruiker/Documents/GitHub/DimDoors/src/main/java/com/zixiken/dimdoors/shared/RiftSavedData.java</file>
<file>file:/C:/Users/Gebruiker/Documents/GitHub/DimDoors/src/main/java/com/zixiken/dimdoors/shared/Location.java</file>
</group>
</open-files>
</auxiliary>
</gradle-project-properties>

View file

@ -5,11 +5,9 @@
*/
package com.zixiken.dimdoors.shared;
import com.zixiken.dimdoors.DimDoors;
import java.io.File;
import net.minecraft.world.World;
import net.minecraft.world.WorldSavedData;
import net.minecraftforge.fml.common.FMLCommonHandler;
/**
*

View file

@ -0,0 +1,60 @@
package com.zixiken.dimdoors.shared;
import com.zixiken.dimdoors.tileentities.DDTileEntityBase;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.DimensionManager;
/**
*
* @author Robijnvogel
*/
public class Location {
public int dimensionID;
public BlockPos pos;
Location(int dimID, int x, int y, int z) {
this.dimensionID = dimID;
pos = new BlockPos(x, y, z);
}
Location(int dimID, BlockPos pos) {
this.dimensionID = dimID;
this.pos = pos.up(0); //copyOf
}
public TileEntity getTileEntity() {
World world = DimensionManager.getWorld(dimensionID); //@todo HOW?
return world.getTileEntity(pos);
}
static Location getLocation(TileEntity tileEntity) {
World world = tileEntity.getWorld();
int dimID = world.provider.getDimension();
BlockPos blockPos = tileEntity.getPos();
return new Location(dimID, blockPos);
}
static NBTBase writeToNBT(Location location) {
NBTTagCompound locationNBT = new NBTTagCompound();
locationNBT.setInteger("worldID", location.dimensionID);
locationNBT.setInteger("x", location.pos.getX());
locationNBT.setInteger("y", location.pos.getY());
locationNBT.setInteger("z", location.pos.getZ());
return locationNBT;
}
static Location readFromNBT(NBTTagCompound locationNBT) {
int worldID = locationNBT.getInteger("worldID");
int x = locationNBT.getInteger("x");
int y = locationNBT.getInteger("y");
int z = locationNBT.getInteger("z");
BlockPos blockPos = new BlockPos(x, y, z);
return new Location(worldID, blockPos);
}
}

View file

@ -5,10 +5,12 @@
*/
package com.zixiken.dimdoors.shared;
import com.zixiken.dimdoors.DimDoors;
import com.zixiken.dimdoors.tileentities.DDTileEntityBase;
import java.util.HashMap;
import java.util.Map;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
/**
@ -20,30 +22,30 @@ public class RiftRegistry {
public static final RiftRegistry Instance = new RiftRegistry();
// Privates
private int nextUnusedID;
private final Map<Integer, DDTileEntityBase> riftList;
private int nextRiftID;
private final Map<Integer, Location> riftList;
// Methods
public RiftRegistry() {
nextUnusedID = 0;
nextRiftID = 0;
riftList = new HashMap();
}
public void reset() {
nextUnusedID = 0;
nextRiftID = 0;
riftList.clear();
}
public void readFromNBT(NBTTagCompound nbt) {
nextUnusedID = nbt.getInteger("nextUnusedID");
nextRiftID = nbt.getInteger("nextUnusedID");
if (nbt.hasKey("riftData")) {
NBTTagCompound riftsNBT = nbt.getCompoundTag("riftData");
int i = 1;
String tag = "" + i;
while (riftsNBT.hasKey(tag)) {
NBTTagCompound riftNBT = riftsNBT.getCompoundTag(tag);
DDTileEntityBase rift = DDTileEntityBase.readFromNBT(i, riftNBT);
riftList.put(i, rift);
Location riftLocation = Location.readFromNBT(riftNBT);
riftList.put(i, riftLocation);
i++;
tag = "" + i;
@ -52,30 +54,48 @@ public class RiftRegistry {
}
public void writeToNBT(NBTTagCompound nbt) {
nbt.setInteger("nextUnusedID", nextUnusedID);
nbt.setInteger("nextUnusedID", nextRiftID);
NBTTagCompound riftsNBT = new NBTTagCompound();
for (Map.Entry<Integer, DDTileEntityBase> entry : riftList.entrySet()) {
riftsNBT.setTag("" + entry.getKey(), DDTileEntityBase.writeToNBT(entry.getValue()));
for (Map.Entry<Integer, Location> entry : riftList.entrySet()) {
riftsNBT.setTag("" + entry.getKey(), Location.writeToNBT(entry.getValue()));
}
nbt.setTag("riftData", riftsNBT);
}
public int registerNewRift(DDTileEntityBase rift, World world) {
riftList.put(nextUnusedID, rift);
public int registerNewRift(DDTileEntityBase rift) {
riftList.put(nextRiftID, Location.getLocation(rift));
nextUnusedID++;
RiftSavedData.get(world).markDirty(); //Notify that this needs to be saved on world save
return nextUnusedID -1;
nextRiftID++;
RiftSavedData.get(DimDoors.getDefWorld()).markDirty(); //Notify that this needs to be saved on world save
return nextRiftID -1;
}
public void removeRift(int riftID, World world) {
if (riftList.containsKey(riftID)) {
riftList.remove(riftID);
RiftSavedData.get(world).markDirty(); //Notify that this needs to be saved on world save
RiftSavedData.get(DimDoors.getDefWorld()).markDirty(); //Notify that this needs to be saved on world save
}
}
public DDTileEntityBase getRift(int ID) {
public Location getRiftLocation(int ID) {
return riftList.get(ID);
}
public void pair(int riftID, int riftID2) {
Location location = riftList.get(riftID);
TileEntity tileEntity = location.getTileEntity(); //@todo this method might need to be in another class?
if (tileEntity != null && tileEntity instanceof DDTileEntityBase) {
DDTileEntityBase rift = (DDTileEntityBase) tileEntity;
rift.pair(riftID2);
}
}
public void unpair(int riftID) {
Location location = riftList.get(riftID);
TileEntity tileEntity = location.getTileEntity();
if (tileEntity != null && tileEntity instanceof DDTileEntityBase) {
DDTileEntityBase rift = (DDTileEntityBase) tileEntity;
rift.unpair();
}
}
}

View file

@ -5,7 +5,6 @@
*/
package com.zixiken.dimdoors.shared;
import com.zixiken.dimdoors.DimDoors;
import java.io.File;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;

View file

@ -1,10 +1,8 @@
package com.zixiken.dimdoors.tileentities;
import com.zixiken.dimdoors.world.RiftHandler;
import com.zixiken.dimdoors.shared.RiftRegistry;
import java.util.Random;
import net.minecraft.block.state.IBlockState;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
@ -27,36 +25,30 @@ public abstract class DDTileEntityBase extends TileEntity
}
public void pair(int otherRiftID) {
RiftHandler rifthandler;
if (isPaired) {
if (otherRiftID == pairedRiftID) {
return;
}
else {
rifthandler.unpair(pairedRiftID);
RiftRegistry.Instance.unpair(pairedRiftID);
}
}
pairedRiftID = otherRiftID;
rifthandler.pair(pairedRiftID, riftID);
RiftRegistry.Instance.pair(pairedRiftID, riftID);
isPaired = true;
}
public void unpair() {
RiftHandler rifthandler;
if (!isPaired) {
return;
}
else {
isPaired = false;
rifthandler.unpair(pairedRiftID);
RiftRegistry.Instance.unpair(pairedRiftID);
}
}
public static NBTTagCompound writeToNBT(DDTileEntityBase rift) {
}
public static DDTileEntityBase readFromNBT(int dim, NBTTagCompound riftNBT) {
private void register() {
riftID = RiftRegistry.Instance.registerNewRift(this);
}
}

View file

@ -1,30 +0,0 @@
package com.zixiken.dimdoors.world;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
/**
*
* @author Robijnvogel
*/
public class Location {
public String dimensionUID;
public BlockPos pos;
Location(String dimUID, int x, int y, int z) {
this.dimensionUID = dimUID;
pos = new BlockPos(x, y, z);
}
Location(String dimUID, BlockPos pos) {
this.dimensionUID = dimUID;
this.pos = pos.up(0); //copyOf
}
TileEntity getTileEntity() {
World world = getWorld(dimensionUID); //@todo HOW?
return world.getTileEntity(pos);
}
}

View file

@ -1,57 +0,0 @@
package com.zixiken.dimdoors.world;
import com.zixiken.dimdoors.tileentities.DDTileEntityBase;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.minecraft.tileentity.TileEntity;
/**
*
* @author Robijnvogel
*/
public class RiftHandler {
private Map<Integer, Location> riftLocations; //it needs to be a Map if we want to be able to remove rifts without the IDs shifting (as would happen in a List
private int nextRiftID;
RiftHandler() { //@todo don't forget to create one object of this class at world-load
riftLocations = new HashMap<>();
nextRiftID = 0;
readfromNBT(); //and also add a method to write to NBT
}
public int addRift(Location location) {
int riftID = nextRiftID;
nextRiftID++;
riftLocations.put(riftID, location);
return riftID;
}
public void removeRift(int riftID, Location location) {
riftLocations.remove(riftID);
}
public Location getLocation(int riftID) {
Location location = riftLocations.get(riftID);
return location;
}
public void pair(int riftID, int riftID2) {
Location location = riftLocations.get(riftID);
TileEntity tileEntity = location.getTileEntity(); //@todo this method might need to be in another class?
if (tileEntity != null && tileEntity instanceof DDTileEntityBase) {
DDTileEntityBase rift = (DDTileEntityBase) tileEntity;
rift.pair(riftID2);
}
}
public void unpair(int riftID) {
Location location = riftLocations.get(riftID);
TileEntity tileEntity = location.getTileEntity();
if (tileEntity != null && tileEntity instanceof DDTileEntityBase) {
DDTileEntityBase rift = (DDTileEntityBase) tileEntity;
rift.unpair();
}
}
}