NaturesAura/src/main/java/de/ellpeck/naturesaura/Helper.java

156 lines
6.4 KiB
Java
Raw Normal View History

2018-10-14 14:27:18 +02:00
package de.ellpeck.naturesaura;
2018-10-20 21:19:08 +02:00
import de.ellpeck.naturesaura.aura.Capabilities;
import de.ellpeck.naturesaura.aura.IAuraRecharge;
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-20 21:19:08 +02:00
import net.minecraft.util.EnumFacing;
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-20 21:19:08 +02:00
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
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-18 18:00:21 +02:00
import org.lwjgl.opengl.GL11;
2018-10-14 14:27:18 +02:00
2018-10-20 21:19:08 +02:00
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
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
2018-10-18 18:00:21 +02:00
@SideOnly(Side.CLIENT)
public static void renderItemInGui(ItemStack stack, int x, int y, float scale) {
GlStateManager.pushMatrix();
GlStateManager.enableBlend();
GlStateManager.blendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
RenderHelper.enableGUIStandardItemLighting();
GlStateManager.enableDepth();
GlStateManager.enableRescaleNormal();
GlStateManager.translate(x, y, 0);
GlStateManager.scale(scale, scale, scale);
Minecraft.getMinecraft().getRenderItem().renderItemAndEffectIntoGUI(stack, 0, 0);
Minecraft.getMinecraft().getRenderItem().renderItemOverlayIntoGUI(Minecraft.getMinecraft().fontRenderer, stack, 0, 0, null);
RenderHelper.disableStandardItemLighting();
GlStateManager.popMatrix();
}
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-20 21:19:08 +02:00
public static ICapabilityProvider makeRechargeProvider(ItemStack stack) {
return new ICapabilityProvider() {
private final IAuraRecharge recharge = () -> {
if (stack.getItemDamage() > 0) {
stack.setItemDamage(stack.getItemDamage() - 1);
return true;
}
return false;
};
@Override
public boolean hasCapability(@Nonnull Capability<?> capability, @Nullable EnumFacing facing) {
return capability == Capabilities.auraRecharge;
}
@Nullable
@Override
public <T> T getCapability(@Nonnull Capability<T> capability, @Nullable EnumFacing facing) {
return capability == Capabilities.auraRecharge ? (T) this.recharge : null;
}
};
}
2018-10-14 14:27:18 +02:00
}