Skip to content

Instantly share code, notes, and snippets.

@LOOHP
Last active June 4, 2024 18:13
Show Gist options
  • 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 org.bukkit.util.Vector;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
public class AnimatedIronGolemSpawn extends JavaPlugin implements Listener {
private static final boolean FOLIA = isFolia();
private static boolean isFolia() {
try {
Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
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) || entity.getScoreboardTags().contains("AnimatedSpawn"))) {
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);
BlockFace facing = ironGolem.getFacing();
Vector vector = facing.getDirection().rotateAroundY(Math.toRadians(90));
Block[] ironBlocks = new Block[] {originBlock, centerBlock, centerBlock.getLocation().add(vector).getBlock(), centerBlock.getLocation().add(vector.multiply(-1)).getBlock()};
Block head = centerBlock.getRelative(BlockFace.UP);
BlockData ironBlockData = Bukkit.createBlockData(Material.IRON_BLOCK);
BlockData pumpkinData = Bukkit.createBlockData(Material.CARVED_PUMPKIN, d -> ((Directional) d).setFacing(facing));
runTaskLater(this, location, () -> players.forEach(each -> {
each.sendBlockChange(ironBlocks[0].getLocation(), ironBlockData);
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(), ironBlockData);
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(), ironBlockData);
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(), ironBlockData);
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(), pumpkinData);
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, ironBlockData);
world.spawnParticle(Particle.BLOCK_CRACK, ironBlocks[1].getLocation().add(0.5, 0.5, 0.5), 50, ironBlockData);
world.spawnParticle(Particle.BLOCK_CRACK, ironBlocks[2].getLocation().add(0.5, 0.5, 0.5), 50, ironBlockData);
world.spawnParticle(Particle.BLOCK_CRACK, ironBlocks[3].getLocation().add(0.5, 0.5, 0.5), 50, ironBlockData);
world.spawnParticle(Particle.BLOCK_CRACK, head.getLocation().add(0.5, 0.5, 0.5), 50, pumpkinData);
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() * 16;
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