11package io .github .mattidragon .demobox ;
22
3- import com .mojang .serialization .MapCodec ;
4- import com .mojang .serialization .codecs .RecordCodecBuilder ;
53import net .minecraft .ChatFormatting ;
64import net .minecraft .commands .CommandSourceStack ;
5+ import net .minecraft .commands .functions .CommandFunction ;
76import net .minecraft .core .BlockPos ;
87import net .minecraft .core .Holder ;
98import net .minecraft .core .HolderSet ;
109import net .minecraft .core .RegistryAccess ;
1110import net .minecraft .network .chat .Component ;
12- import net .minecraft .resources .Identifier ;
1311import net .minecraft .server .level .ServerLevel ;
1412import net .minecraft .server .level .ServerPlayer ;
1513import net .minecraft .server .permissions .LevelBasedPermissionSet ;
4341import java .util .concurrent .CompletableFuture ;
4442
4543public class DemoBoxGame {
46- public static final GameType <Settings > TYPE = GameTypes .register (DemoBox .id ("demo_box" ), Settings .CODEC , DemoBoxGame ::open );
44+ public static final GameType <DemoConfig > TYPE = GameTypes .register (DemoBox .id ("demo_box" ), DemoConfig .CODEC , DemoBoxGame ::open );
4745
4846 private final ServerLevel world ;
4947 private final GameSpace gameSpace ;
50- private final Settings settings ;
48+ private final DemoConfig config ;
5149
52- public DemoBoxGame (ServerLevel world , GameSpace gameSpace , Settings settings ) {
50+ public DemoBoxGame (ServerLevel world , GameSpace gameSpace , DemoConfig config ) {
5351 this .world = world ;
5452 this .gameSpace = gameSpace ;
55- this .settings = settings ;
53+ this .config = config ;
5654 }
5755
5856 public static void register () {
5957 }
6058
61- public static CompletableFuture <GameSpace > open (Settings settings ) {
62- var config = new GameConfig <>(TYPE , null , null , null , null , CustomValuesConfig .empty (), settings );
59+ public static CompletableFuture <GameSpace > open (DemoConfig demoConfig ) {
60+ var config = new GameConfig <>(TYPE , null , null , null , null , CustomValuesConfig .empty (), demoConfig );
6361 return GameSpaceManager .get ().open (Holder .direct (config ));
6462 }
6563
66- private static GameOpenProcedure open (GameOpenContext <Settings > context ) {
64+ private static GameOpenProcedure open (GameOpenContext <DemoConfig > context ) {
6765 return context .openWithLevel (createLevelConfig (context .server ().registryAccess ()), (activity , world ) -> {
6866 var instance = new DemoBoxGame (world , activity .getGameSpace (), context .config ());
6967 instance .setup ();
@@ -83,17 +81,17 @@ private static GameOpenProcedure open(GameOpenContext<Settings> context) {
8381
8482 private void setup () {
8583 world .getStructureManager ()
86- .get (settings . structureId )
84+ .get (config . structure () )
8785 .ifPresent (template -> {
8886 var size = template .getSize ();
8987 var pos = new BlockPos (size .getX () / -2 , 1 , size .getZ () / -2 );
9088 template .placeInWorld (world , pos , pos , new StructurePlaceSettings (), world .getRandom (), 0 );
9189 });
92- executeFunctions ( settings . functions , null );
90+ executeCommands ( config . setupCommands () , null );
9391 }
9492
9593 private void onPlayerLeave (ServerPlayer player ) {
96- if (gameSpace .getPlayers ().stream ().allMatch (player2 -> player2 ! = player )) {
94+ if (gameSpace .getPlayers ().stream ().allMatch (player2 -> player2 = = player )) {
9795 gameSpace .close (GameCloseReason .FINISHED );
9896 }
9997 }
@@ -103,7 +101,7 @@ private void onPlayerJoin(ServerPlayer player) {
103101 player .sendSystemMessage (Component .translatable ("demobox.info.2" ).withStyle (ChatFormatting .WHITE ));
104102 player .sendSystemMessage (Component .translatable ("demobox.info.3" ).withStyle (ChatFormatting .WHITE ));
105103 player .sendSystemMessage (Component .translatable ("demobox.info.4" ).withStyle (ChatFormatting .WHITE ));
106- executeFunctions ( settings . playerFunctions , player );
104+ executeCommands ( config . joinCommands () , player );
107105 }
108106
109107 private Component onJoinMessage (ServerPlayer player , @ Nullable Component currentText , Component defaultText ) {
@@ -119,21 +117,16 @@ private JoinOfferResult onPlayerOffered(JoinOffer offer) {
119117 }
120118
121119 private JoinAcceptorResult onPlayerAccepted (JoinAcceptor joinAcceptor ) {
122- return joinAcceptor .teleport (world , settings . playerPos );
120+ return joinAcceptor .teleport (world , config . spawnPos () );
123121 }
124122
125- private void executeFunctions ( List < Identifier > functions , Entity entity ) {
123+ private void executeCommands ( String commands , Entity entity ) {
126124 var server = world .getServer ();
127- var manager = server .getFunctions ();
128- for (var id : functions ) {
129- manager .get (id ).ifPresentOrElse (
130- function -> manager .execute (
131- function ,
132- new CommandSourceStack (server , Vec3 .ZERO , Vec2 .ZERO , world , LevelBasedPermissionSet .GAMEMASTER , "DemoBox Setup" , Component .literal ("DemoBox Setup" ), server , entity ).withSuppressedOutput ()
133- ),
134- () -> DemoBox .LOGGER .warn ("Missing function: {}" , id )
135- );
136- }
125+ var source = new CommandSourceStack (server , Vec3 .ZERO , Vec2 .ZERO , world , LevelBasedPermissionSet .GAMEMASTER , "DemoBox Setup" , Component .literal ("DemoBox Setup" ), server , entity ).withSuppressedOutput ();
126+ var dispatcher = source .dispatcher ();
127+
128+ var function = CommandFunction .fromLines (DemoBox .id ("/virtual_setup_function" ), dispatcher , source , commands .lines ().toList ());
129+ server .getFunctions ().execute (function , source );
137130 }
138131
139132 @ NotNull
@@ -152,14 +145,4 @@ private static RuntimeLevelConfig createLevelConfig(RegistryAccess registryManag
152145 worldConfig .setSeed (1 );
153146 return worldConfig ;
154147 }
155-
156- public record Settings (Identifier structureId , Vec3 playerPos , List <Identifier > functions ,
157- List <Identifier > playerFunctions ) {
158- public static final MapCodec <Settings > CODEC = RecordCodecBuilder .mapCodec (instance -> instance .group (
159- Identifier .CODEC .fieldOf ("structureId" ).forGetter (Settings ::structureId ),
160- Vec3 .CODEC .fieldOf ("playerPos" ).forGetter (Settings ::playerPos ),
161- Identifier .CODEC .listOf ().fieldOf ("functions" ).forGetter (Settings ::functions ),
162- Identifier .CODEC .listOf ().fieldOf ("playerFunctions" ).forGetter (Settings ::playerFunctions )
163- ).apply (instance , Settings ::new ));
164- }
165148}
0 commit comments