More Progress on Rewrite

Fixed the code in ItemLinkSignature and ItemStabilizedRiftSignature.
Removed obsolete code from PocketManager - we don't need to worry about
storing link keys anymore. All the data is stored in NBT, which means no
more generating unique keys or worrying about saving and loading them.
This commit is contained in:
SenseiKiwi
2013-08-31 20:44:31 -04:00
parent 1cc1c374f6
commit 4f3ab403d3
3 changed files with 121 additions and 380 deletions

View File

@@ -5,7 +5,6 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Random;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
@@ -44,14 +43,10 @@ public class PocketManager
private static boolean isInitialized = false;
private static boolean isSaving = false;
private static Random random = new Random();
//HashMap containing all the dims registered with DimDoors, sorted by dim ID. loaded on startup
private static HashMap<Integer, NewDimData> dimensionData = new HashMap<Integer, NewDimData>();
//HashMap for temporary storage of Link Signature damage hash values. See itemLinkSignature for more details
private static HashMap<Integer, IDimLink> keyLinkMapping = new HashMap<Integer, IDimLink>();
public static boolean isInitialized()
{
return isInitialized;
@@ -89,7 +84,7 @@ public class PocketManager
}
}
public boolean resetPocket(NewDimData dimension)
public boolean clearPocket(NewDimData dimension)
{
if (!dimension.isPocketDimension() || DimensionManager.getWorld(dimension.id()) != null)
{
@@ -103,7 +98,7 @@ public class PocketManager
return true;
}
public static boolean pruneDimension(NewDimData dimension, boolean deleteFolder)
public static boolean deletePocket(NewDimData dimension, boolean deleteFolder)
{
//FIXME: Shouldn't the links in and out of this dimension be altered somehow? Otherwise we have links pointing
//into a deleted dimension!
@@ -112,7 +107,6 @@ public class PocketManager
if (dimension.isPocketDimension() && DimensionManager.getWorld(dimension.id()) == null)
{
dimensionData.remove(dimension.id());
//FIXME: I added the following line. Seems like a good idea. Is it?
DimensionManager.unregisterDimension(dimension.id());
if (deleteFolder)
{
@@ -146,21 +140,6 @@ public class PocketManager
}
}
/**
* Used to associate a damage value on a Rift Signature with a link pair. See LinkSignature for details.
* @return
*/
public static int createUniqueLinkKey()
{
int linkKey;
do
{
linkKey = random.nextInt(30000);
}
while (keyLinkMapping.containsKey(linkKey));
return linkKey;
}
/**
* Function that saves all dim data in a hashMap. Calling too often can cause Concurrent modification exceptions, so be careful.
* @return
@@ -184,7 +163,6 @@ public class PocketManager
isSaving = true;
HashMap<String, Object> comboSave = new HashMap<String, Object>();
comboSave.put("dimensionData", dimensionData);
comboSave.put("keyLinkMapping", keyLinkMapping);
FileOutputStream saveFile = null;
try
@@ -246,15 +224,6 @@ public class PocketManager
ObjectSaveInputStream save = new ObjectSaveInputStream(saveFile);
HashMap<String, Object> comboSave = (HashMap<String, Object>) save.readObject();
try
{
keyLinkMapping = (HashMap<Integer, IDimLink>) comboSave.get("keyLinkMapping");
}
catch (Exception e)
{
System.out.println("Could not load Link Signature list. Link Sig items will lose their stored locations.");
}
try
{
dimensionData = (HashMap<Integer, NewDimData>) comboSave.get("dimensionData");
@@ -283,15 +252,6 @@ public class PocketManager
ObjectSaveInputStream save = new ObjectSaveInputStream(saveFile);
HashMap<String, Object> comboSave = (HashMap<String, Object>) save.readObject();
try
{
keyLinkMapping = (HashMap<Integer, IDimLink>) comboSave.get("keyLinkMapping");
}
catch (Exception e2)
{
System.out.println("Could not load Link Signature list. Link Sig items will loose restored locations.");
}
try
{
dimensionData = (HashMap<Integer, NewDimData>) comboSave.get("dimensionData");
@@ -395,7 +355,6 @@ public class PocketManager
save();
unregisterDimensions();
dimensionData.clear();
keyLinkMapping.clear();
}
public static Iterable<NewDimData> getDimensions()

View File

@@ -2,47 +2,21 @@ package StevenDimDoors.mod_pocketDim.items;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import StevenDimDoors.mod_pocketDim.DDProperties;
import StevenDimDoors.mod_pocketDim.mod_pocketDim;
import StevenDimDoors.mod_pocketDim.core.PocketManager;
import StevenDimDoors.mod_pocketDim.util.Point4D;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class ItemStabilizedRiftSignature extends itemLinkSignature
{
private static DDProperties properties = null;
public ItemStabilizedRiftSignature(int par)
public ItemStabilizedRiftSignature(int itemID)
{
super(par);
this.setMaxStackSize(1);
this.setCreativeTab(mod_pocketDim.dimDoorsCreativeTab);
this.setMaxDamage(0);
this.hasSubtypes = true;
if (properties == null)
properties = DDProperties.instance();
}
@SideOnly(Side.CLIENT)
@Override
public boolean hasEffect(ItemStack par1ItemStack)
{
// adds effect if item has a link stored
if(par1ItemStack.hasTagCompound())
{
if(par1ItemStack.stackTagCompound.getBoolean("isCreated"))
{
return true;
}
}
return false;
super(itemID);
}
public void registerIcons(IconRegister par1IconRegister)
@@ -51,66 +25,29 @@ public class ItemStabilizedRiftSignature extends itemLinkSignature
}
@Override
public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int par7, float par8, float par9, float par10)
{
int key;
NewLinkData linkData;
int thisWorldID=par3World.provider.dimensionId;
Integer[] linkCoords =this.readFromNBT(par1ItemStack);
int offset = 2;
if(par1ItemStack.getTagCompound()!=null)
//Check if the Stabilized Rift Signature has been initialized
Point4D source = getSource(stack);
if (source != null)
{
if(par1ItemStack.getTagCompound().getBoolean("isCreated"))
//Yes, it's initialized. Check if the player can pay an Ender Pearl to create a rift.
if (player.inventory.hasItem(Item.enderPearl.itemID))
{
boolean hasEnder = false;
// checks to see if the item has a link stored, if so, it creates it
if (par2EntityPlayer.inventory.hasItem(Item.enderPearl.itemID)||par2EntityPlayer.inventory.hasItem(properties.StableFabricItemID))
if (tryItemUse(stack, player, world, x, y, z) && !player.capabilities.isCreativeMode)
{
if (!par2EntityPlayer.inventory.consumeInventoryItem(properties.StableFabricItemID))
{
par2EntityPlayer.inventory.consumeInventoryItem(Item.enderPearl.itemID);
}
hasEnder=true;
}
if (par3World.getBlockId(par4, par5, par6)==Block.snow.blockID)
{
offset = 1;
}
if (hasEnder&&!par3World.isRemote)
{
if(PocketManager.instance.getLinkDataFromCoords(linkCoords[0], linkCoords[1], linkCoords[2], par3World)==null)
{
PocketManager.instance.createLink(linkCoords[3], par3World.provider.dimensionId, linkCoords[0], linkCoords[1], linkCoords[2],par4, par5+offset, par6);
}
PocketManager.instance.createLink(par3World.provider.dimensionId, linkCoords[3], par4, par5+offset, par6, linkCoords[0], linkCoords[1], linkCoords[2]);
par2EntityPlayer.worldObj.playSoundAtEntity(par2EntityPlayer,"mods.DimDoors.sfx.riftEnd", (float) .6, 1);
par2EntityPlayer.sendChatToPlayer("Rift Created");
}
else if (!par3World.isRemote)
{
par2EntityPlayer.sendChatToPlayer("No Ender Pearls!");
player.inventory.consumeInventoryItem(Item.enderPearl.itemID);
}
}
}
else if (!par3World.isRemote)
else
{
if(par3World.getBlockId(par4, par5, par6)==Block.snow.blockID)
{
offset = 1;
}
//otherwise, it creates the first half of the link. Next click will complete it.
key = PocketManager.instance.createUniqueInterDimLinkKey();
this.writeToNBT(par1ItemStack, par4, par5+offset, par6,par3World.provider.dimensionId);
par2EntityPlayer.worldObj.playSoundAtEntity(par2EntityPlayer,"mods.DimDoors.sfx.riftStart", (float) .6, 1);
par2EntityPlayer.sendChatToPlayer("Rift Signature Stored");
//Initialization doesn't cost any materials
tryItemUse(stack, player, world, x, y, z);
}
return true;
}
/**
* allows items to add custom lines of information to the mouseover description
*/
@@ -119,13 +56,10 @@ public class ItemStabilizedRiftSignature extends itemLinkSignature
@Override
public void addInformation(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, List par3List, boolean par4)
{
if (par1ItemStack.hasTagCompound())
Point4D source = getSource(par1ItemStack);
if (source != null)
{
if (par1ItemStack.stackTagCompound.getBoolean("isCreated"))
{
Integer[] coords = this.readFromNBT(par1ItemStack);
par3List.add("Leads to (" + coords[0] + ", " + coords[1] + ", " + coords[2] + ") at dimension #" + coords[3]);
}
par3List.add("Leads to (" + source.getX() + ", " + source.getY() + ", " + source.getZ() + ") at dimension #" + source.getDimension());
}
else
{
@@ -134,65 +68,4 @@ public class ItemStabilizedRiftSignature extends itemLinkSignature
par3List.add("that link the locations together.");
}
}
public void writeToNBT(ItemStack itemStack,int x, int y, int z, int dimID)
{
NBTTagCompound tag;
if(itemStack.hasTagCompound())
{
tag = itemStack.getTagCompound();
}
else
{
tag= new NBTTagCompound();
}
tag.setInteger("linkX", x);
tag.setInteger("linkY", y);
tag.setInteger("linkZ", z);
tag.setInteger("linkDimID", dimID);
tag.setBoolean("isCreated", true);
itemStack.setTagCompound(tag);
}
/**
* Read the stack fields from a NBT object.
*/
public Integer[] readFromNBT(ItemStack itemStack)
{
NBTTagCompound tag;
Integer[] linkCoords = new Integer[5];
if(itemStack.hasTagCompound())
{
tag = itemStack.getTagCompound();
if(!tag.getBoolean("isCreated"))
{
return null;
}
linkCoords[0]=tag.getInteger("linkX");
linkCoords[1]=tag.getInteger("linkY");
linkCoords[2]=tag.getInteger("linkZ");
linkCoords[3]=tag.getInteger("linkDimID");
}
return linkCoords;
}
@Override
public void onCreated(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
{
if(!par2World.isRemote)
{
/**
//creates the first half of the link on item creation
int key= dimHelper.instance.createUniqueInterDimLinkKey();
LinkData linkData= new LinkData(par2World.provider.dimensionId,MathHelper.floor_double(par3EntityPlayer.posX),MathHelper.floor_double(par3EntityPlayer.posY),MathHelper.floor_double(par3EntityPlayer.posZ));
System.out.println(key);
dimHelper.instance.interDimLinkList.put(key, linkData);
par1ItemStack.setItemDamage(key);
**/
}
}
}

View File

@@ -2,23 +2,23 @@ package StevenDimDoors.mod_pocketDim.items;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;
import StevenDimDoors.mod_pocketDim.DDProperties;
import net.minecraftforge.common.DimensionManager;
import StevenDimDoors.mod_pocketDim.mod_pocketDim;
import StevenDimDoors.mod_pocketDim.core.IDimLink;
import StevenDimDoors.mod_pocketDim.core.NewDimData;
import StevenDimDoors.mod_pocketDim.core.PocketManager;
import StevenDimDoors.mod_pocketDim.util.Point4D;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class itemLinkSignature extends Item
{
public itemLinkSignature(int itemID)
{
super(itemID);
@@ -26,240 +26,149 @@ public class itemLinkSignature extends Item
this.setCreativeTab(mod_pocketDim.dimDoorsCreativeTab);
this.setMaxDamage(0);
this.hasSubtypes = true;
if (properties == null)
properties = DDProperties.instance();
}
private static DDProperties properties = null;
@SideOnly(Side.CLIENT)
@Override
public boolean hasEffect(ItemStack par1ItemStack)
public boolean hasEffect(ItemStack stack)
{
// adds effect if item has a link stored
if(par1ItemStack.hasTagCompound())
{
if(par1ItemStack.stackTagCompound.getBoolean("isCreated"))
{
return true;
//Make the item glow if it has one endpoint stored
return (stack.getItemDamage() != 0);
}
}
return false;
}
public void registerIcons(IconRegister par1IconRegister)
{
this.itemIcon = par1IconRegister.registerIcon(mod_pocketDim.modid + ":" + this.getUnlocalizedName());
}
@Override
public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int par7, float par8, float par9, float par10)
{
int key;
ILinkData linkData;
int thisWorldID=par3World.provider.dimensionId;
if(!par3World.isRemote)
{
//par1ItemStack= par2EntityPlayer.getCurrentEquippedItem();
Integer[] linkCoords =this.readFromNBT(par1ItemStack);
//System.out.println(key);
int offset = 2;
int idBlock = par3World.getBlockId(par4, par5, par6);
if(Block.blocksList.length>idBlock&&idBlock!=0)
{
if(Block.blocksList[idBlock].isBlockReplaceable(par3World, par4, par5, par6))
{
offset = 1;
}
}
if(par3World.getBlockId(par4, par5, par6) == properties.DimensionalDoorID && par3World.getBlockId(par4, par5 + 1, par6) == properties.DimensionalDoorID)
{
offset = 1;
}
else
if(par3World.getBlockId(par4, par5, par6)==properties.WarpDoorID&&par3World.getBlockId(par4, par5+1, par6)==properties.WarpDoorID)
{
offset = 1;
}
else
if (par3World.getBlockId(par4, par5, par6)==properties.DimensionalDoorID&&par3World.getBlockId(par4, par5-1, par6)==properties.DimensionalDoorID)
{
offset = 0;
}
else
if (par3World.getBlockId(par4, par5, par6) == properties.WarpDoorID && par3World.getBlockId(par4, par5-1, par6)==properties.WarpDoorID)
{
offset = 0;
}
int orientation = MathHelper.floor_double((double)((par2EntityPlayer.rotationYaw + 180.0F) * 4.0F / 360.0F) - 0.5D) & 3;
for(int count = 0;count<3;count++)
{
if(PocketManager.instance.getLinkDataFromCoords(par4, par5+count, par6,par3World)!=null)
{
int id= (par3World.getBlockId(par4, par5+count, par6));
if(id == properties.DimensionalDoorID||id==properties.WarpDoorID||id== properties.UnstableDoorID)
{
orientation = PocketManager.instance.getLinkDataFromCoords(par4, par5+count, par6,par3World).linkOrientation;
}
}
}
if(par1ItemStack.getTagCompound()!=null)
{
if(par1ItemStack.getTagCompound().getBoolean("isCreated"))
{
// checks to see if the item has a link stored, if so, it creates it
PocketManager.instance.createLink(par3World.provider.dimensionId, linkCoords[3], par4, par5+offset, par6, linkCoords[0], linkCoords[1], linkCoords[2],orientation);
PocketManager.instance.createLink(linkCoords[3], par3World.provider.dimensionId, linkCoords[0], linkCoords[1], linkCoords[2],par4, par5+offset, par6,linkCoords[4]);
--par1ItemStack.stackSize;
par2EntityPlayer.sendChatToPlayer("Rift Created");
par1ItemStack.stackTagCompound=null;
par2EntityPlayer.worldObj.playSoundAtEntity(par2EntityPlayer,"mods.DimDoors.sfx.riftEnd", (float) .6, 1);
}
}
else
{
//otherwise, it creates the first half of the link. Next click will complete it.
key= PocketManager.instance.createUniqueInterDimLinkKey();
this.writeToNBT(par1ItemStack, par4, par5+offset, par6,par3World.provider.dimensionId,orientation);
par2EntityPlayer.sendChatToPlayer("Rift Signature Stored");
par2EntityPlayer.worldObj.playSoundAtEntity(par2EntityPlayer,"mods.DimDoors.sfx.riftStart", (float) .6, 1);
}
//dimHelper.instance.save();
}
tryItemUse(stack, player, world, x, y, z);
return true;
}
@SideOnly(Side.CLIENT)
protected boolean tryItemUse(ItemStack stack, EntityPlayer player, World world, int x, int y, int z)
{
if (!world.isRemote)
{
//We don't check for replaceable blocks. The user can deal with that. <_<
y += 2; //Increase y by 2 to place the rift at head level
if (!player.canPlayerEdit(x, y, z, 0, stack))
{
return false;
}
Point4D source = getSource(stack);
if (source != null)
{
//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()).setLinkType(IDimLink.TYPE_NORMAL);
IDimLink reverse = destinationDimension.createLink(x, y, z).setLinkType(IDimLink.TYPE_NORMAL);
link.setDestination(x, y, z, destinationDimension);
reverse.setDestination(source.getX(), source.getY(), source.getZ(), sourceDimension);
//Try placing a rift at the destination point
if (!mod_pocketDim.blockRift.isBlockImmune(world, x, y, z))
{
world.setBlock(x, y, z, mod_pocketDim.blockRift.blockID);
}
//Try placing a rift at the source point, but check if its world is loaded first
World sourceWorld = DimensionManager.getWorld(sourceDimension.id());
if (sourceWorld != null &&
!mod_pocketDim.blockRift.isBlockImmune(sourceWorld, source.getX(), source.getY(), source.getZ()))
{
sourceWorld.setBlock(source.getX(), source.getY(), source.getY(), mod_pocketDim.blockRift.blockID);
}
if (!player.capabilities.isCreativeMode)
{
stack.stackSize--;
}
clearSource(stack);
player.sendChatToPlayer("Rift Created");
world.playSoundAtEntity(player,"mods.DimDoors.sfx.riftEnd", 0.6f, 1);
}
else
{
//The link signature has not been used. Store its current target as the first location.
setSource(stack, x, y, z, PocketManager.getDimensionData(world));
player.sendChatToPlayer("Location Stored in Rift Signature");
world.playSoundAtEntity(player,"mods.DimDoors.sfx.riftStart", 0.6f, 1);
}
return true;
}
return false;
}
/**
* allows items to add custom lines of information to the mouseover description
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@SideOnly(Side.CLIENT)
public void addInformation(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, List par3List, boolean par4)
{
if(par1ItemStack.hasTagCompound())
Point4D source = getSource(par1ItemStack);
if (source != null)
{
if(par1ItemStack.stackTagCompound.getBoolean("isCreated"))
{
Integer[] coords = this.readFromNBT(par1ItemStack);
par3List.add(String.valueOf("Leads to dim "+coords[3] +" with depth "+(PocketManager.instance.getDimDepth(coords[3]))));
par3List.add("at x="+coords[0]+" y="+coords[1]+" z="+coords[2]);
}
par3List.add("Leads to (" + source.getX() + ", " + source.getY() + ", " + source.getZ() + ") at dimension #" + source.getDimension());
}
else
{
par3List.add("First click stores location,");
par3List.add ("second click creates two rifts,");
par3List.add("that link the first location");
par3List.add("with the second location");
par3List.add("First click stores a location;");
par3List.add("second click creates a pair of");
par3List.add("rifts linking the two locations.");
}
}
public void writeToNBT(ItemStack itemStack,int x, int y, int z, int dimID,int orientation)
public static void setSource(ItemStack itemStack, int x, int y, int z, NewDimData dimension)
{
NBTTagCompound tag;
if(itemStack.hasTagCompound())
{
tag = itemStack.getTagCompound();
}
else
{
tag= new NBTTagCompound();
}
NBTTagCompound tag = new NBTTagCompound();
tag.setInteger("linkX", x);
tag.setInteger("linkY", y);
tag.setInteger("linkZ", z);
tag.setInteger("linkDimID", dimID);
tag.setBoolean("isCreated", true);
tag.setInteger("orientation", orientation);
tag.setInteger("linkDimID", dimension.id());
itemStack.setTagCompound(tag);
itemStack.setItemDamage(1);
}
/**
* Read the stack fields from a NBT object.
*/
public Integer[] readFromNBT(ItemStack itemStack)
public static void clearSource(ItemStack itemStack)
{
//Don't just set the tag to null since there may be other data there (e.g. for renamed items)
NBTTagCompound tag = itemStack.getTagCompound();
tag.removeTag("linkX");
tag.removeTag("linkY");
tag.removeTag("linkZ");
tag.removeTag("linkDimID");
itemStack.setItemDamage(0);
}
NBTTagCompound tag;
Integer[] linkCoords = new Integer[5];
public static Point4D getSource(ItemStack itemStack)
{
if (itemStack.getItemDamage() != 0)
{
if (itemStack.hasTagCompound())
{
tag = itemStack.getTagCompound();
NBTTagCompound tag = itemStack.getTagCompound();
if(!tag.getBoolean("isCreated"))
Integer x = tag.getInteger("linkX");
Integer y = tag.getInteger("linkY");
Integer z = tag.getInteger("linkZ");
Integer dimID = tag.getInteger("linkDimID");
if (x != null && y != null && z != null && dimID != null)
{
return new Point4D(x, y, z, dimID);
}
}
itemStack.setItemDamage(0);
}
return null;
}
linkCoords[0]=tag.getInteger("linkX");
linkCoords[1]=tag.getInteger("linkY");
linkCoords[2]=tag.getInteger("linkZ");
linkCoords[3]=tag.getInteger("linkDimID");
linkCoords[4]=tag.getInteger("orientation");
}
return linkCoords;
}
@Override
public void onCreated(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
{
if(!par2World.isRemote)
{
/**
//creates the first half of the link on item creation
int key= dimHelper.instance.createUniqueInterDimLinkKey();
LinkData linkData= new LinkData(par2World.provider.dimensionId,MathHelper.floor_double(par3EntityPlayer.posX),MathHelper.floor_double(par3EntityPlayer.posY),MathHelper.floor_double(par3EntityPlayer.posZ));
System.out.println(key);
dimHelper.instance.interDimLinkList.put(key, linkData);
par1ItemStack.setItemDamage(key);
**/
}
}
}