diff --git a/build.gradle b/build.gradle index 6d5b7fe3..87dea644 100644 --- a/build.gradle +++ b/build.gradle @@ -15,9 +15,9 @@ version = "3.0.0-a1" group= "com.zixiken.dimdoors" // http://maven.apache.org/guides/mini/guide-naming-conventions.html archivesBaseName = "dimdoors" -sourceCompatibility = targetCompatibility = "1.6" // Need this here so eclipse task generates correctly. +sourceCompatibility = targetCompatibility = "1.8" // Need this here so eclipse task generates correctly. compileJava { - sourceCompatibility = targetCompatibility = "1.6" + sourceCompatibility = targetCompatibility = "1.8" } minecraft { diff --git a/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorBase.java b/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorBase.java index b4abfad0..6f5d0a0f 100644 --- a/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorBase.java +++ b/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorBase.java @@ -113,8 +113,7 @@ public abstract class BlockDimDoorBase extends BlockDoor implements IDimDoor, IT } @Override - public TileEntity createNewTileEntity(World world, int metadata) { - Thread.currentThread().getStackTrace(); + public TileEntity createNewTileEntity(World world, int metadata) { //gets called upon world load as well return new TileEntityDimDoor(); } @@ -175,12 +174,13 @@ public abstract class BlockDimDoorBase extends BlockDoor implements IDimDoor, IT } @Override - public boolean canPlaceBlockAt(World worldIn, BlockPos pos) { - IBlockState stateBot = worldIn.getBlockState(pos); - IBlockState stateTop = worldIn.getBlockState(pos.up()); - return pos.getY() >= worldIn.getHeight() - 1 ? false - : worldIn.getBlockState(pos.down()).isSideSolid(worldIn, pos.down(), EnumFacing.UP) - && canPlaceBottomAt(worldIn, pos, stateBot) && canPlaceTopAt(worldIn, pos, stateTop); + public boolean canPlaceBlockAt(World worldIn, BlockPos pos) { //returns whether or not the entire door (2 blocks tall) can be placed at this pos. pos is the position of the bottom half of the door + IBlockState groundState = worldIn.getBlockState(pos.down()); + IBlockState bottomState = worldIn.getBlockState(pos); + IBlockState topState = worldIn.getBlockState(pos.up()); + return pos.up().getY() > worldIn.getHeight() - 1 || pos.getY() < 1 ? false //top half can never be placed above buildHeight (255), (worldIn.getHeight() should return 256) and bottom half can never be placed below y=1 + : groundState.isSideSolid(worldIn, pos.down(), EnumFacing.UP) + && canPlaceBottomAt(worldIn, pos, bottomState) && canPlaceTopAt(worldIn, pos, topState); } private boolean canPlaceBottomAt(World worldIn, BlockPos pos, IBlockState state) { diff --git a/src/main/java/com/zixiken/dimdoors/items/ItemDoorBase.java b/src/main/java/com/zixiken/dimdoors/items/ItemDoorBase.java index 02190d00..ff28533a 100644 --- a/src/main/java/com/zixiken/dimdoors/items/ItemDoorBase.java +++ b/src/main/java/com/zixiken/dimdoors/items/ItemDoorBase.java @@ -43,7 +43,7 @@ public abstract class ItemDoorBase extends ItemDoor { this.setMaxStackSize(64); this.setCreativeTab(DimDoors.dimDoorsCreativeTab); - doorItemMapping.put(this, this); + doorItemMapping.put(this, this); //@todo Why? if (vanillaDoor != null) { doorItemMapping.put(vanillaDoor, this); } @@ -70,80 +70,73 @@ public abstract class ItemDoorBase extends ItemDoor { BlockPos pos = hit.getBlockPos(); if (worldIn.getBlockState(pos).getBlock() == ModBlocks.blockRift) { EnumActionResult canDoorBePlacedOnGroundBelowRift - = onItemUse(stack, playerIn, worldIn, pos.down(2), hand, EnumFacing.UP, - (float) hit.hitVec.xCoord, (float) hit.hitVec.yCoord, (float) hit.hitVec.zCoord); + = tryPlaceDoorOnTopOfBlock(stack, playerIn, worldIn, pos.down(2), hand, + (float) hit.hitVec.xCoord, (float) hit.hitVec.yCoord, (float) hit.hitVec.zCoord); //stack may be changed by this method return new ActionResult(canDoorBePlacedOnGroundBelowRift, stack); } } - return new ActionResult(EnumActionResult.PASS, stack); + return new ActionResult(EnumActionResult.FAIL, stack); //@todo, should return onItemUse(params) here? will door placement on block not work otherwise? } @Override - public EnumActionResult onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) { - //@todo also check for rift raytracing replacement; - if (worldIn.isRemote) { - return EnumActionResult.FAIL; + public EnumActionResult onItemUse(ItemStack stack, EntityPlayer playerIn, World world, BlockPos pos, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) { + if (world.isRemote) { + return EnumActionResult.FAIL; //@todo, is this needed, or does this always get called from the onItemRightClick(params) method? } - IBlockState iblockstate = worldIn.getBlockState(pos); - Block block = iblockstate.getBlock(); - if (side != EnumFacing.UP || block == Blocks.AIR) { + + Block block = world.getBlockState(pos).getBlock(); + if (block.isReplaceable(world, pos) && block != Blocks.AIR) { //why are we even checking for Blocks.AIR here? How would one use an item on an air block. It has no hitbox, does it? + pos = pos.offset(EnumFacing.DOWN); //the bottom part of the door can replace this block, so we will try to place it on the block under it + block = world.getBlockState(pos).getBlock(); //update the block to be placed on + side = EnumFacing.UP; //make sure that the next if-statement returns true + } + + if (side != EnumFacing.UP || block == Blocks.AIR) { //only place the door if the item is "used" on the top of the block it is to be placed on and we might as well check if that block is air or not even though I do not think it is needed (isSideSolid gets checked later) return EnumActionResult.FAIL; } else { - - if (!block.isReplaceable(worldIn, pos)) { - pos = pos.offset(side); //we know that (side == EnumFacing.UP) - } - - // Retrieve the actual door type that we want to use here. - // It's okay if stack isn't an ItemDoor. In that case, the lookup will - // return null, just as if the item was an unrecognized door type. - ItemDoorBase mappedItem = doorItemMapping.get(stack.getItem()); - if (mappedItem == null) { - return EnumActionResult.FAIL; - } - BlockDimDoorBase doorBlock = mappedItem.getDoorBlock(); - - if (playerIn.canPlayerEdit(pos, side, stack) && playerIn.canPlayerEdit(pos.up(), side, stack) - && doorBlock.canPlaceBlockAt(worldIn, pos)) { - - TileEntity possibleOldRift = worldIn.getTileEntity(pos.up()); - EnumFacing enumfacing = EnumFacing.fromAngle((double) playerIn.rotationYaw); - int i = enumfacing.getFrontOffsetX(); - int j = enumfacing.getFrontOffsetZ(); - boolean flag = i < 0 && hitZ < 0.5F || i > 0 && hitZ > 0.5F || j < 0 && hitX > 0.5F || j > 0 && hitX < 0.5F; - placeDoor(worldIn, pos, enumfacing, doorBlock, flag); - SoundType soundtype = worldIn.getBlockState(pos).getBlock().getSoundType(worldIn.getBlockState(pos), worldIn, pos, playerIn); - worldIn.playSound(playerIn, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F); - --stack.stackSize; - - DDTileEntityBase newTileEntityDimDoor = (DDTileEntityBase) worldIn.getTileEntity(pos.up()); - if (possibleOldRift instanceof DDTileEntityBase) { // - DDTileEntityBase oldRift = (DDTileEntityBase) possibleOldRift; - newTileEntityDimDoor.loadDataFrom(oldRift); - } else { - newTileEntityDimDoor.register(); - } - if (newTileEntityDimDoor instanceof TileEntityDimDoor) { - TileEntityDimDoor tileEntityDimDoor = (TileEntityDimDoor) newTileEntityDimDoor; - tileEntityDimDoor.orientation - = newTileEntityDimDoor.getWorld().getBlockState(newTileEntityDimDoor.getPos()).getValue(BlockDimDoor.FACING).getOpposite(); - //storing the orientation inside the tile-entity, because that thing can actually save the orientation in the worldsave, unlike the block itself, which fucks up somehow - } - return EnumActionResult.SUCCESS; - } else { - return EnumActionResult.FAIL; - } + return tryPlaceDoorOnTopOfBlock(stack, playerIn, world, pos, hand, hitX, hitY, hitZ); } } +//pos = position of block, the door gets placed on - public static boolean canPlace(World world, BlockPos pos) { - IBlockState state = world.getBlockState(pos); + private EnumActionResult tryPlaceDoorOnTopOfBlock(ItemStack stack, EntityPlayer playerIn, World world, BlockPos pos, EnumHand hand, float hitX, float hitY, float hitZ) { + // Retrieve the actual door type that we want to use here. + // It's okay if stack isn't an ItemDoor. In that case, the lookup will + // return null, just as if the item was an unrecognized door type. + ItemDoorBase mappedItem = doorItemMapping.get(stack.getItem()); + if (mappedItem == null) { + return EnumActionResult.FAIL; + } + pos = pos.up(); //change pos to position the bottom half of the door gets placed at + BlockDimDoorBase doorBlock = mappedItem.getDoorBlock(); + if (playerIn.canPlayerEdit(pos, EnumFacing.UP, stack) && playerIn.canPlayerEdit(pos.up(), EnumFacing.UP, stack) + && doorBlock.canPlaceBlockAt(world, pos)) { + //calculate what side the door should be facing + EnumFacing enumfacing = EnumFacing.fromAngle((double) playerIn.rotationYaw); + int i = enumfacing.getFrontOffsetX(); + int j = enumfacing.getFrontOffsetZ(); + boolean flag = i < 0 && hitZ < 0.5F || i > 0 && hitZ > 0.5F || j < 0 && hitX > 0.5F || j > 0 && hitX < 0.5F; //Vanilla Minecraft code not consistently using EnumFacing + //fetch "the" tile entity at the top block of where the door is going to be placed + TileEntity possibleOldRift = world.getTileEntity(pos.up()); + //place the door + placeDoor(world, pos, enumfacing, doorBlock, flag); + SoundType soundtype = world.getBlockState(pos).getBlock().getSoundType(world.getBlockState(pos), world, pos, playerIn); + world.playSound(playerIn, pos, soundtype.getPlaceSound(), SoundCategory.BLOCKS, (soundtype.getVolume() + 1.0F) / 2.0F, soundtype.getPitch() * 0.8F); + --stack.stackSize; - return (state.getBlock() == ModBlocks.blockRift || state.equals(Blocks.AIR) || state.getMaterial().isReplaceable()); + //fetch the TileEntityDimDoor at the top block of where the door has just been placed + TileEntityDimDoor newTileEntityDimDoor = (TileEntityDimDoor) world.getTileEntity(pos.up()); + //set the tile-entity's initial data + newTileEntityDimDoor.uponDoorPlacement(possibleOldRift); + return EnumActionResult.SUCCESS; + } else { + return EnumActionResult.FAIL; + } } - + /** * Copied from minecraft Item.class TODO we probably can improve this + * //@todo * * @param world * @param player diff --git a/src/main/java/com/zixiken/dimdoors/shared/Pocket.java b/src/main/java/com/zixiken/dimdoors/shared/Pocket.java index 44540ba0..a7a23ebf 100644 --- a/src/main/java/com/zixiken/dimdoors/shared/Pocket.java +++ b/src/main/java/com/zixiken/dimdoors/shared/Pocket.java @@ -5,8 +5,10 @@ */ package com.zixiken.dimdoors.shared; +import com.zixiken.dimdoors.tileentities.DDTileEntityBase; import java.util.ArrayList; import java.util.List; +import java.util.Random; import net.minecraft.nbt.NBTBase; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagInt; @@ -19,95 +21,105 @@ import net.minecraft.nbt.NBTTagString; */ class Pocket { - private final int ID; + private int ID; //this gets reset every session private final int size; //in chunks private final int depth; private final EnumPocketType typeID; // dungeon, pocket, or personal pocket private final int x; //pocket-relative 0 coordinate, should be at x * PocketRegistry.Instance.gridSize * 16 private final int z; //pocket-relative 0 coordinate, should be at z * PocketRegistry.Instance.gridSize * 16 private final List playerUUIDs; - private final List doorIDs; //first one of these should be the entrance door? Does that even matter? - private final int entranceDoorID; + private final List riftIDs; //@todo first one of these should be the entrance door id? Does that even matter? //when adding any new variables, don't forget to add them to the write and load functions - public Pocket(int ID, int size, int depth, EnumPocketType typeID, int x, int z, int entranceDoorID) { - this.ID = ID; + public Pocket(int size, int depth, EnumPocketType typeID, int x, int z, List riftIDs) { this.size = size; this.depth = depth; this.typeID = typeID; this.x = x; this.z = z; - this.entranceDoorID = entranceDoorID; //keeping this stored after pocket generation for personal pocket dimensions mostly + this.riftIDs = riftIDs; playerUUIDs = new ArrayList(); - doorIDs = new ArrayList(); + PocketRegistry.Instance.registerNewPocket(this); + + for(int riftID: riftIDs) { + DDTileEntityBase rift = (DDTileEntityBase) RiftRegistry.Instance.getRiftLocation(riftID).getTileEntity(); + rift.setPocketID(this.ID); //set the rift's pocket ID to this pocket's pocket ID; + } } public int getID() { return ID; } - + public int getX() { return x; } - + public int getZ() { return z; } int getEntranceDoorID() { - return entranceDoorID; + if (riftIDs.isEmpty()) { + return -1; + } else if (riftIDs.size() == 1) { + return riftIDs.get(0); + } else { + Random random = new Random(); + int index = random.nextInt(riftIDs.size()); + return riftIDs.get(index); + } + } + + public void setID(int newID) { + ID = newID; } - static Pocket readFromNBT(NBTTagCompound pocketNBT) { - int ID = pocketNBT.getInteger("ID");; + static void readFromNBT(NBTTagCompound pocketNBT) { int size = pocketNBT.getInteger("size"); int depth = pocketNBT.getInteger("depth"); EnumPocketType typeID = EnumPocketType.getFromInt(pocketNBT.getInteger("typeID")); int x = pocketNBT.getInteger("x"); int z = pocketNBT.getInteger("z"); - int entranceDoorID = pocketNBT.getInteger("entranceDoorID"); - Pocket pocket = new Pocket(ID, size, depth, typeID, x, z, entranceDoorID); + List riftIDs = new ArrayList(); + NBTTagList doorsTagList = (NBTTagList) pocketNBT.getTag("doorIDs"); + for (int i = 0; i < doorsTagList.tagCount(); i++) { + int doorID = doorsTagList.getIntAt(i); + riftIDs.add(doorID); + } + Pocket pocket = new Pocket(size, depth, typeID, x, z, riftIDs); - NBTTagList playersTagList = (NBTTagList) pocketNBT.getTag("playerUUIDs"); + NBTTagList playersTagList = (NBTTagList) pocketNBT.getTag("playerUUIDs"); //@todo, maybe it is bad practice to put this behind the creation statement of the Pocket? for (int i = 0; i < playersTagList.tagCount(); i++) { String playerUUID = playersTagList.getStringTagAt(i); pocket.playerUUIDs.add(playerUUID); } - - NBTTagList doorsTagList = (NBTTagList) pocketNBT.getTag("doorIDs"); - for (int i = 0; i < doorsTagList.tagCount(); i++) { - int doorID = doorsTagList.getIntAt(i); - pocket.doorIDs.add(doorID); - } - - return pocket; + + PocketRegistry.Instance.registerNewPocket(pocket); } static NBTBase writeToNBT(Pocket pocket) { NBTTagCompound pocketNBT = new NBTTagCompound(); - - pocketNBT.setInteger("ID", pocket.ID); pocketNBT.setInteger("size", pocket.size); pocketNBT.setInteger("depth", pocket.depth); pocketNBT.setInteger("typeID", pocket.typeID.getIntValue()); pocketNBT.setInteger("x", pocket.x); pocketNBT.setInteger("z", pocket.z); - pocketNBT.setInteger("entranceDoorID", pocket.entranceDoorID); + + NBTTagList doorsTagList = new NBTTagList(); + for (int i = 0; i < pocket.riftIDs.size(); i++) { + NBTTagInt doorTag = new NBTTagInt(pocket.riftIDs.get(i)); + doorsTagList.appendTag(doorTag); + } + pocketNBT.setTag("doorIDs", doorsTagList); NBTTagList playersTagList = new NBTTagList(); for (int i = 0; i < pocket.playerUUIDs.size(); i++) { NBTTagString playerTag = new NBTTagString(pocket.playerUUIDs.get(i)); playersTagList.appendTag(playerTag); } + pocketNBT.setTag("playerUUIDs", playersTagList); - - NBTTagList doorsTagList = new NBTTagList(); - for (int i = 0; i < pocket.doorIDs.size(); i++) { - NBTTagInt doorTag = new NBTTagInt(pocket.doorIDs.get(i)); - doorsTagList.appendTag(doorTag); - } - pocketNBT.setTag("doorIDs", doorsTagList); - return pocketNBT; } } diff --git a/src/main/java/com/zixiken/dimdoors/shared/PocketRegistry.java b/src/main/java/com/zixiken/dimdoors/shared/PocketRegistry.java index a7e4f11c..220e4ae1 100644 --- a/src/main/java/com/zixiken/dimdoors/shared/PocketRegistry.java +++ b/src/main/java/com/zixiken/dimdoors/shared/PocketRegistry.java @@ -72,13 +72,12 @@ public class PocketRegistry { maxPocketSize = nbt.getInteger("maxPocketSize"); privatePocketSize = nbt.getInteger("privatePocketSize"); publicPocketSize = nbt.getInteger("publicPocketSize"); - nextUnusedID = nbt.getInteger("nextUnusedID"); + nextUnusedID = nbt.getInteger("nextUnusedID"); //@todo, we might not need to save this if (nbt.hasKey("pocketData")) { NBTTagList pocketTagList = (NBTTagList) nbt.getTag("pocketData"); for (int i = 0; i < pocketTagList.tagCount(); i++) { NBTTagCompound pocketTag = pocketTagList.getCompoundTagAt(i); - Pocket pocket = Pocket.readFromNBT(pocketTag); - pocketList.put(pocket.getID(), pocket); + Pocket.readFromNBT(pocketTag); //this also re-registers the pocket } } } else { //load privates from config @@ -101,6 +100,7 @@ public class PocketRegistry { public int registerNewPocket(Pocket pocket) { pocketList.put(nextUnusedID, pocket); + pocket.setID(nextUnusedID); nextUnusedID++; PocketSavedData.get(DimDoors.getDefWorld()).markDirty(); //Notify that this needs to be saved on world save @@ -129,17 +129,14 @@ public class PocketRegistry { } private Pocket generateRandomPocketAt(EnumPocketType typeID, int depth, Location shortenedLocation) { - int x = shortenedLocation.getPos().getX(); - int z = shortenedLocation.getPos().getZ(); - int actualX = x * gridSize * 16; - int actualZ = z * gridSize * 16; + int shortenedX = shortenedLocation.getPos().getX(); + int shortenedZ = shortenedLocation.getPos().getZ(); int dimID = shortenedLocation.getDimensionID(); - PocketTemplate pocketPlacer = getPocketTemplate(typeID, depth, maxPocketSize); + PocketTemplate pocketTemplate = getRandomPocketTemplate(typeID, depth, maxPocketSize); - int entranceDoorID = pocketPlacer.place(actualX, 0, actualZ, dimID); - - Pocket pocket = new Pocket(nextUnusedID, pocketPlacer.getSize(), depth, typeID, x, z, entranceDoorID); + Pocket pocket = pocketTemplate.place(shortenedX, 0, shortenedZ, gridSize, dimID, nextUnusedID, depth, typeID); + nextUnusedID++; return pocket; } @@ -157,7 +154,7 @@ public class PocketRegistry { return location; } - private PocketTemplate getPocketTemplate(EnumPocketType typeID, int depth, int maxPocketSize) { + private PocketTemplate getRandomPocketTemplate(EnumPocketType typeID, int depth, int maxPocketSize) { switch (typeID) { case PRIVATE: return SchematicHandler.Instance.getPersonalPocketSchematic(maxPocketSize); diff --git a/src/main/java/com/zixiken/dimdoors/shared/PocketTemplate.java b/src/main/java/com/zixiken/dimdoors/shared/PocketTemplate.java index 82199728..5429066e 100644 --- a/src/main/java/com/zixiken/dimdoors/shared/PocketTemplate.java +++ b/src/main/java/com/zixiken/dimdoors/shared/PocketTemplate.java @@ -81,9 +81,13 @@ class PocketTemplate { //there is exactly one pocket placer for each different s this.schematic = schematic; } - int place(int xBase, int yBase, int zBase, int dimID) { //returns the riftID of the entrance DimDoor + Pocket place(int shortenedX, int yBase, int shortenedZ, int gridSize, int dimID, int pocketID, int depth, EnumPocketType pocketTypeID) { //returns the riftID of the entrance DimDoor + int xBase = shortenedX * gridSize * 16; + int zBase = shortenedZ * gridSize * 16; + if (schematic == null) { DimDoors.log(this.getClass(), "The schematic for variant " + variantName + " somehow didn't load correctly against despite all precautions."); + return null; } //@todo make sure that the door tile entities get registered! WorldServer world = DimDoors.proxy.getWorldServer(dimID); @@ -100,11 +104,13 @@ class PocketTemplate { //there is exactly one pocket placer for each different s List rifts = new ArrayList(); for (NBTTagCompound tileEntityNBT : schematic.tileEntities) { BlockPos pos = new BlockPos(xBase + tileEntityNBT.getInteger("x"), yBase + tileEntityNBT.getInteger("y"), zBase + tileEntityNBT.getInteger("z")); - IBlockState state = world.getBlockState(pos); - state.getBlock().createTileEntity(world, state); + //IBlockState state = world.getBlockState(pos); + //state.getBlock().createTileEntity(world, state); //this should not be needed. The blocks will already have created their respecitve tile-entities TileEntity tileEntity = world.getTileEntity(pos); - tileEntity.readFromNBT(tileEntityNBT); - tileEntity.markDirty(); + if (tileEntity != null) { + tileEntity.readFromNBT(tileEntityNBT); + tileEntity.markDirty(); + } if (tileEntity instanceof DDTileEntityBase) { DDTileEntityBase rift = (DDTileEntityBase) tileEntity; @@ -112,23 +118,14 @@ class PocketTemplate { //there is exactly one pocket placer for each different s } } - List dimDoorTiles = new ArrayList(); + List riftIDs = new ArrayList(); for (DDTileEntityBase rift : rifts) { + rift.setIsInDungeon(true); rift.register(); - if (rift instanceof TileEntityDimDoor) { - TileEntityDimDoor dimDoorTile = (TileEntityDimDoor) rift; - dimDoorTiles.add(dimDoorTile); - } - } - - if (dimDoorTiles.isEmpty()) { - return -1; - } else if (dimDoorTiles.size() == 1) { - return dimDoorTiles.get(0).getRiftID(); - } else { - Random random = new Random(); - int index = random.nextInt(dimDoorTiles.size()); - return dimDoorTiles.get(index).getRiftID(); + RiftRegistry.Instance.registerRiftAtDepth(rift.getRiftID(), depth); + riftIDs.add(rift.getRiftID()); } + + return new Pocket(size, depth, pocketTypeID, shortenedX, shortenedZ, riftIDs); } } diff --git a/src/main/java/com/zixiken/dimdoors/shared/RiftRegistry.java b/src/main/java/com/zixiken/dimdoors/shared/RiftRegistry.java index f836d995..9df9903b 100644 --- a/src/main/java/com/zixiken/dimdoors/shared/RiftRegistry.java +++ b/src/main/java/com/zixiken/dimdoors/shared/RiftRegistry.java @@ -30,7 +30,7 @@ public class RiftRegistry { // Privates private int nextRiftID; private int maximumDungeonDepth = 2; - private final Map riftList; //maps all rifts in the world to their ID + private final Map riftList; //maps all rifts in the world to their ID //@todo, make this a List of (comparable) locations? //@todo somehow remove rifts from this list even if they are removed in creative private final Map unpairedRiftList; //maps of all rifts in the world that are not paired to their ID private final List> unpairedDepthRiftList; //List of all "unpairedRiftList s" per Dungeon Depth. Depth 0 is almost anything outside the dungeon dimension diff --git a/src/main/java/com/zixiken/dimdoors/shared/Schematic.java b/src/main/java/com/zixiken/dimdoors/shared/Schematic.java index c2368ec6..8e9f4248 100644 --- a/src/main/java/com/zixiken/dimdoors/shared/Schematic.java +++ b/src/main/java/com/zixiken/dimdoors/shared/Schematic.java @@ -84,7 +84,7 @@ class Schematic { blockString = blockStateString; stateString = ""; } - Block block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(blockString)); //@todo is this okay? + Block block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(blockString)); IBlockState blockstate = block.getDefaultState(); if (!stateString.equals("")) { @@ -119,31 +119,26 @@ class Schematic { for (int i = 0; i < properties.length; i++) { String propertyString = properties[i]; String[] propertyAndBlockStrings = propertyString.split("="); - String propertyName = propertyAndBlockStrings[0]; - String blockValue = propertyAndBlockStrings[1]; - propertyAndBlockStringsMap.put(propertyName, blockValue); + propertyAndBlockStringsMap.put(propertyAndBlockStrings[0], propertyAndBlockStrings[1]); } BlockStateContainer container = block.getBlockState(); Collection possibleBlockStates = container.getValidStates(); - +int newInt = possibleBlockStates.size(); IBlockState chosenState = block.getDefaultState(); - for (IBlockState blockState : possibleBlockStates) { - for (Entry entry : propertyAndBlockStringsMap.entrySet()) { - IProperty property = container.getProperty(entry.getKey()); - if (property != null) { - Comparable value = null; - for (Comparable object : property.getAllowedValues()) { - if (object.equals(entry.getValue())) { - value = object; - break; - } - } - if (value != null) { - chosenState = chosenState.withProperty((IProperty) property, (Comparable) value); + for (Entry entry : propertyAndBlockStringsMap.entrySet()) { + IProperty property = container.getProperty(entry.getKey()); + if (property != null) { + Comparable value = null; + for (Comparable object : property.getAllowedValues()) { + if (object.equals(entry.getValue())) { + value = object; + break; } } + if (value != null) { + chosenState = chosenState.withProperty((IProperty) property, (Comparable) value); + } } - } return chosenState; } diff --git a/src/main/java/com/zixiken/dimdoors/shared/SchematicHandler.java b/src/main/java/com/zixiken/dimdoors/shared/SchematicHandler.java index d109d307..283c9f61 100644 --- a/src/main/java/com/zixiken/dimdoors/shared/SchematicHandler.java +++ b/src/main/java/com/zixiken/dimdoors/shared/SchematicHandler.java @@ -133,56 +133,49 @@ public class SchematicHandler { JsonArray variations = jsonTemplate.getAsJsonArray("variations"); List pocketTemplates = new ArrayList(); - if (jsonType.equals("Singular")) { //@todo, make sure there is only one Json-reader block, instead of one and its copy - JsonObject chosenVariation = null; - int chosenVariationSize = 0; - for (int i = 0; i < variations.size(); i++) { - JsonObject variation = variations.get(i).getAsJsonObject(); - int variationSize = variation.get("size").getAsInt(); - if (variationSize <= maxPocketSize && variationSize > chosenVariationSize) { + JsonObject chosenVariation = null; //only applicable if jsonType == "Singular" + int chosenVariationSize = 0; //only applicable if jsonType == "Singular" + List validVariations = new ArrayList(); + //put all valid variation JsonObjects into an array list + for (int i = 0; i < variations.size(); i++) { + JsonObject variation = variations.get(i).getAsJsonObject(); + int variationSize = variation.get("size").getAsInt(); + + if (variationSize > maxPocketSize) { + //do not add it + } else if (jsonType.equals("Singular")) { + if (variationSize > chosenVariationSize) { chosenVariationSize = variationSize; chosenVariation = variation; if (variationSize == maxPocketSize) { break; //this one gets chosen } } + } else if (jsonType.equals("Multiple")) { + validVariations.add(variation); + } else { //@todo more options? + DimDoors.log(this.getClass(), "JsonType " + jsonType + " is not a valid JsonType. Json was not loaded."); } - if (chosenVariation != null) { - //this block equals - String variantName = chosenVariation.get("variantName").getAsString(); - EnumPocketType typeID = EnumPocketType.getFromInt(chosenVariation.get("typeID").getAsInt()); - int minDepth = chosenVariation.get("minDepth").getAsInt(); - int maxDepth = chosenVariation.get("maxDepth").getAsInt(); - JsonArray weightsJsonArray = chosenVariation.get("weights").getAsJsonArray(); - int[] weights = new int[weightsJsonArray.size()]; - for (int i = 0; i < weightsJsonArray.size(); i++) { - weights[i] = weightsJsonArray.get(i).getAsInt(); - } - PocketTemplate pocketTemplate = new PocketTemplate(variantName, chosenVariationSize, typeID, minDepth, maxDepth, weights); - pocketTemplates.add(pocketTemplate); - ///this block equals + } + if (chosenVariation != null) { + validVariations.add(chosenVariation); + } + + //convert the valid variations arraylist to a list of pocket templates + for (JsonObject variation : validVariations) { + String variantName = variation.get("variantName").getAsString(); + int variationSize = variation.get("size").getAsInt(); + int minDepth = variation.get("minDepth").getAsInt(); + int maxDepth = variation.get("maxDepth").getAsInt(); + JsonArray weightsJsonArray = variation.get("weights").getAsJsonArray(); + int[] weights = new int[weightsJsonArray.size()]; + for (int j = 0; j < weightsJsonArray.size(); j++) { + weights[j] = weightsJsonArray.get(j).getAsInt(); } - } else if (jsonType.equals("Multiple")) { - for (int i = 0; i < variations.size(); i++) { - JsonObject variation = variations.get(i).getAsJsonObject(); - int variationSize = variation.get("size").getAsInt(); - if (variationSize <= maxPocketSize) { - //this block - String variantName = variation.get("variantName").getAsString(); - EnumPocketType typeID = EnumPocketType.getFromInt(variation.get("typeID").getAsInt()); - int minDepth = variation.get("minDepth").getAsInt(); - int maxDepth = variation.get("maxDepth").getAsInt(); - JsonArray weightsJsonArray = variation.get("weights").getAsJsonArray(); - int[] weights = new int[weightsJsonArray.size()]; - for (int j = 0; j < weightsJsonArray.size(); j++) { - weights[j] = weightsJsonArray.get(j).getAsInt(); - } - PocketTemplate pocketTemplate = new PocketTemplate(variantName, variationSize, typeID, minDepth, maxDepth, weights); - pocketTemplates.add(pocketTemplate); - ///this block - } - } - } //@todo, more options? + PocketTemplate pocketTemplate = new PocketTemplate(variantName, variationSize, pocketType, minDepth, maxDepth, weights); + pocketTemplates.add(pocketTemplate); + } + return pocketTemplates; } } diff --git a/src/main/java/com/zixiken/dimdoors/tileentities/DDTileEntityBase.java b/src/main/java/com/zixiken/dimdoors/tileentities/DDTileEntityBase.java index 7168351b..00c693a8 100644 --- a/src/main/java/com/zixiken/dimdoors/tileentities/DDTileEntityBase.java +++ b/src/main/java/com/zixiken/dimdoors/tileentities/DDTileEntityBase.java @@ -1,6 +1,5 @@ package com.zixiken.dimdoors.tileentities; -import com.zixiken.dimdoors.DimDoors; import com.zixiken.dimdoors.shared.Location; import com.zixiken.dimdoors.shared.RiftRegistry; import java.util.Random; @@ -63,11 +62,12 @@ public abstract class DDTileEntityBase extends TileEntity { public void readFromNBT(NBTTagCompound nbt) { super.readFromNBT(nbt); try { - this.isPaired = nbt.getBoolean("isPaired"); - this.riftID = nbt.getInteger("riftID"); - this.pairedRiftID = nbt.getInteger("pairedRiftID"); + isPaired = nbt.getBoolean("isPaired"); + riftID = nbt.getInteger("riftID"); + pairedRiftID = nbt.getInteger("pairedRiftID"); } catch (Exception e) { } + register(); //only actually gets registered if riftID == -1 } @Override diff --git a/src/main/java/com/zixiken/dimdoors/tileentities/TileEntityDimDoor.java b/src/main/java/com/zixiken/dimdoors/tileentities/TileEntityDimDoor.java index 198341f4..bccf3850 100644 --- a/src/main/java/com/zixiken/dimdoors/tileentities/TileEntityDimDoor.java +++ b/src/main/java/com/zixiken/dimdoors/tileentities/TileEntityDimDoor.java @@ -1,10 +1,13 @@ package com.zixiken.dimdoors.tileentities; +import com.zixiken.dimdoors.blocks.BlockDimDoor; import com.zixiken.dimdoors.shared.Location; import com.zixiken.dimdoors.shared.RiftRegistry; import java.util.Random; +import javax.annotation.Nullable; import net.minecraft.entity.Entity; import net.minecraft.nbt.NBTTagCompound; +import net.minecraft.tileentity.TileEntity; import net.minecraft.util.EnumFacing; public class TileEntityDimDoor extends DDTileEntityBase { @@ -66,13 +69,26 @@ public class TileEntityDimDoor extends DDTileEntityBase { @Override public boolean tryTeleport(Entity entity) { if (!isPaired()) { - int randomPairedRiftID = RiftRegistry.Instance.getRandomUnpairedRiftID(getRiftID()); + int randomPairedRiftID = RiftRegistry.Instance.getRandomUnpairedRiftID(getRiftID()); if (randomPairedRiftID < 0) { return false; } - RiftRegistry.Instance.pair(getRiftID(),randomPairedRiftID); + RiftRegistry.Instance.pair(getRiftID(), randomPairedRiftID); //@todo try to automatically pair this door somehow } return RiftRegistry.Instance.teleportEntityToRift(entity, getPairedRiftID()); } + + public void uponDoorPlacement(@Nullable TileEntity possibleOldRift) { + if (possibleOldRift instanceof DDTileEntityBase) { + DDTileEntityBase oldRift = (DDTileEntityBase) possibleOldRift; + //load data from old rift (that must already have been registered) + loadDataFrom(oldRift); + } else { + //default data and set register this rift in the registry + register(); + } + //storing the orientation inside the tile-entity, because that thing can actually save the orientation in the worldsave, unlike the block itself, which fail at that stuff somehow + this.orientation = this.getWorld().getBlockState(this.getPos()).getValue(BlockDimDoor.FACING).getOpposite(); + } }