Skip to content

Commit

Permalink
Polish translation, performance and worldgen fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Jun 2, 2023
1 parent 35540ae commit 1c9e7e8
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import net.minecraft.item.ItemStack;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.entry.RegistryElementCodec;
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.util.dynamic.Codecs;
import net.minecraft.world.dimension.DimensionOptions;
import xyz.nucleoid.codecs.MoreCodecs;
Expand All @@ -14,11 +16,11 @@
import java.util.List;
import java.util.Optional;

public record ItemHuntConfig(PlayerConfig playerConfig, RegistryKey<DimensionOptions> dimensionOptionsKey, int duration,
public record ItemHuntConfig(PlayerConfig playerConfig, DimensionOptions dimensionOptions, int duration,
Optional<String> statisticBundleNamespace, boolean crafting, Optional<List<ItemStack>> startItems) {
public static final Codec<ItemHuntConfig> CODEC = RecordCodecBuilder.create(instance -> instance.group(
PlayerConfig.CODEC.fieldOf("players").forGetter(ItemHuntConfig::playerConfig),
RegistryKey.createCodec(RegistryKeys.DIMENSION).optionalFieldOf("dimension_options", DimensionOptions.OVERWORLD).forGetter(ItemHuntConfig::dimensionOptionsKey),
DimensionOptions.CODEC.fieldOf("dimension_options").forGetter(ItemHuntConfig::dimensionOptions),
Codecs.POSITIVE_INT.optionalFieldOf("duration", 180).forGetter(ItemHuntConfig::duration),
GameStatisticBundle.NAMESPACE_CODEC.optionalFieldOf("statistic_bundle").forGetter(ItemHuntConfig::statisticBundleNamespace),
Codec.BOOL.optionalFieldOf("crafting", false).forGetter(ItemHuntConfig::crafting),
Expand Down
40 changes: 28 additions & 12 deletions src/main/java/io/github/jerozgen/itemhunt/game/ItemHuntGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,21 @@
import net.minecraft.registry.RegistryKeys;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.network.SpawnLocating;
import net.minecraft.server.world.ChunkTicketType;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Unit;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.random.Random;
import net.minecraft.world.Heightmap;
import net.minecraft.world.border.WorldBorder;
import net.minecraft.world.border.WorldBorderListener;
import net.minecraft.world.dimension.DimensionTypes;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.fantasy.RuntimeWorldConfig;
import xyz.nucleoid.fantasy.util.VoidChunkGenerator;
import xyz.nucleoid.plasmid.game.GameActivity;
import xyz.nucleoid.plasmid.game.GameOpenContext;
import xyz.nucleoid.plasmid.game.GameOpenProcedure;
Expand All @@ -35,29 +40,42 @@ public record ItemHuntGame(ItemHuntConfig config, GameSpace gameSpace, ServerWor

public static GameOpenProcedure open(GameOpenContext<ItemHuntConfig> context) {
var config = context.config();
var dimensionOptions = context.server().getRegistryManager().get(RegistryKeys.DIMENSION).get(config.dimensionOptionsKey());
if (dimensionOptions == null)
throw new NullPointerException("Couldn't find %s dimension options from %s game config"
.formatted(config.dimensionOptionsKey().getValue(), context.game().source()));
var dimensionOptions = config.dimensionOptions();

var worldConfig = new RuntimeWorldConfig()
.setDimensionType(dimensionOptions.dimensionTypeEntry())
.setGenerator(dimensionOptions.chunkGenerator())
.setSeed(context.server().getOverworld().getRandom().nextLong());
return context.openWithWorld(worldConfig, (activity, world) -> {
.setSeed(Random.create().nextLong());

var waitingWorldConfig = new RuntimeWorldConfig()
.setDimensionType(DimensionTypes.OVERWORLD)
.setGenerator(new VoidChunkGenerator(context.server().getRegistryManager().get(RegistryKeys.BIOME)))
.setWorldConstructor(LazyWaitingWorld::new);


return context.open((activity) -> {
var gameSpace = activity.getGameSpace();
var t = System.currentTimeMillis();
var waitingWorld = gameSpace.getWorlds().add(waitingWorldConfig);
System.out.println("WAIT: " + (System.currentTimeMillis() - t));
t = System.currentTimeMillis();
var world = gameSpace.getWorlds().add(worldConfig);
System.out.println("REG: " + (System.currentTimeMillis() - t));

var statistics = config.statisticBundleNamespace()
.map(value -> gameSpace.getStatistics().bundle(value))
.orElse(null);
var game = new ItemHuntGame(config, gameSpace, world, findSpawnPos(world), statistics);
activity.listen(GameActivityEvents.CREATE, () -> {
var waitingPhase = new ItemHuntWaitingPhase(game);
var waitingPhase = new ItemHuntWaitingPhase(game, waitingWorld);
game.gameSpace.setActivity(waitingPhase::setup);
});
});
}

public ItemHuntGame {
world.getWorldBorder().addListener(getWorldBorderListener());
world.getChunkManager().addTicket(ChunkTicketType.START, new ChunkPos(spawnPos), 3, Unit.INSTANCE);
}

public void setup(GameActivity activity) {
Expand Down Expand Up @@ -93,12 +111,10 @@ private static BlockPos findSpawnPos(ServerWorld world) {
var chunkPos = new ChunkPos(spawnPos);
var x = chunkPos.getStartX() + 8;
var z = chunkPos.getStartZ() + 8;
int y = chunkManager.getChunkGenerator().getSpawnHeight(world);
if (y < world.getBottomY())
y = world.getTopY(Heightmap.Type.WORLD_SURFACE, x, z);
int y = chunkManager.getChunkGenerator().getHeightOnGround(x, z, Heightmap.Type.MOTION_BLOCKING, world, chunkManager.getNoiseConfig());
spawnPos = new BlockPos(x, y, z);

var dx = 0;
/*var dx = 0;
var dz = 0;
var stepX = 0;
var stepZ = -1;
Expand All @@ -117,7 +133,7 @@ private static BlockPos findSpawnPos(ServerWorld world) {
}
dx += stepX;
dz += stepZ;
}
}*/

return spawnPos;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.github.jerozgen.itemhunt.game;

import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.RegistryKeys;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.World;
import net.minecraft.world.biome.BiomeKeys;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.ChunkStatus;
import net.minecraft.world.chunk.EmptyChunk;
import net.minecraft.world.chunk.WorldChunk;
import org.jetbrains.annotations.Nullable;
import xyz.nucleoid.fantasy.RuntimeWorld;
import xyz.nucleoid.fantasy.RuntimeWorldConfig;

public class LazyWaitingWorld extends RuntimeWorld {
private final WorldChunk chunk;

protected LazyWaitingWorld(MinecraftServer server, RegistryKey<World> registryKey, RuntimeWorldConfig config, Style style) {
super(server, registryKey, config, style);
this.chunk = new EmptyChunk(this, ChunkPos.ORIGIN, server.getRegistryManager().get(RegistryKeys.BIOME).getEntry(BiomeKeys.THE_VOID).get()) {
@Override
public BlockState getBlockState(BlockPos pos) {
return Blocks.BARRIER.getDefaultState();
}
};
}

@Nullable
@Override
public Chunk getChunk(int chunkX, int chunkZ, ChunkStatus leastStatus, boolean create) {
return this.chunk;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import io.github.jerozgen.itemhunt.game.ItemHuntTexts;
import net.minecraft.network.packet.s2c.play.WorldBorderInitializeS2CPacket;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.util.math.ChunkSectionPos;
import net.minecraft.world.GameMode;
import xyz.nucleoid.plasmid.game.GameActivity;
import xyz.nucleoid.plasmid.game.GameResult;
Expand All @@ -14,8 +17,16 @@
import xyz.nucleoid.plasmid.game.player.PlayerOfferResult;

public class ItemHuntWaitingPhase extends ItemHuntPhase {
public ItemHuntWaitingPhase(ItemHuntGame game) {
private final ServerWorld waitingWorld;
private boolean hasMoved = false;
private long spawnChunk;
private ItemHuntActivePhase delayedPhase;

public ItemHuntWaitingPhase(ItemHuntGame game, ServerWorld waitingWorld) {
super(game);
this.waitingWorld = waitingWorld;
this.spawnChunk = new ChunkPos(game.spawnPos()).toLong();
System.out.println(this.game.spawnPos());
}

@Override
Expand All @@ -26,6 +37,26 @@ protected void setupPhase(GameActivity activity) {
activity.listen(GamePlayerEvents.OFFER, this::offerPlayer);
activity.listen(GamePlayerEvents.ADD, this::addPlayer);
activity.listen(GameActivityEvents.REQUEST_START, this::requestStart);
activity.listen(GameActivityEvents.TICK, this::tick);
}

private void tick() {
if (this.hasMoved) {
return;
}

if (this.isMainWorldReady()) {
for (var x : this.game.gameSpace().getPlayers()) {
x.teleport(this.game.world(), this.game.spawnPos().getX(), this.game.spawnPos().getY(), this.game.spawnPos().getZ(), 0, 0);
x.networkHandler.sendPacket(new WorldBorderInitializeS2CPacket(game.world().getWorldBorder()));

}
this.hasMoved = true;

if (this.delayedPhase != null) {
game.gameSpace().setActivity(this.delayedPhase::setup);
}
}
}

private void start() {
Expand All @@ -36,8 +67,17 @@ private void start() {
worldBorder.setWarningBlocks(-100);
}

private boolean isMainWorldReady() {
return this.game.world().isChunkLoaded(this.spawnChunk);
//return this.game.world().getChunkManager().getLoadedChunkCount() >= 36;
}

private ServerWorld getActiveWorld() {
return this.isMainWorldReady() ? this.game.world() : this.waitingWorld;
}

private PlayerOfferResult offerPlayer(PlayerOffer offer) {
return offer.accept(game.world(), game.spawnPos().toCenterPos()).and(() -> {
return offer.accept(this.getActiveWorld(), game.spawnPos().toCenterPos()).and(() -> {
offer.player().sendMessage(ItemHuntTexts.description(game), false);
offer.player().changeGameMode(GameMode.ADVENTURE);
});
Expand All @@ -49,7 +89,12 @@ private void addPlayer(ServerPlayerEntity player) {

private GameResult requestStart() {
var activePhase = new ItemHuntActivePhase(game);
game.gameSpace().setActivity(activePhase::setup);
if (this.hasMoved) {
game.gameSpace().setActivity(activePhase::setup);
} else {
this.delayedPhase = activePhase;
}

return GameResult.ok();
}
}
12 changes: 11 additions & 1 deletion src/main/resources/data/itemhunt/games/itemhunt.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,17 @@
"ready_seconds": 20
}
},
"dimension_options": "minecraft:overworld",
"dimension_options": {
"type": "minecraft:overworld",
"generator": {
"type": "minecraft:noise",
"biome_source": {
"type": "minecraft:multi_noise",
"preset": "minecraft:overworld"
},
"settings": "minecraft:overworld"
}
},
"statistic_bundle": "itemhunt",
"start_items": [
{"id": "minecraft:iron_axe", "Count": 1, "tag": {"Unbreakable": true}},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,17 @@
}
},
"duration": 360,
"dimension_options": "minecraft:overworld",
"dimension_options": {
"type": "minecraft:overworld",
"generator": {
"type": "minecraft:noise",
"biome_source": {
"type": "minecraft:multi_noise",
"preset": "minecraft:overworld"
},
"settings": "minecraft:overworld"
}
},
"statistic_bundle": "itemhunt_with_crafting",
"crafting": true,
"start_items": [
Expand Down
26 changes: 26 additions & 0 deletions src/main/resources/data/itemhunt/lang/pl_pl.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"gameType.itemhunt.itemhunt": "Item Hunt",

"game.itemhunt.itemhunt": "Item Hunt",
"game.itemhunt.itemhunt_with_crafting": "Item Hunt z wytwarzaniem",

"game.itemhunt.desc1": "Zdobądź jak najwięcej",
"game.itemhunt.desc2": "unikalnych przedmiotów!",

"statistic.bundle.itemhunt": "Item Hunt",
"statistic.bundle.itemhunt_with_crafting": "Item Hunt z wytwarzaniem",

"statistic.itemhunt.items_obtained": "Zdobytych przedmiotów",

"text.itemhunt.desc": "Zdobądź jak najwięcej unikalnych przedmiotów!",
"text.itemhunt.item_obtained": "+ %s",
"text.itemhunt.bossbar.time_left": "Pozostało %s",
"text.itemhunt.end.singleplayer.to_player": "Koniec! Zdobyłeś %s unikalnych przedmiotów",
"text.itemhunt.end.singleplayer.to_spectator": "Koniec! %s zdobył %s unikalnych przedmiotów",
"text.itemhunt.end.multiplayer.winner": "%s wygrał z %s unikalnymi przedmiotami",
"text.itemhunt.end.multiplayer.winners": "%s wygrali z %s unikalnymi przedmiotami",
"text.itemhunt.end.gui.title": "Przedmioty %s",
"text.itemhunt.end.gui.previous_page": "Poprzednia Strona",
"text.itemhunt.end.gui.next_page": "Następna Strona",
"text.itemhunt.error.finished": "Gra się zakończyła!"
}

0 comments on commit 1c9e7e8

Please sign in to comment.