Skip to content

Commit

Permalink
Prevent players from exiting the spawn using barriers
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 committed Oct 9, 2023
1 parent 6c9c1a3 commit c4852f7
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,73 @@
package io.github.haykam821.minefield.game.map;

import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
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.math.Box;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.gen.chunk.ChunkGenerator;
import xyz.nucleoid.map_templates.BlockBounds;
import xyz.nucleoid.map_templates.MapTemplate;
import xyz.nucleoid.plasmid.game.world.generator.TemplateChunkGenerator;

public class MinefieldMap {
private static final BlockState AIR = Blocks.AIR.getDefaultState();

private final MapTemplate template;
private final BlockBounds start;
private final Box spawnBox;
private final Vec3d spawnPos;
private final BlockBounds end;
private final Vec3d guideTextPos;

public MinefieldMap(MapTemplate template, BlockBounds start, BlockBounds end, Vec3d guideTextPos) {
this.template = template;
this.start = start;
this.spawnBox = new Box(start.min().add(0, 1, 0), start.max().add(1, 3, 1));
this.spawnPos = start.centerBottom().add(0, 1, 0);
this.end = end;
this.guideTextPos = guideTextPos;
}

public void removeBarrierPerimeter(ServerWorld world) {
BlockPos.Mutable pos = new BlockPos.Mutable();

pos.setY(this.start.min().getY() + 2);

int barrierMinX = this.start.min().getX() - 1;
int barrierMaxX = this.start.max().getX() + 1;

int barrierMinZ = this.start.min().getZ() - 1;
int barrierMaxZ = this.start.max().getZ() + 1;

for (int x = barrierMinX; x <= barrierMaxX; x += 1) {
pos.setX(x);

pos.setZ(barrierMinZ);
world.setBlockState(pos, AIR);

pos.setZ(barrierMaxZ);
world.setBlockState(pos, AIR);
}

for (int z = barrierMinZ + 1; z <= barrierMaxZ - 1; z += 1) {
pos.setZ(z);

pos.setX(barrierMinX);
world.setBlockState(pos, AIR);

pos.setX(barrierMaxX);
world.setBlockState(pos, AIR);
}
}

public boolean isInSpawn(ServerPlayerEntity player) {
return this.spawnBox.contains(player.getPos());
}

public Vec3d getSpawnPos() {
return this.spawnPos;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,46 @@ public class MinefieldMapBuilder {
private static final BlockState MINES_FLOOR = Blocks.SANDSTONE.getDefaultState();
private static final BlockState MINE = Blocks.STONE_PRESSURE_PLATE.getDefaultState();
private static final BlockState END_FLOOR = Blocks.EMERALD_BLOCK.getDefaultState();
private static final BlockState BARRIER = Blocks.BARRIER.getDefaultState();

private final MinefieldConfig config;

public MinefieldMapBuilder(MinefieldConfig config) {
this.config = config;
}

private void placeBarrierPerimeter(MapTemplate template, BlockBounds start) {
BlockPos.Mutable pos = new BlockPos.Mutable();

pos.setY(start.min().getY() + 2);

int barrierMinX = start.min().getX() - 1;
int barrierMaxX = start.max().getX() + 1;

int barrierMinZ = start.min().getZ() - 1;
int barrierMaxZ = start.max().getZ() + 1;

for (int x = barrierMinX; x <= barrierMaxX; x += 1) {
pos.setX(x);

pos.setZ(barrierMinZ);
template.setBlockState(pos, BARRIER);

pos.setZ(barrierMaxZ);
template.setBlockState(pos, BARRIER);
}

for (int z = barrierMinZ + 1; z <= barrierMaxZ - 1; z += 1) {
pos.setZ(z);

pos.setX(barrierMinX);
template.setBlockState(pos, BARRIER);

pos.setX(barrierMaxX);
template.setBlockState(pos, BARRIER);
}
}

public MinefieldMap create() {
MapTemplate template = MapTemplate.createEmpty();
MinefieldMapConfig mapConfig = this.config.getMapConfig();
Expand Down Expand Up @@ -51,6 +84,8 @@ public MinefieldMap create() {
}
}

this.placeBarrierPerimeter(template, start);

return new MinefieldMap(template, start, end, guideTextPos);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,13 @@ private void enable() {

for (ServerPlayerEntity player : this.players) {
player.changeGameMode(GameMode.ADVENTURE);
this.map.spawn(player, this.world);

if (!this.singleplayer && this.statistics != null) {
this.statistics.forPlayer(player).increment(StatisticKeys.GAMES_PLAYED, 1);
}
}

this.map.removeBarrierPerimeter(this.world);
}

private void tick() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private void enable() {

private void tick() {
for (ServerPlayerEntity player : this.gameSpace.getPlayers()) {
if (this.map.isBelowPlatform(player)) {
if (!this.map.isInSpawn(player)) {
this.map.spawn(player, this.world);
}
}
Expand Down

0 comments on commit c4852f7

Please sign in to comment.