3287
社区成员
如题。看起来是Java lumbda,可是 idea 转 kotlin 和自己转都很多错误,请教正确的转换写法:
public interface SheepShearCallback {
Event<SheepShearCallback> EVENT = EventFactory.createArrayBacked(SheepShearCallback.class,
(listeners) -> (player, sheep) -> {
for (SheepShearCallback listener : listeners) {
ActionResult result = listener.interact(player, sheep);
if(result != ActionResult.PASS) {
return result;
}
}
return ActionResult.PASS;
});
ActionResult interact(PlayerEntity player, SheepEntity sheep);
}
首先看看相关包是否导入成功,这边转换之后发现没有相关的包,语法上应该idea的转换没有多大问题
interface SheepShearCallback {
fun interact(player: PlayerEntity?, sheep: SheepEntity?): ActionResult
companion object {
val EVENT: Event<SheepShearCallback> = EventFactory.createArrayBacked(
SheepShearCallback::class.java,
{ listeners ->
label@{ player, sheep ->
for (listener in listeners) {
val result: ActionResult = listener.interact(player, sheep)
if (result !== ActionResult.PASS) {
return@label result
}
}
ActionResult.PASS
}
})
}
}