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

120 lines
4.9 KiB
Java
Raw Normal View History

/*
2016-05-16 22:52:27 +02:00
* This file ("LensColor.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
2016-05-16 22:52:27 +02:00
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
2017-01-01 16:23:26 +01:00
* © 2015-2017 Ellpeck
*/
2016-01-05 04:47:35 +01:00
package de.ellpeck.actuallyadditions.mod.items.lens;
2016-01-08 13:31:58 +01:00
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;
import de.ellpeck.actuallyadditions.mod.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;
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
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
2016-03-18 23:47:22 +01: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);
if(returnStack != null && returnStack.getItem() instanceof ItemBlock){
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);
tile.extractEnergy(ENERGY_USE);
}
}
2018-07-28 02:48:26 +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);
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;
}
private ItemStack tryConvert(ItemStack stack, IBlockState hitState, BlockPos hitBlock, IAtomicReconstructor tile){
if(StackUtil.isValid(stack)){
Item item = stack.getItem();
if(item != null){
2016-05-19 20:05:12 +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);
}
}
}
}
2017-08-30 18:28:35 +02:00
return ItemStack.EMPTY.copy();
}
@Override
public float[] getColor(){
2016-11-02 19:36:32 +01:00
float[] colors = POSSIBLE_COLORS[this.rand.nextInt(POSSIBLE_COLORS.length)];
return new float[]{colors[0]/255F, colors[1]/255F, colors[2]/255F};
}
@Override
public int getDistance(){
return 10;
}
@Override
public boolean canInvoke(IAtomicReconstructor tile, EnumFacing sideToShootTo, int energyUsePerShot){
return tile.getEnergy()-energyUsePerShot >= ENERGY_USE;
}
2016-01-05 04:47:35 +01:00
}