mirror of
https://github.com/GTNewHorizons/ForgeMultipart.git
synced 2026-11-27 15:33:05 +00:00
refactor: port multipart compatibility to java
Preserve the Scala Function4 singleton ABI and MCPC failure fallback.
This commit is contained in:
1 parent
53e5901360
commit
b2bdf569e8
8 files changed
+183
-55
No files matched your search
@@ -0,0 +1,49 @@
|
||||
package codechicken.multipart.handler;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import scala.runtime.AbstractFunction4;
|
||||
|
||||
/** Scala-object-compatible implementation for the optional MCPC placement hook. */
|
||||
public final class MCPCCompatModule$ {
|
||||
|
||||
public static final MCPCCompatModule$ MODULE$ = new MCPCCompatModule$();
|
||||
|
||||
private MCPCCompatModule$() {}
|
||||
|
||||
public void load() {
|
||||
try {
|
||||
Method canPlacePart = World.class
|
||||
.getDeclaredMethod("canPlaceMultipart", Block.class, Integer.TYPE, Integer.TYPE, Integer.TYPE);
|
||||
MultipartCompatiblity.canAddPart_$eq(new MCPCPlacement(canPlacePart));
|
||||
} catch (Exception e) {
|
||||
MultipartProxy.logger().error("Failed to integrate MCPC placement hooks", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class MCPCPlacement extends AbstractFunction4<World, Object, Object, Object, Object> {
|
||||
|
||||
private final Method canPlacePart;
|
||||
|
||||
MCPCPlacement(Method canPlacePart) {
|
||||
this.canPlacePart = canPlacePart;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object apply(World world, Object x, Object y, Object z) {
|
||||
try {
|
||||
return ((Boolean) canPlacePart.invoke(world, MultipartProxy.block(), x, y, z)).booleanValue();
|
||||
} catch (Exception e) {
|
||||
return throwUnchecked(e);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static <E extends Throwable, R> R throwUnchecked(Throwable throwable) throws E {
|
||||
throw (E) throwable;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package codechicken.multipart.handler;
|
||||
|
||||
/** Static facade for the optional MCPC placement hook. */
|
||||
public final class MCPCCompatModule {
|
||||
|
||||
private MCPCCompatModule() {}
|
||||
|
||||
public static void load() {
|
||||
MCPCCompatModule$.MODULE$.load();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package codechicken.multipart.handler;
|
||||
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import cpw.mods.fml.common.FMLCommonHandler;
|
||||
import scala.Function4;
|
||||
import scala.runtime.AbstractFunction4;
|
||||
|
||||
/** Scala-object-compatible implementation for the multipart placement compatibility hook. */
|
||||
public final class MultipartCompatiblity$ {
|
||||
|
||||
public static final MultipartCompatiblity$ MODULE$ = new MultipartCompatiblity$();
|
||||
|
||||
private Function4<World, Object, Object, Object, Object> canAddPart = new AllowPlacement();
|
||||
|
||||
private MultipartCompatiblity$() {}
|
||||
|
||||
public Function4<World, Object, Object, Object, Object> canAddPart() {
|
||||
return canAddPart;
|
||||
}
|
||||
|
||||
public void canAddPart_$eq(Function4<World, Object, Object, Object, Object> callback) {
|
||||
canAddPart = callback;
|
||||
}
|
||||
|
||||
public void load() {
|
||||
if (FMLCommonHandler.instance().getModName().contains("mcpc")) {
|
||||
MCPCCompatModule.load();
|
||||
}
|
||||
}
|
||||
|
||||
private static final class AllowPlacement extends AbstractFunction4<World, Object, Object, Object, Object> {
|
||||
|
||||
AllowPlacement() {}
|
||||
|
||||
@Override
|
||||
public Object apply(World world, Object x, Object y, Object z) {
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package codechicken.multipart.handler;
|
||||
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import scala.Function4;
|
||||
|
||||
/** Static facade for the multipart placement compatibility hook. */
|
||||
public final class MultipartCompatiblity {
|
||||
|
||||
private MultipartCompatiblity() {}
|
||||
|
||||
public static Function4<World, Object, Object, Object, Object> canAddPart() {
|
||||
return MultipartCompatiblity$.MODULE$.canAddPart();
|
||||
}
|
||||
|
||||
public static void canAddPart_$eq(Function4<World, Object, Object, Object, Object> callback) {
|
||||
MultipartCompatiblity$.MODULE$.canAddPart_$eq(callback);
|
||||
}
|
||||
|
||||
public static void load() {
|
||||
MultipartCompatiblity$.MODULE$.load();
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
package codechicken.multipart.handler
|
||||
|
||||
import net.minecraft.world.World
|
||||
import cpw.mods.fml.common.FMLCommonHandler
|
||||
import net.minecraft.block.Block
|
||||
|
||||
object MultipartCompatiblity {
|
||||
var canAddPart = (world: World, x: Int, y: Int, z: Int) => true
|
||||
|
||||
def load() {
|
||||
if (FMLCommonHandler.instance().getModName.contains("mcpc"))
|
||||
MCPCCompatModule.load()
|
||||
}
|
||||
}
|
||||
|
||||
object MCPCCompatModule {
|
||||
def load() {
|
||||
try {
|
||||
val m_canPlacePart = classOf[World].getDeclaredMethod(
|
||||
"canPlaceMultipart",
|
||||
classOf[Block],
|
||||
classOf[Int],
|
||||
classOf[Int],
|
||||
classOf[Int]
|
||||
)
|
||||
MultipartCompatiblity.canAddPart =
|
||||
(world: World, x: Int, y: Int, z: Int) => {
|
||||
m_canPlacePart
|
||||
.invoke(
|
||||
world,
|
||||
MultipartProxy.block,
|
||||
x: Integer,
|
||||
y: Integer,
|
||||
z: Integer
|
||||
)
|
||||
.asInstanceOf[Boolean]
|
||||
.booleanValue()
|
||||
}
|
||||
} catch {
|
||||
case e: Exception =>
|
||||
MultipartProxy.logger.error(
|
||||
"Failed to integrate MCPC placement hooks",
|
||||
e
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user