Use AstraPlus block storage and item delivery APIs
This commit is contained in:
@@ -13,8 +13,7 @@ import online.astradev.content.weapon.Ammunition;
|
||||
public final class AstraContent {
|
||||
public AstraContent(IEventBus bus) {
|
||||
Defaults.install(FMLPaths.GAMEDIR.get());
|
||||
Rails.register(bus);
|
||||
NeoForge.EVENT_BUS.register(Rails.class);
|
||||
Rails.register();
|
||||
NeoForge.EVENT_BUS.register(Ammunition.class);
|
||||
NeoForge.EVENT_BUS.register(online.astradev.content.rail.RailCommands.class);
|
||||
NeoForge.EVENT_BUS.register(online.astradev.content.train.Trains.class);
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
package online.astradev.content.rail;
|
||||
|
||||
import com.mojang.brigadier.context.CommandContext;
|
||||
import com.mojang.brigadier.exceptions.CommandSyntaxException;
|
||||
|
||||
import net.minecraft.commands.CommandSourceStack;
|
||||
import net.minecraft.commands.Commands;
|
||||
import net.neoforged.bus.api.SubscribeEvent;
|
||||
import net.neoforged.neoforge.event.RegisterCommandsEvent;
|
||||
|
||||
import online.astradev.plus.api.AstraPlusApi;
|
||||
|
||||
public final class RailCommands {
|
||||
private RailCommands() {}
|
||||
|
||||
@@ -13,15 +19,7 @@ public final class RailCommands {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
var kind = RailKind.decode(i);
|
||||
give.then(
|
||||
Commands.literal(kind.id().getPath())
|
||||
.executes(
|
||||
ctx -> {
|
||||
var player = ctx.getSource().getPlayerOrException();
|
||||
var stack = Rails.stack(kind, 64);
|
||||
if (!player.getInventory().add(stack))
|
||||
player.drop(stack, false);
|
||||
return 1;
|
||||
}));
|
||||
Commands.literal(kind.id().getPath()).executes(context -> give(context, kind)));
|
||||
}
|
||||
event.getDispatcher()
|
||||
.register(
|
||||
@@ -29,4 +27,12 @@ public final class RailCommands {
|
||||
.requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
|
||||
.then(give));
|
||||
}
|
||||
|
||||
private static int give(CommandContext<CommandSourceStack> context, RailKind kind)
|
||||
throws CommandSyntaxException {
|
||||
var player = context.getSource().getPlayerOrException();
|
||||
var stack = Rails.stack(kind, 64);
|
||||
AstraPlusApi.give(player, stack);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package online.astradev.content.rail;
|
||||
|
||||
import com.mojang.serialization.Codec;
|
||||
import static online.astradev.plus.api.AstraPlusApi.position;
|
||||
import static online.astradev.plus.api.AstraPlusApi.world;
|
||||
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.server.level.ServerLevel;
|
||||
@@ -10,39 +11,23 @@ import net.minecraft.world.level.block.Blocks;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.chunk.LevelChunk;
|
||||
import net.neoforged.bus.api.*;
|
||||
import net.neoforged.neoforge.attachment.AttachmentType;
|
||||
import net.neoforged.neoforge.common.NeoForge;
|
||||
import net.neoforged.neoforge.event.entity.player.PlayerInteractEvent;
|
||||
import net.neoforged.neoforge.event.level.ChunkEvent;
|
||||
import net.neoforged.neoforge.event.server.ServerStoppedEvent;
|
||||
import net.neoforged.neoforge.event.tick.ServerTickEvent;
|
||||
import net.neoforged.neoforge.registries.*;
|
||||
|
||||
import online.astradev.plus.api.content.BlockData;
|
||||
import online.astradev.plus.api.content.ContentRegistry;
|
||||
import online.astradev.plus.api.world.BlockPosition;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/** Native powered rails own placement, shape, redstone, collision and destruction. */
|
||||
public final class Rails {
|
||||
private static final DeferredRegister<AttachmentType<?>> TYPES =
|
||||
DeferredRegister.create(NeoForgeRegistries.Keys.ATTACHMENT_TYPES, "astracontent");
|
||||
public static final Supplier<AttachmentType<Map<Long, Integer>>> DATA =
|
||||
TYPES.register(
|
||||
"copper_rails",
|
||||
() ->
|
||||
AttachmentType.<Map<Long, Integer>>builder(() -> new HashMap<>())
|
||||
.serialize(
|
||||
Codec.unboundedMap(
|
||||
Codec.STRING.xmap(
|
||||
Long::parseLong,
|
||||
String::valueOf),
|
||||
Codec.intRange(0, 7))
|
||||
.xmap(
|
||||
map ->
|
||||
(Map<Long, Integer>)
|
||||
new HashMap<>(map),
|
||||
map -> map)
|
||||
.fieldOf("rails"),
|
||||
map -> !map.isEmpty())
|
||||
.build());
|
||||
private static final BlockData<Integer> DATA =
|
||||
new ContentRegistry("astracontent")
|
||||
.blockData("copper_rails", BlockData.integers("rails", 0, 7));
|
||||
|
||||
private record Position(ServerLevel level, BlockPos pos) {}
|
||||
|
||||
@@ -53,14 +38,14 @@ public final class Rails {
|
||||
|
||||
private Rails() {}
|
||||
|
||||
public static void register(IEventBus bus) {
|
||||
TYPES.register(bus);
|
||||
public static void register() {
|
||||
NeoForge.EVENT_BUS.register(Rails.class);
|
||||
}
|
||||
|
||||
public static RailKind find(ServerLevel level, BlockPos pos) {
|
||||
var chunk = level.getChunkSource().getChunkNow(pos.getX() >> 4, pos.getZ() >> 4);
|
||||
if (chunk == null || !chunk.hasData(DATA)) return null;
|
||||
Integer code = chunk.getData(DATA).get(pos.asLong());
|
||||
if (chunk == null) return null;
|
||||
Integer code = DATA.get(world(level), position(pos)).orElse(null);
|
||||
return code == null ? null : RailKind.decode(code);
|
||||
}
|
||||
|
||||
@@ -84,9 +69,7 @@ public final class Rails {
|
||||
}
|
||||
|
||||
public static void put(ServerLevel level, BlockPos pos, RailKind kind) {
|
||||
var chunk = level.getChunkAt(pos);
|
||||
chunk.getData(DATA).put(pos.asLong(), kind.code());
|
||||
chunk.markUnsaved();
|
||||
DATA.put(world(level), position(pos), kind.code());
|
||||
ACTIVE.putIfAbsent(new Position(level, pos.immutable()), null);
|
||||
}
|
||||
|
||||
@@ -104,9 +87,7 @@ public final class Rails {
|
||||
|
||||
private static void clear(ServerLevel level, BlockPos pos) {
|
||||
var chunk = level.getChunkSource().getChunkNow(pos.getX() >> 4, pos.getZ() >> 4);
|
||||
if (chunk != null
|
||||
&& chunk.hasData(DATA)
|
||||
&& chunk.getData(DATA).remove(pos.asLong()) != null) chunk.markUnsaved();
|
||||
if (chunk != null) DATA.remove(world(level), position(pos));
|
||||
var display = ACTIVE.remove(new Position(level, pos));
|
||||
if (display != null) display.discard();
|
||||
}
|
||||
@@ -115,24 +96,17 @@ public final class Rails {
|
||||
public static void load(ChunkEvent.Load event) {
|
||||
if (!(event.getLevel() instanceof ServerLevel level)
|
||||
|| !(event.getChunk() instanceof LevelChunk chunk)) return;
|
||||
level.getServer()
|
||||
.execute(
|
||||
() -> {
|
||||
if (level.getChunkSource()
|
||||
.getChunkNow(
|
||||
chunk.getPos().x(), chunk.getPos().z())
|
||||
!= chunk
|
||||
|| !chunk.hasData(DATA)) return;
|
||||
chunk.getData(DATA)
|
||||
.forEach(
|
||||
(packed, code) -> {
|
||||
var pos = BlockPos.of(packed);
|
||||
if (pos.getX() >> 4 == chunk.getPos().x()
|
||||
&& pos.getZ() >> 4 == chunk.getPos().z())
|
||||
ACTIVE.putIfAbsent(
|
||||
new Position(level, pos), null);
|
||||
});
|
||||
});
|
||||
level.getServer().execute(() -> restoreDisplays(level, chunk));
|
||||
}
|
||||
|
||||
private static void restoreDisplays(ServerLevel level, LevelChunk chunk) {
|
||||
var chunkPos = chunk.getPos();
|
||||
if (level.getChunkSource().getChunkNow(chunkPos.x(), chunkPos.z()) != chunk) return;
|
||||
var origin = new BlockPosition(chunkPos.x() << 4, level.getMinY(), chunkPos.z() << 4);
|
||||
for (var saved : DATA.inChunk(world(level), origin).keySet()) {
|
||||
var pos = new BlockPos(saved.x(), saved.y(), saved.z());
|
||||
ACTIVE.putIfAbsent(new Position(level, pos), null);
|
||||
}
|
||||
}
|
||||
|
||||
@SubscribeEvent
|
||||
|
||||
@@ -17,22 +17,20 @@ public final class TrainData extends SavedData {
|
||||
Codec.STRING.xmap(UUID::fromString, UUID::toString);
|
||||
static final Codec<Link> CODEC =
|
||||
RecordCodecBuilder.create(
|
||||
i ->
|
||||
i.group(
|
||||
UUID_CODEC.fieldOf("a").forGetter(Link::a),
|
||||
Codec.intRange(0, 1)
|
||||
.fieldOf("end_a")
|
||||
.forGetter(Link::endA),
|
||||
UUID_CODEC.fieldOf("b").forGetter(Link::b),
|
||||
Codec.intRange(0, 1)
|
||||
.fieldOf("end_b")
|
||||
.forGetter(Link::endB),
|
||||
Codec.STRING.fieldOf("item").forGetter(Link::item),
|
||||
Codec.BOOL.fieldOf("paid").forGetter(Link::paid),
|
||||
Codec.doubleRange(1, 4)
|
||||
.fieldOf("length")
|
||||
.forGetter(Link::length))
|
||||
.apply(i, Link::new));
|
||||
instance -> {
|
||||
var a = UUID_CODEC.fieldOf("a").forGetter(Link::a);
|
||||
var endA = Codec.intRange(0, 1).fieldOf("end_a").forGetter(Link::endA);
|
||||
var b = UUID_CODEC.fieldOf("b").forGetter(Link::b);
|
||||
var endB = Codec.intRange(0, 1).fieldOf("end_b").forGetter(Link::endB);
|
||||
var item = Codec.STRING.fieldOf("item").forGetter(Link::item);
|
||||
var paid = Codec.BOOL.fieldOf("paid").forGetter(Link::paid);
|
||||
var length =
|
||||
Codec.doubleRange(1, 4)
|
||||
.fieldOf("length")
|
||||
.forGetter(Link::length);
|
||||
var fields = instance.group(a, endA, b, endB, item, paid, length);
|
||||
return fields.apply(instance, Link::new);
|
||||
});
|
||||
|
||||
public boolean touches(UUID id) {
|
||||
return a.equals(id) || b.equals(id);
|
||||
|
||||
@@ -21,6 +21,6 @@ side="SERVER"
|
||||
[[dependencies.astracontent]]
|
||||
modId="astraplus"
|
||||
type="required"
|
||||
versionRange="[0.9,0.10)"
|
||||
versionRange="[0.9.2,0.10)"
|
||||
ordering="AFTER"
|
||||
side="SERVER"
|
||||
|
||||
Reference in New Issue
Block a user