mirror of
https://github.com/GTNewHorizons/ForgeMultipart.git
synced 2026-11-27 23:45:52 +00:00
refactor: port MultipartHelper to Java
MultipartHelper, MultipartHelper$ and MultipartHelper$IPartTileConverter are all public-member- and descriptor-identical to the reference, and the full emitted class list is unchanged -- this port neither adds nor removes a class. MultipartHelper gains a private constructor and its companion's class initializer loses ACC_PUBLIC, neither of which is nameable by a caller. MultipartHelper$ was kept deliberately. It is not among the 17 MODULE$ singletons read from bytecode and no jar links against it, but guidenh names it as a reflective string, and removing a companion breaks that invisibly. createTileFromParts now builds one JavaConversions wrapper for both calls instead of one per call site, and convertMulti returns Collections.emptyList or singletonList instead of a Scala Seq wrapper. The declared type was always java.lang.Iterable. 107 plain-JVM tests and 10 Forge server tests pass unchanged.
This commit is contained in:
1 parent
0fdf9e5e75
commit
c3d5b08ff7
6 files changed
+230
-118
No files matched your search
@@ -0,0 +1,31 @@
|
||||
package codechicken.multipart;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
/**
|
||||
* Scala companion singleton, kept because guidenh reflects on this name as well as on MultipartHelper. Pure forwarders.
|
||||
*/
|
||||
public final class MultipartHelper$ {
|
||||
|
||||
public static final MultipartHelper$ MODULE$ = new MultipartHelper$();
|
||||
|
||||
private MultipartHelper$() {}
|
||||
|
||||
public TileEntity createTileFromNBT(World world, NBTTagCompound tag) {
|
||||
return MultipartHelper.createTileFromNBT(world, tag);
|
||||
}
|
||||
|
||||
public void sendDescPacket(World world, TileEntity tile) {
|
||||
MultipartHelper.sendDescPacket(world, tile);
|
||||
}
|
||||
|
||||
public void registerTileConverter(MultipartHelper.IPartTileConverter<?> converter) {
|
||||
MultipartHelper.registerTileConverter(converter);
|
||||
}
|
||||
|
||||
public TileMultipart createTileFromParts(Iterable<TMultiPart> parts) {
|
||||
return MultipartHelper.createTileFromParts(parts);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package codechicken.multipart;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.chunk.Chunk;
|
||||
|
||||
import codechicken.lib.packet.PacketCustom;
|
||||
import codechicken.multipart.handler.MultipartSPH;
|
||||
import codechicken.multipart.handler.MultipartSaveLoad;
|
||||
import scala.collection.JavaConversions;
|
||||
|
||||
/**
|
||||
* Static helper class for handling the unusual way that multipart tile entities load from nbt and send description
|
||||
* packets.
|
||||
* <p>
|
||||
* Multipart tile entities will all save themselves with the id "savedMultipart" which if normally loaded by minecraft,
|
||||
* will create a dummy tile entity which just holds the NBT it was read from. These dummies are then converted to actual
|
||||
* container tiles on the ChunkLoad event. The createTileFromNBT function should be used to construct a multipart tile
|
||||
* from NBT without the ChunkLoad event.
|
||||
* <p>
|
||||
* Multipart tile entities do not send description packets via the conventional means of one packet per tile when
|
||||
* PlayerInstance calls for it, to do so would be terribly inefficient. Instead, the ChunkWatch event is used to batch
|
||||
* all the describing data for a chunk into one packet which is compressed using relative positions. The sendDescPacket
|
||||
* function should be used to send the description packet of a tile without a ChunkWatch event.
|
||||
* <p>
|
||||
* An example of using this class to move blocks/tile entites around can be found at
|
||||
* www.chickenbones.craftsaddle.org/Files/Other/ItemDevTool2.java
|
||||
* <p>
|
||||
* The reference carried a commented-out sendDescPackets batching several tiles at once. It was removed for compilation
|
||||
* until a PlayerInstance access transformer is pulled into forge, and is not reinstated here.
|
||||
*/
|
||||
public final class MultipartHelper {
|
||||
|
||||
private MultipartHelper() {}
|
||||
|
||||
public static TileEntity createTileFromNBT(World world, NBTTagCompound tag) {
|
||||
if (!tag.getString("id").equals("savedMultipart")) {
|
||||
return null;
|
||||
}
|
||||
|
||||
MultipartSaveLoad.loadingWorld_$eq(world);
|
||||
return TileMultipart.createFromNBT(tag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Note. This method should only be used to send tiles that have been created on the server mid-game via an NBT load
|
||||
* to clients.
|
||||
*/
|
||||
public static void sendDescPacket(World world, TileEntity tile) {
|
||||
Chunk c = world.getChunkFromBlockCoords(tile.xCoord, tile.zCoord);
|
||||
PacketCustom pkt = MultipartSPH.getDescPacket(c, Arrays.asList(tile).iterator());
|
||||
if (pkt != null) {
|
||||
pkt.sendToChunk(world, c.xPosition, c.zPosition);
|
||||
}
|
||||
}
|
||||
|
||||
public static void registerTileConverter(IPartTileConverter<?> converter) {
|
||||
MultipartSaveLoad.converters().$plus$eq(converter);
|
||||
}
|
||||
|
||||
public static TileMultipart createTileFromParts(Iterable<TMultiPart> parts) {
|
||||
scala.collection.Iterable<TMultiPart> scalaParts = JavaConversions.iterableAsScalaIterable(parts);
|
||||
TileMultipart tile = MultipartGenerator$.MODULE$.generateCompositeTile(null, scalaParts, false);
|
||||
tile.loadParts(scalaParts);
|
||||
return tile;
|
||||
}
|
||||
|
||||
/** Helper class for converting tile entities to multiparts on chunk load. */
|
||||
public abstract static class IPartTileConverter<T extends TileEntity> {
|
||||
|
||||
private final Class<T> clazz;
|
||||
|
||||
public IPartTileConverter(Class<T> clazz) {
|
||||
this.clazz = clazz;
|
||||
}
|
||||
|
||||
public Class<T> clazz() {
|
||||
return clazz;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if this tile can be converted by this converter. If canConvert returns true, but convertMulti
|
||||
* returns no parts, the tile will be deleted
|
||||
*/
|
||||
public boolean canConvert(TileEntity tile) {
|
||||
return clazz.isInstance(tile);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Iterable<TMultiPart> convert(TileEntity tile) {
|
||||
return convertMulti((T) tile);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the TileEntity into multiple parts
|
||||
*
|
||||
* @return An iterable list of parts or an empty list to delete the tile
|
||||
*/
|
||||
public Iterable<TMultiPart> convertMulti(T tile) {
|
||||
TMultiPart part = convertOne(tile);
|
||||
return part == null ? Collections.<TMultiPart>emptyList() : Collections.singletonList(part);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the TileEntity into a part
|
||||
*
|
||||
* @return The converted part, or null to delete the tile
|
||||
*/
|
||||
public abstract TMultiPart convertOne(T tile);
|
||||
}
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
package codechicken.multipart
|
||||
|
||||
import net.minecraft.tileentity.TileEntity
|
||||
import net.minecraft.nbt.NBTTagCompound
|
||||
import net.minecraft.world.World
|
||||
import codechicken.multipart.handler.MultipartSaveLoad
|
||||
import com.google.common.collect.LinkedListMultimap
|
||||
import scala.collection.JavaConversions._
|
||||
import scala.collection.JavaConverters._
|
||||
import codechicken.multipart.handler.MultipartSPH
|
||||
import net.minecraft.world.WorldServer
|
||||
import java.util.Arrays
|
||||
import java.lang.Iterable
|
||||
|
||||
/** Static helper class for handling the unusual way that multipart tile
|
||||
* entities load from nbt and send description packets <br> Multipart tile
|
||||
* entities will all save themselves with the id "savedMultipart" which if
|
||||
* normally loaded by minecraft, will create a dummy tile entity which just
|
||||
* holds the NBT it was read from. These dummies are then converted to actual
|
||||
* container tiles on the ChunkLoad event. The createTileFromNBT function
|
||||
* should be used to construct a multipart tile from NBT without the ChunkLoad
|
||||
* event. <br> Multipart tile entities do not send description packets via the
|
||||
* conventional means of one packet per tile when PlayerInstance calls for it,
|
||||
* to do so would be terribly inefficient. Instead, the ChunkWatch event is
|
||||
* used to batch all the describing data for a chunk into one packet which is
|
||||
* compressed using relative positions. The sendDescPacket(s) functions should
|
||||
* be used to send the description packet of a tile or tiles without a
|
||||
* ChunkWatch event. <br> An example of using this class to move blocks/tile
|
||||
* entites around can be found at
|
||||
* www.chickenbones.craftsaddle.org/Files/Other/ItemDevTool2.java
|
||||
*/
|
||||
object MultipartHelper {
|
||||
/*val f_playersInChunk = classOf[PlayerInstance].getDeclaredField(
|
||||
new ObfMapping("net/minecraft/server/management/PlayerInstance", "playersInChunk", "Ljava/util/List;")
|
||||
.toRuntime.s_name)
|
||||
f_playersInChunk.setAccessible(true)
|
||||
|
||||
def playersInChunk(inst:PlayerInstance) = f_playersInChunk.get(inst).asInstanceOf[List[_]]*/
|
||||
|
||||
def createTileFromNBT(world: World, tag: NBTTagCompound): TileEntity = {
|
||||
if (!tag.getString("id").equals("savedMultipart"))
|
||||
return null
|
||||
|
||||
MultipartSaveLoad.loadingWorld = world
|
||||
return TileMultipart.createFromNBT(tag)
|
||||
}
|
||||
|
||||
/** Note. This method should only be used to send tiles that have been created
|
||||
* on the server mid-game via an NBT load to clients.
|
||||
*/
|
||||
def sendDescPacket(world: World, tile: TileEntity) {
|
||||
val c = world.getChunkFromBlockCoords(tile.xCoord, tile.zCoord)
|
||||
val pkt = MultipartSPH.getDescPacket(c, Arrays.asList(tile).iterator)
|
||||
if (pkt != null)
|
||||
pkt.sendToChunk(world, c.xPosition, c.zPosition)
|
||||
}
|
||||
|
||||
/* Removed for compilation until PlayerInstance access transformer is pulled in forge
|
||||
def sendDescPackets(world:World, tiles:Iterable[TileEntity]) {
|
||||
val map = LinkedListMultimap.create[Long, TileEntity]()
|
||||
tiles.filter(_.isInstanceOf[TileMultipart]).foreach(t => map.put(t.xCoord.toLong<<32|t.zCoord, t))
|
||||
|
||||
val mgr = world.asInstanceOf[WorldServer].getPlayerManager
|
||||
map.asMap.entrySet.foreach{e =>
|
||||
val coord = e.getKey
|
||||
val c = world.getChunkFromBlockCoords((coord>>32).toInt, coord.toInt)
|
||||
lazy val pkt = MultipartSPH.getDescPacket(c, e.getValue.iterator)
|
||||
val inst = mgr.getOrCreateChunkWatcher(c.xPosition, c.zPosition, false)
|
||||
if(!inst.playersWatchingChunk.isEmpty)
|
||||
inst.sendToAllPlayersWatchingChunk(pkt.toPacket)
|
||||
}
|
||||
}*/
|
||||
|
||||
/** Helper class for converting tile entities to multiparts on chunk load
|
||||
*/
|
||||
abstract class IPartTileConverter[T <: TileEntity](val clazz: Class[T]) {
|
||||
|
||||
/** @return
|
||||
* true if this tile can be converted by this converter. If canConvert
|
||||
* returns true, but convertMulti returns no parts, the tile will be
|
||||
* deleted
|
||||
*/
|
||||
def canConvert(tile: TileEntity) = clazz.isInstance(tile)
|
||||
|
||||
def convert(tile: TileEntity) = convertMulti(tile.asInstanceOf[T])
|
||||
|
||||
/** Convert the TileEntity into multiple parts
|
||||
* @return
|
||||
* An iterable list of parts or an empty list to delete the tile
|
||||
*/
|
||||
def convertMulti(tile: T): Iterable[TMultiPart] = convertOne(tile) match {
|
||||
case null => Seq()
|
||||
case p => Seq(p)
|
||||
}
|
||||
|
||||
/** Convert the TileEntity into a part
|
||||
* @return
|
||||
* The converted part, or null to delete the tile
|
||||
*/
|
||||
def convertOne(tile: T): TMultiPart
|
||||
}
|
||||
|
||||
def registerTileConverter(converter: IPartTileConverter[_]) {
|
||||
MultipartSaveLoad.converters += converter
|
||||
}
|
||||
|
||||
def createTileFromParts(parts: Iterable[TMultiPart]) = {
|
||||
val tile = MultipartGenerator.generateCompositeTile(null, parts, false)
|
||||
tile.loadParts(parts)
|
||||
tile
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user