2015-11-14 21:27:36 +01:00
|
|
|
/*
|
2016-05-16 22:52:27 +02:00
|
|
|
* This file ("TileEntityAtomicReconstructor.java") is part of the Actually Additions mod for Minecraft.
|
2015-11-14 21:27:36 +01:00
|
|
|
* 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
|
2015-11-14 21:27:36 +01:00
|
|
|
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
|
|
|
|
*
|
2016-05-16 22:54:42 +02:00
|
|
|
* © 2015-2016 Ellpeck
|
2015-11-14 21:27:36 +01:00
|
|
|
*/
|
|
|
|
|
2016-01-05 04:47:35 +01:00
|
|
|
package de.ellpeck.actuallyadditions.mod.tile;
|
2015-11-14 21:27:36 +01:00
|
|
|
|
2016-05-15 12:00:42 +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.ILensItem;
|
|
|
|
import de.ellpeck.actuallyadditions.api.lens.Lens;
|
2016-05-01 22:26:26 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.misc.SoundHandler;
|
2016-06-15 16:43:59 +02:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.AssetUtil;
|
2016-11-16 20:31:16 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
|
2016-01-05 04:47:35 +01:00
|
|
|
import de.ellpeck.actuallyadditions.mod.util.WorldUtil;
|
2016-07-04 20:15:41 +02:00
|
|
|
import net.minecraft.block.state.IBlockState;
|
2015-11-14 23:05:20 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
2015-11-14 21:27:36 +01:00
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
2016-01-07 21:41:28 +01:00
|
|
|
import net.minecraft.util.EnumFacing;
|
2016-05-01 22:26:26 +02:00
|
|
|
import net.minecraft.util.SoundCategory;
|
2016-03-18 23:47:22 +01:00
|
|
|
import net.minecraft.util.math.BlockPos;
|
2016-01-06 18:10:01 +01:00
|
|
|
import net.minecraft.world.World;
|
2016-11-26 08:58:42 +01:00
|
|
|
import net.minecraftforge.energy.IEnergyStorage;
|
2015-11-14 21:27:36 +01:00
|
|
|
|
2016-11-26 20:43:50 +01:00
|
|
|
public class TileEntityAtomicReconstructor extends TileEntityInventoryBase implements IEnergyDisplay, IAtomicReconstructor{
|
2015-11-15 03:15:19 +01:00
|
|
|
|
2015-12-09 17:58:20 +01:00
|
|
|
public static final int ENERGY_USE = 1000;
|
2016-11-26 20:43:50 +01:00
|
|
|
public final CustomEnergyStorage storage = new CustomEnergyStorage(300000, 5000, 0);
|
2016-05-07 02:49:03 +02:00
|
|
|
public int counter;
|
2015-11-14 21:27:36 +01:00
|
|
|
private int currentTime;
|
2015-12-21 22:18:33 +01:00
|
|
|
private int oldEnergy;
|
2015-11-14 21:27:36 +01:00
|
|
|
|
2015-11-22 18:52:11 +01:00
|
|
|
public TileEntityAtomicReconstructor(){
|
|
|
|
super(1, "reconstructor");
|
|
|
|
}
|
|
|
|
|
2016-05-07 02:49:03 +02:00
|
|
|
public static void shootLaser(World world, double startX, double startY, double startZ, double endX, double endY, double endZ, Lens currentLens){
|
2016-11-08 20:04:36 +01:00
|
|
|
world.playSound(null, startX, startY, startZ, SoundHandler.reconstructor, SoundCategory.BLOCKS, 0.35F, 1.0F);
|
2016-11-07 19:58:34 +01:00
|
|
|
AssetUtil.shootParticles(world, startX, startY, startZ, endX, endY, endZ, currentLens.getColor(), 8, 2F, 1F);
|
2016-05-07 02:49:03 +02:00
|
|
|
}
|
|
|
|
|
2016-02-01 17:49:55 +01:00
|
|
|
@Override
|
2016-07-02 15:01:46 +02:00
|
|
|
public void writeSyncableNBT(NBTTagCompound compound, NBTType type){
|
|
|
|
super.writeSyncableNBT(compound, type);
|
|
|
|
if(type != NBTType.SAVE_BLOCK){
|
|
|
|
compound.setInteger("CurrentTime", this.currentTime);
|
|
|
|
compound.setInteger("Counter", this.counter);
|
|
|
|
}
|
2016-02-01 17:49:55 +01:00
|
|
|
this.storage.writeToNBT(compound);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean shouldSyncSlots(){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-07-02 15:01:46 +02:00
|
|
|
public void readSyncableNBT(NBTTagCompound compound, NBTType type){
|
|
|
|
super.readSyncableNBT(compound, type);
|
|
|
|
if(type != NBTType.SAVE_BLOCK){
|
|
|
|
this.currentTime = compound.getInteger("CurrentTime");
|
|
|
|
this.counter = compound.getInteger("Counter");
|
|
|
|
}
|
2016-02-01 17:49:55 +01:00
|
|
|
this.storage.readFromNBT(compound);
|
|
|
|
}
|
|
|
|
|
2015-11-14 21:27:36 +01:00
|
|
|
@Override
|
|
|
|
public void updateEntity(){
|
2015-11-18 23:11:24 +01:00
|
|
|
super.updateEntity();
|
2015-11-14 21:27:36 +01:00
|
|
|
if(!this.worldObj.isRemote){
|
2016-07-02 15:01:46 +02:00
|
|
|
if(!this.isRedstonePowered && !this.isPulseMode){
|
2015-11-14 23:05:20 +01:00
|
|
|
if(this.currentTime > 0){
|
|
|
|
this.currentTime--;
|
|
|
|
if(this.currentTime <= 0){
|
2015-12-18 16:22:39 +01:00
|
|
|
this.doWork();
|
2015-11-14 21:27:36 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-14 23:05:20 +01:00
|
|
|
else{
|
2015-11-28 19:02:01 +01:00
|
|
|
this.currentTime = 100;
|
2015-11-14 23:05:20 +01:00
|
|
|
}
|
2015-11-14 21:27:36 +01:00
|
|
|
}
|
2015-12-21 22:18:33 +01:00
|
|
|
|
|
|
|
if(this.oldEnergy != this.storage.getEnergyStored() && this.sendUpdateWithInterval()){
|
|
|
|
this.oldEnergy = this.storage.getEnergyStored();
|
|
|
|
}
|
2015-11-14 21:27:36 +01:00
|
|
|
}
|
2015-12-03 21:05:45 +01:00
|
|
|
|
2015-11-14 21:27:36 +01:00
|
|
|
}
|
|
|
|
|
2015-12-18 16:22:39 +01:00
|
|
|
private void doWork(){
|
2015-12-18 16:25:12 +01:00
|
|
|
if(this.storage.getEnergyStored() >= ENERGY_USE){
|
2016-07-04 20:15:41 +02:00
|
|
|
IBlockState state = this.worldObj.getBlockState(this.pos);
|
|
|
|
EnumFacing sideToManipulate = WorldUtil.getDirectionByPistonRotation(state.getBlock().getMetaFromState(state));
|
2015-12-18 16:25:12 +01:00
|
|
|
//The Lens the Reconstructor currently has installed
|
2016-05-14 14:14:06 +02:00
|
|
|
Lens currentLens = this.getLens();
|
2016-07-20 09:49:19 +02:00
|
|
|
if(currentLens.canInvoke(this, sideToManipulate, ENERGY_USE)){
|
|
|
|
//Extract energy for shooting the laser itself too!
|
2016-11-23 18:10:55 +01:00
|
|
|
this.storage.extractEnergyInternal(ENERGY_USE, false);
|
2015-12-18 16:22:39 +01:00
|
|
|
|
2016-07-20 09:49:19 +02:00
|
|
|
int distance = currentLens.getDistance();
|
|
|
|
for(int i = 0; i < distance; i++){
|
|
|
|
BlockPos hitBlock = this.pos.offset(sideToManipulate, i+1);
|
|
|
|
|
|
|
|
if(currentLens.invoke(this.worldObj.getBlockState(hitBlock), hitBlock, this) || i >= distance-1){
|
|
|
|
shootLaser(this.worldObj, this.getX(), this.getY(), this.getZ(), hitBlock.getX(), hitBlock.getY(), hitBlock.getZ(), currentLens);
|
|
|
|
break;
|
|
|
|
}
|
2015-12-18 16:25:12 +01:00
|
|
|
}
|
2015-12-18 16:22:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-14 14:14:06 +02:00
|
|
|
@Override
|
|
|
|
public Lens getLens(){
|
2016-11-20 20:13:26 +01:00
|
|
|
if(StackUtil.isValid(this.slots.get(0))){
|
|
|
|
if(this.slots.get(0).getItem() instanceof ILensItem){
|
|
|
|
return ((ILensItem)this.slots.get(0).getItem()).getLens();
|
2015-11-22 18:52:11 +01:00
|
|
|
}
|
|
|
|
}
|
2016-05-15 12:00:42 +02:00
|
|
|
return this.counter >= 500 ? ActuallyAdditionsAPI.lensDisruption : ActuallyAdditionsAPI.lensDefaultConversion;
|
2015-11-15 14:25:12 +01:00
|
|
|
}
|
|
|
|
|
2015-11-14 21:27:36 +01:00
|
|
|
@Override
|
2016-02-01 17:49:55 +01:00
|
|
|
public int getX(){
|
|
|
|
return this.getPos().getX();
|
2016-01-11 22:41:33 +01:00
|
|
|
}
|
2015-11-14 21:27:36 +01:00
|
|
|
|
2016-02-01 17:43:14 +01:00
|
|
|
@Override
|
2016-02-01 17:49:55 +01:00
|
|
|
public int getY(){
|
|
|
|
return this.getPos().getY();
|
2016-02-01 17:43:14 +01:00
|
|
|
}
|
|
|
|
|
2016-01-11 22:41:33 +01:00
|
|
|
@Override
|
2016-02-01 17:49:55 +01:00
|
|
|
public int getZ(){
|
|
|
|
return this.getPos().getZ();
|
2015-12-01 19:48:09 +01:00
|
|
|
}
|
|
|
|
|
2015-11-14 21:27:36 +01:00
|
|
|
@Override
|
2016-02-01 17:49:55 +01:00
|
|
|
public World getWorldObject(){
|
|
|
|
return this.getWorld();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void extractEnergy(int amount){
|
2016-11-23 18:25:08 +01:00
|
|
|
this.storage.extractEnergy(amount, false);
|
2016-02-01 17:49:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
public boolean isItemValidForSlot(int i, ItemStack stack){
|
2016-11-16 20:31:16 +01:00
|
|
|
return StackUtil.isValid(stack) && stack.getItem() instanceof ILensItem;
|
2016-02-01 17:49:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void markDirty(){
|
|
|
|
super.markDirty();
|
|
|
|
this.sendUpdate();
|
2015-11-14 21:27:36 +01:00
|
|
|
}
|
|
|
|
|
2015-11-22 18:52:11 +01:00
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
public boolean canInsertItem(int slot, ItemStack stack, EnumFacing side){
|
2015-11-29 13:17:45 +01:00
|
|
|
return this.isItemValidForSlot(slot, stack);
|
2015-11-22 18:52:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-05-29 23:49:35 +02:00
|
|
|
public boolean canExtractItem(int slot, ItemStack stack, EnumFacing side){
|
2015-11-22 18:52:11 +01:00
|
|
|
return true;
|
|
|
|
}
|
2015-12-13 00:54:25 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getEnergy(){
|
|
|
|
return this.storage.getEnergyStored();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-11-23 18:10:55 +01:00
|
|
|
public CustomEnergyStorage getEnergyStorage(){
|
2016-07-21 13:22:55 +02:00
|
|
|
return this.storage;
|
2015-12-13 00:54:25 +01:00
|
|
|
}
|
2015-12-18 16:22:39 +01:00
|
|
|
|
2016-07-02 18:39:47 +02:00
|
|
|
@Override
|
|
|
|
public boolean needsHoldShift(){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-18 16:22:39 +01:00
|
|
|
@Override
|
2016-07-02 15:01:46 +02:00
|
|
|
public boolean isRedstoneToggle(){
|
|
|
|
return true;
|
2015-12-18 16:22:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void activateOnPulse(){
|
|
|
|
this.doWork();
|
|
|
|
}
|
2016-11-26 08:58:42 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public IEnergyStorage getEnergyStorage(EnumFacing facing){
|
|
|
|
return this.storage;
|
|
|
|
}
|
2015-11-14 21:27:36 +01:00
|
|
|
}
|