Skip to content

Instantly share code, notes, and snippets.

@KAmaia
Created April 8, 2020 03:53
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 KAmaia/16b0621986c7aa63bec9297f3fbf602a to your computer and use it in GitHub Desktop.
Save KAmaia/16b0621986c7aa63bec9297f3fbf602a to your computer and use it in GitHub Desktop.
package com.amaiaindustries.gravelbreak;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.BoundingBox;
public class GravelBreak extends JavaPlugin implements Listener {
ArrayList<Material> torches = new ArrayList<Material>(
Arrays.asList(Material.TORCH, Material.WALL_TORCH, Material.REDSTONE_TORCH, Material.REDSTONE_WALL_TORCH));
ArrayList<Block> blocks = new ArrayList<Block>();
Logger logger;
public void onEnable() {
getServer().getPluginManager().registerEvents(this, this);
logger = Logger.getLogger("Minecraft");
sendToConsole("GRAVELBREAAAAAK STARTING UP!");
}
@EventHandler
public void checkForFallingGravel(EntityChangeBlockEvent e) {
World world = e.getBlock().getWorld();
Entity fallingBlock = e.getEntity();
Block block = e.getBlock();
Location breakLoc = new Location(world, 0.0, 0, 0.0);
if (fallingBlock.getType() != EntityType.FALLING_BLOCK) {
// we don't care! SKIP!
return;
}
if (block.getType() != Material.GRAVEL) {
// we don't care...skip!
return;
}
if (isTorchBeneath(world, fallingBlock.getLocation())) {
sendToConsole("isTorchBeneath returned true. Moving on.");
breakLoc = findTorchLocation(world, fallingBlock.getLocation());
} else {
sendToChat("No Torch Found, Returning!");
return;
}
int runs = 0;
breakLoc = new Location(world, breakLoc.getX(), breakLoc.getBlockY(), breakLoc.getZ());
sendToConsole("setting Bounding Box at: " + breakLoc.toVector());
BoundingBox bb = world.getBlockAt(breakLoc).getBoundingBox();
// We know we can't iterate over more than the world height, so we'll take the
// minor performance hit.
while (runs < world.getMaxHeight()) {
Collection<Entity> fblist = world.getNearbyEntities(breakLoc, 1, 1, 1);
if (fblist.isEmpty()) {
sendToConsole("No Entities Found in bounding Box. Returning");
return;
}
for (Entity ent : fblist) {
sendToChat(ent.getUniqueId().toString());
sendToChat("Ent Valid? " + ent.isValid());
if (ent.isValid() ) {
if (ent.getLocation().getBlockY() == breakLoc.getBlockY() + 1) {
ent.remove();
}
} else {
return;
}
}
runs++;
}
}
private boolean isTorchBeneath(World w, Location loc) {
Location scanLoc = new Location(w, loc.getX(), loc.getBlockY(), loc.getZ());
do {
// decrement scanLoc's Y value at the start of every run
scanLoc.setY(scanLoc.getY() - 1);
sendToConsole("Scanning Block: " + scanLoc.toVector() + " For Torches");
Material scanType = scanLoc.getBlock().getType();
if (scanType == Material.AIR) {
// don't care, skip
continue;
} else if (scanType == Material.TORCH) {
sendToConsole("Torch Found At: " + scanLoc.toVector());
return true;
} else
sendToConsole("No Torch Found");
return false;
} while (scanLoc.getY() > 0);
return false;
}
// THIS IS MAGIC TO CLEAN UP MY CODE! NEVER EVER EVER CALL THIS WITHOUT CALLING
// isTorchBeneath() BEFORE HAND!!!
private Location findTorchLocation(World w, Location loc) {
sendToConsole("Entered into Find Torch Location");
Location scanLoc = new Location(w, loc.getX(), loc.getBlockY(), loc.getZ());
Material scanType = w.getBlockAt(scanLoc).getType();
do {
// decrement scanLoc's Y value at the start of every run.
scanLoc.setY(scanLoc.getY() - 1);
scanType = scanLoc.getBlock().getType();
}
// we only care about finding Torches, and this should never run, if
// isTorchBeneath has not been run.
while (scanType != Material.TORCH);
Location retLoc = new Location(w, scanLoc.getX(), scanLoc.getBlockY(), scanLoc.getZ());
sendToConsole("Sending Location: " + retLoc.toVector() + " As a Torch!");
return retLoc;
}
private void sendToChat(String message) {
Bukkit.broadcastMessage(message);
}
private void sendToConsole(String message) {
Bukkit.getConsoleSender().sendMessage(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment