Skip to content

Instantly share code, notes, and snippets.

@d3lt3x
Last active April 20, 2023 20:20
Show Gist options
  • Save d3lt3x/85b15cf8df5dd4ef692872c842e11522 to your computer and use it in GitHub Desktop.
Save d3lt3x/85b15cf8df5dd4ef692872c842e11522 to your computer and use it in GitHub Desktop.
Marks a selected area in minecraft using the Paper API
package me.delta.mc.wand.main;
import org.bukkit.Color;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.BlockDisplay;
import org.bukkit.entity.Display;
import org.bukkit.util.BlockVector;
import org.bukkit.util.BoundingBox;
import org.bukkit.util.Transformation;
import org.joml.AxisAngle4f;
import org.joml.Vector3f;
import java.util.HashSet;
import java.util.Set;
public class BlockSelection {
private final World world;
private final BoundingBox box;
private final Color color;
private final Material material;
private Set<BlockDisplay> selection = new HashSet<>(6);
//Recommended Material: Stained Glass
public BlockSelection(World world, Block corner1, Block corner2, Color color, Material material) {
this.box = BoundingBox.of(corner1, corner2).expand(0.01);
this.world = world;
this.color = color;
this.material = material;
}
public BlockSelection createSelection() {
Location loc1 = new BlockVector(this.box.getMin()).toLocation(this.world);
Location loc2 = new BlockVector(this.box.getMax()).toLocation(this.world);
this.spawnDisplay(new Transformation(new Vector3f(), new AxisAngle4f(), new Vector3f((float) this.box.getWidthX(), (float) this.box.getHeight(), 0), new AxisAngle4f()), loc1);
this.spawnDisplay(new Transformation(new Vector3f(), new AxisAngle4f(), new Vector3f(0, (float) this.box.getHeight(), (float) this.box.getWidthZ()), new AxisAngle4f()), loc1);
this.spawnDisplay(new Transformation(new Vector3f(), new AxisAngle4f(), new Vector3f((float) this.box.getWidthX(), 0, (float) this.box.getWidthZ()), new AxisAngle4f()), loc1);
this.spawnDisplay(new Transformation(new Vector3f(), new AxisAngle4f(), new Vector3f((float) -this.box.getWidthX(), (float) -this.box.getHeight(), 0), new AxisAngle4f()), loc2);
this.spawnDisplay(new Transformation(new Vector3f(), new AxisAngle4f(), new Vector3f(0, (float) -this.box.getHeight(), (float) -this.box.getWidthZ()), new AxisAngle4f()), loc2);
this.spawnDisplay(new Transformation(new Vector3f(), new AxisAngle4f(), new Vector3f((float) -this.box.getWidthX(), 0, (float) -this.box.getWidthZ()), new AxisAngle4f()), loc2);
return this;
}
private BlockDisplay spawnDisplay(Transformation transformation, Location location) {
return this.world.spawn(location, BlockDisplay.class, blockDisplay -> {
blockDisplay.setBlock(this.material.createBlockData());
blockDisplay.setGravity(false);
blockDisplay.setInvulnerable(true);
blockDisplay.setGlowing(true);
blockDisplay.setPersistent(true);
blockDisplay.setTransformation(transformation);
blockDisplay.setBrightness(new Display.Brightness(15, 15));
blockDisplay.setShadowStrength(0);
blockDisplay.setShadowRadius(0);
blockDisplay.setGlowColorOverride(this.color);
this.selection.add(blockDisplay);
});
}
public void deselect() {
if (this.selection == null) return;
this.selection.forEach(blockDisplay -> blockDisplay.remove());
this.selection = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment