-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDemoBoxGame.java
More file actions
135 lines (120 loc) · 6.77 KB
/
DemoBoxGame.java
File metadata and controls
135 lines (120 loc) · 6.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package io.github.mattidragon.demobox;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.block.Blocks;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.entry.RegistryEntryList;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.structure.StructurePlacementData;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec2f;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.GameRules;
import net.minecraft.world.biome.BiomeKeys;
import net.minecraft.world.gen.chunk.FlatChunkGenerator;
import net.minecraft.world.gen.chunk.FlatChunkGeneratorConfig;
import net.minecraft.world.gen.chunk.FlatChunkGeneratorLayer;
import org.jetbrains.annotations.NotNull;
import xyz.nucleoid.fantasy.RuntimeWorldConfig;
import xyz.nucleoid.plasmid.game.*;
import xyz.nucleoid.plasmid.game.config.CustomValuesConfig;
import xyz.nucleoid.plasmid.game.config.GameConfig;
import xyz.nucleoid.plasmid.game.event.GamePlayerEvents;
import xyz.nucleoid.plasmid.game.manager.GameSpaceManager;
import xyz.nucleoid.plasmid.game.manager.ManagedGameSpace;
import xyz.nucleoid.plasmid.game.player.PlayerOffer;
import xyz.nucleoid.plasmid.game.player.PlayerOfferResult;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
public class DemoBoxGame {
public static final GameType<Settings> TYPE = GameType.register(DemoBox.id("demo_box"), Settings.CODEC, DemoBoxGame::open);
private final ServerWorld world;
private final GameSpace gameSpace;
private final Settings settings;
public DemoBoxGame(ServerWorld world, GameSpace gameSpace, Settings settings) {
this.world = world;
this.gameSpace = gameSpace;
this.settings = settings;
}
public static CompletableFuture<ManagedGameSpace> open(Settings settings) {
var config = new GameConfig<>(null, TYPE, null, null, null, null, CustomValuesConfig.empty(), settings);
return GameSpaceManager.get().open(config);
}
private static GameOpenProcedure open(GameOpenContext<Settings> context) {
return context.openWithWorld(createWorldConfig(context.server().getRegistryManager()), (activity, world) -> {
var instance = new DemoBoxGame(world, activity.getGameSpace(), context.config());
instance.setup();
activity.listen(GamePlayerEvents.OFFER, instance::onPlayerOffered);
activity.listen(GamePlayerEvents.LEAVE, instance::onPlayerLeave);
activity.listen(GamePlayerEvents.JOIN, instance::onPlayerJoin);
});
}
private void setup() {
world.getStructureTemplateManager()
.getTemplate(settings.structureId)
.ifPresent(template -> {
var size = template.getSize();
var pos = new BlockPos(size.getX() / -2, 1, size.getZ() / -2);
template.place(world, pos, pos, new StructurePlacementData(), world.random, 0);
});
var server = world.getServer();
var manager = server.getCommandFunctionManager();
for (var id : settings.functions) {
manager.getFunction(id).ifPresentOrElse(function -> manager.execute(function, new ServerCommandSource(server, Vec3d.ZERO, Vec2f.ZERO, world, 2, "DemoBox Setup", Text.literal("DemoBox Setup"), server, null).withSilent()),
() -> DemoBox.LOGGER.warn("Missing function: {}", id));
}
}
private void onPlayerLeave(ServerPlayerEntity player) {
if (gameSpace.getPlayers().stream().allMatch(player2 -> player2 != player)) {
gameSpace.close(GameCloseReason.FINISHED);
}
}
private void onPlayerJoin(ServerPlayerEntity player) {
player.sendMessage(Text.translatable("demobox.info.1").formatted(Formatting.GREEN, Formatting.BOLD));
player.sendMessage(Text.translatable("demobox.info.2").formatted(Formatting.WHITE));
player.sendMessage(Text.translatable("demobox.info.3").formatted(Formatting.WHITE));
player.sendMessage(Text.translatable("demobox.info.4").formatted(Formatting.WHITE));
var server = player.getServer();
var manager = server.getCommandFunctionManager();
for (var id : settings.onJoinFunctions) {
manager.getFunction(id).ifPresentOrElse(function -> manager.execute(function, new ServerCommandSource(server, player.getPos(), player.getRotationClient(), world, settings.permissionLevel(), player.getNameForScoreboard(), player.getDisplayName(), server, player).withSilent()),
() -> DemoBox.LOGGER.warn("Missing function: {}", id));
}
}
@NotNull
private PlayerOfferResult onPlayerOffered(PlayerOffer offer) {
return offer.accept(world, settings.playerPos);
}
@NotNull
private static RuntimeWorldConfig createWorldConfig(DynamicRegistryManager registryManager) {
var worldConfig = new RuntimeWorldConfig();
worldConfig.setFlat(true);
var generatorConfig = new FlatChunkGeneratorConfig(Optional.of(RegistryEntryList.of()), registryManager.get(RegistryKeys.BIOME).entryOf(BiomeKeys.PLAINS), List.of());
generatorConfig.getLayers().add(new FlatChunkGeneratorLayer(1, Blocks.BARRIER));
generatorConfig.updateLayerBlocks();
worldConfig.setGenerator(new FlatChunkGenerator(generatorConfig));
var disabledRules = Arrays.asList(GameRules.DO_DAYLIGHT_CYCLE, GameRules.DO_WEATHER_CYCLE, GameRules.DO_MOB_SPAWNING, GameRules.DO_PATROL_SPAWNING, GameRules.DO_INSOMNIA, GameRules.DO_TRADER_SPAWNING);
for (var booleanRuleKey : disabledRules) {
worldConfig.setGameRule(booleanRuleKey, false);
}
worldConfig.setSeed(1);
return worldConfig;
}
public record Settings(Identifier structureId, Vec3d playerPos, List<Identifier> functions, List<Identifier> onJoinFunctions, int permissionLevel) {
public static final Codec<Settings> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Identifier.CODEC.fieldOf("structureId").forGetter(Settings::structureId),
Vec3d.CODEC.fieldOf("playerPos").forGetter(Settings::playerPos),
Identifier.CODEC.listOf().fieldOf("functions").forGetter(Settings::functions),
Identifier.CODEC.listOf().fieldOf("onJoinFunctions").forGetter(Settings::onJoinFunctions),
Codec.INT.fieldOf("permissionLevel").forGetter(Settings::permissionLevel)
).apply(instance, Settings::new));
}
}