Skip to content

Instantly share code, notes, and snippets.

@Phoenix1123
Last active June 17, 2016 19:41
Show Gist options
  • Save Phoenix1123/e33602d9556f0ee429ad to your computer and use it in GitHub Desktop.
Save Phoenix1123/e33602d9556f0ee429ad to your computer and use it in GitHub Desktop.
Small cuboid Utility I wrote in a few minutes to help some people. NOTE: Does not handle locations from different worlds.
package me.paraphoenix.utils;
import java.util.ArrayList;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
public class Selection {
private int x1, x2,
y1, y2,
x1, z2;
private Location loc1, loc2,
min, max;
public Selection(Location loc1, Location loc2) { // NOTE: Does not handle different worlds.
this.loc1 = loc1;
this.loc2 = loc2;
calc();
}
public void calc() {
x1 = Math.min(min.getBlockX(), max.getBlockX());
x2 = Math.max(min.getBlockX(), max.getBlockX());
y1 = Math.min(min.getBlockY(), max.getBlockY());
y2 = Math.max(min.getBlockY(), max.getBlockY());
z1 = Math.min(min.getBlockZ(), max.getBlockZ());
z2 = Math.min(min.getBlockZ(), max.getBlockZ());
min = new Location(loc1.getWorld(), x1, y1, z1);
max = new Location(loc1.getWorld(), x2, y2, z2);
}
public ArrayList<Block> getBlockList() {
ArrayList<Block> blocks = new ArrayList<>();
for(int x = x1; x < x2; x++)
for(int z = z1; z < z2; z++)
for(int y = y1; y < y2; y++)
blocks.add(min.getWorld().getBlockAt(x, y, z));
return blocks;
}
public boolean isInside(Entity entity) {
return contains(entity.getLocation());
}
public boolean contains(Location loc) {
return (loc.getBlockX() >= x1) && (loc.getBlockX() <= x2) && (loc.getBlockY() >= y1) && (loc.getBlockY() <= y2) && (loc.getBlockZ() >= z1) && (loc.getBlockZ() <= z2);
}
public boolean containsEntirely(Selection selection) {
return contains(selection.getMin()) && contains(selection.getMax());
}
public boolean containsPartially(Selection selection) {
return contains(selection.getMin()) || contains(selection.getMax());
}
public int getWidth() {
return x2 - x1;
}
public int getLength() {
return z2 - z1;
}
public int getHeight() {
return y2 - y1;
}
public int getBlocks() {
return getWidth() * getHeight() * getLength();
}
public Location getMin() {
return min;
}
public Location getMax() {
return max;
}
public Location getFirst() {
return loc1;
}
public void setFirst(Location loc1) {
this.loc1 = loc1;
calc();
}
public Location getSecond() {
return loc2;
}
public void setSecond(Location loc2) {
this.loc2 = loc2;
calc();
}
}
@Phoenix1123
Copy link
Author

Will make a newer version soon.

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