Skip to content

Commit

Permalink
Update to 1.19.4. Implement set biome and refreshChunk
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinDaGame committed May 15, 2023
1 parent 276ae69 commit 8ab3bd4
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 39 deletions.
4 changes: 2 additions & 2 deletions VoxelSniperForge/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ minecraft {
//
// Use non-default mappings at your own risk. They may not always work.
// Simply re-run your setup task after changing the mappings to update your workspace.
mappings channel: 'official', version: '1.19.2'
mappings channel: 'official', version: '1.19.4'

// accessTransformer = file('src/main/resources/META-INF/accesstransformer.cfg')

Expand Down Expand Up @@ -137,7 +137,7 @@ dependencies {
// Specify the version of Minecraft to use. If this is any group other than 'net.minecraft' it is assumed
// that the dep is a ForgeGradle 'patcher' dependency, and its patches will be applied.
// The userdev artifact is a special name and will get all sorts of transformations applied to it.
minecraft 'net.minecraftforge:forge:1.19.2-43.1.30'
minecraft 'net.minecraftforge:forge:1.19.4-45.0.63'
shadow "net.kyori:adventure-text-serializer-gson:4.11.0"
annotationProcessor "org.spongepowered:mixin:0.8.5:processor"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@
import net.minecraftforge.server.ServerLifecycleHooks;
import net.minecraftforge.server.permission.events.PermissionGatherEvent;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand All @@ -50,6 +50,7 @@ public class VoxelSniperForge implements IVoxelsniper {
private VoxelSniperConfiguration voxelSniperConfiguration;
private static VoxelSniperForge instance;

private final Map<UUID, ForgePlayer> players = new HashMap<>();
public static VoxelSniperForge getInstance() {
return instance;
}
Expand Down Expand Up @@ -119,10 +120,11 @@ public IPlayer getPlayer(String name) {
return getPlayer(ServerLifecycleHooks.getCurrentServer().getPlayerList().getPlayerByName(name));
}

public IPlayer getPlayer(ServerPlayer p) {
if (p == null) return null;
// TODO keep track of players
return new ForgePlayer(p);
public IPlayer getPlayer(@NotNull ServerPlayer p) {
if (this.players.get(p.getUUID()) != null) return this.players.get(p.getUUID());
ForgePlayer res = new ForgePlayer(p);
this.players.put(res.getUniqueId(), res);
return res;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.util.Map;

import net.minecraft.commands.arguments.blocks.BlockStateParser;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.RedStoneWireBlock;
import net.minecraft.world.level.block.state.BlockState;
Expand Down Expand Up @@ -47,11 +47,10 @@ public boolean matches(IBlockData newData) {
if (newData == null) {
return false;
}
if (!(newData instanceof ForgeBlockData)) {
if (!(newData instanceof ForgeBlockData forge)) {
return false;
}

ForgeBlockData forge = (ForgeBlockData) newData;
if (this.state.getBlock() != forge.state.getBlock()) {
return false;
}
Expand Down Expand Up @@ -117,7 +116,7 @@ public static ForgeBlockData createNewData(BlockMaterial material, String data)
}

StringReader reader = new StringReader(data);
BlockStateParser.BlockResult arg = BlockStateParser.parseForBlock(Registry.BLOCK, reader, false);
BlockStateParser.BlockResult arg = BlockStateParser.parseForBlock(BuiltInRegistries.BLOCK.asLookup(), reader, false);
Preconditions.checkArgument(!reader.canRead(), "Found trailing data: " + data);

blockData = arg.blockState();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.util.List;

import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
Expand Down Expand Up @@ -57,7 +58,8 @@ public BaseLocation getLocation() {

@Override
public IWorld getWorld() {
return new ForgeWorld(this.entity.getLevel());
//TODO check if this is correct
return new ForgeWorld((ServerLevel) this.entity.getLevel());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public final class ForgeLocation {
public static BlockPos toForgeBlockPos(BaseLocation location) {
return new BlockPos(location.getX(), location.getY(), location.getZ());
return new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ());
}
public static BaseLocation fromForgeBlockPos(ForgeWorld world, BlockPos pos) {
return new BaseLocation(world, pos.getX(), pos.getY(), pos.getZ());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.kevindagame.voxelsniperforge.world;

import com.github.kevindagame.VoxelSniper;
import com.github.kevindagame.util.brushOperation.BrushOperation;
import com.github.kevindagame.voxelsniper.biome.VoxelBiome;
import com.github.kevindagame.voxelsniper.block.IBlock;
Expand All @@ -12,36 +13,41 @@
import com.github.kevindagame.voxelsniper.world.IWorld;
import com.github.kevindagame.voxelsniperforge.block.ForgeBlock;
import com.github.kevindagame.voxelsniperforge.chunk.ForgeChunk;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.Registry;
import net.minecraft.core.QuartPos;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LightningBolt;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.chunk.PalettedContainer;
import net.minecraft.world.level.biome.BiomeResolver;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.chunk.ChunkStatus;
import net.minecraft.world.level.levelgen.Heightmap;

import net.minecraft.world.level.levelgen.structure.BoundingBox;
import org.apache.commons.lang3.mutable.MutableInt;
import org.jetbrains.annotations.NotNull;


import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;

public record ForgeWorld(@NotNull Level level) implements IWorld {
public record ForgeWorld(@NotNull ServerLevel level) implements IWorld {

public Level getLevel() {
return level;
}

@Override
public IBlock getBlock(BaseLocation location) {
if (location.getWorld() != this)
throw new IllegalArgumentException("location doesn't belong to this World");
return new ForgeBlock(location, new BlockPos(location.getX(), location.getY(), location.getZ()));
return new ForgeBlock(location, new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ()));
}

@Override
Expand All @@ -61,7 +67,7 @@ public int getMaxWorldHeight() {

@Override
public IChunk getChunkAtLocation(int x, int z) {
return new ForgeChunk(level.getChunkAt(new BlockPos(x, 0, z)), this);
return new ForgeChunk(level.getChunkAt(new BlockPos(x * 16, 0, z * 16)), this);
}

@Override
Expand All @@ -71,8 +77,8 @@ public List<IEntity> getNearbyEntities(BaseLocation location, double x, double y

@Override
public void refreshChunk(int x, int z) {
//Does this work?
level.getChunkAt(new BlockPos(x, 0, z)).setUnsaved(true);
var chunk = level.getChunk(x, z, ChunkStatus.FULL, false);
level.getChunkSource().chunkMap.resendBiomesForChunks(List.of(chunk));
}

@Override
Expand All @@ -92,29 +98,40 @@ public String getName() {
public void spawn(BaseLocation location, VoxelEntityType entity) {
//TODO test this
var tag = EntityType.byString(entity.getKey());
if (tag.isPresent()){
if (tag.isPresent()) {
Entity created = tag.get().create(level);
assert created != null;
created.setPos(location.getX(), location.getY(), location.getZ());
level.addFreshEntity(created);
}
else{
} else {
throw new IllegalArgumentException("Invalid entity type");
}
}

//code from mojang itself: FillBiomeCommand::makeResolver. idk what it does but it works
private static BiomeResolver makeResolver(MutableInt p_262615_, ChunkAccess p_262698_, BoundingBox p_262622_, Holder<Biome> p_262705_, Predicate<Holder<Biome>> p_262695_) {
return (p_262550_, p_262551_, p_262552_, p_262553_) -> {
int i = QuartPos.toBlock(p_262550_);
int j = QuartPos.toBlock(p_262551_);
int k = QuartPos.toBlock(p_262552_);
Holder<Biome> holder = p_262698_.getNoiseBiome(p_262550_, p_262551_, p_262552_);
if (p_262622_.isInside(i, j, k) && p_262695_.test(holder)) {
p_262615_.increment();
return p_262705_;
} else {
return holder;
}
};
}

@Override
public void setBiome(int x, int y, int z, VoxelBiome selectedBiome) {
//TODO test this
var chunk = ((ForgeChunk)getChunkAtLocation(x, z)).getChunk();
var biomes = (PalettedContainer<Holder<Biome>>) chunk.getSection(chunk.getSectionIndex(y)).getBiomes();
biomes.getAndSetUnchecked(
x & 3, y & 3, z & 3,
level.registryAccess().registry(Registry.BIOME_REGISTRY)
.orElseThrow()
.getHolderOrThrow(ResourceKey.create(Registry.BIOME_REGISTRY, new ResourceLocation(selectedBiome.getKey())))
);
chunk.setUnsaved(true);
var biome = level.registryAccess().registry(Registries.BIOME)
.orElseThrow()
.getHolderOrThrow(ResourceKey.create(Registries.BIOME, new ResourceLocation(selectedBiome.getKey())));
var chunkAccess = level.getChunk(new BlockPos(x, y, z));
chunkAccess.fillBiomesFromNoise(makeResolver(new MutableInt(), chunkAccess, new BoundingBox(x, y, z, x, y, z), biome, biomeHolder -> true), level.getChunkSource().randomState().sampler());
chunkAccess.setUnsaved(true);
}

@Override
Expand All @@ -140,6 +157,8 @@ public Iterator<IBlock> getBlockIterator(VoxelVector origin, VoxelVector directi

@Override
public VoxelBiome getBiome(BaseLocation location) {
throw new UnsupportedOperationException("Not implemented yet");
var biome = level.getBiome(new BlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ()));
var biomeId = biome.unwrapKey().get().location();
return new VoxelBiome(biomeId.getNamespace(), biomeId.getPath(), VoxelSniper.voxelsniper.getVersion());
}
}

0 comments on commit 8ab3bd4

Please sign in to comment.