Untitled

 avatar
unknown
plain_text
5 months ago
7.7 kB
5
Indexable
package org.ogfactions.obsidiandestroyer;

import java.util.HashMap;
import java.util.Map;

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.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin implements Listener {

    private final Map<Location, Integer> obsidianDamageMap = new HashMap<>();
    private final int explosionRadius = 2; // Radius to check for obsidian blocks around the explosion

    @Override
    public void onEnable() {
        Bukkit.getServer().getPluginManager().registerEvents(this, this);
        getLogger().info("ObsidianDestroyer Plugin Enabled");
    }

    @EventHandler
    public void onExplosionTnt(EntityExplodeEvent event) {
        if (event.getEntityType() == EntityType.TNT) {
            Location explosionLocation = event.getLocation();

            // Check for obsidian blocks in a radius around the explosion
            for (int x = -explosionRadius; x <= explosionRadius; x++) {
                for (int y = -explosionRadius; y <= explosionRadius; y++) {
                    for (int z = -explosionRadius; z <= explosionRadius; z++) {
                        Location checkLocation = explosionLocation.clone().add(x, y, z);
                        Block block = checkLocation.getBlock();

                        if (block.getType() == Material.OBSIDIAN) {
                            Location blockLocation = block.getLocation();
                            // Round block coordinates to ensure consistency in damage tracking
                            Location roundedLocation = new Location(blockLocation.getWorld(),
                                    blockLocation.getBlockX(), blockLocation.getBlockY(), blockLocation.getBlockZ());

                            int damagePoints = obsidianDamageMap.getOrDefault(roundedLocation, 0);

                            // Increment damage points by 1
                            damagePoints++;

                            // Check if the obsidian block is covered by water
                            boolean isCoveredByWater = isObsidianCoveredByWater(block);

                            // If covered by water, obsidian has 10 health points, else it has 5 health points
                            int maxHealth = isCoveredByWater ? 10 : 5;

                            if (damagePoints >= maxHealth) {
                                block.setType(Material.AIR);
                                obsidianDamageMap.remove(roundedLocation);
                            } else {
                                obsidianDamageMap.put(roundedLocation, damagePoints);
                            }
                        } else {
                        	if (!checkForCannon(explosionLocation, 10)) {
                       		 block.breakNaturally();
                        	}
                        }
                    }
                }
            }
        }
    }
    
    @EventHandler
    public void onExplosionCreeper(EntityExplodeEvent event) {
        if (event.getEntityType() == EntityType.CREEPER) {
            Location explosionLocation = event.getLocation();
            

            // Check for obsidian blocks in a radius around the explosion
            for (int x = -explosionRadius; x <= explosionRadius; x++) {
                for (int y = -explosionRadius; y <= explosionRadius; y++) {
                    for (int z = -explosionRadius; z <= explosionRadius; z++) {
                        Location checkLocation = explosionLocation.clone().add(x, y, z);
                        Block block = checkLocation.getBlock();

                        if (block.getType() == Material.OBSIDIAN) {
                            Location blockLocation = block.getLocation();
                            // Round block coordinates to ensure consistency in damage tracking
                            Location roundedLocation = new Location(blockLocation.getWorld(),
                                    blockLocation.getBlockX(), blockLocation.getBlockY(), blockLocation.getBlockZ());

                            int damagePoints = obsidianDamageMap.getOrDefault(roundedLocation, 0);

                            // Increment damage points by 1
                            damagePoints++;

                            // Check if the obsidian block is covered by water
                            boolean isCoveredByWater = isObsidianCoveredByWater(block);

                            // If covered by water, obsidian has 15 health points, else it has 10 health points
                            int maxHealth = isCoveredByWater ? 15 : 10;

                            if (damagePoints >= maxHealth) {
                                block.setType(Material.AIR);
                                obsidianDamageMap.remove(roundedLocation);
                            } else {
                                obsidianDamageMap.put(roundedLocation, damagePoints);
                            }
                        } else {
                        	block.breakNaturally();
                        }
                    }
                }
            }
        }
    }


    private boolean isObsidianCoveredByWater(Block block) {
        // Get the blocks around the obsidian (6 directions: above, below, left, right, front, back)
        Block[] adjacentBlocks = {
            block.getRelative(0, 1, 0), // above
            block.getRelative(0, -1, 0), // below
            block.getRelative(1, 0, 0), // east
            block.getRelative(-1, 0, 0), // west
            block.getRelative(0, 0, 1), // south
            block.getRelative(0, 0, -1) // north
        };

        for (Block adjacentBlock : adjacentBlocks) {
            if (adjacentBlock.getType() == Material.WATER || adjacentBlock.getType() == Material.LAVA) {
                return true;
            }
        }
        return false;
    }

    @Override
    public void onDisable() {
        getLogger().info("ObsidianDestroyer Plugin Disabled");
    }
    
    public boolean checkForCannon(Location explosionLocation, int radius) {
        World world = explosionLocation.getWorld();
        int redstoneCount = 0;
        int repeaterCount = 0;

        // Loop through a cube around the explosion location with the given radius
        for (int x = -radius; x <= radius; x++) {
            for (int y = -radius; y <= radius; y++) {
                for (int z = -radius; z <= radius; z++) {
                    Block block = world.getBlockAt(
                        explosionLocation.getBlockX() + x,
                        explosionLocation.getBlockY() + y,
                        explosionLocation.getBlockZ() + z
                    );

                    // Check for redstone and repeaters
                    if (block.getType() == Material.REDSTONE_WIRE) {
                        redstoneCount++;
                    } else if (block.getType() == Material.REPEATER) {
                        repeaterCount++;
                    }

                    // If enough blocks are found, return true early
                    if (redstoneCount > 1 && repeaterCount > 1) {
                        return true;
                    }
                }
            }
        }

        // Return false if criteria are not met
        return false;
    }
}
Editor is loading...
Leave a Comment