ActuallyAdditions/src/main/java/ellpeck/actuallyadditions/util/WorldUtil.java

129 lines
5.8 KiB
Java
Raw Normal View History

2015-04-19 01:50:02 +02:00
package ellpeck.actuallyadditions.util;
2015-05-20 22:39:43 +02:00
import cofh.api.energy.EnergyStorage;
import cofh.api.energy.IEnergyReceiver;
2015-05-30 17:47:57 +02:00
import net.minecraft.block.Block;
2015-05-04 17:26:50 +02:00
import net.minecraft.entity.item.EntityItem;
2015-05-30 17:47:57 +02:00
import net.minecraft.init.Blocks;
2015-04-19 01:50:02 +02:00
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChunkCoordinates;
import net.minecraft.world.World;
2015-05-04 17:26:50 +02:00
import net.minecraft.world.WorldServer;
2015-05-22 17:48:50 +02:00
import net.minecraftforge.common.IPlantable;
2015-05-04 17:26:50 +02:00
import net.minecraftforge.common.util.ForgeDirection;
2015-05-30 17:47:57 +02:00
import net.minecraftforge.fluids.*;
import org.apache.logging.log4j.Level;
2015-04-19 01:50:02 +02:00
public class WorldUtil{
2015-05-04 17:26:50 +02:00
public static ChunkCoordinates getCoordsFromSide(ForgeDirection side, int x, int y, int z){
if(side == ForgeDirection.UNKNOWN) return null;
return new ChunkCoordinates(x+side.offsetX, y+side.offsetY, z+side.offsetZ);
2015-04-19 01:50:02 +02:00
}
2015-05-04 17:26:50 +02:00
public static void breakBlockAtSide(ForgeDirection side, World world, int x, int y, int z){
2015-05-25 17:00:54 +02:00
if(side == ForgeDirection.UNKNOWN){
world.setBlockToAir(x, y, z);
return;
}
2015-04-19 01:50:02 +02:00
ChunkCoordinates c = getCoordsFromSide(side, x, y, z);
if(c != null){
world.setBlockToAir(c.posX, c.posY, c.posZ);
}
}
2015-05-20 22:39:43 +02:00
public static void pushEnergy(World world, int x, int y, int z, ForgeDirection side, EnergyStorage storage){
TileEntity tile = getTileEntityFromSide(side, world, x, y, z);
if(tile != null && tile instanceof IEnergyReceiver && storage.getEnergyStored() > 0){
2015-05-20 22:39:43 +02:00
if(((IEnergyReceiver)tile).canConnectEnergy(side.getOpposite())){
int receive = ((IEnergyReceiver)tile).receiveEnergy(side.getOpposite(), Math.min(storage.getMaxExtract(), storage.getEnergyStored()), false);
storage.extractEnergy(receive, false);
world.markBlockForUpdate(x+side.offsetX, y+side.offsetY, z+side.offsetZ);
}
}
}
public static void pushFluid(World world, int x, int y, int z, ForgeDirection side, FluidTank tank){
TileEntity tile = getTileEntityFromSide(side, world, x, y, z);
if(tile != null && tank.getFluid() != null && tile instanceof IFluidHandler){
if(((IFluidHandler)tile).canFill(side.getOpposite(), tank.getFluid().getFluid())){
int receive = ((IFluidHandler)tile).fill(side.getOpposite(), tank.getFluid(), true);
tank.drain(receive, true);
world.markBlockForUpdate(x+side.offsetX, y+side.offsetY, z+side.offsetZ);
}
}
}
2015-05-30 17:47:57 +02:00
public static ItemStack placeBlockAtSide(ForgeDirection side, World world, int x, int y, int z, ItemStack stack){
if(world instanceof WorldServer && stack != null && stack.getItem() != null){
2015-05-27 21:57:53 +02:00
//Fluids
FluidStack fluid = FluidContainerRegistry.getFluidForFilledItem(stack);
if(fluid != null && fluid.getFluid().getBlock() != null && fluid.getFluid().getBlock().canPlaceBlockAt(world, x+side.offsetX, y+side.offsetY, z+side.offsetZ)){
2015-05-30 17:47:57 +02:00
Block block = world.getBlock(x+side.offsetX, y+side.offsetY, z+side.offsetZ);
if(!(block instanceof IFluidBlock) && block != Blocks.lava && block != Blocks.water && block != Blocks.flowing_lava && block != Blocks.flowing_water){
if(world.setBlock(x+side.offsetX, y+side.offsetY, z+side.offsetZ, fluid.getFluid().getBlock())){
return stack.getItem().getContainerItem(stack);
}
}
2015-05-27 21:57:53 +02:00
}
//Plants
2015-05-22 17:48:50 +02:00
if(stack.getItem() instanceof IPlantable){
if(((IPlantable)stack.getItem()).getPlant(world, x, y, z).canPlaceBlockAt(world, x+side.offsetX, y+side.offsetY, z+side.offsetZ)){
2015-05-30 17:47:57 +02:00
if(world.setBlock(x+side.offsetX, y+side.offsetY, z+side.offsetZ, ((IPlantable)stack.getItem()).getPlant(world, x, y, z))){
stack.stackSize--;
return stack;
}
2015-05-22 17:48:50 +02:00
}
}
2015-05-27 21:57:53 +02:00
2015-05-30 17:47:57 +02:00
try{
//Blocks
stack.tryPlaceItemIntoWorld(FakePlayerUtil.newFakePlayer(world), world, x, y, z, side == ForgeDirection.UNKNOWN ? 0 : side.ordinal(), 0, 0, 0);
return stack;
}
catch(Exception e){
ModUtil.AA_LOGGER.log(Level.ERROR, "Something that places Blocks at "+x+", "+y+", "+z+" in World "+world.provider.dimensionId+" threw an Exception! Don't let that happen again!");
}
2015-04-19 01:50:02 +02:00
}
2015-05-30 17:47:57 +02:00
return stack;
2015-04-19 01:50:02 +02:00
}
2015-05-04 17:26:50 +02:00
public static boolean dropItemAtSide(ForgeDirection side, World world, int x, int y, int z, ItemStack stack){
if(side != ForgeDirection.UNKNOWN){
ChunkCoordinates coords = getCoordsFromSide(side, x, y, z);
if(coords != null){
EntityItem item = new EntityItem(world, coords.posX+0.5, coords.posY+0.5, coords.posZ+0.5, stack);
item.motionX = 0;
item.motionY = 0;
item.motionZ = 0;
world.spawnEntityInWorld(item);
}
}
return false;
}
public static TileEntity getTileEntityFromSide(ForgeDirection side, World world, int x, int y, int z){
2015-04-19 01:50:02 +02:00
ChunkCoordinates c = getCoordsFromSide(side, x, y, z);
if(c != null){
return world.getTileEntity(c.posX, c.posY, c.posZ);
}
2015-05-04 17:26:50 +02:00
return null;
}
public static ForgeDirection getDirectionByRotatingSide(int side){
2015-05-24 13:58:34 +02:00
switch(side){
case 0: return ForgeDirection.UP;
case 1: return ForgeDirection.DOWN;
case 2: return ForgeDirection.NORTH;
case 3: return ForgeDirection.EAST;
case 4: return ForgeDirection.SOUTH;
case 5: return ForgeDirection.WEST;
default: return ForgeDirection.UNKNOWN;
}
2015-04-19 01:50:02 +02:00
}
}