Add a WorldProvider#canSleepHere() method to allow providers to control sleeping in beds separately from re-spawning. (#4741)

This commit is contained in:
Matti Ruohonen 2018-04-01 10:27:58 +03:00 committed by LexManos
parent 7c098462fe
commit e82a7605dc
3 changed files with 84 additions and 1 deletions

View file

@ -0,0 +1,14 @@
--- ../src-base/minecraft/net/minecraft/block/BlockBed.java
+++ ../src-work/minecraft/net/minecraft/block/BlockBed.java
@@ -84,8 +84,10 @@
}
}
- if (p_180639_1_.field_73011_w.func_76567_e() && p_180639_1_.func_180494_b(p_180639_2_) != Biomes.field_76778_j)
+ net.minecraft.world.WorldProvider.WorldSleepResult sleepResult = p_180639_1_.field_73011_w.canSleepAt(p_180639_4_, p_180639_2_);
+ if (sleepResult != net.minecraft.world.WorldProvider.WorldSleepResult.BED_EXPLODES)
{
+ if (sleepResult == net.minecraft.world.WorldProvider.WorldSleepResult.DENY) return true;
if (((Boolean)p_180639_3_.func_177229_b(field_176471_b)).booleanValue())
{
EntityPlayer entityplayer = this.func_176470_e(p_180639_1_, p_180639_2_);

View file

@ -65,7 +65,7 @@
} }
@SideOnly(Side.CLIENT) @SideOnly(Side.CLIENT)
@@ -241,6 +216,352 @@ @@ -241,6 +216,371 @@
return new WorldBorder(); return new WorldBorder();
} }
@ -256,6 +256,25 @@
+ return null; + return null;
+ } + }
+ +
+ /**
+ * Determines if the player can sleep in this world (or if the bed should explode for example).
+ *
+ * @param player The player that is attempting to sleep
+ * @param pos The location where the player tries to sleep at (the position of the clicked on bed for example)
+ * @return the result of a player trying to sleep at the given location
+ */
+ public WorldSleepResult canSleepAt(net.minecraft.entity.player.EntityPlayer player, BlockPos pos)
+ {
+ return (this.func_76567_e() && this.field_76579_a.func_180494_b(pos) != net.minecraft.init.Biomes.field_76778_j) ? WorldSleepResult.ALLOW : WorldSleepResult.BED_EXPLODES;
+ }
+
+ public static enum WorldSleepResult
+ {
+ ALLOW,
+ DENY,
+ BED_EXPLODES;
+ }
+
+ /*======================================= Start Moved From World =========================================*/ + /*======================================= Start Moved From World =========================================*/
+ +
+ public Biome getBiomeForCoords(BlockPos pos) + public Biome getBiomeForCoords(BlockPos pos)

View file

@ -0,0 +1,50 @@
package net.minecraftforge.debug;
import org.apache.logging.log4j.Logger;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.DimensionType;
import net.minecraft.world.WorldProvider;
import net.minecraftforge.common.DimensionManager;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventHandler;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
@Mod(modid = CanSleepAtTest.MODID, name = "CanSleepAtTest", version = "0.0.0", acceptableRemoteVersions = "*")
public class CanSleepAtTest
{
public static final String MODID = "can_sleep_at_test";
public static final boolean ENABLED = false;
public static DimensionType dimType = null;
public static int dimId;
private static Logger logger;
@EventHandler
public void onPreInit(FMLPreInitializationEvent event)
{
if (ENABLED)
{
logger = event.getModLog();
dimId = DimensionManager.getNextFreeDimId();
dimType = DimensionType.register("CanSleepAtTest", "_cansleepattest", dimId, WorldProviderTest.class, false);
DimensionManager.registerDimension(dimId, dimType);
logger.info("Registered CanSleepAtTest dimension as DIM {}", dimId);
}
}
public static class WorldProviderTest extends WorldProvider
{
@Override
public DimensionType getDimensionType()
{
return CanSleepAtTest.dimType;
}
@Override
public WorldSleepResult canSleepAt(EntityPlayer player, BlockPos pos)
{
// Creates a 5x5 blocks wide grid of the different sleep results
return WorldSleepResult.values()[((pos.getX() / 5) + (pos.getZ() / 5)) % WorldSleepResult.values().length];
}
}
}