Skip to content

Commit

Permalink
map loading
Browse files Browse the repository at this point in the history
  • Loading branch information
ellieisjelly committed Jan 2, 2024
1 parent f289968 commit 4076c8f
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package me.ellieis;
package me.ellieis.Sabotage;

import me.ellieis.game.SabotageConfig;
import me.ellieis.game.SabotageLobby;
import me.ellieis.Sabotage.game.SabotageConfig;
import me.ellieis.Sabotage.game.SabotageGame;
import net.fabricmc.api.ModInitializer;

import net.minecraft.util.Identifier;
Expand All @@ -13,7 +13,7 @@ public class Sabotage implements ModInitializer {
public static final String MOD_ID = "sabotage";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
private static final Identifier SABOTAGE_ID = new Identifier(MOD_ID, "sabotage");
public static final GameType GAME_TYPE = GameType.register(SABOTAGE_ID, SabotageConfig.CODEC, SabotageLobby::Open);
public static final GameType GAME_TYPE = GameType.register(SABOTAGE_ID, SabotageConfig.CODEC, SabotageGame::Open);

@Override
public void onInitialize() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.ellieis.game;
package me.ellieis.Sabotage.game;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,51 @@
package me.ellieis.game;
package me.ellieis.Sabotage.game;

import net.minecraft.block.Blocks;
import me.ellieis.Sabotage.game.map.SabotageMap;
import me.ellieis.Sabotage.game.map.SabotageMapBuilder;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.GameMode;
import xyz.nucleoid.fantasy.RuntimeWorldConfig;
import xyz.nucleoid.map_templates.MapTemplate;
import xyz.nucleoid.plasmid.game.GameOpenContext;
import xyz.nucleoid.plasmid.game.GameOpenProcedure;
import xyz.nucleoid.plasmid.game.GameSpace;
import xyz.nucleoid.plasmid.game.event.GamePlayerEvents;
import xyz.nucleoid.plasmid.game.player.PlayerOffer;
import xyz.nucleoid.plasmid.game.player.PlayerOfferResult;
import xyz.nucleoid.plasmid.game.world.generator.TemplateChunkGenerator;

public class SabotageLobby {
public class SabotageGame {
private final SabotageConfig config;
private final GameSpace gameSpace;
private final SabotageMap map;
private final ServerWorld world;
public SabotageLobby(SabotageConfig config, GameSpace gameSpace, ServerWorld world) {
public SabotageGame(SabotageConfig config, GameSpace gameSpace, SabotageMap map, ServerWorld world) {
this.config = config;
this.gameSpace = gameSpace;
this.map = map;
this.world = world;
}
public static GameOpenProcedure Open(GameOpenContext<SabotageConfig> context) {
SabotageConfig config = context.game().config();
// create a very simple map with a stone block at (0; 64; 0)
MapTemplate template = MapTemplate.createEmpty();
template.setBlockState(new BlockPos(0, 64, 0), Blocks.STONE.getDefaultState());

// create a chunk generator that will generate from this template that we just created
TemplateChunkGenerator generator = new TemplateChunkGenerator(context.server(), template);

MinecraftServer server = context.server();
// set up how the world that this minigame will take place in should be constructed
SabotageMap map = SabotageMapBuilder.build(server, new Identifier("sabotage:lobby"));
RuntimeWorldConfig worldConfig = new RuntimeWorldConfig()
.setGenerator(generator)
.setGenerator(map.asChunkGenerator(server))
.setTimeOfDay(6000);

return context.openWithWorld(worldConfig, (activity, world) -> {
SabotageLobby game = new SabotageLobby(config, activity.getGameSpace(), world);
SabotageGame game = new SabotageGame(config, activity.getGameSpace(), map, world);
activity.listen(GamePlayerEvents.OFFER, game::onOffer);
activity.listen(GamePlayerEvents.JOIN, game::onJoin);
});
}

private void onJoin(ServerPlayerEntity plr) {
this.map.spawnEntity(this.world, plr);
}
private PlayerOfferResult onOffer(PlayerOffer offer) {
ServerPlayerEntity plr = offer.player();
return offer.accept(this.world, new Vec3d(0.0, 66.0, 0.0)).and(() -> {
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/me/ellieis/Sabotage/game/map/SabotageMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package me.ellieis.Sabotage.game.map;

import net.minecraft.entity.Entity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.gen.chunk.ChunkGenerator;

import xyz.nucleoid.map_templates.MapTemplate;
import xyz.nucleoid.map_templates.TemplateRegion;
import xyz.nucleoid.plasmid.game.world.generator.TemplateChunkGenerator;

public class SabotageMap {
private final MapTemplate template;
private final TemplateRegion spawn;

public SabotageMap(MapTemplate template) {
this.template = template;
this.spawn = template.getMetadata().getFirstRegion("spawn");
}

public MapTemplate getTemplate() {
return this.template;
}

public TemplateRegion getSpawn() {
return this.spawn;
}

public void spawnEntity(ServerWorld world, Entity entity) {
float yaw = this.spawn.getData().getFloat("Rotation");
Vec3d pos = this.spawn.getBounds().center();
if (entity instanceof ServerPlayerEntity) {
ServerPlayerEntity plr = (ServerPlayerEntity) entity;
plr.teleport(world, pos.getX(), pos.getY(), pos.getZ(), yaw, 0);
} else {
entity.teleport(pos.getX(), pos.getY(), pos.getZ());
entity.setYaw(yaw);
}
}

public ChunkGenerator asChunkGenerator(MinecraftServer server) {
return new TemplateChunkGenerator(server, this.template);
}
}
21 changes: 21 additions & 0 deletions src/main/java/me/ellieis/Sabotage/game/map/SabotageMapBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package me.ellieis.Sabotage.game.map;

import net.minecraft.server.MinecraftServer;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import xyz.nucleoid.map_templates.MapTemplate;
import xyz.nucleoid.map_templates.MapTemplateSerializer;
import xyz.nucleoid.plasmid.game.GameOpenException;

import java.io.IOException;

public class SabotageMapBuilder {
public static SabotageMap build(MinecraftServer server, Identifier identifier) {
try {
MapTemplate template = MapTemplateSerializer.loadFromResource(server, identifier);
return new SabotageMap(template);
} catch(IOException exception) {
throw new GameOpenException(Text.of("Failed to load map " + identifier), exception);
}
}
}
Binary file not shown.
2 changes: 1 addition & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"environment": "*",
"entrypoints": {
"main": [
"me.ellieis.Sabotage"
"me.ellieis.Sabotage.Sabotage"
]
},
"depends": {
Expand Down

0 comments on commit 4076c8f

Please sign in to comment.