Skip to content

Instantly share code, notes, and snippets.

@covers1624
Created August 7, 2018 17:22
Show Gist options
  • Save covers1624/99eed3a286820de14146730bf19025f6 to your computer and use it in GitHub Desktop.
Save covers1624/99eed3a286820de14146730bf19025f6 to your computer and use it in GitHub Desktop.
diff --git a/src/main/scala/codechicken/multipart/BlockMultipart.scala b/src/main/scala/codechicken/multipart/BlockMultipart.scala
index 7f5303e..81218fe 100644
--- a/src/main/scala/codechicken/multipart/BlockMultipart.scala
+++ b/src/main/scala/codechicken/multipart/BlockMultipart.scala
@@ -1,13 +1,16 @@
package codechicken.multipart
import java.util.{Random, ArrayList => JArrayList, EnumSet => JEnumSet, List => JList}
+import java.lang.{Boolean => JBool}
import codechicken.lib.raytracer.{CuboidRayTraceResult, RayTracer}
import codechicken.lib.vec.Vector3
+import codechicken.multipart.handler.MultipartProxy
import codechicken.multipart.handler.MultipartSaveLoad.TileNBTContainer
import net.minecraft.block.Block
import net.minecraft.block.material.Material
-import net.minecraft.block.state.{BlockFaceShape, IBlockState}
+import net.minecraft.block.properties.PropertyBool
+import net.minecraft.block.state.{BlockFaceShape, BlockStateContainer, IBlockState}
import net.minecraft.client.Minecraft
import net.minecraft.client.particle.ParticleManager
import net.minecraft.entity.Entity
@@ -27,6 +30,12 @@ class PartRayTraceResult(val partIndex: Int, crtr: CuboidRayTraceResult)
extends CuboidRayTraceResult(new Vector3(crtr.hitVec), crtr.getBlockPos, crtr.sideHit, crtr.cuboid6, crtr.dist)
object BlockMultipart {
+
+ //Used to check if we are in the default state and return a TileNBTContainer.
+ val DEFAULT_PROP = PropertyBool.create("default")
+
+ def getRuntimeState = MultipartProxy.block.getDefaultState.withProperty(BlockMultipart.DEFAULT_PROP, JBool.FALSE)
+
def getTile(world: IBlockAccess, pos: BlockPos) = world.getTileEntity(pos) match {
case t: TileMultipart if t.partList.nonEmpty => t
case _ => null
@@ -62,10 +71,21 @@ object BlockMultipart {
class BlockMultipart extends Block(Material.ROCK) {
import BlockMultipart._
+ setDefaultState(getDefaultState.withProperty(DEFAULT_PROP, JBool.TRUE))
override def hasTileEntity(state: IBlockState) = true
- override def createTileEntity(world: World, state: IBlockState) = if(!world.isRemote) new TileNBTContainer else null
+ override def createTileEntity(world: World, state: IBlockState) = {
+ if(world.isRemote || !state.getProperties.containsKey(DEFAULT_PROP) || !state.getValue(DEFAULT_PROP)) {
+ null
+ } else {
+ new TileNBTContainer
+ }
+ }
+
+ override def getMetaFromState(state: IBlockState) = 0
+
+ override protected def createBlockState() = new BlockStateContainer(this, DEFAULT_PROP)
override def isBlockNormalCube(state: IBlockState) = false
diff --git a/src/main/scala/codechicken/multipart/TileMultipart.scala b/src/main/scala/codechicken/multipart/TileMultipart.scala
index 1fc1065..bbfd764 100644
--- a/src/main/scala/codechicken/multipart/TileMultipart.scala
+++ b/src/main/scala/codechicken/multipart/TileMultipart.scala
@@ -10,6 +10,7 @@ import codechicken.lib.render.{CCRenderState, RenderUtils}
import codechicken.lib.vec.{Cuboid6, Vector3}
import codechicken.lib.world.IChunkLoadTile
import codechicken.multipart.handler.{MultipartCompatiblity, MultipartProxy, MultipartSPH}
+import net.minecraft.block.state.IBlockState
import net.minecraft.client.Minecraft
import net.minecraft.client.particle.ParticleManager
import net.minecraft.client.renderer.texture.TextureAtlasSprite
@@ -515,6 +516,8 @@ class TileMultipart extends TileEntity with IChunkLoadTile {
val pos = Vector3.fromTileCenter(this)
items.foreach(item => TileMultipart.dropItem(item, world, pos))
}
+
+ override def shouldRefresh(world: World, pos: BlockPos, oldState: IBlockState, newState: IBlockState) = oldState.getBlock != newState.getBlock
}
trait TileMultipartClient extends TileMultipart {
diff --git a/src/main/scala/codechicken/multipart/handler/MultipartSaveLoad.scala b/src/main/scala/codechicken/multipart/handler/MultipartSaveLoad.scala
index 3d4368c..0cbd56d 100644
--- a/src/main/scala/codechicken/multipart/handler/MultipartSaveLoad.scala
+++ b/src/main/scala/codechicken/multipart/handler/MultipartSaveLoad.scala
@@ -3,10 +3,13 @@ package codechicken.multipart.handler
import java.util.Collections
import codechicken.multipart
-import codechicken.multipart.{TileMultipart, WrappedTileEntityRegistry}
+import codechicken.multipart.{BlockMultipart, TileMultipart, WrappedTileEntityRegistry}
+import net.minecraft.block.state.IBlockState
import net.minecraft.nbt.NBTTagCompound
import net.minecraft.tileentity.TileEntity
+import net.minecraft.util.math.BlockPos
import net.minecraft.util.{ITickable, ResourceLocation}
+import net.minecraft.world.World
import net.minecraft.world.chunk.Chunk
import net.minecraftforge.fml.common.registry.GameRegistry
@@ -44,7 +47,7 @@ object MultipartSaveLoad {
var loaded = false
//The NBT of the tile.
//We save this back out in case something breaks.
- var tag: NBTTagCompound = new NBTTagCompound()
+ var tag: NBTTagCompound = _
override def readFromNBT(t: NBTTagCompound) {
super.readFromNBT(t)
@@ -52,27 +55,28 @@ object MultipartSaveLoad {
}
override def writeToNBT(compound: NBTTagCompound) = {
- compound.merge(tag)
+ if (tag != null) {
+ compound.merge(tag)
+ }
super.writeToNBT(compound)
}
override def update() {
if (!failed && !loaded) {
if (tag != null) {
- if (!world.isRemote && tag != null) {
- val newTile = TileMultipart.createFromNBT(tag)
- val chunk = world.getChunkFromBlockCoords(pos)
- if (newTile != null) {
- newTile.validate()
- world.setTileEntity(pos, newTile)
- newTile.notifyTileChange()
- val packet = MultipartSPH.getDescPacket(chunk, Collections.singleton[TileEntity](newTile).iterator)
- packet.sendToChunk(world, chunk.getPos.x, chunk.getPos.z)
- loaded = true
- } else {
- world.removeTileEntity(pos)
- }
+ val newTile = TileMultipart.createFromNBT(tag)
+ val chunk = world.getChunkFromBlockCoords(pos)
+ chunk.setBlockState(pos, BlockMultipart.getRuntimeState)
+ if (newTile != null) {
+ newTile.validate()
+ world.setTileEntity(pos, newTile)
+ newTile.notifyTileChange()
+ val packet = MultipartSPH.getDescPacket(chunk, Collections.singleton[TileEntity](newTile).iterator)
+ packet.sendToChunk(world, chunk.getPos.x, chunk.getPos.z)
+ } else {
+ world.removeTileEntity(pos)
}
+ loaded = true
} else {
ticks += 1
if ((ticks % 600) == 0) {
@@ -82,6 +86,8 @@ object MultipartSaveLoad {
}
}
}
+
+ override def shouldRefresh(world: World, pos: BlockPos, oldState: IBlockState, newState: IBlockState) = oldState.getBlock != newState.getBlock
}
def load() {
@@ -100,6 +106,7 @@ object MultipartSaveLoad {
t.getValue match {
case container: TileNBTContainer =>
val newTile = TileMultipart.createFromNBT(container.tag)
+ chunk.setBlockState(container.getPos, BlockMultipart.getRuntimeState)
if (newTile != null) {
newTile.setWorld(chunk.getWorld)
newTile.validate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment