ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/common/items/lens/LensColor.java

103 lines
4.6 KiB
Java
Raw Normal View History

2020-09-09 16:49:01 +02:00
package de.ellpeck.actuallyadditions.common.items.lens;
2018-07-28 02:48:26 +02:00
import java.util.List;
import java.util.Map;
import java.util.Random;
import de.ellpeck.actuallyadditions.api.ActuallyAdditionsAPI;
2016-01-05 14:57:50 +01:00
import de.ellpeck.actuallyadditions.api.internal.IAtomicReconstructor;
2016-01-05 04:47:35 +01:00
import de.ellpeck.actuallyadditions.api.lens.Lens;
import de.ellpeck.actuallyadditions.api.recipe.IColorLensChanger;
2020-09-09 16:49:01 +02:00
import de.ellpeck.actuallyadditions.common.util.StackUtil;
import net.minecraft.block.Block;
2016-03-18 23:47:22 +01:00
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
2016-03-18 23:47:22 +01:00
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.util.FakePlayerFactory;
2019-05-02 09:10:29 +02:00
public class LensColor extends Lens {
2015-12-19 10:30:39 +01:00
public static final int ENERGY_USE = 200;
//Thanks to xdjackiexd for this, as I couldn't be bothered
2019-05-02 09:10:29 +02:00
public static final float[][] POSSIBLE_COLORS = { { 158F, 43F, 39F }, //Red
{ 234F, 126F, 53F }, //Orange
{ 194F, 181F, 28F }, //Yellow
{ 57F, 186F, 46F }, //Lime Green
{ 54F, 75F, 24F }, //Green
{ 99F, 135F, 210F }, //Light Blue
{ 38F, 113F, 145F }, //Cyan
{ 37F, 49F, 147F }, //Blue
{ 126F, 52F, 191F }, //Purple
{ 190F, 73F, 201F }, //Magenta
{ 217F, 129F, 153F }, //Pink
{ 86F, 51F, 28F }, //Brown
};
2016-11-08 20:04:36 +01:00
private final Random rand = new Random();
@Override
2019-05-02 09:10:29 +02:00
public boolean invoke(IBlockState hitState, BlockPos hitBlock, IAtomicReconstructor tile) {
if (hitBlock != null) {
if (tile.getEnergy() >= ENERGY_USE) {
2016-07-04 20:15:41 +02:00
IBlockState state = tile.getWorldObject().getBlockState(hitBlock);
Block block = state.getBlock();
int meta = block.getMetaFromState(state);
ItemStack returnStack = this.tryConvert(new ItemStack(block, 1, meta), hitState, hitBlock, tile);
2019-05-02 09:10:29 +02:00
if (returnStack != null && returnStack.getItem() instanceof ItemBlock) {
2019-02-27 19:53:05 +01:00
Block toPlace = Block.getBlockFromItem(returnStack.getItem());
IBlockState state2Place = toPlace.getStateForPlacement(tile.getWorldObject(), hitBlock, EnumFacing.UP, 0, 0, 0, returnStack.getMetadata(), FakePlayerFactory.getMinecraft((WorldServer) tile.getWorldObject()), EnumHand.MAIN_HAND);
tile.getWorldObject().setBlockState(hitBlock, state2Place, 2);
2019-02-27 19:53:05 +01:00
tile.extractEnergy(ENERGY_USE);
}
}
2019-05-02 09:10:29 +02:00
List<EntityItem> items = tile.getWorldObject().getEntitiesWithinAABB(EntityItem.class, new AxisAlignedBB(hitBlock.getX(), hitBlock.getY(), hitBlock.getZ(), hitBlock.getX() + 1, hitBlock.getY() + 1, hitBlock.getZ() + 1));
for (EntityItem item : items) {
if (!item.isDead && StackUtil.isValid(item.getItem()) && tile.getEnergy() >= ENERGY_USE) {
ItemStack newStack = this.tryConvert(item.getItem(), hitState, hitBlock, tile);
2019-05-02 09:10:29 +02:00
if (StackUtil.isValid(newStack)) {
item.setDead();
EntityItem newItem = new EntityItem(tile.getWorldObject(), item.posX, item.posY, item.posZ, newStack);
2016-11-26 21:32:27 +01:00
tile.getWorldObject().spawnEntity(newItem);
2016-01-05 04:47:35 +01:00
tile.extractEnergy(ENERGY_USE);
}
}
}
}
return false;
}
2019-05-02 09:10:29 +02:00
private ItemStack tryConvert(ItemStack stack, IBlockState hitState, BlockPos hitBlock, IAtomicReconstructor tile) {
if (StackUtil.isValid(stack)) {
Item item = stack.getItem();
2019-05-02 09:10:29 +02:00
for (Map.Entry<Item, IColorLensChanger> changer : ActuallyAdditionsAPI.RECONSTRUCTOR_LENS_COLOR_CHANGERS.entrySet()) {
if (item == changer.getKey()) { return changer.getValue().modifyItem(stack, hitState, hitBlock, tile); }
}
}
2018-07-28 03:40:36 +02:00
return ItemStack.EMPTY;
}
@Override
2019-05-02 09:10:29 +02:00
public float[] getColor() {
2016-11-02 19:36:32 +01:00
float[] colors = POSSIBLE_COLORS[this.rand.nextInt(POSSIBLE_COLORS.length)];
2019-05-02 09:10:29 +02:00
return new float[] { colors[0] / 255F, colors[1] / 255F, colors[2] / 255F };
}
@Override
2019-05-02 09:10:29 +02:00
public int getDistance() {
return 10;
}
@Override
2019-05-02 09:10:29 +02:00
public boolean canInvoke(IAtomicReconstructor tile, EnumFacing sideToShootTo, int energyUsePerShot) {
return tile.getEnergy() - energyUsePerShot >= ENERGY_USE;
}
2016-01-05 04:47:35 +01:00
}