Skip to content

Instantly share code, notes, and snippets.

@AlexCouch
Created August 21, 2018 10:21
Show Gist options
  • Save AlexCouch/b768806c08811087049475e4bd8cd8c9 to your computer and use it in GitHub Desktop.
Save AlexCouch/b768806c08811087049475e4bd8cd8c9 to your computer and use it in GitHub Desktop.
object DesktopComputerBlock : Block(Material.IRON), ITileEntityProvider {
init{
val name = "desktop"
this.unlocalizedName = name
this.registryName = ResourceLocation(modid, name)
this.setCreativeTab(CreativeTabs.MISC)
}
override fun onBlockActivated(worldIn: World, pos: BlockPos, state: IBlockState, playerIn: EntityPlayer, hand: EnumHand, facing: EnumFacing, hitX: Float, hitY: Float, hitZ: Float): Boolean {
if(!worldIn.isRemote && hand == EnumHand.MAIN_HAND){
if(playerIn is EntityPlayerMP){
val te = worldIn.getTileEntity(pos) ?: throw IllegalStateException("No tile entity placed! Report to author!")
if(te is TileEntityDesktopComputer){
if(te.started){
te.openGui()
}else{
te.startup(playerIn)
}
}else{
throw IllegalStateException("No desktop tile entity placed! What did you do??????")
}
}
}
return super.onBlockActivated(worldIn, pos, state, playerIn, hand, facing, hitX, hitY, hitZ)
}
override fun createNewTileEntity(worldIn: World, meta: Int): TileEntity = TileEntityDesktopComputer()
override fun isFullCube(state: IBlockState?): Boolean = false
override fun isOpaqueCube(state: IBlockState?): Boolean = false
}
@Savable
@TileRegister("desktop")
class TileEntityDesktopComputer : TileMod(){
@Save
var os: OperatingSystem? = null
@Save
val storage = NBTTagCompound()
@Save
var system: CouchDesktopSystem? = null
@Save
var started = false
fun startup(player: EntityPlayerMP){
if(started) return
println("Starting system...")
system = CouchDesktopSystem(this)
system?.player = player
started = true
this.system?.start()
markDirty()
}
fun openGui(){
stream.sendTo(OpenTerminalGuiMessage(this.pos), this.system?.player!!)
}
fun writeToStorage(name: String, nbt: NBTBase){
if(storage.hasKey(name)){
val tag = storage.getCompoundTag(name)
if(tag == nbt){
return
}
}
storage.setTag(name, nbt)
}
fun writeOverStorageAt(at: String, nbt: NBTBase){
storage.removeTag(at)
writeToStorage(at, nbt)
}
fun readFromStorage(name: String): NBTBase{
if(storage.hasKey(name)){
return storage.getTag(name)
}
throw IllegalArgumentException("No such tag in storage: $name")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment