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

393 lines
18 KiB
Java
Raw Normal View History

2015-08-29 14:33:25 +02:00
/*
* This file ("WorldUtil.java") is part of the Actually Additions Mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* <EFBFBD> 2015 Ellpeck
*/
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-06-28 03:12:32 +02:00
import net.minecraft.block.material.Material;
import net.minecraft.client.Minecraft;
import net.minecraft.enchantment.EnchantmentHelper;
2015-05-04 17:26:50 +02:00
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
2015-07-08 11:49:38 +02:00
import net.minecraft.entity.player.EntityPlayerMP;
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.network.play.client.C07PacketPlayerDigging;
import net.minecraft.network.play.server.S23PacketBlockChange;
2015-04-19 01:50:02 +02:00
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.MathHelper;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
2015-04-19 01:50:02 +02:00
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.*;
2015-04-19 01:50:02 +02:00
2015-06-28 03:12:32 +02:00
import java.util.ArrayList;
2015-04-19 01:50:02 +02:00
public class WorldUtil{
2015-10-03 10:16:18 +02:00
/**
* Vertical Directions in Order:
* Up, Down
*/
public static final ForgeDirection[] VERTICAL_DIRECTIONS_ORDER = new ForgeDirection[]{ForgeDirection.UP, ForgeDirection.DOWN};
/**
* Cardinal Directions in Order:
* North, East, South, West
*/
public static final ForgeDirection[] CARDINAL_DIRECTIONS_ORDER = new ForgeDirection[]{ForgeDirection.NORTH, ForgeDirection.EAST, ForgeDirection.SOUTH, ForgeDirection.WEST};
public static void breakBlockAtSide(ForgeDirection side, World world, int x, int y, int z){
breakBlockAtSide(side, world, x, y, z, 0);
}
2015-10-05 16:53:28 +02:00
public static void breakBlockAtSide(ForgeDirection side, World world, int x, int y, int z, int offset){
2015-05-25 17:00:54 +02:00
if(side == ForgeDirection.UNKNOWN){
world.setBlockToAir(x, y, z);
return;
}
2015-10-05 16:53:28 +02:00
WorldPos c = getCoordsFromSide(side, world, x, y, z, offset);
2015-04-19 01:50:02 +02:00
if(c != null){
world.setBlockToAir(c.getX(), c.getY(), c.getZ());
2015-04-19 01:50:02 +02:00
}
}
2015-10-05 16:53:28 +02:00
public static WorldPos getCoordsFromSide(ForgeDirection side, World world, int x, int y, int z, int offset){
2015-10-03 10:19:40 +02:00
if(side == ForgeDirection.UNKNOWN){
return null;
}
2015-10-05 16:53:28 +02:00
return new WorldPos(world, x+side.offsetX*(offset+1), y+side.offsetY*(offset+1), z+side.offsetZ*(offset+1));
2015-10-03 10:19:40 +02:00
}
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);
}
}
}
2015-10-03 10:19:40 +02:00
public static TileEntity getTileEntityFromSide(ForgeDirection side, World world, int x, int y, int z){
2015-10-05 16:53:28 +02:00
WorldPos c = getCoordsFromSide(side, world, x, y, z, 0);
2015-10-03 10:19:40 +02:00
if(c != null){
return world.getTileEntity(c.getX(), c.getY(), c.getZ());
}
return null;
}
/**
* Checks if a given Block with a given Meta is present in given Positions
*
* @param positions The Positions, an array of {xCoord, yCoord, zCoord} arrays containing RELATIVE Positions
* @param block The Block
* @param meta The Meta
* @param world The World
* @param x The Start X Coord
* @param y The Start Y Coord
* @param z The Start Z Coord
* @return Is every block present?
*/
public static boolean hasBlocksInPlacesGiven(int[][] positions, Block block, int meta, World world, int x, int y, int z){
for(int[] xYZ : positions){
if(!(world.getBlock(x+xYZ[0], y+xYZ[1], z+xYZ[2]) == block && world.getBlockMetadata(x+xYZ[0], y+xYZ[1], z+xYZ[2]) == meta)){
return false;
}
}
return true;
}
2015-07-06 17:48:38 +02:00
public static void updateTileAndTilesAround(TileEntity tile){
tile.getWorldObj().markBlockForUpdate(tile.xCoord+1, tile.yCoord, tile.zCoord);
tile.getWorldObj().markBlockForUpdate(tile.xCoord-1, tile.yCoord, tile.zCoord);
tile.getWorldObj().markBlockForUpdate(tile.xCoord, tile.yCoord+1, tile.zCoord);
tile.getWorldObj().markBlockForUpdate(tile.xCoord, tile.yCoord-1, tile.zCoord);
tile.getWorldObj().markBlockForUpdate(tile.xCoord, tile.yCoord, tile.zCoord+1);
tile.getWorldObj().markBlockForUpdate(tile.xCoord, tile.yCoord, tile.zCoord-1);
2015-10-26 22:47:26 +01:00
tile.getWorldObj().markBlockForUpdate(tile.xCoord, tile.yCoord, tile.zCoord);
2015-07-06 17:48:38 +02:00
tile.markDirty();
}
2015-05-20 22:39:43 +02:00
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);
}
}
}
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){
2015-09-20 14:47:52 +02:00
ModUtil.LOGGER.error("Something that places Blocks at "+x+", "+y+", "+z+" in World "+world.provider.dimensionId+" threw an Exception! Don't let that happen again!");
2015-05-30 17:47:57 +02:00
}
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){
2015-10-05 16:53:28 +02:00
WorldPos coords = getCoordsFromSide(side, world, x, y, z, 0);
2015-05-04 17:26:50 +02:00
if(coords != null){
EntityItem item = new EntityItem(world, coords.getX()+0.5, coords.getY()+0.5, coords.getZ()+0.5, stack);
2015-05-04 17:26:50 +02:00
item.motionX = 0;
item.motionY = 0;
item.motionZ = 0;
world.spawnEntityInWorld(item);
}
}
return false;
}
public static void fillBucket(FluidTank tank, ItemStack[] slots, int inputSlot, int outputSlot){
if(slots[inputSlot] != null && tank.getFluid() != null){
ItemStack filled = FluidContainerRegistry.fillFluidContainer(tank.getFluid(), slots[inputSlot].copy());
if(filled != null && FluidContainerRegistry.isEmptyContainer(slots[inputSlot]) && (slots[outputSlot] == null || (slots[outputSlot].isItemEqual(filled) && slots[outputSlot].stackSize < slots[outputSlot].getMaxStackSize()))){
int cap = FluidContainerRegistry.getContainerCapacity(tank.getFluid(), slots[inputSlot]);
if(cap > 0 && cap <= tank.getFluidAmount()){
2015-10-02 16:48:01 +02:00
if(slots[outputSlot] == null){
slots[outputSlot] = FluidContainerRegistry.fillFluidContainer(tank.getFluid(), slots[inputSlot].copy());
2015-10-02 16:48:01 +02:00
}
else{
slots[outputSlot].stackSize++;
}
if(slots[outputSlot] != null){
tank.drain(cap, true);
slots[inputSlot].stackSize--;
2015-10-03 10:16:18 +02:00
if(slots[inputSlot].stackSize <= 0){
slots[inputSlot] = null;
}
}
}
}
}
}
public static void emptyBucket(FluidTank tank, ItemStack[] slots, int inputSlot, int outputSlot){
emptyBucket(tank, slots, inputSlot, outputSlot, null);
}
public static void emptyBucket(FluidTank tank, ItemStack[] slots, int inputSlot, int outputSlot, Fluid containedFluid){
if(slots[inputSlot] != null && FluidContainerRegistry.isFilledContainer(slots[inputSlot]) && (slots[outputSlot] == null || (slots[outputSlot].isItemEqual(FluidContainerRegistry.drainFluidContainer(slots[inputSlot].copy())) && slots[outputSlot].stackSize < slots[outputSlot].getMaxStackSize()))){
if(containedFluid == null || FluidContainerRegistry.containsFluid(slots[inputSlot], new FluidStack(containedFluid, 0))){
if((tank.getFluid() == null || FluidContainerRegistry.getFluidForFilledItem(slots[inputSlot]).isFluidEqual(tank.getFluid())) && tank.getCapacity()-tank.getFluidAmount() >= FluidContainerRegistry.getContainerCapacity(slots[inputSlot])){
2015-10-02 16:48:01 +02:00
if(slots[outputSlot] == null){
slots[outputSlot] = FluidContainerRegistry.drainFluidContainer(slots[inputSlot].copy());
2015-10-02 16:48:01 +02:00
}
else{
slots[outputSlot].stackSize++;
}
tank.fill(FluidContainerRegistry.getFluidForFilledItem(slots[inputSlot]), true);
slots[inputSlot].stackSize--;
2015-10-03 10:16:18 +02:00
if(slots[inputSlot].stackSize <= 0){
slots[inputSlot] = null;
}
}
}
}
}
2015-05-04 17:26:50 +02:00
public static ForgeDirection getDirectionBySidesInOrder(int side){
if(side >= 0 && side < VERTICAL_DIRECTIONS_ORDER.length+CARDINAL_DIRECTIONS_ORDER.length){
if(side < VERTICAL_DIRECTIONS_ORDER.length){
return VERTICAL_DIRECTIONS_ORDER[side];
}
2015-10-02 16:48:01 +02:00
else{
return CARDINAL_DIRECTIONS_ORDER[side-VERTICAL_DIRECTIONS_ORDER.length];
}
2015-05-24 13:58:34 +02:00
}
return ForgeDirection.UNKNOWN;
2015-04-19 01:50:02 +02:00
}
2015-06-28 03:12:32 +02:00
public static ArrayList<Material> getMaterialsAround(World world, int x, int y, int z){
ArrayList<Material> blocks = new ArrayList<Material>();
blocks.add(world.getBlock(x+1, y, z).getMaterial());
blocks.add(world.getBlock(x-1, y, z).getMaterial());
blocks.add(world.getBlock(x, y, z+1).getMaterial());
blocks.add(world.getBlock(x, y, z-1).getMaterial());
return blocks;
}
2015-10-10 02:51:06 +02:00
public static boolean addToInventory(ItemStack[] slots, ArrayList<ItemStack> stacks, boolean actuallyDo){
return addToInventory(slots, 0, slots.length, stacks, actuallyDo);
}
/**
* Add an ArrayList of ItemStacks to an Array of slots
*
* @param slots The slots to try to put the items into
* @param stacks The stacks to be put into the slots (Items don't actually get removed from there!)
* @param actuallyDo Do it or just test if it works?
* @return Does it work?
*/
2015-10-10 02:51:06 +02:00
public static boolean addToInventory(ItemStack[] slots, int start, int end, ArrayList<ItemStack> stacks, boolean actuallyDo){
ItemStack[] theSlots;
if(actuallyDo){
theSlots = slots;
}
else{
//Create "Test Slots" to put the items into to try if it works out in the end
theSlots = new ItemStack[slots.length];
for(int i = 0; i < theSlots.length; i++){
if(slots[i] != null){
theSlots[i] = slots[i].copy();
}
}
}
2015-07-07 12:32:25 +02:00
int working = 0;
for(ItemStack stackToPutIn : stacks){
2015-10-10 02:51:06 +02:00
for(int i = start; i < end; i++){
if(stackToPutIn != null && (theSlots[i] == null || (theSlots[i].isItemEqual(stackToPutIn) && theSlots[i].getMaxStackSize() >= theSlots[i].stackSize+stackToPutIn.stackSize))){
if(theSlots[i] == null){
theSlots[i] = stackToPutIn.copy();
}
else{
2015-10-10 02:51:06 +02:00
theSlots[i].stackSize += stackToPutIn.stackSize;
2015-07-07 12:32:25 +02:00
}
working++;
2015-07-07 12:32:25 +02:00
break;
}
}
}
return working >= stacks.size();
}
2015-07-07 12:34:20 +02:00
public static int findFirstFilledSlot(ItemStack[] slots){
2015-07-07 12:32:25 +02:00
for(int i = 0; i < slots.length; i++){
if(slots[i] != null){
return i;
}
}
return 0;
}
2015-07-08 11:49:38 +02:00
public static MovingObjectPosition getNearestPositionWithAir(World world, EntityPlayer player, int reach){
return getMovingObjectPosWithReachDistance(world, player, reach, false, false, true);
}
private static MovingObjectPosition getMovingObjectPosWithReachDistance(World world, EntityPlayer player, double distance, boolean p1, boolean p2, boolean p3){
2015-07-07 23:06:53 +02:00
float f = 1.0F;
float f1 = player.prevRotationPitch+(player.rotationPitch-player.prevRotationPitch)*f;
float f2 = player.prevRotationYaw+(player.rotationYaw-player.prevRotationYaw)*f;
double d0 = player.prevPosX+(player.posX-player.prevPosX)*(double)f;
double d1 = player.prevPosY+(player.posY-player.prevPosY)*(double)f+(double)(world.isRemote ? player.getEyeHeight()-player.getDefaultEyeHeight() : player.getEyeHeight());
2015-07-07 23:06:53 +02:00
double d2 = player.prevPosZ+(player.posZ-player.prevPosZ)*(double)f;
Vec3 vec3 = Vec3.createVectorHelper(d0, d1, d2);
float f3 = MathHelper.cos(-f2*0.017453292F-(float)Math.PI);
float f4 = MathHelper.sin(-f2*0.017453292F-(float)Math.PI);
float f5 = -MathHelper.cos(-f1*0.017453292F);
float f6 = MathHelper.sin(-f1*0.017453292F);
float f7 = f4*f5;
float f8 = f3*f5;
Vec3 vec31 = vec3.addVector((double)f7*distance, (double)f6*distance, (double)f8*distance);
2015-07-08 11:49:38 +02:00
return world.func_147447_a(vec3, vec31, p1, p2, p3);
}
2015-10-03 10:19:40 +02:00
public static MovingObjectPosition getNearestBlockWithDefaultReachDistance(World world, EntityPlayer player){
return getMovingObjectPosWithReachDistance(world, player, player instanceof EntityPlayerMP ? ((EntityPlayerMP)player).theItemInWorldManager.getBlockReachDistance() : 5.0D, false, true, false);
}
/**
* Harvests a Block by a Player
*
* @param world The World
* @param xPos The X Coordinate
* @param yPos The Y Coordinate
* @param zPos The Z Coordinate
* @param player The Player
* @return If the Block could be harvested normally (so that it drops an item)
*/
public static boolean playerHarvestBlock(World world, int xPos, int yPos, int zPos, EntityPlayer player){
Block block = world.getBlock(xPos, yPos, zPos);
int meta = world.getBlockMetadata(xPos, yPos, zPos);
//If the Block can be harvested or not
boolean canHarvest = block.canHarvestBlock(player, meta);
if(!world.isRemote){
//Server-Side only, special cases
block.onBlockHarvested(world, xPos, yPos, zPos, meta, player);
}
else{
//Shows the Harvest Particles and plays the Block's Sound
world.playAuxSFX(2001, xPos, yPos, zPos, Block.getIdFromBlock(block)+(meta << 12));
}
//If the Block was actually "removed", meaning it will drop an Item
boolean removed = block.removedByPlayer(world, player, xPos, yPos, zPos, canHarvest);
//Actually removes the Block from the World
if(removed){
//Before the Block is destroyed, special cases
block.onBlockDestroyedByPlayer(world, xPos, yPos, zPos, meta);
if(!world.isRemote && !player.capabilities.isCreativeMode){
//Actually drops the Block's Items etc.
if(canHarvest){
block.harvestBlock(world, player, xPos, yPos, zPos, meta);
}
//Only drop XP when no Silk Touch is applied
if(!EnchantmentHelper.getSilkTouchModifier(player)){
//Drop XP depending on Fortune Level
block.dropXpOnBlockBreak(world, xPos, yPos, zPos, block.getExpDrop(world, meta, EnchantmentHelper.getFortuneModifier(player)));
}
}
}
if(!world.isRemote){
//Update the Client of a Block Change
((EntityPlayerMP)player).playerNetServerHandler.sendPacket(new S23PacketBlockChange(xPos, yPos, zPos, world));
}
else{
//Check the Server if a Block that changed on the Client really changed, if not, revert the change
Minecraft.getMinecraft().getNetHandler().addToSendQueue(new C07PacketPlayerDigging(2, xPos, yPos, zPos, Minecraft.getMinecraft().objectMouseOver.sideHit));
}
return removed;
}
2015-04-19 01:50:02 +02:00
}