Skip to content

Instantly share code, notes, and snippets.

@LOOHP
Last active August 19, 2023 12:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LOOHP/d2f04878803e7ae4867d167b347abbf6 to your computer and use it in GitHub Desktop.
Save LOOHP/d2f04878803e7ae4867d167b347abbf6 to your computer and use it in GitHub Desktop.
Source Code for FallingSnow (https://www.spigotmc.org/resources/92367/)
package com.loohp.fallingsnow;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.type.Snow;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.FallingBlock;
import org.bukkit.event.Cancellable;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.block.BlockPistonExtendEvent;
import org.bukkit.event.block.BlockPistonRetractEvent;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.event.entity.EntityDropItemEvent;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
public class FallingSnow extends JavaPlugin implements Listener {
public static FallingSnow plugin;
public NamespacedKey fallingSnowTag;
public PersistentDataType<Byte, Byte> fallingSnowTagType = PersistentDataType.BYTE;
public byte fallingSnowTagValueFalse = 0;
public byte fallingSnowTagValueTrue = 1;
public Set<Block> checkedBlocks = ConcurrentHashMap.newKeySet();
private static final boolean FOLIA = classExist("io.papermc.paper.threadedregions.scheduler.AsyncScheduler");
private static boolean classExist(String className) {
try {
Class.forName(className);
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
public static void runTaskTimer(Plugin plugin, Runnable runnable, int delay, int period) {
if (FOLIA) {
//noinspection rawtypes,unchecked
Bukkit.getGlobalRegionScheduler().runAtFixedRate(plugin, (Consumer) st -> runnable.run(), delay, period);
} else {
Bukkit.getScheduler().runTaskTimer(plugin, runnable, delay, period);
}
}
public static void runTaskLater(Plugin plugin, Location location, Runnable runnable, int delay) {
if (FOLIA) {
//noinspection rawtypes,unchecked
Bukkit.getRegionScheduler().runDelayed(plugin, location, (Consumer) st -> runnable.run(), delay);
} else {
Bukkit.getScheduler().runTaskLater(plugin, runnable, delay);
}
}
@Override
public void onEnable() {
plugin = this;
fallingSnowTag = new NamespacedKey(this, "IsFallingSnow");
getServer().getPluginManager().registerEvents(this, this);
getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "[FallingSnow] FallingSnow has been enabled.");
runTaskTimer(this, () -> checkedBlocks.clear(), 1, 1);
}
@EventHandler
public void onPhysics(BlockPhysicsEvent event) {
Block block = event.getBlock();
checkSnowLayer(block);
checkSnowLayer(block.getRelative(BlockFace.UP));
}
@EventHandler
public void onPistonExtend(BlockPistonExtendEvent event) {
for (Block block : event.getBlocks()) {
checkSnowLayer(block);
checkSnowLayer(block.getRelative(BlockFace.UP));
}
}
@EventHandler
public void onPistonRetract(BlockPistonRetractEvent event) {
for (Block block : event.getBlocks()) {
checkSnowLayer(block);
checkSnowLayer(block.getRelative(BlockFace.UP));
}
}
public void checkSnowLayer(Block block) {
if (!checkedBlocks.contains(block) && block.getType().equals(Material.SNOW)) {
BlockData data = block.getBlockData().clone();
runTaskLater(this, block.getLocation(), () -> {
if (block.getType().equals(Material.AIR) && block.getRelative(BlockFace.DOWN).getType().equals(Material.AIR)) {
FallingBlock fallingBlock = block.getWorld().spawnFallingBlock(block.getLocation().add(0.5, 0, 0.5), data);
fallingBlock.getPersistentDataContainer().set(fallingSnowTag, fallingSnowTagType, fallingSnowTagValueTrue);
}
}, 2);
}
checkedBlocks.add(block);
}
public void handleSnowLayer(FallingBlock fallingBlock, Block original, Cancellable event) {
BlockData blockData = fallingBlock.getBlockData();
if (blockData.getMaterial().equals(Material.SNOW)) {
Snow snow = (Snow) blockData;
if (original.getType().equals(Material.SNOW)) {
event.setCancelled(true);
Snow originalSnow = (Snow) original.getBlockData();
int rawLayers = originalSnow.getLayers() + snow.getLayers();
int layers = Math.min(rawLayers, 8);
originalSnow.setLayers(layers);
original.setBlockData(originalSnow);
if (rawLayers > layers) {
Block up = original.getRelative(BlockFace.UP);
if (up.getType().equals(Material.AIR)) {
up.setType(Material.SNOW);
Snow upSnow = (Snow) up.getBlockData();
upSnow.setLayers(rawLayers - layers);
up.setBlockData(upSnow);
}
}
}
}
}
@EventHandler
public void onFallingBlockLanding(EntityChangeBlockEvent event) {
if (event.getEntityType().equals(EntityType.FALLING_BLOCK)) {
handleSnowLayer((FallingBlock) event.getEntity(), event.getBlock(), event);
}
}
@EventHandler
public void onFallingBlockPop(EntityDropItemEvent event) {
if (event.getEntityType().equals(EntityType.FALLING_BLOCK)) {
FallingBlock fallingBlock = (FallingBlock) event.getEntity();
PersistentDataContainer dataContainer = fallingBlock.getPersistentDataContainer();
if (dataContainer.getOrDefault(fallingSnowTag, fallingSnowTagType, fallingSnowTagValueFalse) >= fallingSnowTagValueTrue) {
event.setCancelled(true);
}
handleSnowLayer(fallingBlock, fallingBlock.getLocation().getBlock(), event);
}
}
}
@xiaoyueyoqwq
Copy link

Hey, this is a crash report issue
https://paste.ubuntu.com/p/GyxSNV9G6f/
Maybe I can upload issue in here? Don't blame me if I'm doing something wrong, because I didn't find a place to file an issue :(

@LOOHP
Copy link
Author

LOOHP commented Apr 23, 2023

Will be fixed on Folia soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment