140 lines
6.4 KiB
Java
140 lines
6.4 KiB
Java
package keystrokesmod.utility;
|
|
|
|
import net.minecraft.block.*;
|
|
import net.minecraft.block.material.Material;
|
|
import net.minecraft.block.state.IBlockState;
|
|
import net.minecraft.client.Minecraft;
|
|
import net.minecraft.enchantment.Enchantment;
|
|
import net.minecraft.enchantment.EnchantmentHelper;
|
|
import net.minecraft.init.Blocks;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.potion.Potion;
|
|
import net.minecraft.util.BlockPos;
|
|
import net.minecraft.util.MovingObjectPosition;
|
|
import net.minecraft.util.Vec3;
|
|
|
|
public class BlockUtils {
|
|
public static final Minecraft mc = Minecraft.getMinecraft();
|
|
|
|
public static boolean isSamePos(BlockPos blockPos, BlockPos blockPos2) {
|
|
return blockPos == blockPos2 || (blockPos.getX() == blockPos2.getX() && blockPos.getY() == blockPos2.getY() && blockPos.getZ() == blockPos2.getZ());
|
|
}
|
|
|
|
public static boolean notFull(Block block) {
|
|
return block instanceof BlockFenceGate || block instanceof BlockLadder || block instanceof BlockFlowerPot || block instanceof BlockBasePressurePlate || isFluid(block) || block instanceof BlockFence || block instanceof BlockAnvil || block instanceof BlockEnchantmentTable || block instanceof BlockChest || block instanceof BlockWeb || block instanceof BlockTallGrass;
|
|
}
|
|
|
|
public static boolean isFluid(Block block) {
|
|
return block.getMaterial() == Material.lava || block.getMaterial() == Material.water;
|
|
}
|
|
|
|
public static boolean isInteractable(Block block) {
|
|
return block instanceof BlockFurnace || block instanceof BlockTrapDoor || block instanceof BlockDoor || block instanceof BlockContainer || block instanceof BlockJukebox || block instanceof BlockFenceGate || block instanceof BlockChest || block instanceof BlockEnderChest || block instanceof BlockEnchantmentTable || block instanceof BlockBrewingStand || block instanceof BlockBed || block instanceof BlockDropper || block instanceof BlockDispenser || block instanceof BlockHopper || block instanceof BlockAnvil || block instanceof BlockNote || block instanceof BlockWorkbench || block instanceof BlockButton;
|
|
}
|
|
|
|
public static boolean isInteractable(MovingObjectPosition mv) {
|
|
if (mv == null || mv.typeOfHit != MovingObjectPosition.MovingObjectType.BLOCK || mv.getBlockPos() == null) {
|
|
return false;
|
|
}
|
|
IBlockState iblockstate = mc.theWorld.getBlockState(mv.getBlockPos());
|
|
if (!mc.thePlayer.isSneaking() || mc.thePlayer.getHeldItem() == null || mc.thePlayer.getHeldItem().getItem().doesSneakBypassUse(mc.theWorld, mv.getBlockPos(), mc.thePlayer)) {
|
|
return isInteractable(iblockstate.getBlock());
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static boolean isObsidian(Block block) {
|
|
return block instanceof BlockObsidian;
|
|
}
|
|
|
|
public static boolean isObsidian(MovingObjectPosition mv) {
|
|
if (mv == null || mv.typeOfHit != MovingObjectPosition.MovingObjectType.BLOCK || mv.getBlockPos() == null) {
|
|
return false;
|
|
}
|
|
IBlockState iblockstate = mc.theWorld.getBlockState(mv.getBlockPos());
|
|
return isObsidian(iblockstate.getBlock());
|
|
}
|
|
|
|
public static Vec3 getHitVec(Vec3 hitVec, BlockPos blockPos) {
|
|
float x = (float)(hitVec.xCoord - blockPos.getX());
|
|
float y = (float)(hitVec.yCoord - blockPos.getY());
|
|
float z = (float)(hitVec.zCoord - blockPos.getZ());
|
|
return new Vec3(x, y, z);
|
|
}
|
|
|
|
public static float getBlockHardness(final Block block, final ItemStack itemStack, boolean ignoreSlow, boolean ignoreGround) {
|
|
final float getBlockHardness = block.getBlockHardness(mc.theWorld, null);
|
|
if (getBlockHardness < 0.0f) {
|
|
return 0.0f;
|
|
}
|
|
return (block.getMaterial().isToolNotRequired() || (itemStack != null && itemStack.canHarvestBlock(block))) ? (getToolDigEfficiency(itemStack, block, ignoreSlow, ignoreGround) / getBlockHardness / 30.0f) : (getToolDigEfficiency(itemStack, block, ignoreSlow, ignoreGround) / getBlockHardness / 100.0f);
|
|
}
|
|
|
|
public static float getToolDigEfficiency(ItemStack itemStack, Block block, boolean ignoreSlow, boolean ignoreGround) {
|
|
float n = (itemStack == null) ? 1.0f : itemStack.getItem().getStrVsBlock(itemStack, block);
|
|
if (n > 1.0f) {
|
|
final int getEnchantmentLevel = EnchantmentHelper.getEnchantmentLevel(Enchantment.efficiency.effectId, itemStack);
|
|
if (getEnchantmentLevel > 0 && itemStack != null) {
|
|
n += getEnchantmentLevel * getEnchantmentLevel + 1;
|
|
}
|
|
}
|
|
if (mc.thePlayer.isPotionActive(Potion.digSpeed)) {
|
|
n *= 1.0f + (mc.thePlayer.getActivePotionEffect(Potion.digSpeed).getAmplifier() + 1) * 0.2f;
|
|
}
|
|
if (!ignoreSlow) {
|
|
if (mc.thePlayer.isPotionActive(Potion.digSlowdown)) {
|
|
float n2;
|
|
switch (mc.thePlayer.getActivePotionEffect(Potion.digSlowdown).getAmplifier()) {
|
|
case 0: {
|
|
n2 = 0.3f;
|
|
break;
|
|
}
|
|
case 1: {
|
|
n2 = 0.09f;
|
|
break;
|
|
}
|
|
case 2: {
|
|
n2 = 0.0027f;
|
|
break;
|
|
}
|
|
default: {
|
|
n2 = 8.1E-4f;
|
|
break;
|
|
}
|
|
}
|
|
n *= n2;
|
|
}
|
|
if (mc.thePlayer.isInsideOfMaterial(Material.water) && !EnchantmentHelper.getAquaAffinityModifier(mc.thePlayer)) {
|
|
n /= 5.0f;
|
|
}
|
|
if (!mc.thePlayer.onGround && !ignoreGround) {
|
|
n /= 5.0f;
|
|
}
|
|
}
|
|
return n;
|
|
}
|
|
|
|
public static Block getBlock(BlockPos blockPos) {
|
|
return getBlockState(blockPos).getBlock();
|
|
}
|
|
|
|
public static Block getBlock(double x, double y, double z) {
|
|
return getBlockState(new BlockPos(x, y, z)).getBlock();
|
|
}
|
|
|
|
public static IBlockState getBlockState(BlockPos blockPos) {
|
|
return mc.theWorld.getBlockState(blockPos);
|
|
}
|
|
|
|
public static boolean check(final BlockPos blockPos, final Block block) {
|
|
return getBlock(blockPos) == block;
|
|
}
|
|
|
|
public static boolean replaceable(BlockPos blockPos) {
|
|
if (!Utils.nullCheck()) {
|
|
return true;
|
|
}
|
|
return getBlock(blockPos).isReplaceable(mc.theWorld, blockPos);
|
|
}
|
|
}
|