2018-10-14 14:27:18 +02:00
|
|
|
package de.ellpeck.naturesaura;
|
|
|
|
|
2018-10-18 13:34:37 +02:00
|
|
|
import de.ellpeck.naturesaura.blocks.tiles.TileEntityImpl;
|
2018-10-16 01:36:30 +02:00
|
|
|
import net.minecraft.block.state.IBlockState;
|
2018-10-16 11:49:30 +02:00
|
|
|
import net.minecraft.client.Minecraft;
|
|
|
|
import net.minecraft.client.renderer.GlStateManager;
|
|
|
|
import net.minecraft.client.renderer.RenderHelper;
|
|
|
|
import net.minecraft.client.renderer.block.model.ItemCameraTransforms;
|
2018-10-18 13:34:37 +02:00
|
|
|
import net.minecraft.entity.player.EntityPlayer;
|
2018-10-16 01:36:30 +02:00
|
|
|
import net.minecraft.item.ItemStack;
|
2018-10-14 14:27:18 +02:00
|
|
|
import net.minecraft.tileentity.TileEntity;
|
2018-10-18 13:34:37 +02:00
|
|
|
import net.minecraft.util.EnumHand;
|
2018-10-14 14:27:18 +02:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
|
|
|
import net.minecraft.world.World;
|
2018-10-15 18:36:46 +02:00
|
|
|
import net.minecraftforge.fml.relauncher.Side;
|
|
|
|
import net.minecraftforge.fml.relauncher.SideOnly;
|
2018-10-18 13:34:37 +02:00
|
|
|
import net.minecraftforge.items.IItemHandlerModifiable;
|
2018-10-14 14:27:18 +02:00
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public final class Helper {
|
|
|
|
|
|
|
|
public static List<TileEntity> getTileEntitiesInArea(World world, BlockPos pos, int radius) {
|
|
|
|
List<TileEntity> tiles = new ArrayList<>();
|
|
|
|
for (int x = (pos.getX() - radius) >> 4; x <= (pos.getX() + radius) >> 4; x++) {
|
|
|
|
for (int z = (pos.getZ() - radius) >> 4; z <= (pos.getZ() + radius) >> 4; z++) {
|
|
|
|
for (TileEntity tile : world.getChunk(x, z).getTileEntityMap().values()) {
|
|
|
|
if (tile.getPos().distanceSq(pos) <= radius * radius) {
|
|
|
|
tiles.add(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tiles;
|
|
|
|
}
|
|
|
|
|
2018-10-16 01:36:30 +02:00
|
|
|
public static boolean checkMultiblock(World world, BlockPos pos, BlockPos[] positions, IBlockState state, boolean blockOnly) {
|
|
|
|
for (BlockPos offset : positions) {
|
|
|
|
IBlockState current = world.getBlockState(pos.add(offset));
|
|
|
|
if (blockOnly ? current.getBlock() != state.getBlock() : current != state) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-15 18:36:46 +02:00
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public static int blendColors(int c1, int c2, float ratio) {
|
|
|
|
int a = (int) ((c1 >> 24 & 0xFF) * ratio + (c2 >> 24 & 0xFF) * (1 - ratio));
|
|
|
|
int r = (int) ((c1 >> 16 & 0xFF) * ratio + (c2 >> 16 & 0xFF) * (1 - ratio));
|
|
|
|
int g = (int) ((c1 >> 8 & 0xFF) * ratio + (c2 >> 8 & 0xFF) * (1 - ratio));
|
|
|
|
int b = (int) ((c1 & 0xFF) * ratio + (c2 & 0xFF) * (1 - ratio));
|
|
|
|
return ((a & 255) << 24) | ((r & 255) << 16) | ((g & 255) << 8) | (b & 255);
|
|
|
|
}
|
2018-10-16 01:36:30 +02:00
|
|
|
|
2018-10-16 11:49:30 +02:00
|
|
|
public static int getItemIndex(List<ItemStack> list, ItemStack item) {
|
|
|
|
for (int i = 0; i < list.size(); i++) {
|
|
|
|
if (list.get(i).isItemEqual(item)) {
|
|
|
|
return i;
|
2018-10-16 01:36:30 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-16 11:49:30 +02:00
|
|
|
return -1;
|
2018-10-16 01:36:30 +02:00
|
|
|
}
|
|
|
|
|
2018-10-16 11:49:30 +02:00
|
|
|
@SideOnly(Side.CLIENT)
|
|
|
|
public static void renderItemInWorld(ItemStack stack) {
|
|
|
|
if (!stack.isEmpty()) {
|
|
|
|
GlStateManager.pushMatrix();
|
|
|
|
GlStateManager.disableLighting();
|
|
|
|
GlStateManager.pushAttrib();
|
|
|
|
RenderHelper.enableStandardItemLighting();
|
|
|
|
Minecraft.getMinecraft().getRenderItem().renderItem(stack, ItemCameraTransforms.TransformType.FIXED);
|
|
|
|
RenderHelper.disableStandardItemLighting();
|
|
|
|
GlStateManager.popAttrib();
|
|
|
|
GlStateManager.enableLighting();
|
|
|
|
GlStateManager.popMatrix();
|
2018-10-16 01:36:30 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-18 13:34:37 +02:00
|
|
|
|
|
|
|
public static boolean putStackOnTile(EntityPlayer player, EnumHand hand, BlockPos pos, int slot) {
|
|
|
|
TileEntity tile = player.world.getTileEntity(pos);
|
|
|
|
if (tile instanceof TileEntityImpl) {
|
|
|
|
IItemHandlerModifiable handler = ((TileEntityImpl) tile).getItemHandler(null);
|
|
|
|
if (handler != null) {
|
|
|
|
ItemStack handStack = player.getHeldItem(hand);
|
|
|
|
if (!handStack.isEmpty()) {
|
|
|
|
ItemStack remain = handler.insertItem(slot, handStack, player.world.isRemote);
|
|
|
|
if (!ItemStack.areItemStacksEqual(remain, handStack)) {
|
|
|
|
if (!player.world.isRemote) {
|
|
|
|
player.setHeldItem(hand, remain);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!handler.getStackInSlot(slot).isEmpty()) {
|
|
|
|
if (!player.world.isRemote) {
|
|
|
|
player.addItemStackToInventory(handler.getStackInSlot(slot));
|
|
|
|
handler.setStackInSlot(slot, ItemStack.EMPTY);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-14 14:27:18 +02:00
|
|
|
}
|