Pull teleportBlocks method into general utility class

It's useful in other places and those other places really shouldn't be firing off the TeleposeEvent.
This commit is contained in:
Nick 2016-03-29 17:38:11 -07:00
parent 63b541da06
commit 1cdcd81508
3 changed files with 71 additions and 74 deletions

View file

@ -5,7 +5,7 @@ import WayofTime.bloodmagic.api.Constants;
import WayofTime.bloodmagic.api.network.SoulNetwork;
import WayofTime.bloodmagic.api.ritual.*;
import WayofTime.bloodmagic.api.util.helper.NetworkHelper;
import WayofTime.bloodmagic.tile.TileTeleposer;
import WayofTime.bloodmagic.util.Utils;
import net.minecraft.block.Block;
import net.minecraft.block.BlockOre;
import net.minecraft.block.BlockRedstoneOre;
@ -130,7 +130,7 @@ public class RitualMagnetic extends Ritual
if (isBlockOre(block, meta))
{
TileTeleposer.teleportBlocks(world, newPos, world, replacement);
Utils.swapLocations(world, newPos, world, replacement);
network.syphon(getRefreshCost());
k++;
this.lastPos = new BlockPos(i, j, k);

View file

@ -1,6 +1,5 @@
package WayofTime.bloodmagic.tile;
import WayofTime.bloodmagic.api.BlockStack;
import WayofTime.bloodmagic.api.Constants;
import WayofTime.bloodmagic.api.event.TeleposeEvent;
import WayofTime.bloodmagic.api.teleport.TeleportQueue;
@ -10,22 +9,16 @@ import WayofTime.bloodmagic.api.util.helper.PlayerHelper;
import WayofTime.bloodmagic.block.BlockTeleposer;
import WayofTime.bloodmagic.item.ItemTelepositionFocus;
import WayofTime.bloodmagic.ritual.portal.Teleports;
import WayofTime.bloodmagic.util.Utils;
import com.google.common.base.Strings;
import net.minecraft.block.BlockPortal;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.init.Blocks;
import net.minecraft.init.SoundEvents;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ITickable;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.common.FMLCommonHandler;
import java.util.List;
@ -96,7 +89,8 @@ public class TileTeleposer extends TileInventory implements ITickable
{
for (int k = -(focusLevel - 1); k <= (focusLevel - 1); k++)
{
if (teleportBlocks(worldObj, pos.add(i, 1 + j, k), focusWorld, focusPos.add(i, 1 + j, k)))
TeleposeEvent event = new TeleposeEvent(worldObj, pos.add(i, 1 + j, k), focusWorld, focusPos.add(i, 1 + j, k));
if (Utils.swapLocations(event.initalWorld, event.initialBlockPos, event.finalWorld, event.finalBlockPos) && !MinecraftForge.EVENT_BUS.post(event))
{
blocksTransported++;
}
@ -157,67 +151,4 @@ public class TileTeleposer extends TileInventory implements ITickable
{
return teleposer.getStackInSlot(0) != null && teleposer.getStackInSlot(0).getItem() instanceof ItemTelepositionFocus && !Strings.isNullOrEmpty(((ItemTelepositionFocus) teleposer.getStackInSlot(0).getItem()).getOwnerName(teleposer.getStackInSlot(0)));
}
public static boolean teleportBlocks(World initialWorld, BlockPos initialPos, World finalWorld, BlockPos finalPos)
{
TileEntity initialTile = initialWorld.getTileEntity(initialPos);
TileEntity finalTile = finalWorld.getTileEntity(finalPos);
NBTTagCompound initialTag = new NBTTagCompound();
NBTTagCompound finalTag = new NBTTagCompound();
if (initialTile != null)
initialTile.writeToNBT(initialTag);
if (finalTile != null)
finalTile.writeToNBT(finalTag);
BlockStack initialStack = BlockStack.getStackFromPos(initialWorld, initialPos);
BlockStack finalStack = BlockStack.getStackFromPos(finalWorld, finalPos);
if ((initialStack.getBlock().equals(Blocks.air) && finalStack.getBlock().equals(Blocks.air)) || initialStack.getBlock() instanceof BlockPortal || finalStack.getBlock() instanceof BlockPortal)
{
return false;
}
TeleposeEvent event = new TeleposeEvent(initialWorld, initialPos, finalWorld, finalPos);
if (MinecraftForge.EVENT_BUS.post(event))
return false;
initialWorld.playSound(initialPos.getX(), initialPos.getY(), initialPos.getZ(), SoundEvents.entity_endermen_teleport, SoundCategory.AMBIENT, 1.0F, 1.0F, false);
finalWorld.playSound(finalPos.getX(), finalPos.getY(), finalPos.getZ(), SoundEvents.entity_endermen_teleport, SoundCategory.AMBIENT, 1.0F, 1.0F, false);
//Finally, we get to do something! (CLEARING TILES)
if (finalStack.getBlock() != null)
finalWorld.removeTileEntity(finalPos);
if (initialStack.getBlock() != null)
initialWorld.removeTileEntity(initialPos);
//TILES CLEARED
IBlockState initialBlockState = initialWorld.getBlockState(initialPos);
IBlockState finalBlockState = finalWorld.getBlockState(finalPos);
finalWorld.setBlockState(finalPos, initialBlockState, 3);
if (initialTile != null)
{
TileEntity newTileInitial = TileEntity.createTileEntity(FMLCommonHandler.instance().getMinecraftServerInstance(), initialTag);
finalWorld.setTileEntity(finalPos, newTileInitial);
newTileInitial.setPos(finalPos);
newTileInitial.setWorldObj(finalWorld);
}
initialWorld.setBlockState(initialPos, finalBlockState, 3);
if (finalTile != null)
{
TileEntity newTileFinal = TileEntity.createTileEntity(FMLCommonHandler.instance().getMinecraftServerInstance(), finalTag);
initialWorld.setTileEntity(initialPos, newTileFinal);
newTileFinal.setPos(initialPos);
newTileFinal.setWorldObj(initialWorld);
}
initialWorld.notifyNeighborsOfStateChange(initialPos, finalStack.getBlock());
finalWorld.notifyNeighborsOfStateChange(finalPos, initialStack.getBlock());
return true;
}
}

View file

@ -1,12 +1,15 @@
package WayofTime.bloodmagic.util;
import WayofTime.bloodmagic.api.BlockStack;
import WayofTime.bloodmagic.api.altar.EnumAltarComponent;
import WayofTime.bloodmagic.api.event.TeleposeEvent;
import WayofTime.bloodmagic.registry.ModBlocks;
import WayofTime.bloodmagic.tile.TileInventory;
import com.google.common.collect.Iterables;
import net.minecraft.block.Block;
import net.minecraft.block.BlockPortal;
import net.minecraft.block.state.IBlockState;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.EntityLivingBase;
@ -14,6 +17,7 @@ import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.MobEffects;
import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.item.Item;
@ -21,16 +25,21 @@ import net.minecraft.item.ItemArmor;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.potion.Potion;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.ISpecialArmor;
import net.minecraftforge.common.ISpecialArmor.ArmorProperties;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.IFluidBlock;
import net.minecraftforge.fluids.IFluidHandler;
import net.minecraftforge.fml.common.FMLCommonHandler;
import java.util.ArrayList;
@ -556,6 +565,63 @@ public class Utils
return (state instanceof IFluidBlock || state.getMaterial().isLiquid());
}
public static boolean swapLocations(World initialWorld, BlockPos initialPos, World finalWorld, BlockPos finalPos)
{
TileEntity initialTile = initialWorld.getTileEntity(initialPos);
TileEntity finalTile = finalWorld.getTileEntity(finalPos);
NBTTagCompound initialTag = new NBTTagCompound();
NBTTagCompound finalTag = new NBTTagCompound();
if (initialTile != null)
initialTile.writeToNBT(initialTag);
if (finalTile != null)
finalTile.writeToNBT(finalTag);
BlockStack initialStack = BlockStack.getStackFromPos(initialWorld, initialPos);
BlockStack finalStack = BlockStack.getStackFromPos(finalWorld, finalPos);
if ((initialStack.getBlock().equals(Blocks.air) && finalStack.getBlock().equals(Blocks.air)) || initialStack.getBlock() instanceof BlockPortal || finalStack.getBlock() instanceof BlockPortal)
return false;
initialWorld.playSound(initialPos.getX(), initialPos.getY(), initialPos.getZ(), SoundEvents.entity_endermen_teleport, SoundCategory.AMBIENT, 1.0F, 1.0F, false);
finalWorld.playSound(finalPos.getX(), finalPos.getY(), finalPos.getZ(), SoundEvents.entity_endermen_teleport, SoundCategory.AMBIENT, 1.0F, 1.0F, false);
//Finally, we get to do something! (CLEARING TILES)
if (finalStack.getBlock() != null)
finalWorld.removeTileEntity(finalPos);
if (initialStack.getBlock() != null)
initialWorld.removeTileEntity(initialPos);
//TILES CLEARED
IBlockState initialBlockState = initialWorld.getBlockState(initialPos);
IBlockState finalBlockState = finalWorld.getBlockState(finalPos);
finalWorld.setBlockState(finalPos, initialBlockState, 3);
if (initialTile != null)
{
TileEntity newTileInitial = TileEntity.createTileEntity(FMLCommonHandler.instance().getMinecraftServerInstance(), initialTag);
finalWorld.setTileEntity(finalPos, newTileInitial);
newTileInitial.setPos(finalPos);
newTileInitial.setWorldObj(finalWorld);
}
initialWorld.setBlockState(initialPos, finalBlockState, 3);
if (finalTile != null)
{
TileEntity newTileFinal = TileEntity.createTileEntity(FMLCommonHandler.instance().getMinecraftServerInstance(), finalTag);
initialWorld.setTileEntity(initialPos, newTileFinal);
newTileFinal.setPos(initialPos);
newTileFinal.setWorldObj(initialWorld);
}
initialWorld.notifyNeighborsOfStateChange(initialPos, finalStack.getBlock());
finalWorld.notifyNeighborsOfStateChange(finalPos, initialStack.getBlock());
return true;
}
//Shamelessly ripped off of CoFH Lib
public static boolean fillContainerFromHandler(World world, IFluidHandler handler, EntityPlayer player, FluidStack tankFluid)
{