refactor: port multipart generator to Java

This commit is contained in:
Algent committed 2026-09-02 15:31:07 +02:00
1 parent e6af4251ae
commit 09670a09ce
8 files changed
+345 -254

No files matched your search

@@ -0,0 +1,210 @@
package codechicken.multipart;
import java.util.BitSet;
import java.util.LinkedHashSet;
import java.util.Set;
import net.minecraft.network.play.server.S23PacketBlockChange;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import codechicken.lib.packet.PacketCustom;
import codechicken.lib.vec.BlockCoord;
import codechicken.multipart.asm.ASMImplicits;
import codechicken.multipart.asm.MultipartMixinFactory;
import codechicken.multipart.asm.ScratchBitSet;
import codechicken.multipart.asm.ScratchBitSet$class;
import codechicken.multipart.handler.MultipartProxy;
import codechicken.multipart.handler.MultipartProxy$;
import scala.Option;
import scala.Tuple2;
import scala.collection.Iterable;
import scala.collection.Iterator;
import scala.collection.immutable.Nil$;
import scala.collection.mutable.HashMap;
import scala.collection.mutable.Map;
public final class MultipartGenerator$ implements ScratchBitSet {
public static final MultipartGenerator$ MODULE$ = new MultipartGenerator$();
private final Map<Class<?>, BitSet> tileTraitMap = new HashMap<>();
private final Map<String, String> interfaceTraitMap_c = new HashMap<>();
private final Map<String, String> interfaceTraitMap_s = new HashMap<>();
private final Map<Class<?>, BitSet> partTraitMap_c = new HashMap<>();
private final Map<Class<?>, BitSet> partTraitMap_s = new HashMap<>();
private final int clientTraitId;
private ThreadLocal<BitSet> codechicken$multipart$asm$ScratchBitSet$$bitSets;
private MultipartGenerator$() {
ScratchBitSet$class.$init$(this);
clientTraitId = MultipartMixinFactory.registerTrait("codechicken.multipart.TileMultipartClient");
}
@Override
public ThreadLocal<BitSet> codechicken$multipart$asm$ScratchBitSet$$bitSets() {
return codechicken$multipart$asm$ScratchBitSet$$bitSets;
}
@Override
public void codechicken$multipart$asm$ScratchBitSet$_setter_$codechicken$multipart$asm$ScratchBitSet$$bitSets_$eq(
ThreadLocal value) {
codechicken$multipart$asm$ScratchBitSet$$bitSets = value;
}
@Override
public BitSet getBitSet() {
return ScratchBitSet$class.getBitSet(this);
}
@Override
public BitSet freshBitSet() {
return ScratchBitSet$class.freshBitSet(this);
}
public Map<String, String> codechicken$multipart$MultipartGenerator$$interfaceTraitMap(boolean client) {
return client ? interfaceTraitMap_c : interfaceTraitMap_s;
}
public BitSet codechicken$multipart$MultipartGenerator$$traitsForPart(TMultiPart part, boolean client) {
Map<Class<?>, BitSet> cache = client ? partTraitMap_c : partTraitMap_s;
Option<BitSet> cached = cache.get(part.getClass());
if (cached.isDefined()) return cached.get();
Set<String> traits = new LinkedHashSet<>();
collectTraits(part.getClass(), codechicken$multipart$MultipartGenerator$$interfaceTraitMap(client), traits);
BitSet bitset = new BitSet();
for (String trait : traits) bitset.set(MultipartMixinFactory.getId(trait));
cache.put(part.getClass(), bitset);
return bitset;
}
private void collectTraits(Class<?> type, Map<String, String> map, Set<String> traits) {
for (Class<?> iface : type.getInterfaces()) collectTraits(iface, map, traits);
Option<String> trait = map.get(ASMImplicits.nodeName(type.getName()));
if (trait.isDefined()) traits.add(trait.get());
if (type.getSuperclass() != null) collectTraits(type.getSuperclass(), map, traits);
}
private BitSet setTraits(TMultiPart part, boolean client) {
BitSet bitset = freshBitSet();
bitset.or(codechicken$multipart$MultipartGenerator$$traitsForPart(part, client));
if (client) bitset.set(clientTraitId);
return bitset;
}
private BitSet setTraits(Iterable<TMultiPart> parts, boolean client) {
BitSet bitset = freshBitSet();
for (Iterator<TMultiPart> it = parts.iterator(); it.hasNext();) {
bitset.or(codechicken$multipart$MultipartGenerator$$traitsForPart(it.next(), client));
}
if (client) bitset.set(clientTraitId);
return bitset;
}
/** Replaces the tile if the new part requires additional interfaces, then adds the part. */
public TileMultipart addPart(World world, BlockCoord pos, TMultiPart part) {
Tuple2<TileMultipart, Object> convertedTile = TileMultipart.getOrConvertTile2(world, pos);
TileMultipart tile = convertedTile._1();
boolean converted = (Boolean) convertedTile._2();
BitSet bitset = setTraits(part, world.isRemote);
TileMultipart next = tile;
if (next != null) {
if (converted) {
next.partList().apply(0).invalidateConvertedTile();
world.setBlock(pos.x, pos.y, pos.z, MultipartProxy.block(), 0, 0);
silentAddTile(world, pos, next);
PacketCustom.sendToChunk(
new S23PacketBlockChange(pos.x, pos.y, pos.z, world),
world,
pos.x >> 4,
pos.z >> 4);
next.partList().apply(0).onConverted();
next.writeAddPart(next.partList().apply(0));
}
BitSet tileTraits = tileTraitMap.apply(tile.getClass());
bitset.andNot(tileTraits);
if (!bitset.isEmpty()) {
bitset.or(tileTraits);
next = (TileMultipart) MultipartMixinFactory.construct(bitset, Nil$.MODULE$);
tile.setValid(false);
silentAddTile(world, pos, next);
next.from(tile);
}
} else {
world.setBlock(pos.x, pos.y, pos.z, MultipartProxy.block(), 0, 0);
next = (TileMultipart) MultipartMixinFactory.construct(bitset, Nil$.MODULE$);
silentAddTile(world, pos, next);
}
next.addPart_impl(part);
return next;
}
public void silentAddTile(World world, BlockCoord pos, TileEntity tile) {
Chunk chunk = world.getChunkFromBlockCoords(pos.x, pos.z);
if (chunk != null) chunk.func_150812_a(pos.x & 15, pos.y, pos.z & 15, tile);
}
/** Reuses the tile only when it has exactly the interfaces required by the parts. */
public TileMultipart generateCompositeTile(TileEntity tile, Iterable<TMultiPart> parts, boolean client) {
BitSet bitset = setTraits(parts, client);
if (tile instanceof TileMultipart && bitset.equals(tileTraitMap.apply(tile.getClass()))) {
return (TileMultipart) tile;
}
return (TileMultipart) MultipartMixinFactory.construct(bitset, Nil$.MODULE$);
}
/** Replaces a tile whose remaining parts no longer require all its interfaces. */
public TileMultipart partRemoved(TileMultipart tile) {
TileMultipart next = generateCompositeTile(tile, tile.partList(), tile.getWorldObj().isRemote);
if (!next.equals(tile)) {
tile.setValid(false);
silentAddTile(tile.getWorldObj(), new BlockCoord(tile), next);
next.from(tile);
next.notifyTileChange();
}
return next;
}
public void registerTrait(String marker, String trait) {
registerTrait(marker, trait, trait);
}
public void registerTrait(String marker, String clientTrait, String serverTrait) {
marker = ASMImplicits.nodeName(marker);
clientTrait = ASMImplicits.nodeName(clientTrait);
serverTrait = ASMImplicits.nodeName(serverTrait);
registerSide(interfaceTraitMap_c, marker, clientTrait);
registerSide(interfaceTraitMap_s, marker, serverTrait);
}
private void registerSide(Map<String, String> map, String marker, String trait) {
if (trait == null) return;
if (map.contains(marker)) {
MultipartProxy.logger().error("Trait already registered for " + marker);
} else {
map.put(marker, trait);
MultipartMixinFactory.registerTrait(trait);
}
}
public void registerPassThroughInterface(String name) {
registerPassThroughInterface(name, true, true);
}
public void registerPassThroughInterface(String name, boolean client, boolean server) {
String trait = MultipartMixinFactory.generatePassThroughTrait(name);
if (trait == null) return;
registerTrait(name, client ? trait : null, server ? trait : null);
}
public void registerTileClass(Class<? extends TileMultipart> type, BitSet traits) {
BitSet copy = new BitSet();
copy.or(traits);
tileTraitMap.put(type, copy);
MultipartProxy$.MODULE$.onTileClassBuilt(type);
}
}
@@ -0,0 +1,53 @@
package codechicken.multipart;
import java.util.BitSet;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import codechicken.lib.vec.BlockCoord;
/**
* Manages dynamic construction of multipart container tiles. Register a mixin against a part's marker interface to add
* tile-level logic or interfaces whenever that part is present. Generated classes can be inspected with the ASM debug
* configuration option.
*/
public final class MultipartGenerator {
private MultipartGenerator() {}
public static BitSet getBitSet() {
return MultipartGenerator$.MODULE$.getBitSet();
}
public static BitSet freshBitSet() {
return MultipartGenerator$.MODULE$.freshBitSet();
}
/** Adds a tile without notifying neighbouring blocks or adding it to the tick list. */
public static void silentAddTile(World world, BlockCoord pos, TileEntity tile) {
MultipartGenerator$.MODULE$.silentAddTile(world, pos, tile);
}
/** Registers the same tile trait for both sides. */
public static void registerTrait(String marker, String trait) {
MultipartGenerator$.MODULE$.registerTrait(marker, trait);
}
/** Registers side-specific tile traits; either trait may be null to exclude that side. */
public static void registerTrait(String marker, String clientTrait, String serverTrait) {
MultipartGenerator$.MODULE$.registerTrait(marker, clientTrait, serverTrait);
}
public static void registerPassThroughInterface(String name) {
MultipartGenerator$.MODULE$.registerPassThroughInterface(name);
}
/**
* Adds the interface to the container and forwards its methods to its single implementing part. A second part
* implementing the same interface is rejected by the generated trait's occlusion check.
*/
public static void registerPassThroughInterface(String name, boolean client, boolean server) {
MultipartGenerator$.MODULE$.registerPassThroughInterface(name, client, server);
}
}
@@ -1,240 +0,0 @@
package codechicken.multipart
import net.minecraft.tileentity.TileEntity
import net.minecraft.world.World
import codechicken.lib.vec.BlockCoord
import codechicken.multipart.handler.{MultipartProxy, MultipartProxy$}
import codechicken.lib.packet.PacketCustom
import net.minecraft.network.play.server.S23PacketBlockChange
import java.util.BitSet
import scala.collection.mutable.Map
import codechicken.multipart.asm.{ScratchBitSet, MultipartMixinFactory}
import codechicken.multipart.asm.ASMImplicits._
/** This class manages the dynamic construction and allocation of container
* TileMultipart instances.
*
* Classes that extend TileMultipart, adding tile centric logic, optimisations
* or interfaces, can be registered to a marker interface on a part instance.
* When a part is added to the tile that implements the certain marker
* interface, the container tile will be replaced with a class that includes
* the functionality from the corresponding mixin class.
*
* Classes are generated in a similar fashion to the way scala traits are
* compiled. To see the output, simply enable the config option and look in the
* asm/multipart folder of you .minecraft directory.
*
* There are several mixin traits that come with the API included in the
* scalatraits package. TPartialOcclusionTile is defined as class instead of
* trait to give an example for Java programmers.
*/
object MultipartGenerator extends ScratchBitSet {
private val tileTraitMap = Map[Class[_], BitSet]()
private val interfaceTraitMap_c = Map[String, String]()
private val interfaceTraitMap_s = Map[String, String]()
private val partTraitMap_c = Map[Class[_], BitSet]()
private val partTraitMap_s = Map[Class[_], BitSet]()
private val clientTraitId =
MultipartMixinFactory.registerTrait(
"codechicken.multipart.TileMultipartClient"
)
private def partTraitMap(client: Boolean) =
if (client) partTraitMap_c else partTraitMap_s
private def interfaceTraitMap(client: Boolean) =
if (client) interfaceTraitMap_c else interfaceTraitMap_s
private def traitsForPart(part: TMultiPart, client: Boolean) =
partTraitMap(client).getOrElseUpdate(
part.getClass, {
def heirachy(clazz: Class[_]): Seq[Class[_]] = {
var superClasses: Seq[Class[_]] =
clazz.getInterfaces.flatMap(c => heirachy(c)) :+ clazz
if (clazz.getSuperclass != null)
superClasses = superClasses ++ heirachy(clazz.getSuperclass)
return superClasses
}
val map = interfaceTraitMap(client)
val traits =
heirachy(part.getClass).flatMap(c => map.get(c.nodeName)).distinct
val bitset = new BitSet
traits.map(MultipartMixinFactory.getId).foreach(bitset.set)
bitset
}
)
private def setTraits(part: TMultiPart, client: Boolean): BitSet =
setTraits(Seq(part), client)
private def setTraits(parts: Iterable[TMultiPart], client: Boolean) = {
val bitset = freshBitSet
parts.foreach(p => bitset.or(traitsForPart(p, client)))
if (client) bitset.set(clientTraitId)
bitset
}
/** Check if part adds any new interfaces to tile, if so, replace tile with a
* new copy and call tile.addPart(part) returns true if tile was replaced
*/
private[multipart] def addPart(
world: World,
pos: BlockCoord,
part: TMultiPart
): TileMultipart = {
val gtile = TileMultipart.getOrConvertTile2(world, pos)
val tile = gtile._1
val converted = gtile._2.asInstanceOf[Boolean]
val bitset = setTraits(part, world.isRemote)
var ntile = tile
if (ntile != null) {
if (converted) { // perform client conversion
ntile.partList.apply(0).invalidateConvertedTile()
world.setBlock(pos.x, pos.y, pos.z, MultipartProxy.block, 0, 0)
silentAddTile(world, pos, ntile)
PacketCustom.sendToChunk(
new S23PacketBlockChange(pos.x, pos.y, pos.z, world),
world,
pos.x >> 4,
pos.z >> 4
)
ntile.partList.apply(0).onConverted()
ntile.writeAddPart(ntile.partList.apply(0))
}
val tileTraits = tileTraitMap(tile.getClass)
bitset.andNot(tileTraits)
if (!bitset.isEmpty) {
bitset.or(tileTraits)
ntile = MultipartMixinFactory.construct(bitset)
tile.setValid(false)
silentAddTile(world, pos, ntile)
ntile.from(tile)
}
} else {
world.setBlock(pos.x, pos.y, pos.z, MultipartProxy.block, 0, 0)
ntile = MultipartMixinFactory.construct(bitset)
silentAddTile(world, pos, ntile)
}
ntile.addPart_impl(part)
return ntile
}
/** Adds a tile entity to the world without notifying neighbor blocks or
* adding it to the tick list
*/
def silentAddTile(world: World, pos: BlockCoord, tile: TileEntity) {
val chunk = world.getChunkFromBlockCoords(pos.x, pos.z)
if (chunk != null)
chunk.func_150812_a(pos.x & 15, pos.y, pos.z & 15, tile)
}
/** Check if tile satisfies all the interfaces required by parts. If not,
* return a new generated copy of tile
*/
private[multipart] def generateCompositeTile(
tile: TileEntity,
parts: Iterable[TMultiPart],
client: Boolean
) = {
val bitset = setTraits(parts, client)
if (
tile != null && tile
.isInstanceOf[TileMultipart] && bitset == tileTraitMap(tile.getClass)
)
tile.asInstanceOf[TileMultipart]
else
MultipartMixinFactory.construct(bitset)
}
/** Check if there are any redundant interfaces on tile, if so, replace tile
* with new copy
*/
private[multipart] def partRemoved(tile: TileMultipart) = {
val ntile =
generateCompositeTile(tile, tile.partList, tile.getWorldObj.isRemote)
if (ntile != tile) {
tile.setValid(false)
silentAddTile(tile.getWorldObj, new BlockCoord(tile), ntile)
ntile.from(tile)
ntile.notifyTileChange()
}
ntile
}
/** register s_trait to be applied to tiles containing parts implementing
* s_interface
*/
def registerTrait(s_interface: String, s_trait: String): Unit =
registerTrait(s_interface, s_trait, s_trait)
/** register traits to be applied to tiles containing parts implementing
* s_interface c_trait for client worlds (may be null) s_trait for server
* worlds (may be null)
*/
def registerTrait(
s_interface$ : String,
c_trait$ : String,
s_trait$ : String
) {
val s_interface = nodeName(s_interface$)
val c_trait = nodeName(c_trait$)
val s_trait = nodeName(s_trait$)
def registerSide(map: Map[String, String], traitName: String) = if (
traitName != null
) {
if (map.contains(s_interface))
MultipartProxy.logger.error(
"Trait already registered for " + s_interface
)
else {
map.put(s_interface, traitName)
MultipartMixinFactory.registerTrait(traitName)
}
}
registerSide(interfaceTraitMap_c, c_trait)
registerSide(interfaceTraitMap_s, s_trait)
}
def registerPassThroughInterface(s_interface: String): Unit =
registerPassThroughInterface(s_interface, true, true)
/** A passthrough interface, is an interface to be implemented on the
* container tile instance, for which all calls are passed directly to the
* single implementing part. Registering a passthrough interface is
* equivalent to defining a mixin class as follows.
* 1. field 'impl' which contains the reference to the corresponding part
* 2. occlusionTest is overriden to prevent more than one part with
* s_interface existing in the block space 3. implementing s_interface
* and passing all calls directly to the part instance.
*
* This allows compatibility with APIs that expect interfaces on the tile
* entity.
*/
def registerPassThroughInterface(
s_interface: String,
client: Boolean,
server: Boolean
) {
val tType = MultipartMixinFactory.generatePassThroughTrait(s_interface)
if (tType == null)
return
registerTrait(
s_interface,
if (client) tType else null,
if (server) tType else null
)
}
private[multipart] def registerTileClass(
clazz: Class[_ <: TileMultipart],
traits: BitSet
) {
tileTraitMap.put(clazz, traits.copy)
MultipartProxy$.MODULE$.onTileClassBuilt(clazz)
}
}
@@ -6,7 +6,7 @@ import codechicken.lib.asm.{ObfMapping, CC_ClassWriter}
import org.objectweb.asm.{Label, FieldVisitor, MethodVisitor}
import org.objectweb.asm.Opcodes._
import org.objectweb.asm.tree.{MethodNode, ClassNode}
import codechicken.multipart.{MultipartGenerator, TileMultipart}
import codechicken.multipart.{MultipartGenerator$, TileMultipart}
import java.util.BitSet
import ASMImplicits._
@@ -245,5 +245,5 @@ object MultipartMixinFactory extends ASMMixinFactory(classOf[TileMultipart]) {
clazz: Class[_ <: TileMultipart],
traitSet: BitSet
) =
MultipartGenerator.registerTileClass(clazz, traitSet)
MultipartGenerator$.MODULE$.registerTileClass(clazz, traitSet)
}