Permalink
Browse files

Add a few optimisations to the Snow mechanic due to recent Spigot/Pap…

…er changes.
  • Loading branch information...
me4502 committed Oct 5, 2018
1 parent bae5163 commit 95797cca68f93ca72dc4b04fcac8180bc79cbe5a
@@ -1,12 +1,14 @@
package com.sk89q.craftbook.mechanics;
import com.google.common.base.Objects;
import com.google.common.collect.Lists;
import com.sk89q.craftbook.AbstractCraftBookMechanic;
import com.sk89q.craftbook.CraftBookPlayer;
import com.sk89q.craftbook.bukkit.CraftBookPlugin;
import com.sk89q.craftbook.util.BlockSyntax;
import com.sk89q.craftbook.util.BlockUtil;
import com.sk89q.craftbook.util.EventUtil;
import com.sk89q.craftbook.util.LocationUtil;
import com.sk89q.craftbook.util.ProtectionUtil;
import com.sk89q.util.yaml.YAMLProcessor;
import com.sk89q.worldedit.blocks.Blocks;
@@ -22,6 +24,7 @@
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.data.Bisected;
import org.bukkit.block.data.Levelled;
import org.bukkit.block.data.type.Slab;
import org.bukkit.block.data.type.Stairs;
import org.bukkit.entity.Player;
@@ -51,7 +54,6 @@
*
* @author Me4502
*/
@SuppressWarnings("deprecation")
public class Snow extends AbstractCraftBookMechanic {
@Override
@@ -155,24 +157,25 @@ public void onPlayerMove(PlayerMoveEvent event) {
CraftBookPlayer player = CraftBookPlugin.inst().wrapPlayer(event.getPlayer());
if (event.getTo().getBlock().getType() == Material.SNOW) {
Levelled levelled = (Levelled) event.getTo().getBlock().getBlockData();
if(slowdown) {
if(event.getTo().getBlock().getData() > 4)
if(levelled.getLevel() > 4)
event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 20, 2), true);
else if(event.getTo().getBlock().getData() > 0)
else if(levelled.getLevel() > 0)
event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 20, 1), true);
}
if (trample) {
if (!player.hasPermission("craftbook.mech.snow.trample"))
return;
if (jumpTrample && !(event.getFrom().getY() - event.getTo().getY() >= 0.1D))
return;
if (CraftBookPlugin.inst().getRandom().nextInt(20) == 0) {
if (event.getTo().getBlock().getData() == 0x0 && partialTrample)
if (levelled.getLevel() == 0 && partialTrample)
return;
if (!player.hasPermission("craftbook.mech.snow.trample"))
return;
if (CraftBookPlugin.inst().getConfiguration().pedanticBlockChecks && !ProtectionUtil
.canBuild(event.getPlayer(), event.getPlayer().getLocation(), false))
return;
@@ -182,6 +185,8 @@ else if(event.getTo().getBlock().getData() > 0)
}
}
private static final BlockFace[] UPDATE_FACES = {BlockFace.UP, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST};
@EventHandler(priority = EventPriority.HIGH)
public void onBlockBreak(BlockBreakEvent event) {
@@ -192,12 +197,11 @@ public void onBlockBreak(BlockBreakEvent event) {
event.setCancelled(true);
event.getBlock().setType(Material.AIR, false);
for(ItemStack stack : BlockUtil.getBlockDrops(event.getBlock(), event.getPlayer().getItemInHand()))
for(ItemStack stack : BlockUtil.getBlockDrops(event.getBlock(), event.getPlayer().getInventory().getItemInMainHand()))
event.getBlock().getWorld().dropItemNaturally(BlockUtil.getBlockCentre(event.getBlock()), stack);
if(realistic) {
BlockFace[] faces = new BlockFace[]{BlockFace.UP, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST};
for(BlockFace dir : faces) Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(event.getBlock().getRelative(dir), 0, event.getBlock().getLocation().toVector().toBlockVector()), animationTicks);
for(BlockFace dir : UPDATE_FACES) Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(event.getBlock().getRelative(dir), 0, event.getBlock().getLocation().toVector().toBlockVector()), animationTicks);
}
}
}
@@ -252,24 +256,34 @@ public void run () {
}
boolean meltMode = !chunk.getWorld().hasStorm() && meltSunlight;
Block chunkBase = chunk.getBlock(0, 0, 0);
Block highest = chunk.getWorld().getHighestBlockAt(chunkBase.getX() + CraftBookPlugin.inst().getRandom().nextInt(16), chunkBase.getZ() + CraftBookPlugin.inst().getRandom().nextInt(16));
if(highest.getType() == Material.SNOW || highest.getType() == Material.SNOW_BLOCK || highest.getType() == Material.ICE || isReplacable(highest)) {
if(highest.getWorld().hasStorm() && highest.getType() != Material.ICE) {
if(highest.getTemperature() < 0.15) {
Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(highest, 1), animationTicks);
long delay = meltMode ? 5L : 20L;
if (!LocationUtil.isBorderChunk(chunk)) {
Block chunkBase = chunk.getBlock(0, 0, 0);
Block highest = chunk.getWorld().getHighestBlockAt(chunkBase.getX() + CraftBookPlugin.inst().getRandom().nextInt(16),
chunkBase.getZ() + CraftBookPlugin.inst().getRandom().nextInt(16));
if (highest.getType() == Material.SNOW || highest.getType() == Material.SNOW_BLOCK || highest.getType() == Material.ICE
|| isReplacable(highest)) {
if (highest.getWorld().hasStorm() && highest.getType() != Material.ICE) {
if (highest.getTemperature() < 0.15) {
Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(highest, 1), animationTicks);
}
} else if (meltMode) {
if (highest.getType() == Material.SNOW && meltPartial)
if (highest.getData() == 0)
return;
if (highest.getTemperature() > 0.05)
Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(highest, -1), animationTicks);
}
} else if(meltMode) {
if(highest.getType() == Material.SNOW && meltPartial)
if(highest.getData() == 0) return;
if(highest.getTemperature() > 0.05)
Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(highest, -1), animationTicks);
}
delay *= getRandomDelay();
} else {
delay *= 50;
}
Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), this, getRandomDelay() * (meltMode ? 5L : 20L));
Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), this, delay);
}
}
@@ -494,8 +508,7 @@ boolean decreaseSnow(Block snow, boolean disperse) {
snow.setBlockData(snowData);
}
if(disperse && realistic) {
BlockFace[] faces = new BlockFace[]{BlockFace.UP, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST};
for(BlockFace dir : faces) Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(snow.getRelative(dir), 0, snow.getLocation().toVector().toBlockVector()),
for(BlockFace dir : UPDATE_FACES) Bukkit.getScheduler().runTaskLater(CraftBookPlugin.inst(), new SnowHandler(snow.getRelative(dir), 0, snow.getLocation().toVector().toBlockVector()),
animationTicks);
}
@@ -526,9 +539,7 @@ public boolean equals(Object o) {
@Override
public int hashCode() {
// Constants correspond to glibc's lcg algorithm parameters
return (Arrays.hashCode(worldname) * 1103515245 + 12345 ^ x * 1103515245 + 12345 ^ y
* 1103515245 + 12345 ^ z * 1103515245 + 12345) * 1103515245 + 12345;
return Objects.hashCode(worldname, x, y, z);
}
}
@@ -3,8 +3,10 @@
import java.util.HashSet;
import com.sk89q.craftbook.bukkit.util.CraftBookBukkitUtil;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Entity;
@@ -272,6 +274,21 @@ public static Location getCenterOfBlock(Block block) {
return radiusEntities.toArray(new Player[radiusEntities.size()]);
}
public static boolean isBorderChunk(Chunk chunk) {
World world = chunk.getWorld();
for (int x = -1; x < 2; x++) {
for (int z = -1; z < 2; z++) {
if (x == 0 && z == 0) continue;
if (!world.isChunkLoaded(chunk.getX() + x, chunk.getZ() + z)) {
return true;
}
}
}
return false;
}
/**
* Gets an array of {@link BlockFace} that are direct.
*

0 comments on commit 95797cc

Please sign in to comment.