From 68881e17ce511cdf9b22e31f7ae92b8ea6d33fe4 Mon Sep 17 00:00:00 2001 From: Waterpicker Date: Mon, 16 Jan 2017 19:57:53 -0600 Subject: [PATCH] DimDoor and Rift- (re)placement Main: Removed "custom" code for DimDoors' Placement and rewrote it When right-clicking rifts with any Dimensional Door, the game will try to place the Dimensional Door onto the rift. Rifts will now enherit their properties from broken DimDoors and Dimdoors will enherit their properties from rifts they are placed over. Other: Made the DimDoors logger a bit more powerful. Made RiftRegistry reset on server-load Created a setup for the RiftConnectionTool Item. Layout: Fixed TileEntityRift.java's indentation Changed some variable names Authored by Robijnvogel and squashed by Waterpicker. --- .../com/zixiken/dimdoors/DDProxyCommon.java | 1 - .../java/com/zixiken/dimdoors/DimDoors.java | 6 +- .../zixiken/dimdoors/EventHookContainer.java | 24 -- .../dimdoors/blocks/BlockDimDoorBase.java | 47 ++-- .../zixiken/dimdoors/blocks/BlockRift.java | 10 +- .../zixiken/dimdoors/items/ItemDoorBase.java | 192 +++++++-------- .../items/ItemRiftConnectionTool.java | 45 ++++ .../zixiken/dimdoors/shared/RiftRegistry.java | 12 +- .../tileentities/DDTileEntityBase.java | 2 +- .../dimdoors/tileentities/TileEntityRift.java | 228 +++++++++--------- 10 files changed, 297 insertions(+), 270 deletions(-) delete mode 100644 src/main/java/com/zixiken/dimdoors/EventHookContainer.java create mode 100644 src/main/java/com/zixiken/dimdoors/items/ItemRiftConnectionTool.java diff --git a/src/main/java/com/zixiken/dimdoors/DDProxyCommon.java b/src/main/java/com/zixiken/dimdoors/DDProxyCommon.java index ba4eec7a..725bebd4 100644 --- a/src/main/java/com/zixiken/dimdoors/DDProxyCommon.java +++ b/src/main/java/com/zixiken/dimdoors/DDProxyCommon.java @@ -22,7 +22,6 @@ public abstract class DDProxyCommon implements IDDProxy { @Override public void onPreInitialization(FMLPreInitializationEvent event) { - MinecraftForge.EVENT_BUS.register(new EventHookContainer()); ModBlocks.registerBlocks(); ModItems.registerItems(); diff --git a/src/main/java/com/zixiken/dimdoors/DimDoors.java b/src/main/java/com/zixiken/dimdoors/DimDoors.java index d804d48f..320bdb28 100644 --- a/src/main/java/com/zixiken/dimdoors/DimDoors.java +++ b/src/main/java/com/zixiken/dimdoors/DimDoors.java @@ -2,6 +2,7 @@ package com.zixiken.dimdoors; import com.zixiken.dimdoors.items.ModItems; import com.zixiken.dimdoors.shared.PocketSavedData; +import com.zixiken.dimdoors.shared.RiftRegistry; import com.zixiken.dimdoors.shared.RiftSavedData; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.item.Item; @@ -47,6 +48,7 @@ public class DimDoors { @Mod.EventHandler public void serverLoad(FMLServerStartingEvent event) { //@todo event.registerServerCommand( new DDCommand() ); //to register commands that this mod offers? + RiftRegistry.Instance.reset(); PocketSavedData.get(getDefWorld()); RiftSavedData.get(getDefWorld()); } @@ -63,7 +65,7 @@ public class DimDoors { return proxy.getDefWorld(); //gets the server or client world dim 0 handler } - public static void log(String text) { - FMLLog.info("[DimDoors] " + text, 0); + public static void log(Class classFiredFrom, String text) { + FMLLog.info("[DimDoors] " + text + " (" + classFiredFrom.toString() + " )", 0); } } diff --git a/src/main/java/com/zixiken/dimdoors/EventHookContainer.java b/src/main/java/com/zixiken/dimdoors/EventHookContainer.java deleted file mode 100644 index 80da22dc..00000000 --- a/src/main/java/com/zixiken/dimdoors/EventHookContainer.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.zixiken.dimdoors; - -import com.zixiken.dimdoors.items.ItemDoorBase; -import net.minecraft.item.ItemStack; -import net.minecraft.util.EnumHand; -import net.minecraft.world.World; -import net.minecraftforge.event.entity.player.PlayerInteractEvent; -import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; - -public class EventHookContainer { - - @SubscribeEvent - public void onPlayerEvent(PlayerInteractEvent event) { - // Handle all door placement here - - World world = event.getEntity().world; - ItemStack stack = event.getEntityPlayer().inventory.getCurrentItem(); - if (event.getHand() == EnumHand.OFF_HAND //right-click - && stack != null && ItemDoorBase.tryToPlaceDoor(stack, event.getEntityPlayer(), world, event.getPos(), event.getFace())) // Cancel the event so that we don't get two doors from vanilla doors - { - event.setCanceled(true); - } - } -} diff --git a/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorBase.java b/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorBase.java index bfa48264..61c52506 100644 --- a/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorBase.java +++ b/src/main/java/com/zixiken/dimdoors/blocks/BlockDimDoorBase.java @@ -3,9 +3,10 @@ package com.zixiken.dimdoors.blocks; import java.util.Random; import com.zixiken.dimdoors.DimDoors; +import com.zixiken.dimdoors.shared.RiftRegistry; import com.zixiken.dimdoors.tileentities.DDTileEntityBase; import com.zixiken.dimdoors.tileentities.TileEntityDimDoor; - +import javax.annotation.Nullable; import net.minecraft.block.BlockDoor; import net.minecraft.block.ITileEntityProvider; import net.minecraft.block.material.Material; @@ -13,6 +14,7 @@ import net.minecraft.block.state.IBlockState; import net.minecraft.entity.Entity; import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.init.Blocks; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.tileentity.TileEntity; @@ -24,9 +26,6 @@ import net.minecraft.world.World; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; -import javax.annotation.Nullable; -import net.minecraft.nbt.NBTTagCompound; - public abstract class BlockDimDoorBase extends BlockDoor implements IDimDoor, ITileEntityProvider { public BlockDimDoorBase(Material material) { @@ -153,22 +152,19 @@ public abstract class BlockDimDoorBase extends BlockDoor implements IDimDoor, IT @Override public void breakBlock(World world, BlockPos pos, IBlockState state) { - DDTileEntityBase origRift; - BlockPos pos2 = pos; - // This function runs on the server side after a block is replaced - // We MUST call super.breakBlock() since it involves removing tile entities - if (state.getValue(BlockDoor.HALF) == EnumDoorHalf.LOWER) { - pos2 = pos.up(); - origRift = (DDTileEntityBase) world.getTileEntity(pos2); - world.setBlockToAir(pos2); - } else { + DDTileEntityBase origRift = null; + boolean isTopHalf = state.getValue(BlockDoor.HALF) == EnumDoorHalf.UPPER; + if (isTopHalf) { origRift = (DDTileEntityBase) world.getTileEntity(pos); + RiftRegistry.Instance.setLastChangedRift(origRift); } super.breakBlock(world, pos, state); - world.setBlockState(pos2, ModBlocks.blockRift.getDefaultState()); - DDTileEntityBase newRift = (DDTileEntityBase) world.getTileEntity(pos2); - newRift.loadDataFrom(origRift); - //DimDoors.log("" +newRift.riftID); + if (isTopHalf) { + world.setBlockState(pos, ModBlocks.blockRift.getDefaultState()); + DDTileEntityBase newRift = (DDTileEntityBase) world.getTileEntity(pos); + newRift.loadDataFrom(origRift); + DimDoors.log(this.getClass(), "New Rift rift-ID after breaking door " + newRift.riftID); + } } public DDTileEntityBase getRiftTile(World world, BlockPos pos, IBlockState state) { @@ -180,4 +176,21 @@ public abstract class BlockDimDoorBase extends BlockDoor implements IDimDoor, IT } return (DDTileEntityBase) tileEntity; } + + @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); + } + + private boolean canPlaceBottomAt(World worldIn, BlockPos pos, IBlockState state) { + return (state.equals(Blocks.AIR) || state.getBlock().isReplaceable(worldIn, pos)); + } + + private boolean canPlaceTopAt(World worldIn, BlockPos pos, IBlockState state) { + return (state.getBlock() == ModBlocks.blockRift || state.equals(Blocks.AIR) || state.getMaterial().isReplaceable()); + } } diff --git a/src/main/java/com/zixiken/dimdoors/blocks/BlockRift.java b/src/main/java/com/zixiken/dimdoors/blocks/BlockRift.java index 7f3474fe..55febcb5 100644 --- a/src/main/java/com/zixiken/dimdoors/blocks/BlockRift.java +++ b/src/main/java/com/zixiken/dimdoors/blocks/BlockRift.java @@ -28,7 +28,6 @@ import net.minecraft.world.World; import net.minecraftforge.fluids.IFluidBlock; import com.zixiken.dimdoors.client.GoggleRiftFX; import com.zixiken.dimdoors.tileentities.DDTileEntityBase; -import net.minecraft.block.BlockDoor; import net.minecraftforge.fml.client.FMLClientHandler; import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.SideOnly; @@ -69,6 +68,11 @@ public class BlockRift extends Block implements ITileEntityProvider { blocksImmuneToRift.add(Blocks.DIAMOND_BLOCK); blocksImmuneToRift.add(Blocks.EMERALD_BLOCK); } + + @Override + public boolean isReplaceable(IBlockAccess worldIn, BlockPos pos) { + return false; + } @Override public boolean isCollidable() { @@ -156,8 +160,8 @@ public class BlockRift extends Block implements ITileEntityProvider { } public boolean tryPlacingRift(World world, BlockPos pos) { - return world != null && !isBlockImmune(world, pos) && - world.setBlockState(pos, getDefaultState()); //@todo This returns false, because this block does not have blockstates configured correctly. !isBlockImmune doesn't seem to be ture either though... + return world != null && !isBlockImmune(world, pos) + && world.setBlockState(pos, getDefaultState()); //@todo This returns false, because this block does not have blockstates configured correctly. !isBlockImmune doesn't seem to be true either though... } public boolean isBlockImmune(World world, BlockPos pos) { diff --git a/src/main/java/com/zixiken/dimdoors/items/ItemDoorBase.java b/src/main/java/com/zixiken/dimdoors/items/ItemDoorBase.java index da45352c..66c75817 100644 --- a/src/main/java/com/zixiken/dimdoors/items/ItemDoorBase.java +++ b/src/main/java/com/zixiken/dimdoors/items/ItemDoorBase.java @@ -20,7 +20,8 @@ import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.RayTraceResult; import net.minecraft.util.math.Vec3d; import net.minecraft.world.World; -import com.zixiken.dimdoors.tileentities.TileEntityDimDoor; +import net.minecraft.block.SoundType; +import static net.minecraft.item.ItemDoor.placeDoor; import net.minecraft.tileentity.TileEntity; public abstract class ItemDoorBase extends ItemDoor { @@ -50,130 +51,90 @@ public abstract class ItemDoorBase extends ItemDoor { public abstract void addInformation(ItemStack stack, EntityPlayer playerIn, List tooltip, boolean advanced); /** - * Overriden in subclasses to specify which door block that door item will + * Overridden in subclasses to specify which door block that door item will * place * * @return */ protected abstract BlockDimDoorBase getDoorBlock(); - /** - * Overriden here to remove vanilla block placement functionality from - * dimensional doors, we handle this in the EventHookContainer - */ + @Override + public ActionResult onItemRightClick(ItemStack stack, World worldIn, EntityPlayer playerIn, EnumHand hand) { + RayTraceResult hit = ItemDoorBase.doRayTrace(worldIn, playerIn, true); + if (hit != null) { + DimDoors.log(this.getClass(), "Hit is not null"); + BlockPos pos = hit.getBlockPos(); + if (worldIn.getBlockState(pos).getBlock() == ModBlocks.blockRift) { + DimDoors.log(this.getClass(), "Block is a 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); + return new ActionResult(canDoorBePlacedOnGroundBelowRift, stack); + } + } + DimDoors.log(this.getClass(), "Hit is null"); + return new ActionResult(EnumActionResult.PASS, stack); + } + @Override public EnumActionResult onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) { - return EnumActionResult.FAIL; - } - - /** - * Tries to place a door as a dimensional door - * - * @param stack - * @param player - * @param world - * @param side - * @return - */ - public static boolean tryToPlaceDoor(ItemStack stack, EntityPlayer player, World world, BlockPos pos, EnumFacing side) { - if (world.isRemote) { - return false; + //@todo also check for rift raytracing replacement; + if (worldIn.isRemote) { + return EnumActionResult.FAIL; } + IBlockState iblockstate = worldIn.getBlockState(pos); + Block block = iblockstate.getBlock(); + if (side != EnumFacing.UP || block == Blocks.AIR) { + return EnumActionResult.FAIL; + } else { - // 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 false; - } - BlockDimDoorBase doorBlock = mappedItem.getDoorBlock(); - return ItemDoorBase.placeDoorOnBlock(doorBlock, stack, player, world, pos, side) - || ItemDoorBase.placeDoorOnRift(doorBlock, world, player, stack); - } - - /** - * try to place a door block on a block - * - * @param doorBlock - * @param stack - * @param player - * @param world - * @param pos - * @param side - * @return - */ - public static boolean placeDoorOnBlock(Block doorBlock, ItemStack stack, EntityPlayer player, - World world, BlockPos pos, EnumFacing side) { - if (world.isRemote) { - return false; - } - - // Only place doors on top of blocks - check if we're targeting the top - // side - if (side == EnumFacing.UP) { - Block block = world.getBlockState(pos).getBlock(); - if (!world.getBlockState(pos).equals(Blocks.AIR) && !block.isReplaceable(world, pos)) { - pos = pos.up(); + if (!block.isReplaceable(worldIn, pos)) { + pos = pos.offset(side); //we know that (side == EnumFacing.UP) } - BlockPos upPos = pos.up(); - if (canPlace(world, pos) && canPlace(world, upPos) && player.canPlayerEdit(pos, side, stack) - && player.canPlayerEdit(upPos, side, stack) && stack.stackSize > 0 - && stack.getItem() instanceof ItemDoorBase && world.getBlockState(pos.down()).isSideSolid(world, pos, side)) { - placeDoor(world, pos, EnumFacing.fromAngle(player.rotationYaw), doorBlock, true); - TileEntity tileEntity = world.getTileEntity(pos.up()); - if (tileEntity instanceof DDTileEntityBase) { - DDTileEntityBase rift = (DDTileEntityBase) tileEntity; - rift.register(); + // 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()); + //start logging code + if (possibleOldRift instanceof DDTileEntityBase) { // + DDTileEntityBase oldRift = (DDTileEntityBase) possibleOldRift; + DimDoors.log(this.getClass(), "Old Rift rift-ID before placement: " + oldRift.riftID); } - if (!player.capabilities.isCreativeMode) { - stack.stackSize--; + //end of logging code + 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; + DimDoors.log(this.getClass(), "Old Rift rift-ID after placement: " + oldRift.riftID); + newTileEntityDimDoor.loadDataFrom(oldRift); + } else { + newTileEntityDimDoor.register(); } - return true; + DimDoors.log(this.getClass(), "New Door rift-ID after placement: " + newTileEntityDimDoor.riftID); + + return EnumActionResult.SUCCESS; + } else { + return EnumActionResult.FAIL; } } - return false; - } - - /** - * uses a raytrace to try and place a door on a rift - * - * @param doorBlock - * @param world - * @param player - * @param stack - * @return - */ - public static boolean placeDoorOnRift(Block doorBlock, World world, EntityPlayer player, ItemStack stack) { - if (world.isRemote) { - return false; - } - - RayTraceResult hit = ItemDoorBase.doRayTrace(world, player, true); - if (hit != null) { - BlockPos pos = hit.getBlockPos(); - if (world.getBlockState(pos).getBlock() == ModBlocks.blockRift) { - BlockPos downPos = pos.down(); - if (player.canPlayerEdit(pos, hit.sideHit, stack) - && player.canPlayerEdit(downPos, hit.sideHit, stack) - && canPlace(world, pos) && canPlace(world, downPos)) { - DDTileEntityBase riftOrig = (DDTileEntityBase) world.getTileEntity(pos); - placeDoor(world, downPos, EnumFacing.fromAngle(player.rotationYaw), doorBlock, true); - TileEntityDimDoor newRift = (TileEntityDimDoor) world.getTileEntity(pos); - if (!(stack.getItem() instanceof ItemDoorBase)) { //@todo why does THIS if statement mean that THAT field should be true? - newRift.hasGennedPair = true; - } - newRift.loadDataFrom(riftOrig); //take over the data from the original Rift - //DimDoors.log("" +newRift.riftID); - if (!player.capabilities.isCreativeMode) { - stack.stackSize--; - } - return true; - } - } - } - return false; } public static boolean canPlace(World world, BlockPos pos) { @@ -219,4 +180,17 @@ public abstract class ItemDoorBase extends ItemDoor { } else */ break; } } + + private boolean canDoorBePlacedOnRift(World worldIn, EntityPlayer playerIn) { + RayTraceResult hit = ItemDoorBase.doRayTrace(worldIn, playerIn, true); + if (hit != null) { + DimDoors.log(this.getClass(), "Hit is not null"); + BlockPos pos = hit.getBlockPos(); + if (worldIn.getBlockState(pos).getBlock() == ModBlocks.blockRift) { + return true; + } + } + DimDoors.log(this.getClass(), "Hit is null, or block at pos is not blockRift"); + return false; + } } diff --git a/src/main/java/com/zixiken/dimdoors/items/ItemRiftConnectionTool.java b/src/main/java/com/zixiken/dimdoors/items/ItemRiftConnectionTool.java new file mode 100644 index 00000000..fe5dfde2 --- /dev/null +++ b/src/main/java/com/zixiken/dimdoors/items/ItemRiftConnectionTool.java @@ -0,0 +1,45 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package com.zixiken.dimdoors.items; + +import com.zixiken.dimdoors.tileentities.DDTileEntityBase; +import java.util.Set; +import net.minecraft.block.Block; +import net.minecraft.entity.player.EntityPlayer; +import net.minecraft.item.Item; +import net.minecraft.item.ItemStack; +import net.minecraft.item.ItemTool; +import net.minecraft.util.ActionResult; +import net.minecraft.util.EnumActionResult; +import net.minecraft.util.EnumHand; +import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.RayTraceResult; +import net.minecraft.world.World; + +/** + * + * @author Robijnvogel + */ +public class ItemRiftConnectionTool extends ItemTool { + + ItemRiftConnectionTool(float attackDamageIn, float attackSpeedIn, Item.ToolMaterial materialIn, Set effectiveBlocksIn){ + super(attackDamageIn, attackSpeedIn, materialIn, effectiveBlocksIn); + //@todo add extra stuff + } + //@todo actually implement this item as a tool and stuff + @Override + public ActionResult onItemRightClick(ItemStack stack, World worldIn, EntityPlayer playerIn, EnumHand hand) { + RayTraceResult hit = ItemDoorBase.doRayTrace(worldIn, playerIn, true); + if (hit != null) { + BlockPos pos = hit.getBlockPos(); + if (worldIn.getTileEntity(pos) instanceof DDTileEntityBase) { + //@todo implementation here? + return new ActionResult(EnumActionResult.PASS, stack); + } + } + return new ActionResult(EnumActionResult.FAIL, stack); + } +} diff --git a/src/main/java/com/zixiken/dimdoors/shared/RiftRegistry.java b/src/main/java/com/zixiken/dimdoors/shared/RiftRegistry.java index e2f6c617..d9828786 100644 --- a/src/main/java/com/zixiken/dimdoors/shared/RiftRegistry.java +++ b/src/main/java/com/zixiken/dimdoors/shared/RiftRegistry.java @@ -19,6 +19,7 @@ import net.minecraft.world.World; */ public class RiftRegistry { + private DDTileEntityBase lastBrokenRift = null; //@todo, redo this functionality in a more refined way public static final RiftRegistry Instance = new RiftRegistry(); // Privates @@ -26,7 +27,7 @@ public class RiftRegistry { private final Map riftList; // Methods - public RiftRegistry() { + private RiftRegistry() { nextRiftID = 0; riftList = new HashMap(); } @@ -34,6 +35,7 @@ public class RiftRegistry { public void reset() { nextRiftID = 0; riftList.clear(); + lastBrokenRift = null; } public void readFromNBT(NBTTagCompound nbt) { @@ -98,4 +100,12 @@ public class RiftRegistry { rift.unpair(); } } + + public void setLastChangedRift(DDTileEntityBase origRift) { + lastBrokenRift = origRift; + } + + public DDTileEntityBase getLastChangedRift() { + return lastBrokenRift; + } } diff --git a/src/main/java/com/zixiken/dimdoors/tileentities/DDTileEntityBase.java b/src/main/java/com/zixiken/dimdoors/tileentities/DDTileEntityBase.java index adb1f099..9fadfbf2 100644 --- a/src/main/java/com/zixiken/dimdoors/tileentities/DDTileEntityBase.java +++ b/src/main/java/com/zixiken/dimdoors/tileentities/DDTileEntityBase.java @@ -77,7 +77,7 @@ public abstract class DDTileEntityBase extends TileEntity { } public void loadDataFrom(DDTileEntityBase rift2) { - if (rift2.riftID != -1) { + if (rift2 != null && rift2.riftID != -1) { isPaired = rift2.isPaired; riftID = rift2.riftID; //should not start at 0 pairedRiftID = rift2.pairedRiftID; diff --git a/src/main/java/com/zixiken/dimdoors/tileentities/TileEntityRift.java b/src/main/java/com/zixiken/dimdoors/tileentities/TileEntityRift.java index b3b1c6f8..6fab7b95 100644 --- a/src/main/java/com/zixiken/dimdoors/tileentities/TileEntityRift.java +++ b/src/main/java/com/zixiken/dimdoors/tileentities/TileEntityRift.java @@ -1,9 +1,9 @@ package com.zixiken.dimdoors.tileentities; +import com.zixiken.dimdoors.blocks.ModBlocks; +import com.zixiken.dimdoors.shared.RiftRegistry; import java.util.List; import java.util.Random; - -import com.zixiken.dimdoors.blocks.ModBlocks; import net.minecraft.entity.Entity; import net.minecraft.entity.monster.EntityEnderman; import net.minecraft.entity.player.EntityPlayer; @@ -13,129 +13,133 @@ import net.minecraft.util.math.AxisAlignedBB; import net.minecraft.util.math.BlockPos; public class TileEntityRift extends DDTileEntityBase implements ITickable { - private static final int ENDERMAN_SPAWNING_CHANCE = 1; - private static final int MAX_ENDERMAN_SPAWNING_CHANCE = 32; - private static final int HOSTILE_ENDERMAN_CHANCE = 1; - private static final int MAX_HOSTILE_ENDERMAN_CHANCE = 3; - private static final int UPDATE_PERIOD = 200; //10 seconds - private static Random random = new Random(); + private static final int ENDERMAN_SPAWNING_CHANCE = 1; + private static final int MAX_ENDERMAN_SPAWNING_CHANCE = 32; + private static final int HOSTILE_ENDERMAN_CHANCE = 1; + private static final int MAX_HOSTILE_ENDERMAN_CHANCE = 3; + private static final int UPDATE_PERIOD = 200; //10 seconds - private int updateTimer; - public BlockPos offset = BlockPos.ORIGIN; - public boolean shouldClose = false; - public int spawnedEndermenID = 0; - - public int riftRotation = random.nextInt(360); - public float growth = 0; - - public TileEntityRift() { + private static Random random = new Random(); + + private int updateTimer; + public BlockPos offset = BlockPos.ORIGIN; + public boolean shouldClose = false; + public int spawnedEndermenID = 0; + + public int riftRotation = random.nextInt(360); + public float growth = 0; + + private static int temp = 0; + + public TileEntityRift() { super(); - // Vary the update times of rifts to prevent all the rifts in a cluster - // from updating at the same time. - updateTimer = random.nextInt(UPDATE_PERIOD); - } - - @Override - public void update() { - if (world.getBlockState(pos).getBlock() != ModBlocks.blockRift) { - invalidate(); - return; - } + this.loadDataFrom(RiftRegistry.Instance.getLastChangedRift()); - // Check if this rift should render white closing particles and - // spread the closing effect to other rifts nearby. - if (shouldClose) { - closeRift(); - return; - } - - if (updateTimer >= UPDATE_PERIOD) { - spawnEndermen(); - updateTimer = 0; - } - else if (updateTimer == UPDATE_PERIOD / 2) { - updateNearestRift(); - } - growth += 1F/(growth+1); - updateTimer++; - } + // Vary the update times of rifts to prevent all the rifts in a cluster + // from updating at the same time. + updateTimer = random.nextInt(UPDATE_PERIOD); + } - private void spawnEndermen() { - if (world.isRemote) return; + @Override + public void update() { + if (world.getBlockState(pos).getBlock() != ModBlocks.blockRift) { + invalidate(); + return; + } - // Ensure that this rift is only spawning one Enderman at a time, to prevent hordes of Endermen - Entity entity = world.getEntityByID(this.spawnedEndermenID); - if (entity != null && entity instanceof EntityEnderman) { - return; - } + // Check if this rift should render white closing particles and + // spread the closing effect to other rifts nearby. + if (shouldClose) { + closeRift(); + return; + } - if (random.nextInt(MAX_ENDERMAN_SPAWNING_CHANCE) < ENDERMAN_SPAWNING_CHANCE) { - // Endermen will only spawn from groups of rifts - if (updateNearestRift()) { - List list = world.getEntitiesWithinAABB(EntityEnderman.class, - new AxisAlignedBB(pos.getX() - 9, pos.getY() - 3, pos.getZ() - 9, pos.getX() + 9, pos.getY() + 3, pos.getZ() + 9)); + if (updateTimer >= UPDATE_PERIOD) { + spawnEndermen(); + updateTimer = 0; + } else if (updateTimer == UPDATE_PERIOD / 2) { + updateNearestRift(); + } + growth += 1F / (growth + 1); + updateTimer++; + } - if (list.isEmpty()) { - EntityEnderman enderman = new EntityEnderman(world); - enderman.setLocationAndAngles(pos.getX() + 0.5, pos.getY() - 1, pos.getZ() + 0.5, 5, 6); - world.spawnEntity(enderman); + private void spawnEndermen() { + if (world.isRemote) { + return; + } - if (random.nextInt(MAX_HOSTILE_ENDERMAN_CHANCE) < HOSTILE_ENDERMAN_CHANCE) { - EntityPlayer player = this.world.getClosestPlayerToEntity(enderman, 50); - if (player != null) { - enderman.setAttackTarget(player); - } - } - } - } - } - } + // Ensure that this rift is only spawning one Enderman at a time, to prevent hordes of Endermen + Entity entity = world.getEntityByID(this.spawnedEndermenID); + if (entity != null && entity instanceof EntityEnderman) { + return; + } - private void closeRift() { - world.setBlockToAir(pos); - growth--; //@todo? - } + if (random.nextInt(MAX_ENDERMAN_SPAWNING_CHANCE) < ENDERMAN_SPAWNING_CHANCE) { + // Endermen will only spawn from groups of rifts + if (updateNearestRift()) { + List list = world.getEntitiesWithinAABB(EntityEnderman.class, + new AxisAlignedBB(pos.getX() - 9, pos.getY() - 3, pos.getZ() - 9, pos.getX() + 9, pos.getY() + 3, pos.getZ() + 9)); - public boolean updateNearestRift() { - return false; - } - - @Override - public boolean shouldRenderInPass(int pass) { - return pass == 1; - } + if (list.isEmpty()) { + EntityEnderman enderman = new EntityEnderman(world); + enderman.setLocationAndAngles(pos.getX() + 0.5, pos.getY() - 1, pos.getZ() + 0.5, 5, 6); + world.spawnEntity(enderman); - @Override - public void readFromNBT(NBTTagCompound nbt) { - super.readFromNBT(nbt); - this.updateTimer = nbt.getInteger("updateTimer"); - this.offset = new BlockPos(nbt.getInteger("xOffset"), nbt.getInteger("yOffset"), nbt.getInteger("zOffset")); - this.shouldClose = nbt.getBoolean("shouldClose"); - this.spawnedEndermenID = nbt.getInteger("spawnedEndermenID"); - this.riftRotation = nbt.getInteger("riftRotation"); - this.growth = nbt.getFloat("growth"); - } + if (random.nextInt(MAX_HOSTILE_ENDERMAN_CHANCE) < HOSTILE_ENDERMAN_CHANCE) { + EntityPlayer player = this.world.getClosestPlayerToEntity(enderman, 50); + if (player != null) { + enderman.setAttackTarget(player); + } + } + } + } + } + } - @Override - public NBTTagCompound writeToNBT(NBTTagCompound nbt) - { - super.writeToNBT(nbt); - nbt.setInteger("updateTimer", this.updateTimer); - nbt.setInteger("xOffset", this.offset.getX()); - nbt.setInteger("yOffset", this.offset.getY()); - nbt.setInteger("zOffset", this.offset.getZ()); - nbt.setBoolean("shouldClose", this.shouldClose); - nbt.setInteger("spawnedEndermenID", this.spawnedEndermenID); - nbt.setInteger("riftRotation", this.riftRotation); - nbt.setFloat("growth", this.growth); + private void closeRift() { + world.setBlockToAir(pos); + growth--; //@todo? + } - return nbt; - } + public boolean updateNearestRift() { + return false; + } - @Override - public float[] getRenderColor(Random rand) - { - return null; - } + @Override + public boolean shouldRenderInPass(int pass) { + return pass == 1; + } + + @Override + public void readFromNBT(NBTTagCompound nbt) { + super.readFromNBT(nbt); + this.updateTimer = nbt.getInteger("updateTimer"); + this.offset = new BlockPos(nbt.getInteger("xOffset"), nbt.getInteger("yOffset"), nbt.getInteger("zOffset")); + this.shouldClose = nbt.getBoolean("shouldClose"); + this.spawnedEndermenID = nbt.getInteger("spawnedEndermenID"); + this.riftRotation = nbt.getInteger("riftRotation"); + this.growth = nbt.getFloat("growth"); + } + + @Override + public NBTTagCompound writeToNBT(NBTTagCompound nbt) { + super.writeToNBT(nbt); + nbt.setInteger("updateTimer", this.updateTimer); + nbt.setInteger("xOffset", this.offset.getX()); + nbt.setInteger("yOffset", this.offset.getY()); + nbt.setInteger("zOffset", this.offset.getZ()); + nbt.setBoolean("shouldClose", this.shouldClose); + nbt.setInteger("spawnedEndermenID", this.spawnedEndermenID); + nbt.setInteger("riftRotation", this.riftRotation); + nbt.setFloat("growth", this.growth); + + return nbt; + } + + @Override + public float[] getRenderColor(Random rand) { + return null; + } }