Add server versions of wrapped worlds

This commit is contained in:
tterrag 2020-03-29 14:39:51 -04:00
parent 9f2dcc5635
commit e1e3220b68
4 changed files with 193 additions and 9 deletions

View file

@ -0,0 +1,56 @@
package com.simibubi.create.foundation.utility;
import java.util.HashMap;
import java.util.function.Predicate;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.server.ServerWorld;
public class PlacementSimulationServerWorld extends WrappedServerWorld {
public HashMap<BlockPos, BlockState> blocksAdded;
public PlacementSimulationServerWorld(ServerWorld wrapped) {
super(wrapped);
blocksAdded = new HashMap<>();
}
public void clear() {
blocksAdded.clear();
}
@Override
public boolean setBlockState(BlockPos pos, BlockState newState, int flags) {
blocksAdded.put(pos, newState);
return true;
}
@Override
public boolean setBlockState(BlockPos pos, BlockState state) {
return setBlockState(pos, state, 0);
}
@Override
public boolean hasBlockState(BlockPos pos, Predicate<BlockState> condition) {
return condition.test(getBlockState(pos));
}
@Override
public boolean isBlockPresent(BlockPos pos) {
return true;
}
@Override
public boolean isAreaLoaded(BlockPos center, int range) {
return true;
}
@Override
public BlockState getBlockState(BlockPos pos) {
if (blocksAdded.containsKey(pos))
return blocksAdded.get(pos);
return Blocks.AIR.getDefaultState();
}
}

View file

@ -0,0 +1,131 @@
package com.simibubi.create.foundation.utility;
import java.util.Collections;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.fluid.Fluid;
import net.minecraft.item.crafting.RecipeManager;
import net.minecraft.scoreboard.ServerScoreboard;
import net.minecraft.tags.NetworkTagManager;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.server.ServerTickList;
import net.minecraft.world.server.ServerWorld;
import net.minecraft.world.storage.MapData;
public class WrappedServerWorld extends ServerWorld {
protected ServerWorld world;
public WrappedServerWorld(ServerWorld world) {
super(world.getServer(), world.getServer().getBackgroundExecutor(), world.getSaveHandler(), world.getWorldInfo(), world.getDimension().getType(), world.getProfiler(), null);
this.world = world;
}
@Override
public World getWorld() {
return world;
}
@Override
public int getLight(BlockPos pos) {
return 15;
}
@Override
public void notifyBlockUpdate(BlockPos pos, BlockState oldState, BlockState newState, int flags) {
world.notifyBlockUpdate(pos, oldState, newState, flags);
}
@Override
public ServerTickList<Block> getPendingBlockTicks() {
return world.getPendingBlockTicks();
}
@Override
public ServerTickList<Fluid> getPendingFluidTicks() {
return world.getPendingFluidTicks();
}
@Override
public void playEvent(PlayerEntity player, int type, BlockPos pos, int data) {
}
@Override
public List<ServerPlayerEntity> getPlayers() {
return Collections.emptyList();
}
@Override
public void playSound(PlayerEntity player, double x, double y, double z, SoundEvent soundIn, SoundCategory category,
float volume, float pitch) {
}
@Override
public void playMovingSound(PlayerEntity p_217384_1_, Entity p_217384_2_, SoundEvent p_217384_3_,
SoundCategory p_217384_4_, float p_217384_5_, float p_217384_6_) {
}
@Override
public Entity getEntityByID(int id) {
return null;
}
@Override
public MapData getMapData(String mapName) {
return null;
}
@Override
public boolean addEntity(Entity entityIn) {
entityIn.setWorld(world);
return world.addEntity(entityIn);
}
@Override
public void registerMapData(MapData mapDataIn) {
}
@Override
public int getNextMapId() {
return 0;
}
@Override
public void sendBlockBreakProgress(int breakerId, BlockPos pos, int progress) {
}
@Override
public ServerScoreboard getScoreboard() {
return world.getScoreboard();
}
@Override
public RecipeManager getRecipeManager() {
return world.getRecipeManager();
}
@Override
public NetworkTagManager getTags() {
return world.getTags();
}
@Override
public int getMaxHeight() {
return 256;
}
@Override
public Biome getGeneratorStoredBiome(int p_225604_1_, int p_225604_2_, int p_225604_3_) {
return world.getGeneratorStoredBiome(p_225604_1_, p_225604_2_, p_225604_3_);
}
}

View file

@ -19,7 +19,6 @@ import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.storage.MapData;
//TODO 1.15 this needs to extend ServerWorld
public class WrappedWorld extends World {
protected World world;

View file

@ -1,8 +1,6 @@
package com.simibubi.create.modules.gardens;
import java.util.Random;
import com.simibubi.create.foundation.utility.PlacementSimulationWorld;
import com.simibubi.create.foundation.utility.PlacementSimulationServerWorld;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
@ -13,7 +11,7 @@ import net.minecraft.item.Item;
import net.minecraft.item.ItemUseContext;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
public class TreeFertilizerItem extends Item {
@ -32,7 +30,7 @@ public class TreeFertilizerItem extends Item {
return ActionResultType.SUCCESS;
}
TreesDreamWorld world = new TreesDreamWorld(context.getWorld());
TreesDreamWorld world = new TreesDreamWorld((ServerWorld) context.getWorld());
BlockPos saplingPos = context.getPos();
for (BlockPos pos : BlockPos.getAllInBoxMutable(-1, 0, -1, 1, 0, 1)) {
@ -40,7 +38,7 @@ public class TreeFertilizerItem extends Item {
world.setBlockState(pos.up(10), state.with(SaplingBlock.STAGE, 1));
}
((SaplingBlock) block).grow(world, BlockPos.ZERO.up(10), state.with(SaplingBlock.STAGE, 1), new Random());
((SaplingBlock) block).grow(world, world.getRandom(), BlockPos.ZERO.up(10), state.with(SaplingBlock.STAGE, 1));
for (BlockPos pos : world.blocksAdded.keySet()) {
BlockPos actualPos = pos.add(saplingPos).down(10);
@ -68,9 +66,9 @@ public class TreeFertilizerItem extends Item {
return super.onItemUse(context);
}
private class TreesDreamWorld extends PlacementSimulationWorld {
private class TreesDreamWorld extends PlacementSimulationServerWorld {
protected TreesDreamWorld(World wrapped) {
protected TreesDreamWorld(ServerWorld wrapped) {
super(wrapped);
}