2020-09-09 16:49:01 +02:00
|
|
|
package de.ellpeck.actuallyadditions.common.items.lens;
|
2015-12-03 22:49:31 +01:00
|
|
|
|
2018-07-28 02:48:26 +02:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Random;
|
|
|
|
|
2016-05-05 19:50:59 +02:00
|
|
|
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;
|
2016-05-05 19:50:59 +02:00
|
|
|
import de.ellpeck.actuallyadditions.api.recipe.IColorLensChanger;
|
2020-09-09 16:49:01 +02:00
|
|
|
import de.ellpeck.actuallyadditions.common.util.StackUtil;
|
2015-12-03 22:49:31 +01:00
|
|
|
import net.minecraft.block.Block;
|
2016-03-18 23:47:22 +01:00
|
|
|
import net.minecraft.block.state.IBlockState;
|
2015-12-03 22:49:31 +01:00
|
|
|
import net.minecraft.entity.item.EntityItem;
|
2016-05-05 19:50:59 +02:00
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemBlock;
|
2016-01-17 01:50:35 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2016-07-20 09:49:19 +02:00
|
|
|
import net.minecraft.util.EnumFacing;
|
2017-08-17 07:06:09 +02:00
|
|
|
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;
|
2017-08-17 07:06:09 +02:00
|
|
|
import net.minecraft.world.WorldServer;
|
|
|
|
import net.minecraftforge.common.util.FakePlayerFactory;
|
2015-12-03 22:49:31 +01:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public class LensColor extends Lens {
|
2015-12-03 22:49:31 +01:00
|
|
|
|
2015-12-19 10:30:39 +01:00
|
|
|
public static final int ENERGY_USE = 200;
|
2015-12-03 22:49:31 +01:00
|
|
|
//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
|
2015-12-03 22:49:31 +01:00
|
|
|
};
|
2016-11-08 20:04:36 +01:00
|
|
|
private final Random rand = new Random();
|
2015-12-03 22:49:31 +01:00
|
|
|
|
|
|
|
@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);
|
2017-08-17 07:06:09 +02:00
|
|
|
tile.getWorldObject().setBlockState(hitBlock, state2Place, 2);
|
2019-02-27 19:53:05 +01:00
|
|
|
tile.extractEnergy(ENERGY_USE);
|
2015-12-03 22:49:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2017-06-29 18:30:02 +02:00
|
|
|
ItemStack newStack = this.tryConvert(item.getItem(), hitState, hitBlock, tile);
|
2019-05-02 09:10:29 +02:00
|
|
|
if (StackUtil.isValid(newStack)) {
|
2016-01-17 01:50:35 +01:00
|
|
|
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-17 01:50:35 +01:00
|
|
|
|
2016-01-05 04:47:35 +01:00
|
|
|
tile.extractEnergy(ENERGY_USE);
|
2015-12-03 22:49:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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)) {
|
2016-05-05 19:50:59 +02:00
|
|
|
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); }
|
2016-05-05 19:50:59 +02:00
|
|
|
}
|
|
|
|
}
|
2018-07-28 03:40:36 +02:00
|
|
|
return ItemStack.EMPTY;
|
2016-05-05 19:50:59 +02:00
|
|
|
}
|
|
|
|
|
2015-12-03 22:49:31 +01:00
|
|
|
@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 };
|
2015-12-03 22:49:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-05-02 09:10:29 +02:00
|
|
|
public int getDistance() {
|
2015-12-03 22:49:31 +01:00
|
|
|
return 10;
|
|
|
|
}
|
2016-07-20 09:49:19 +02:00
|
|
|
|
|
|
|
@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-07-20 09:49:19 +02:00
|
|
|
}
|
2016-01-05 04:47:35 +01:00
|
|
|
}
|