-
Notifications
You must be signed in to change notification settings - Fork 691
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add event to allow addons to spawn additional "grid tools" into a che…
…st next to a test-plot (#7760)
- Loading branch information
Showing
9 changed files
with
221 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/java/appeng/server/testplots/SpawnExtraGridTestTools.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package appeng.server.testplots; | ||
|
||
import net.minecraft.resources.ResourceLocation; | ||
import net.neoforged.bus.api.Event; | ||
|
||
import appeng.api.inventories.InternalInventory; | ||
import appeng.api.networking.IGrid; | ||
|
||
/** | ||
* Triggered to spawn additional testing tools into a container placed next to a spawned AE2 grid. | ||
*/ | ||
public class SpawnExtraGridTestTools extends Event { | ||
private final ResourceLocation plotId; | ||
private final InternalInventory inventory; | ||
private final IGrid grid; | ||
|
||
public SpawnExtraGridTestTools(ResourceLocation plotId, InternalInventory inventory, IGrid grid) { | ||
this.plotId = plotId; | ||
this.inventory = inventory; | ||
this.grid = grid; | ||
} | ||
|
||
public ResourceLocation getPlotId() { | ||
return plotId; | ||
} | ||
|
||
public InternalInventory getInventory() { | ||
return inventory; | ||
} | ||
|
||
public IGrid getGrid() { | ||
return grid; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package appeng.server.testplots; | ||
|
||
import java.util.List; | ||
|
||
import net.minecraft.core.GlobalPos; | ||
import net.neoforged.bus.api.SubscribeEvent; | ||
import net.neoforged.fml.common.Mod; | ||
|
||
import appeng.api.config.Actionable; | ||
import appeng.api.features.GridLinkables; | ||
import appeng.blockentity.networking.WirelessAccessPointBlockEntity; | ||
import appeng.core.definitions.AEItems; | ||
|
||
@Mod.EventBusSubscriber | ||
public final class SpawnTestTools { | ||
|
||
@SubscribeEvent | ||
public static void spawnWirelessTerminals(SpawnExtraGridTestTools e) { | ||
// Find a suitable WAP to link to | ||
var waps = e.getGrid().getMachines(WirelessAccessPointBlockEntity.class); | ||
if (waps.isEmpty()) { | ||
return; | ||
} | ||
|
||
var wap = waps.iterator().next(); | ||
var inventory = e.getInventory(); | ||
|
||
for (var item : List.of(AEItems.WIRELESS_CRAFTING_TERMINAL, AEItems.WIRELESS_TERMINAL)) { | ||
var terminal = item.stack(); | ||
// Fully charge it | ||
item.asItem().injectAEPower(terminal, Double.MAX_VALUE, Actionable.MODULATE); | ||
// Link it to the WAP we just placed | ||
GridLinkables.get(item).link(terminal, GlobalPos.of(wap.getLevel().dimension(), wap.getBlockPos())); | ||
inventory.addItems(terminal); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package appeng.server.testworld; | ||
|
||
import java.util.function.BiConsumer; | ||
|
||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.server.level.ServerLevel; | ||
|
||
import appeng.api.networking.IGrid; | ||
import appeng.api.networking.IGridNode; | ||
import appeng.blockentity.networking.CableBusBlockEntity; | ||
import appeng.hooks.ticking.TickHandler; | ||
import appeng.me.helpers.IGridConnectedBlockEntity; | ||
|
||
final class GridInitHelper { | ||
|
||
static void doAfterGridInit(ServerLevel level, BlockPos pos, boolean waitForActive, | ||
BiConsumer<IGrid, IGridNode> consumer) { | ||
Runnable delayedAction = new Runnable() { | ||
private int attempts = 120; | ||
|
||
@Override | ||
public void run() { | ||
// Check if there's a grid node there | ||
var be = level.getBlockEntity(pos); | ||
IGridNode gridNode = null; | ||
if (be instanceof IGridConnectedBlockEntity host) { | ||
gridNode = host.getMainNode().getNode(); | ||
} else if (be instanceof CableBusBlockEntity cableBus) { | ||
var centerPart = cableBus.getCableBus().getPart(null); | ||
if (centerPart != null) { | ||
gridNode = centerPart.getGridNode(); | ||
} else { | ||
return; // Stop -> not eligible | ||
} | ||
} else { | ||
return; // Stop -> not eligible | ||
} | ||
|
||
if (gridNode == null || waitForActive && !gridNode.isActive()) { | ||
if (--attempts > 0) { | ||
TickHandler.instance().addCallable(level, this); | ||
} else { | ||
throw new IllegalStateException("Couldn't access grid node @ " + pos); | ||
} | ||
} else { | ||
consumer.accept(gridNode.getGrid(), gridNode); | ||
} | ||
} | ||
}; | ||
TickHandler.instance().addCallable(level, delayedAction); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/appeng/server/testworld/PlaceItemFrameAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package appeng.server.testworld; | ||
|
||
import java.util.List; | ||
import java.util.function.Consumer; | ||
|
||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.core.Direction; | ||
import net.minecraft.server.level.ServerLevel; | ||
import net.minecraft.world.entity.Entity; | ||
import net.minecraft.world.entity.EntityType; | ||
import net.minecraft.world.entity.decoration.ItemFrame; | ||
import net.minecraft.world.level.levelgen.structure.BoundingBox; | ||
|
||
public record PlaceItemFrameAction(BlockPos pos, Direction facing, | ||
Consumer<ItemFrame> customizer) implements BuildAction { | ||
@Override | ||
public BoundingBox getBoundingBox() { | ||
return new BoundingBox(pos); | ||
} | ||
|
||
@Override | ||
public void spawnEntities(ServerLevel level, BlockPos origin, List<Entity> entities) { | ||
var actualPos = pos.offset(origin); | ||
|
||
var itemFrame = new ItemFrame(EntityType.ITEM_FRAME, level, actualPos, facing); | ||
if (!level.addFreshEntity(itemFrame)) { | ||
return; | ||
} | ||
customizer.accept(itemFrame); | ||
entities.add(itemFrame); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/main/java/appeng/server/testworld/SpawnExtraGridTestToolsChest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package appeng.server.testworld; | ||
|
||
import net.minecraft.core.BlockPos; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.server.level.ServerLevel; | ||
import net.minecraft.world.entity.player.Player; | ||
import net.minecraft.world.level.block.Block; | ||
import net.minecraft.world.level.levelgen.structure.BoundingBox; | ||
import net.neoforged.neoforge.common.NeoForge; | ||
|
||
import appeng.core.definitions.AEBlockEntities; | ||
import appeng.core.definitions.AEBlocks; | ||
import appeng.server.testplots.SpawnExtraGridTestTools; | ||
|
||
/** | ||
* Spawns a sky stone chest at the given position and once the grid at another position is initialized, posts | ||
* {@link SpawnExtraGridTestTools} to allow the chest to be populated. | ||
*/ | ||
public record SpawnExtraGridTestToolsChest(BlockPos chestPos, BlockPos gridPos, | ||
ResourceLocation plotId) implements BuildAction { | ||
@Override | ||
public BoundingBox getBoundingBox() { | ||
return new BoundingBox(chestPos); | ||
} | ||
|
||
@Override | ||
public void build(ServerLevel level, Player player, BlockPos origin) { | ||
var absChestPod = chestPos.offset(origin); | ||
var absGridPos = gridPos.offset(origin); | ||
level.setBlock(absChestPod, AEBlocks.SMOOTH_SKY_STONE_CHEST.block().defaultBlockState(), Block.UPDATE_ALL); | ||
|
||
GridInitHelper.doAfterGridInit(level, absGridPos, false, (grid, gridNode) -> { | ||
var chestOpt = level.getBlockEntity(absChestPod, AEBlockEntities.SKY_CHEST); | ||
chestOpt.ifPresent(chest -> { | ||
var inventory = chest.getInternalInventory(); | ||
NeoForge.EVENT_BUS.post(new SpawnExtraGridTestTools(plotId, inventory, grid)); | ||
}); | ||
}); | ||
} | ||
} |