From e1e3220b688809cc7114f8f0babd0bac30d7f0a4 Mon Sep 17 00:00:00 2001 From: tterrag Date: Sun, 29 Mar 2020 14:39:51 -0400 Subject: [PATCH] Add server versions of wrapped worlds --- .../PlacementSimulationServerWorld.java | 56 ++++++++ .../utility/WrappedServerWorld.java | 131 ++++++++++++++++++ .../foundation/utility/WrappedWorld.java | 1 - .../modules/gardens/TreeFertilizerItem.java | 14 +- 4 files changed, 193 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/simibubi/create/foundation/utility/PlacementSimulationServerWorld.java create mode 100644 src/main/java/com/simibubi/create/foundation/utility/WrappedServerWorld.java diff --git a/src/main/java/com/simibubi/create/foundation/utility/PlacementSimulationServerWorld.java b/src/main/java/com/simibubi/create/foundation/utility/PlacementSimulationServerWorld.java new file mode 100644 index 000000000..942b8917f --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/utility/PlacementSimulationServerWorld.java @@ -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 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 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(); + } + +} diff --git a/src/main/java/com/simibubi/create/foundation/utility/WrappedServerWorld.java b/src/main/java/com/simibubi/create/foundation/utility/WrappedServerWorld.java new file mode 100644 index 000000000..b85ef584d --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/utility/WrappedServerWorld.java @@ -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 getPendingBlockTicks() { + return world.getPendingBlockTicks(); + } + + @Override + public ServerTickList getPendingFluidTicks() { + return world.getPendingFluidTicks(); + } + + @Override + public void playEvent(PlayerEntity player, int type, BlockPos pos, int data) { + } + + @Override + public List 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_); + } + +} diff --git a/src/main/java/com/simibubi/create/foundation/utility/WrappedWorld.java b/src/main/java/com/simibubi/create/foundation/utility/WrappedWorld.java index 0facf4751..89029ea96 100644 --- a/src/main/java/com/simibubi/create/foundation/utility/WrappedWorld.java +++ b/src/main/java/com/simibubi/create/foundation/utility/WrappedWorld.java @@ -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; diff --git a/src/main/java/com/simibubi/create/modules/gardens/TreeFertilizerItem.java b/src/main/java/com/simibubi/create/modules/gardens/TreeFertilizerItem.java index 58cd3e79c..6061504e5 100644 --- a/src/main/java/com/simibubi/create/modules/gardens/TreeFertilizerItem.java +++ b/src/main/java/com/simibubi/create/modules/gardens/TreeFertilizerItem.java @@ -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); }