-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8d06f3f
commit 1fd6e3b
Showing
15 changed files
with
470 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,5 @@ build | |
# other | ||
eclipse | ||
run | ||
|
||
/classes/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/kotlin/atm/bloodworkxgaming/bloodyLib/cache/ItemStackCache.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package atm.bloodworkxgaming.bloodyLib.cache | ||
|
||
import net.minecraft.item.Item | ||
import net.minecraft.item.ItemStack | ||
|
||
abstract class ItemStackCache<T>(private val loader: (stack: ItemStack) -> T) { | ||
private val map = LinkedHashMap<Item, LinkedHashMap<ItemStack, T>>(100, 0.75f, true) | ||
|
||
fun get(stack: ItemStack): T? { | ||
val item = stack.item | ||
val itemMap = map[item] | ||
|
||
if (itemMap == null || itemMap.isEmpty()) { | ||
val loaded = loader(stack) | ||
val innerMap = LinkedHashMap<ItemStack, T>(10, 0.75f, true) | ||
|
||
innerMap[stack] = loaded | ||
map[item] = innerMap | ||
|
||
return loaded | ||
} else { | ||
|
||
for ((k, v) in itemMap) { | ||
if (ItemStack.areItemStacksEqual(k, stack)) return v | ||
} | ||
|
||
val loaded = loader(stack) | ||
|
||
itemMap[stack] = loaded | ||
map[item] = itemMap | ||
|
||
return loaded | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/kotlin/atm/bloodworkxgaming/bloodyLib/cache/SortingLinkedList.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
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) { | ||
private val list = LinkedList<T>(c) | ||
|
||
fun get(stack: ItemStack): T? { | ||
val iter = list.iterator() | ||
var result: T? = null | ||
while (iter.hasNext()) { | ||
val next = iter.next() | ||
if (loader(next, stack)) { | ||
result = next | ||
iter.remove() | ||
break | ||
} | ||
} | ||
|
||
result ?: return null | ||
|
||
list.addFirst(result) | ||
|
||
return result | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/kotlin/atm/bloodworkxgaming/bloodyLib/energy/EnergyStorageBase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package atm.bloodworkxgaming.bloodyLib.energy | ||
|
||
import atm.bloodworkxgaming.bloodyLib.networking.NBTSerializationState | ||
import net.minecraft.nbt.NBTTagInt | ||
import net.minecraftforge.common.util.INBTSerializable | ||
import net.minecraftforge.energy.EnergyStorage | ||
|
||
open class EnergyStorageBase(capacity: Int, maxReceive: Int, maxExtract: Int, energy: Int, private val serializationState: NBTSerializationState?) : | ||
EnergyStorage(capacity, maxReceive, maxExtract, energy), | ||
INBTSerializable<NBTTagInt> { | ||
constructor(capacity: Int, serializationState: NBTSerializationState?) : this(capacity, capacity, capacity, 0, serializationState) | ||
constructor(capacity: Int, maxTransfer: Int, serializationState: NBTSerializationState?) : this(capacity, maxTransfer, maxTransfer, 0, serializationState) | ||
constructor(capacity: Int, maxReceive: Int, maxExtract: Int, serializationState: NBTSerializationState?) : this(capacity, maxReceive, maxExtract, 0, serializationState) | ||
|
||
override fun extractEnergy(maxExtract: Int, simulate: Boolean): Int { | ||
return super.extractEnergy(maxExtract, simulate).apply { serializationState?.scheduleUpdate() } | ||
} | ||
|
||
override fun receiveEnergy(maxReceive: Int, simulate: Boolean): Int { | ||
return super.receiveEnergy(maxReceive, simulate).apply { serializationState?.scheduleUpdate() } | ||
} | ||
|
||
fun extractEnergyInternal(maxExtract: Int, simulate: Boolean): Int { | ||
val energyExtracted = Math.min(energy, maxExtract) | ||
|
||
if (!simulate) | ||
energy -= energyExtracted | ||
|
||
|
||
return energyExtracted | ||
} | ||
|
||
fun receiveEnergyInternal(maxReceive: Int, simulate: Boolean): Int { | ||
val energyReceived = Math.min(capacity - energy, maxReceive) | ||
|
||
if (!simulate) | ||
energy += energyReceived | ||
|
||
|
||
return energyReceived | ||
} | ||
|
||
override fun deserializeNBT(nbt: NBTTagInt?) { | ||
energy = nbt?.int ?: 0 | ||
} | ||
|
||
override fun serializeNBT() = NBTTagInt(energy) | ||
} |
2 changes: 2 additions & 0 deletions
2
src/main/kotlin/atm/bloodworkxgaming/bloodyLib/extensions/ItemExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
package atm.bloodworkxgaming.bloodyLib.extensions | ||
|
75 changes: 75 additions & 0 deletions
75
src/main/kotlin/atm/bloodworkxgaming/bloodyLib/extensions/nbt/NbtExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package atm.bloodworkxgaming.bloodyLib.extensions.nbt | ||
|
||
import net.minecraft.nbt.NBTBase | ||
import net.minecraft.nbt.NBTTagCompound | ||
import net.minecraft.nbt.NBTTagEnd | ||
import net.minecraft.nbt.NBTTagList | ||
import java.util.* | ||
|
||
operator fun NBTBase?.get(key: NbtPath): NBTBase? { | ||
var iter: NBTBase? = this | ||
|
||
for (s in key) { | ||
iter = when (iter) { | ||
null -> return null | ||
is NBTTagCompound -> iter.getTag(s) | ||
else -> return null | ||
} | ||
} | ||
|
||
return iter | ||
} | ||
|
||
operator fun NBTBase?.get(key: String): NBTBase? = this[NbtPath(key)] | ||
operator fun NBTBase?.get(key: Int): NBTBase? { | ||
if (this is NBTTagList) { | ||
val res = this.get(key) | ||
if (res !is NBTTagEnd) return res | ||
} | ||
|
||
return null | ||
} | ||
|
||
operator fun NBTTagCompound.set(key: NbtPath, value: NBTBase) { | ||
val last = key.path.size - 1 | ||
var iter = this | ||
|
||
key.forEachIndexed { index, s -> | ||
if (index == last) { | ||
iter.setTag(s, value) | ||
} else { | ||
iter = iter.getCompoundTag(s).also { iter.setTag(s, it) } | ||
} | ||
} | ||
} | ||
|
||
@Suppress("UNCHECKED_CAST") | ||
fun nbt(map: Map<String, Any>): NBTTagCompound { | ||
val nbt = NBTTagCompound() | ||
|
||
map.forEach { key, value -> | ||
when (value) { | ||
is Byte -> nbt.setByte(key, value) | ||
is Short -> nbt.setShort(key, value) | ||
is Int -> nbt.setInteger(key, value) | ||
is Long -> nbt.setLong(key, value) | ||
is UUID -> nbt.setUniqueId(key, value) | ||
is Float -> nbt.setFloat(key, value) | ||
is Double -> nbt.setDouble(key, value) | ||
is String -> nbt.setString(key, value) | ||
is ByteArray -> nbt.setByteArray(key, value) | ||
is IntArray -> nbt.setIntArray(key, value) | ||
is Boolean -> nbt.setBoolean(key, value) | ||
is NBTTagCompound -> nbt.setTag(key, value) | ||
is Map<*, *> -> nbt.setTag(key, nbt(value as Map<String, Any>)) | ||
} | ||
} | ||
|
||
return nbt | ||
} | ||
|
||
fun nbt(vararg pairs: Pair<String, Any>): NBTTagCompound = nbt(pairs.toMap()) | ||
|
||
infix fun String.nbtTo(s: String): NbtPath { | ||
return NbtPath(this, s) | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/atm/bloodworkxgaming/bloodyLib/extensions/nbt/NbtPath.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package atm.bloodworkxgaming.bloodyLib.extensions.nbt | ||
|
||
data class NbtPath(val path: List<String>) : Iterable<String> { | ||
override fun iterator(): Iterator<String> = path.iterator() | ||
infix fun to(s: String): NbtPath { | ||
return this.copy(path = listOf(*path.toTypedArray(), s)) | ||
} | ||
|
||
constructor(vararg strings: String) : this(strings.toList()) | ||
constructor() : this(emptyList()) | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/kotlin/atm/bloodworkxgaming/bloodyLib/networking/MessageNBTUpdate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package atm.bloodworkxgaming.bloodyLib.networking | ||
|
||
import atm.bloodworkxgaming.bloodyLib.tile.TileEntityBase | ||
import io.netty.buffer.ByteBuf | ||
import net.minecraft.client.Minecraft | ||
import net.minecraft.nbt.NBTTagCompound | ||
import net.minecraft.util.math.BlockPos | ||
import net.minecraftforge.fml.common.network.ByteBufUtils | ||
import net.minecraftforge.fml.common.network.simpleimpl.IMessage | ||
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler | ||
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext | ||
import net.minecraftforge.fml.relauncher.Side | ||
import net.minecraftforge.fml.relauncher.SideOnly | ||
|
||
class MessageNBTUpdate : IMessage { | ||
|
||
private var x: Int = 0 | ||
private var y: Int = 0 | ||
private var z: Int = 0 | ||
private var tag: NBTTagCompound? = null | ||
|
||
constructor() {} | ||
|
||
constructor(te: TileEntityBase, incremental: Boolean = false) { | ||
this.x = te.pos.x | ||
this.y = te.pos.y | ||
this.z = te.pos.z | ||
this.tag = te.writeClientDataToNBT(NBTTagCompound(), incremental) | ||
} | ||
|
||
override fun toBytes(buf: ByteBuf) { | ||
buf.writeInt(x) | ||
buf.writeInt(y) | ||
buf.writeInt(z) | ||
ByteBufUtils.writeTag(buf, tag) | ||
} | ||
|
||
override fun fromBytes(buf: ByteBuf) { | ||
this.x = buf.readInt() | ||
this.y = buf.readInt() | ||
this.z = buf.readInt() | ||
this.tag = ByteBufUtils.readTag(buf) | ||
} | ||
|
||
class MessageNBTUpdateHandler : IMessageHandler<MessageNBTUpdate, IMessage> { | ||
@SideOnly(Side.CLIENT) | ||
override fun onMessage(msg: MessageNBTUpdate, ctx: MessageContext): IMessage? { | ||
Minecraft.getMinecraft().addScheduledTask { | ||
val entity = Minecraft.getMinecraft().player.entityWorld.getTileEntity(BlockPos(msg.x, msg.y, msg.z)) as? TileEntityBase ?: return@addScheduledTask | ||
val tag = msg.tag ?: return@addScheduledTask | ||
entity.readClientDataFromNBT(tag) | ||
} | ||
return null | ||
} | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
src/main/kotlin/atm/bloodworkxgaming/bloodyLib/networking/NBTSerializationState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package atm.bloodworkxgaming.bloodyLib.networking | ||
|
||
import atm.bloodworkxgaming.bloodyLib.tile.TileEntityTickingBase | ||
|
||
data class NBTSerializationState(private val te: TileEntityTickingBase?, private var value: Boolean = true) { | ||
fun getAndInvert(): Boolean { | ||
value = !value | ||
return !value | ||
} | ||
|
||
fun getAndSetFalse(): Boolean { | ||
val temp = value | ||
value = false | ||
return temp | ||
} | ||
|
||
fun setTrue() { | ||
value = true | ||
} | ||
|
||
fun scheduleUpdate(){ | ||
value = true | ||
te?.scheduleUpdate() | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
src/main/kotlin/atm/bloodworkxgaming/bloodyLib/networking/PacketHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package atm.bloodworkxgaming.bloodyLib.networking | ||
|
||
import atm.bloodworkxgaming.bloodyLib.BloodyLib | ||
import atm.bloodworkxgaming.bloodyLib.tile.TileEntityBase | ||
import kotlinx.coroutines.experimental.async | ||
import net.minecraft.tileentity.TileEntity | ||
import net.minecraftforge.fml.common.network.NetworkRegistry | ||
import net.minecraftforge.fml.common.network.NetworkRegistry.TargetPoint | ||
import net.minecraftforge.fml.common.network.simpleimpl.IMessage | ||
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler | ||
import net.minecraftforge.fml.relauncher.Side | ||
|
||
object PacketHandler { | ||
val INSTANCE = NetworkRegistry.INSTANCE.newSimpleChannel(BloodyLib.MOD_ID)!! | ||
private var id = 0 | ||
|
||
init { | ||
registerPacket<MessageNBTUpdate.MessageNBTUpdateHandler, MessageNBTUpdate>() | ||
} | ||
|
||
inline fun <reified HANDLER : IMessageHandler<MESSAGE, IMessage>, reified MESSAGE : IMessage> registerPacket(side: Side = Side.CLIENT) { | ||
registerMessage(HANDLER::class.java, MESSAGE::class.java, side) | ||
} | ||
|
||
fun <REQ : IMessage, REPLY : IMessage> registerMessage(messageHandler: Class<out IMessageHandler<REQ, REPLY>>, requestMessageType: Class<REQ>, side: Side) { | ||
INSTANCE.registerMessage(messageHandler, requestMessageType, id++, side) | ||
} | ||
|
||
fun sendToAllAround(message: IMessage, te: TileEntity, range: Int = 64) = async { | ||
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())) | ||
} | ||
} | ||
|
||
fun sendNBTUpdate(te: TileEntityBase, incremental: Boolean = false) { | ||
sendToAllAround(MessageNBTUpdate(te, incremental), te) | ||
} | ||
} |
Oops, something went wrong.