Skip to content

Instantly share code, notes, and snippets.

@d3lt3x
Created April 20, 2023 19:59
Show Gist options
  • Save d3lt3x/f1c55ed27682e2e2058ee4e1d9d0469d to your computer and use it in GitHub Desktop.
Save d3lt3x/f1c55ed27682e2e2058ee4e1d9d0469d to your computer and use it in GitHub Desktop.
Creates RBG glow below a player in minecraft using the Paper API.
package me.delta.mc.wand.listeners;
import org.bukkit.Bukkit;
import org.bukkit.Color;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.BlockDisplay;
import org.bukkit.entity.Display;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.plugin.Plugin;
import org.bukkit.util.Transformation;
import org.joml.AxisAngle4f;
import org.joml.Vector3f;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
public class PlayerMoveListener implements Listener {
private final Map<Location, BlockDisplay> displays = new HashMap<>();
private final Plugin plugin;
public PlayerMoveListener(Plugin plugin) {
this.plugin = plugin;
this.applyRBG();
}
@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
if (!event.hasChangedBlock()) return;
if (this.displays.containsKey(event.getFrom().getBlock().getLocation().subtract(0, 1, 0))) {
this.displays.get(event.getFrom().getBlock().getLocation().subtract(0, 1, 0)).remove();
this.displays.remove(event.getFrom().getBlock().getLocation().subtract(0, 1, 0));
}
Block block = event.getTo().getBlock().getLocation().subtract(0, 1, 0).getBlock();
if (!block.isSolid()) return;
if (this.displays.containsKey(block.getLocation())) return;
this.spawnDisplay(block.getLocation(), event.getPlayer());
}
private void spawnDisplay(Location location, Player player) {
location.getWorld().spawn(location, BlockDisplay.class, blockDisplay -> {
blockDisplay.setBlock(Material.WHITE_STAINED_GLASS.createBlockData());
blockDisplay.setTransformation(new Transformation(new Vector3f(0, 0.99F, 0), new AxisAngle4f(), new Vector3f(1, 0, 1), new AxisAngle4f()));
blockDisplay.setInvulnerable(true);
blockDisplay.setSilent(true);
blockDisplay.setGravity(false);
blockDisplay.setBrightness(new Display.Brightness(15, 15));
blockDisplay.setShadowStrength(0);
blockDisplay.setVisibleByDefault(false);
player.showEntity(this.plugin, blockDisplay);
blockDisplay.setShadowRadius(0);
this.displays.put(location, blockDisplay);
});
}
private void applyRBG() {
AtomicInteger r = new AtomicInteger(255);
AtomicInteger g = new AtomicInteger();
AtomicInteger b = new AtomicInteger();
Bukkit.getScheduler().runTaskTimer(this.plugin, () -> {
if (r.get() > 0 && b.get() == 0) {
r.getAndAdd(-15);
g.getAndAdd(15);
}
if (g.get() > 0 && r.get() == 0) {
g.getAndAdd(-15);
b.getAndAdd(15);
}
if (b.get() > 0 && g.get() == 0) {
r.getAndAdd(15);
b.getAndAdd(-15);
}
this.displays.values().forEach(blockDisplay -> {
blockDisplay.setGlowColorOverride(Color.fromRGB(r.get(), g.get(), b.get()));
blockDisplay.setGlowing(true);
});
}, 0, 1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment