Refactor shared code for portals

This commit is contained in:
jchung01
2025-05-20 22:49:46 -07:00
parent 81f239c21b
commit 4d04e54d10
4 changed files with 65 additions and 40 deletions
@@ -1,5 +1,3 @@
import native.hellfirepvp.modularmachinery.common.crafting.command.ControllerCommandSender;
import native.hellfirepvp.modularmachinery.common.tiles.base.TileMultiblockMachineController;
import native.net.minecraft.item.Item;
import native.net.minecraft.item.ItemStack;
@@ -9,12 +7,13 @@ import crafttweaker.world.IBlockPos;
import mods.modularmachinery.RecipeBuilder;
import mods.modularmachinery.MachineTickEvent;
import mods.modularmachinery.RecipeStartEvent;
import mods.modularmachinery.RecipeFailureEvent;
import mods.modularmachinery.MMEvents;
import mods.modularmachinery.IMachineController;
// Ideally some code here should be shared with ModularWormholeField, but for some reason cross-script reference doesn't work.
// Shared code
import scripts.util.ModularEventUtils as Util;
static offset as Util.Offset = Util.Offset(5, -4);
var portal = <ore:blockCustomPortal>;
portal.addItems([
<contenttweaker:sedna_portal_block>,
@@ -224,9 +223,9 @@ stargatealkemia.addItemInput(<contenttweaker:alkemia_warper>).setChance(0)
// Do start up sound and message if no current portal, spawn portal if different recipe, or nothing if same portal:
function beeStargateRecipeStart(controller as IMachineController, portalBlock as string, destination as string) {
var pos = getCenter(controller, 5, -4);
var sender = getControllerSender(controller);
controller.world.native.getMinecraftServer().addScheduledTask(function() {
var pos = offset.getCenter(controller);
var sender = Util.getControllerSender(controller);
Util.scheduleTask(controller, function() as void {
var currentPortalBlock = controller.world.getBlockState(pos);
if (currentPortalBlock != IBlockState.getBlockState(portalBlock,"")) {
@@ -250,8 +249,8 @@ MMEvents.onMachinePostTick("bee_stargate", function(event as MachineTickEvent) {
if (ctrl.world.time % 20 != 0 || ctrl.isWorking) {
return;
}
var pos = getCenter(ctrl, 5, -4);
ctrl.world.native.getMinecraftServer().addScheduledTask(function() {
var pos = offset.getCenter(ctrl);
Util.scheduleTask(ctrl, function() as void {
var centerState = ctrl.world.getBlockState(pos);
var itemBlock = ItemStack(Item.getItemFromBlock(centerState.block.native));
// Only clear blocks that aren't unbreakable or are known portal blocks.
@@ -264,7 +263,7 @@ MMEvents.onMachinePostTick("bee_stargate", function(event as MachineTickEvent) {
// Clear portal blocks.
function beeStargateClear(controller as IMachineController, pos as IBlockPos) {
var sender = getControllerSender(controller);
var sender = Util.getControllerSender(controller);
beeStargatePlacePortal(controller, "minecraft:air", pos, sender);
var offlineText = '{"translate":"chat.contenttweaker.stargate.offline"}';
server.commandManager.executeCommand(sender, "playsound minecraft:entity.evocation_illager.cast_spell ambient @a " + pos.x + " " + pos.y + " " + pos.z + " 1 0.5");
@@ -281,14 +280,4 @@ function beeStargatePlacePortal(controller as IMachineController, portalBlock as
if (facing == crafttweaker.world.IFacing.north() || facing == crafttweaker.world.IFacing.south()) { dx = 1; }
else { dz = 1; }
server.commandManager.executeCommand(sender, "fill " + (pos.x - dx) + " " + (pos.y - 1) + " " + (pos.z - dz) + " " + (pos.x + dx) + " " + (pos.y + 1) + " " + (pos.z + dz) + " " + portalBlock);
}
function getCenter(ctrl as IMachineController, offsetUp as int, offsetFacing as int) as IBlockPos {
return ctrl.pos.getOffset(up, offsetUp).getOffset(ctrl.facing, offsetFacing);
}
// The ICommandSender must have the correct associated world, so we're using ControllerCommandSender.
function getControllerSender(controller as IMachineController) as ICommandSender {
var sender = ControllerCommandSender(controller as TileMultiblockMachineController) as native.net.minecraft.command.ICommandSender;
return sender.wrapper;
}
@@ -1,5 +1,3 @@
import native.hellfirepvp.modularmachinery.common.crafting.command.ControllerCommandSender;
import native.hellfirepvp.modularmachinery.common.tiles.base.TileMultiblockMachineController;
import native.net.minecraft.item.Item;
import native.net.minecraft.item.ItemStack;
@@ -9,12 +7,13 @@ import crafttweaker.world.IBlockPos;
import mods.modularmachinery.RecipeBuilder;
import mods.modularmachinery.MachineTickEvent;
import mods.modularmachinery.RecipeStartEvent;
import mods.modularmachinery.RecipeFailureEvent;
import mods.modularmachinery.MMEvents;
import mods.modularmachinery.IMachineController;
// Ideally some code here should be shared with ModularBeeStargate, but for some reason cross-script reference doesn't work.
// Shared code
import scripts.util.ModularEventUtils as Util;
static offset as Util.Offset = Util.Offset(4, -3);
var portal = <ore:blockCustomPortal>;
portal.addItems([
<contenttweaker:taerrapiatta_portal_block>,
@@ -102,9 +101,9 @@ wormholegallifrey.addItemInput(<contenttweaker:gallifrey_warper>).setChance(0)
// Do start up sound and message if no current portal, spawn portal if different recipe, or nothing if same portal:
function wormholeGeneratorRecipeStart(controller as IMachineController, portalBlock as string, destination as string) {
var pos = getCenter(controller, 4, -3);
var sender = getControllerSender(controller);
controller.world.native.getMinecraftServer().addScheduledTask(function() {
var pos = offset.getCenter(controller);
var sender = Util.getControllerSender(controller);
Util.scheduleTask(controller, function() as void {
var currentPortalBlock = controller.world.getBlockState(pos);
if (currentPortalBlock != IBlockState.getBlockState(portalBlock,"")) {
@@ -128,8 +127,8 @@ MMEvents.onMachinePostTick("wormhole_field_generator", function(event as Machine
if (ctrl.world.time % 20 != 0 || ctrl.isWorking) {
return;
}
var pos = getCenter(ctrl, 4, -3);
ctrl.world.native.getMinecraftServer().addScheduledTask(function() {
var pos = offset.getCenter(ctrl);
Util.scheduleTask(ctrl, function() as void {
var centerState = ctrl.world.getBlockState(pos);
var itemBlock = ItemStack(Item.getItemFromBlock(centerState.block.native));
// Only clear blocks that aren't unbreakable or are known portal blocks.
@@ -142,7 +141,7 @@ MMEvents.onMachinePostTick("wormhole_field_generator", function(event as Machine
// Clear portal blocks.
function wormholeGeneratorClear(controller as IMachineController, pos as IBlockPos) {
var sender = getControllerSender(controller);
var sender = Util.getControllerSender(controller);
wormholeGeneratorPlacePortal(controller, "minecraft:air", pos, sender);
var offlineText = '{"translate":"chat.contenttweaker.wormhole_generator.offline"}';
server.commandManager.executeCommand(sender, "playsound minecraft:entity.evocation_illager.cast_spell ambient @a " + pos.x + " " + pos.y + " " + pos.z + " 1 0.5");
@@ -155,14 +154,4 @@ function wormholeGeneratorClear(controller as IMachineController, pos as IBlockP
function wormholeGeneratorPlacePortal(controller as IMachineController, portalBlock as string, pos as IBlockPos, sender as ICommandSender) {
var facing = controller.facing;
server.commandManager.executeCommand(sender, "fill " + (pos.x - 1) + " " + (pos.y - 1) + " " + (pos.z - 1) + " " + (pos.x + 1) + " " + (pos.y + 1) + " " + (pos.z + 1) + " " + portalBlock);
}
function getCenter(ctrl as IMachineController, offsetUp as int, offsetFacing as int) as IBlockPos {
return ctrl.pos.getOffset(up, offsetUp).getOffset(ctrl.facing, offsetFacing);
}
// The ICommandSender must have the correct associated world, so we're using ControllerCommandSender.
function getControllerSender(controller as IMachineController) as ICommandSender {
var sender = ControllerCommandSender(controller as TileMultiblockMachineController) as native.net.minecraft.command.ICommandSender;
return sender.wrapper;
}
+47
View File
@@ -0,0 +1,47 @@
#priority 10
import native.hellfirepvp.modularmachinery.common.crafting.command.ControllerCommandSender;
import native.hellfirepvp.modularmachinery.common.tiles.base.TileMultiblockMachineController;
import crafttweaker.command.ICommandSender;
import crafttweaker.world.IBlockPos;
import mods.modularmachinery.IMachineController;
/*
Utility functions for properly executing commands from a MMCE event.
*/
zenClass Offset {
val yOffset as int;
val facingOffset as int;
zenConstructor(y as int, facing as int) {
yOffset = y;
facingOffset = facing;
}
/*
Return the position of the center to use.
*/
function getCenter(ctrl as IMachineController) as IBlockPos {
return ctrl.pos.getOffset(up, yOffset).getOffset(ctrl.facing, facingOffset);
}
}
/*
Return a command sender based on the MMCE controller.
Required in order to provide dimension context to commands.
*/
function getControllerSender(controller as IMachineController) as ICommandSender {
var sender = ControllerCommandSender(controller as TileMultiblockMachineController) as native.net.minecraft.command.ICommandSender;
return sender.wrapper;
}
/*
Schedule a task to run on the main thread.
You should do this for anything that involves in-world objects (like block state and commands).
The passed task must be `function() as void {...}`.
*/
function scheduleTask(controller as IMachineController, task as function()void) {
controller.world.native.getMinecraftServer().addScheduledTask(task);
}