Untitled

 avatar
unknown
plain_text
5 months ago
6.0 kB
6
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);
                            }
                        }
                    }
                }
            }
        }
    }
    
    @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);
                            }
                        }
                    }
                }
            }
        }
    }


    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;
    }
}
Editor is loading...
Leave a Comment