Skip to content

Commit

Permalink
Added config and Tank
Browse files Browse the repository at this point in the history
  • Loading branch information
BloodWorkXGaming committed Sep 28, 2018
1 parent 31a77f5 commit a4c7037
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 10 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ plugins {
}

apply plugin: 'net.minecraftforge.gradle.forge'
apply plugin: 'java'

version = "1.0.0"
group = "de.bloodworkxgaming.bloodylib" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
Expand Down
14 changes: 14 additions & 0 deletions src/main/kotlin/atm/bloodworkxgaming/bloodyLib/BloodyLibConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package atm.bloodworkxgaming.bloodyLib

import net.minecraftforge.common.config.Config

@Config(modid = BloodyLib.MOD_ID, name = "bloodymods/bloodylib")
object BloodyLibConfig {
@Config.Comment("How many ticks to wait in between sending the changed NBT to the clients")
@JvmStatic
var incrementalNbtUpdateInterval = 5

@Config.Comment("How many ticks to wait in between sending the full NBT to the clients")
@JvmStatic
var fullNbtUpdateInterval = 40
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@
package atm.bloodworkxgaming.bloodyLib.cache

import net.minecraft.item.ItemStack
import java.util.*

abstract class SortingLinkedList<T>(c: Collection<T>, private inline val loader: (T, ItemStack) -> Boolean) {
/**
* This is a delegation of a linked list:
* It is only efficient in the following cases: (or atleast more efficient than a set or arraylist)
* There are no hashcode or eqauls method
* The loader function is not expensive to compute and therefore doesn't make much sense to cache
*
* @param nonMoveCount How many elements from the front the element can be before starting it should start to move it to the front
*/
class SortingLinkedList<K, T>(private inline val loader: (iterator: K, listElement: T) -> Boolean, private val nonMoveCount: Int = 3, c: Collection<T> = emptyList()) : Iterable<T>{

private val list = LinkedList<T>(c)

fun get(stack: ItemStack): T? {
fun get(stack: K): T? {
val iter = list.iterator()
var result: T? = null
var counter = 0
while (iter.hasNext()) {
val next = iter.next()
if (loader(next, stack)) {
counter++

if (loader(stack, next)) {
result = next
iter.remove()

if (counter > nonMoveCount)
iter.remove()
break
}

}

result ?: return null

list.addFirst(result)
if (counter > nonMoveCount)
list.addFirst(result)

return result
}

override fun iterator(): Iterator<T> = list.iterator()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package atm.bloodworkxgaming.bloodyLib.fluid

import atm.bloodworkxgaming.bloodyLib.networking.NBTSerializationState
import com.sun.org.apache.xpath.internal.operations.Bool
import net.minecraft.nbt.NBTTagCompound
import net.minecraftforge.common.util.INBTSerializable
import net.minecraftforge.fluids.Fluid
import net.minecraftforge.fluids.FluidStack
import net.minecraftforge.fluids.FluidTank

class FluidTankBase(private val nbtState: NBTSerializationState, capacity: Int, fluid: FluidStack? = null, canFill: Boolean = true, canDrain: Boolean = true)
: FluidTank(fluid, capacity), INBTSerializable<NBTTagCompound> {

init {
this.canDrain = canDrain
this.canFill = canFill
}

constructor(nbtState: NBTSerializationState, capacity: Int, fluid: Fluid, amount: Int) : this(nbtState, capacity, FluidStack(fluid, amount))

override fun deserializeNBT(nbt: NBTTagCompound?) {
this.readFromNBT(nbt)
}

override fun serializeNBT(): NBTTagCompound = writeToNBT(NBTTagCompound())

override fun onContentsChanged() {
nbtState.scheduleUpdate()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package atm.bloodworkxgaming.bloodyLib.networking
import atm.bloodworkxgaming.bloodyLib.BloodyLib
import atm.bloodworkxgaming.bloodyLib.tile.TileEntityBase
import kotlinx.coroutines.experimental.async
import kotlinx.coroutines.experimental.launch
import net.minecraft.tileentity.TileEntity
import net.minecraftforge.fml.common.network.NetworkRegistry
import net.minecraftforge.fml.common.network.NetworkRegistry.TargetPoint
Expand Down Expand Up @@ -30,7 +31,7 @@ object PacketHandler {
* Using coroutines here for a about 4 times speed improvement over not using them.
* Basically a fire and forget without spawning a ton of threads
*/
fun sendToAllAround(message: IMessage, te: TileEntity, range: Int = 64) = async {
fun sendToAllAround(message: IMessage, te: TileEntity, range: Int = 64) = launch {
val pos = te.pos
synchronized(INSTANCE) {
INSTANCE.sendToAllAround(message, TargetPoint(te.world.provider.dimension, pos.x.toDouble(), pos.y.toDouble(), pos.z.toDouble(), range.toDouble()))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package atm.bloodworkxgaming.bloodyLib.tile

import atm.bloodworkxgaming.bloodyLib.BloodyLibConfig
import net.minecraft.util.ITickable

abstract class TileEntityTickingBase : TileEntityBase(), ITickable {
Expand All @@ -11,10 +12,10 @@ abstract class TileEntityTickingBase : TileEntityBase(), ITickable {
if (world.isRemote) return

val time = world.worldTime
if (sendUpdateScheduled && time - lastNBTUpdate > 5) {
if (sendUpdateScheduled && time - lastNBTUpdate > BloodyLibConfig.incrementalNbtUpdateInterval) {

sendUpdate(
if (time - lastFullNBTUpdate > 40) {
if (time - lastFullNBTUpdate > BloodyLibConfig.fullNbtUpdateInterval) {
lastFullNBTUpdate = time
true
} else {
Expand All @@ -32,7 +33,7 @@ abstract class TileEntityTickingBase : TileEntityBase(), ITickable {

abstract fun updateTickRemote()

fun scheduleUpdate(){
fun scheduleUpdate() {
sendUpdateScheduled = true
}

Expand Down

0 comments on commit a4c7037

Please sign in to comment.