Skip to content

Instantly share code, notes, and snippets.

@LOOHP
Last active April 22, 2023 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LOOHP/ef4d73122fe13f5767cf61ea4aa5ef83 to your computer and use it in GitHub Desktop.
Save LOOHP/ef4d73122fe13f5767cf61ea4aa5ef83 to your computer and use it in GitHub Desktop.
Source code of AnimatedIronGolemSpawn (https://www.spigotmc.org/resources/106692/)
package com.loohp.animatedirongolemspawn;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.Particle;
import org.bukkit.Sound;
import org.bukkit.SoundCategory;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Directional;
import org.bukkit.entity.IronGolem;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
public class AnimatedIronGolemSpawn extends JavaPlugin implements Listener {
private static final BlockData IRON_BLOCK_DATA = Bukkit.createBlockData(Material.IRON_BLOCK);
private static final BlockData PUMPKIN_DATA = Bukkit.createBlockData(Material.CARVED_PUMPKIN, d -> ((Directional) d).setFacing(BlockFace.SOUTH));
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 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() {
getServer().getPluginManager().registerEvents(this, this);
getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "AnimatedIronGolemSpawn has been enabled!");
}
@Override
public void onDisable() {
getServer().getConsoleSender().sendMessage(ChatColor.RED + "AnimatedIronGolemSpawn had been disabled!");
}
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
public void onIronGolemSpawn(CreatureSpawnEvent event) {
LivingEntity entity = event.getEntity();
if (entity instanceof IronGolem && event.getSpawnReason().equals(CreatureSpawnEvent.SpawnReason.VILLAGE_DEFENSE)) {
runSpawnSequence((IronGolem) entity);
}
}
public void runSpawnSequence(IronGolem ironGolem) {
if (ironGolem.isInvisible()) {
return;
}
boolean hasAI = ironGolem.hasAI();
boolean isInvulnerable = ironGolem.isInvulnerable();
ironGolem.setAI(false);
ironGolem.setInvisible(true);
ironGolem.setInvulnerable(true);
Location location = ironGolem.getLocation();
World world = ironGolem.getWorld();
List<Player> players = getPlayersInRange(location);
Block originBlock = location.getBlock();
Block centerBlock = originBlock.getRelative(BlockFace.UP);
Block[] ironBlocks = new Block[] {originBlock, centerBlock, centerBlock.getRelative(BlockFace.EAST), centerBlock.getRelative(BlockFace.WEST)};
Block head = centerBlock.getRelative(BlockFace.UP);
runTaskLater(this, location, () -> players.forEach(each -> {
each.sendBlockChange(ironBlocks[0].getLocation(), IRON_BLOCK_DATA);
world.playSound(ironBlocks[0].getLocation(), Sound.BLOCK_METAL_PLACE, SoundCategory.BLOCKS, 1.0F, 1.2F);
}), 2);
runTaskLater(this, location, () -> players.forEach(each -> {
each.sendBlockChange(ironBlocks[1].getLocation(), IRON_BLOCK_DATA);
world.playSound(ironBlocks[1].getLocation(), Sound.BLOCK_METAL_PLACE, SoundCategory.BLOCKS, 1.0F, 1.2F);
}), 12);
runTaskLater(this, location, () -> players.forEach(each -> {
each.sendBlockChange(ironBlocks[2].getLocation(), IRON_BLOCK_DATA);
world.playSound(ironBlocks[2].getLocation(), Sound.BLOCK_METAL_PLACE, SoundCategory.BLOCKS, 1.0F, 1.2F);
}), 22);
runTaskLater(this, location, () -> players.forEach(each -> {
each.sendBlockChange(ironBlocks[3].getLocation(), IRON_BLOCK_DATA);
world.playSound(ironBlocks[3].getLocation(), Sound.BLOCK_METAL_PLACE, SoundCategory.BLOCKS, 1.0F, 1.2F);
}), 32);
runTaskLater(this, location, () -> players.forEach(each -> {
each.sendBlockChange(head.getLocation(), PUMPKIN_DATA);
world.playSound(head.getLocation(), Sound.BLOCK_WOOD_PLACE, SoundCategory.BLOCKS, 1.0F, 0.8F);
}), 42);
runTaskLater(this, location, () -> {
players.forEach(each -> {
world.spawnParticle(Particle.BLOCK_CRACK, ironBlocks[0].getLocation().add(0.5, 0.5, 0.5), 50, IRON_BLOCK_DATA);
world.spawnParticle(Particle.BLOCK_CRACK, ironBlocks[1].getLocation().add(0.5, 0.5, 0.5), 50, IRON_BLOCK_DATA);
world.spawnParticle(Particle.BLOCK_CRACK, ironBlocks[2].getLocation().add(0.5, 0.5, 0.5), 50, IRON_BLOCK_DATA);
world.spawnParticle(Particle.BLOCK_CRACK, ironBlocks[3].getLocation().add(0.5, 0.5, 0.5), 50, IRON_BLOCK_DATA);
world.spawnParticle(Particle.BLOCK_CRACK, head.getLocation().add(0.5, 0.5, 0.5), 50, PUMPKIN_DATA);
world.playSound(ironBlocks[0].getLocation(), Sound.BLOCK_METAL_BREAK, SoundCategory.BLOCKS, 1.0F, 1.2F);
world.playSound(ironBlocks[1].getLocation(), Sound.BLOCK_METAL_BREAK, SoundCategory.BLOCKS, 1.0F, 1.2F);
world.playSound(ironBlocks[2].getLocation(), Sound.BLOCK_METAL_BREAK, SoundCategory.BLOCKS, 1.0F, 1.2F);
world.playSound(ironBlocks[3].getLocation(), Sound.BLOCK_METAL_BREAK, SoundCategory.BLOCKS, 1.0F, 1.2F);
world.playSound(head.getLocation(), Sound.BLOCK_WOOD_BREAK, SoundCategory.BLOCKS, 1.0F, 0.8F);
each.sendBlockChange(ironBlocks[0].getLocation(), ironBlocks[0].getBlockData());
each.sendBlockChange(ironBlocks[1].getLocation(), ironBlocks[1].getBlockData());
each.sendBlockChange(ironBlocks[2].getLocation(), ironBlocks[2].getBlockData());
each.sendBlockChange(ironBlocks[3].getLocation(), ironBlocks[3].getBlockData());
each.sendBlockChange(head.getLocation(), head.getBlockData());
ironGolem.setAI(hasAI);
ironGolem.setInvisible(false);
ironGolem.setInvulnerable(isInvulnerable);
});
}, 44);
}
public List<Player> getPlayersInRange(Location location) {
World world = location.getWorld();
int distance = world.getSimulationDistance();
int distanceSquared = distance * distance;
return world.getPlayers().stream().filter(each -> each.getLocation().distanceSquared(location) < distanceSquared).collect(Collectors.toList());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment