2017-02-23 13:29:43 +01:00
/ *
* This file is part of TechReborn , licensed under the MIT License ( MIT ) .
*
* Copyright ( c ) 2017 TechReborn
*
* Permission is hereby granted , free of charge , to any person obtaining a copy
* of this software and associated documentation files ( the " Software " ) , to deal
* in the Software without restriction , including without limitation the rights
* to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
* copies of the Software , and to permit persons to whom the Software is
* furnished to do so , subject to the following conditions :
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software .
*
* THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
* IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
* LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE .
* /
2015-04-14 01:12:24 +02:00
package techreborn.tiles ;
2015-04-15 17:23:12 +02:00
import net.minecraft.entity.player.EntityPlayer ;
2015-04-15 18:27:05 +02:00
import net.minecraft.inventory.Container ;
import net.minecraft.inventory.InventoryCrafting ;
2015-04-15 17:23:12 +02:00
import net.minecraft.item.ItemStack ;
2015-04-15 18:27:05 +02:00
import net.minecraft.nbt.NBTTagCompound ;
2015-11-23 15:45:16 +01:00
import net.minecraft.util.EnumFacing ;
2016-04-11 18:43:54 +02:00
import reborncore.api.tile.IInventoryProvider ;
2016-10-08 21:46:16 +02:00
import reborncore.common.IWrenchable ;
2016-08-10 01:45:07 +02:00
import reborncore.common.powerSystem.TilePowerAcceptor ;
2017-06-09 04:12:58 +02:00
import reborncore.common.registration.RebornRegistry ;
import reborncore.common.registration.impl.ConfigRegistry ;
2015-11-08 13:15:45 +01:00
import reborncore.common.util.Inventory ;
import reborncore.common.util.ItemUtils ;
2015-04-15 18:27:05 +02:00
import techreborn.api.RollingMachineRecipe ;
2017-01-08 20:34:46 +01:00
import techreborn.client.container.IContainerProvider ;
import techreborn.client.container.builder.BuiltContainer ;
import techreborn.client.container.builder.ContainerBuilder ;
2015-04-14 20:23:16 +02:00
import techreborn.init.ModBlocks ;
2017-06-09 04:12:58 +02:00
import techreborn.lib.ModInfo ;
2015-04-14 01:12:24 +02:00
2015-04-15 18:27:05 +02:00
//TODO add tick and power bars.
2017-06-09 04:12:58 +02:00
@RebornRegistry ( modID = ModInfo . MOD_ID )
2017-01-08 20:34:46 +01:00
public class TileRollingMachine extends TilePowerAcceptor
2017-06-09 04:12:58 +02:00
implements IWrenchable , IInventoryProvider , IContainerProvider {
@ConfigRegistry ( config = " machines " , category = " rolling_machine " , key = " RollingMachineMaxInput " , comment = " Rolling Machine Max Input (Value in EU) " )
public static int maxInput = 32 ;
@ConfigRegistry ( config = " machines " , category = " rolling_machine " , key = " RollingMachineEnergyPerTick " , comment = " Rolling Machine Energy Per Tick (Value in EU) " )
public static int energyPerTick = 5 ;
@ConfigRegistry ( config = " machines " , category = " rolling_machine " , key = " RollingMachineEnergyRunTime " , comment = " Rolling Machine Run Time " )
public static int runTime = 250 ;
@ConfigRegistry ( config = " machines " , category = " rolling_machine " , key = " RollingMachineMaxEnergy " , comment = " Rolling Machine Max Energy (Value in EU) " )
public static int maxEnergy = 10000 ;
// @ConfigRegistry(config = "machines", category = "rolling_machine", key = "RollingMachineWrenchDropRate", comment = "Rolling Machi Wrench Drop Rate")
public static float wrenchDropRate = 1 . 0F ;
2016-03-25 10:47:34 +01:00
public final InventoryCrafting craftMatrix = new InventoryCrafting ( new RollingTileContainer ( ) , 3 , 3 ) ;
public Inventory inventory = new Inventory ( 3 , " TileRollingMachine " , 64 , this ) ;
public boolean isRunning ;
public int tickTime ;
public ItemStack currentRecipe ;
2017-05-02 19:38:16 +02:00
private int outputSlot ;
2016-03-25 10:47:34 +01:00
2016-10-08 21:46:16 +02:00
public TileRollingMachine ( ) {
2017-06-09 17:31:19 +02:00
super ( ) ;
2017-06-09 04:12:58 +02:00
outputSlot = 0 ;
2016-08-10 01:45:07 +02:00
}
@Override
2017-04-11 20:12:32 +02:00
public double getBaseMaxPower ( ) {
2017-06-09 04:12:58 +02:00
return maxEnergy ;
2016-08-10 01:45:07 +02:00
}
@Override
2017-01-08 14:00:19 +01:00
public boolean canAcceptEnergy ( final EnumFacing direction ) {
2016-08-10 01:45:07 +02:00
return true ;
}
2016-03-25 10:47:34 +01:00
@Override
2017-01-08 14:00:19 +01:00
public boolean canProvideEnergy ( final EnumFacing direction ) {
2016-08-10 01:45:07 +02:00
return false ;
}
@Override
2017-04-11 20:12:32 +02:00
public double getBaseMaxOutput ( ) {
2016-08-10 01:45:07 +02:00
return 0 ;
2016-03-25 10:47:34 +01:00
}
@Override
2017-04-11 20:12:32 +02:00
public double getBaseMaxInput ( ) {
2017-06-09 04:12:58 +02:00
return maxInput ;
2016-03-25 10:47:34 +01:00
}
2016-08-10 01:45:07 +02:00
@Override
2016-10-08 21:46:16 +02:00
public void updateEntity ( ) {
2016-08-10 01:45:07 +02:00
super . updateEntity ( ) ;
2017-01-08 14:00:19 +01:00
this . charge ( 2 ) ;
if ( ! this . world . isRemote ) {
this . currentRecipe = RollingMachineRecipe . instance . findMatchingRecipe ( this . craftMatrix , this . world ) ;
if ( this . currentRecipe ! = null & & this . canMake ( ) ) {
if ( this . tickTime > = this . runTime ) {
this . currentRecipe = RollingMachineRecipe . instance . findMatchingRecipe ( this . craftMatrix , this . world ) ;
if ( this . currentRecipe ! = null ) {
2016-03-25 10:47:34 +01:00
boolean hasCrafted = false ;
2017-05-02 19:38:16 +02:00
if ( this . inventory . getStackInSlot ( outputSlot ) = = ItemStack . EMPTY ) {
this . inventory . setInventorySlotContents ( outputSlot , this . currentRecipe ) ;
2017-01-08 14:00:19 +01:00
this . tickTime = - 1 ;
2016-03-25 10:47:34 +01:00
hasCrafted = true ;
2016-10-08 21:46:16 +02:00
} else {
2017-05-02 19:38:16 +02:00
if ( this . inventory . getStackInSlot ( outputSlot ) . getCount ( ) + this . currentRecipe . getCount ( ) < = this . currentRecipe
2017-06-09 04:12:58 +02:00
. getMaxStackSize ( ) ) {
2017-05-02 19:38:16 +02:00
final ItemStack stack = this . inventory . getStackInSlot ( outputSlot ) ;
2017-01-08 14:00:19 +01:00
stack . setCount ( stack . getCount ( ) + this . currentRecipe . getCount ( ) ) ;
2017-05-02 19:38:16 +02:00
this . inventory . setInventorySlotContents ( outputSlot , stack ) ;
2017-01-08 14:00:19 +01:00
this . tickTime = - 1 ;
2016-03-25 10:47:34 +01:00
hasCrafted = true ;
}
}
2016-10-08 21:46:16 +02:00
if ( hasCrafted ) {
2017-01-08 14:00:19 +01:00
for ( int i = 0 ; i < this . craftMatrix . getSizeInventory ( ) ; i + + ) {
this . craftMatrix . decrStackSize ( i , 1 ) ;
2016-03-25 10:47:34 +01:00
}
2017-01-08 14:00:19 +01:00
this . currentRecipe = null ;
2016-03-25 10:47:34 +01:00
}
}
}
}
2017-01-08 14:00:19 +01:00
if ( this . currentRecipe ! = null ) {
2017-06-09 04:12:58 +02:00
if ( this . canUseEnergy ( energyPerTick ) & & this . tickTime < this . runTime ) {
this . useEnergy ( energyPerTick ) ;
2017-01-08 14:00:19 +01:00
this . tickTime + + ;
2016-03-25 10:47:34 +01:00
}
}
2017-01-08 14:00:19 +01:00
if ( this . currentRecipe = = null ) {
this . tickTime = - 1 ;
2016-03-25 10:47:34 +01:00
}
2016-10-08 21:46:16 +02:00
} else {
2017-01-08 14:00:19 +01:00
this . currentRecipe = RollingMachineRecipe . instance . findMatchingRecipe ( this . craftMatrix , this . world ) ;
if ( this . currentRecipe ! = null ) {
this . inventory . setInventorySlotContents ( 1 , this . currentRecipe ) ;
2016-10-08 21:46:16 +02:00
} else {
2017-01-08 14:00:19 +01:00
this . inventory . setInventorySlotContents ( 1 , ItemStack . EMPTY ) ;
2016-03-25 10:47:34 +01:00
}
}
}
2016-10-08 21:46:16 +02:00
public boolean canMake ( ) {
2017-01-08 14:00:19 +01:00
return RollingMachineRecipe . instance . findMatchingRecipe ( this . craftMatrix , this . world ) ! = null ;
2016-03-25 10:47:34 +01:00
}
@Override
2017-01-08 14:00:19 +01:00
public boolean wrenchCanSetFacing ( final EntityPlayer entityPlayer , final EnumFacing side ) {
2016-03-25 10:47:34 +01:00
return false ;
}
@Override
2016-10-08 21:46:16 +02:00
public EnumFacing getFacing ( ) {
2017-01-08 14:00:19 +01:00
return this . getFacingEnum ( ) ;
2016-03-25 10:47:34 +01:00
}
@Override
2017-01-08 14:00:19 +01:00
public boolean wrenchCanRemove ( final EntityPlayer entityPlayer ) {
2016-04-03 15:34:39 +02:00
return entityPlayer . isSneaking ( ) ;
2016-03-25 10:47:34 +01:00
}
@Override
2016-10-08 21:46:16 +02:00
public float getWrenchDropRate ( ) {
2017-06-09 04:12:58 +02:00
return wrenchDropRate ;
2016-03-25 10:47:34 +01:00
}
@Override
2017-01-08 14:00:19 +01:00
public ItemStack getWrenchDrop ( final EntityPlayer entityPlayer ) {
2017-01-01 08:29:32 +01:00
return new ItemStack ( ModBlocks . ROLLING_MACHINE , 1 ) ;
2016-03-25 10:47:34 +01:00
}
@Override
2017-01-08 14:00:19 +01:00
public void readFromNBT ( final NBTTagCompound tagCompound ) {
2016-03-25 10:47:34 +01:00
super . readFromNBT ( tagCompound ) ;
2017-01-08 14:00:19 +01:00
ItemUtils . readInvFromNBT ( this . craftMatrix , " Crafting " , tagCompound ) ;
this . isRunning = tagCompound . getBoolean ( " isRunning " ) ;
this . tickTime = tagCompound . getInteger ( " tickTime " ) ;
2016-03-25 10:47:34 +01:00
}
@Override
2017-01-08 14:00:19 +01:00
public NBTTagCompound writeToNBT ( final NBTTagCompound tagCompound ) {
2016-03-25 10:47:34 +01:00
super . writeToNBT ( tagCompound ) ;
2017-01-08 14:00:19 +01:00
ItemUtils . writeInvToNBT ( this . craftMatrix , " Crafting " , tagCompound ) ;
this . writeUpdateToNBT ( tagCompound ) ;
2016-05-18 17:53:54 +02:00
return tagCompound ;
2016-03-25 10:47:34 +01:00
}
2017-01-08 14:00:19 +01:00
public void writeUpdateToNBT ( final NBTTagCompound tagCompound ) {
tagCompound . setBoolean ( " isRunning " , this . isRunning ) ;
tagCompound . setInteger ( " tickTime " , this . tickTime ) ;
2016-03-25 10:47:34 +01:00
}
2017-05-02 19:38:16 +02:00
@Override
public int [ ] getSlotsForFace ( final EnumFacing side ) {
if ( side . equals ( EnumFacing . DOWN ) )
return new int [ ] { 0 } ;
return new int [ 0 ] ;
}
2017-06-09 04:12:58 +02:00
2017-05-02 19:38:16 +02:00
@Override
public boolean canInsertItem ( final int Index , final ItemStack itemStack , final EnumFacing side ) {
return false ;
}
@Override
public boolean canExtractItem ( final int Index , final ItemStack itemStack , final EnumFacing side ) {
return Index = = outputSlot ;
}
2016-03-25 10:47:34 +01:00
@Override
2016-10-08 21:46:16 +02:00
public void invalidate ( ) {
2016-03-25 10:47:34 +01:00
super . invalidate ( ) ;
}
@Override
2016-10-08 21:46:16 +02:00
public void onChunkUnload ( ) {
2016-03-25 10:47:34 +01:00
super . onChunkUnload ( ) ;
}
@Override
2016-04-11 18:43:54 +02:00
public Inventory getInventory ( ) {
2017-01-08 14:00:19 +01:00
return this . inventory ;
2016-03-25 10:47:34 +01:00
}
2017-01-08 14:00:19 +01:00
public int getBurnTime ( ) {
return this . tickTime ;
}
public void setBurnTime ( final int burnTime ) {
this . tickTime = burnTime ;
}
public int getBurnTimeRemainingScaled ( final int scale ) {
if ( this . tickTime = = 0 | | this . runTime = = 0 ) {
return 0 ;
}
return this . tickTime * scale / this . runTime ;
}
2017-01-08 20:34:46 +01:00
@Override
public BuiltContainer createContainer ( final EntityPlayer player ) {
return new ContainerBuilder ( " rollingmachine " ) . player ( player . inventory ) . inventory ( 8 , 84 ) . hotbar ( 8 , 142 )
2017-06-09 04:12:58 +02:00
. addInventory ( ) . tile ( this . craftMatrix ) . slot ( 0 , 30 , 17 ) . slot ( 1 , 48 , 17 ) . slot ( 2 , 66 , 17 ) . slot ( 3 , 30 , 35 )
. slot ( 4 , 48 , 35 ) . slot ( 5 , 66 , 35 ) . slot ( 6 , 30 , 53 ) . slot ( 7 , 48 , 53 ) . slot ( 8 , 66 , 53 )
. onCraft ( inv - > this . inventory . setInventorySlotContents ( 1 ,
RollingMachineRecipe . instance . findMatchingRecipe ( inv , this . world ) ) )
. addInventory ( ) . tile ( this ) . outputSlot ( 0 , 124 , 35 ) . energySlot ( 2 , 8 , 51 ) . syncEnergyValue ( )
. syncIntegerValue ( this : : getBurnTime , this : : setBurnTime ) . addInventory ( ) . create ( ) ;
}
private static class RollingTileContainer extends Container {
@Override
public boolean canInteractWith ( final EntityPlayer entityplayer ) {
return true ;
}
2017-01-08 20:34:46 +01:00
}
2015-04-14 01:12:24 +02:00
}