Files
src
build.gradle
settings.gradle
Code
5 382
src/main/java/com/rivex/meteor/RivexMeteor.java
package com.rivex.meteor;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XSound;
import com.cryptomorin.xseries.particles.XParticle;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.Chest;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockExplodeEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.inventory.InventoryMoveItemEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitRunnable;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
public final class RivexMeteor extends JavaPlugin implements Listener {
private File dataFile;
private YamlConfiguration data;
private long nextMeteor;
private boolean falling;
@Override
public void onEnable() {
saveDefaultConfig();
dataFile = new File(getDataFolder(), "data.yml");
data = YamlConfiguration.loadConfiguration(dataFile);
nextMeteor = data.getLong("next-meteor", 0);
if (nextMeteor <= 0) scheduleNext();
getServer().getPluginManager().registerEvents(this, this);
getServer().getScheduler().runTaskTimer(this, this::tick, 20L, 20L);
}
@Override
public void onDisable() {
if (data != null) saveData();
}
private void saveData() {
data.set("next-meteor", nextMeteor);
try {
data.save(dataFile);
} catch (IOException ex) {
getLogger().severe("Could not save meteor data: " + ex.getMessage());
}
}
private void scheduleNext() {
int min = Math.max(1, getConfig().getInt("interval.min-seconds", 1800));
int max = Math.max(min, getConfig().getInt("interval.max-seconds", 3600));
nextMeteor = System.currentTimeMillis() + randomInt(min, max) * 1000L;
saveData();
}
private void tick() {
if (falling || System.currentTimeMillis() < nextMeteor) return;
World world = Bukkit.getWorld(getConfig().getString("world", "world"));
List<Player> players = world == null ? List.of() : world.getPlayers();
if (!players.isEmpty()) {
int min = Math.max(0, getConfig().getInt("spawn.min-distance", 30));
int max = Math.max(min, Math.min(10000, getConfig().getInt("spawn.max-distance", 100)));
min = Math.min(min, max);
int attempts = Math.max(1, Math.min(100, getConfig().getInt("spawn.attempts", 20)));
for (int i = 0; i < attempts; i++) {
Player player = players.get(ThreadLocalRandom.current().nextInt(players.size()));
double angle = ThreadLocalRandom.current().nextDouble(Math.PI * 2);
int distance = randomInt(min, max);
int x = player.getLocation().getBlockX() + (int) Math.round(Math.cos(angle) * distance);
int z = player.getLocation().getBlockZ() + (int) Math.round(Math.sin(angle) * distance);
if (!world.isChunkLoaded(x >> 4, z >> 4)) continue;
Block target = landingBlock(world, x, z);
if (target != null && startMeteor(target)) {
scheduleNext();
return;
}
}
}
nextMeteor = System.currentTimeMillis() + Math.max(1, getConfig().getInt("interval.retry-seconds", 60)) * 1000L;
saveData();
}
private Block landingBlock(World world, int x, int z) {
Block ground = world.getHighestBlockAt(x, z);
Block target = ground.getRelative(0, 1, 0);
if (!world.getWorldBorder().isInside(target.getLocation()) || !ground.getType().isSolid()
|| !target.isEmpty() || target.getY() >= world.getMaxHeight() - 1) return null;
Material chest = XMaterial.matchXMaterial("CHEST").map(XMaterial::parseMaterial).orElse(null);
if (chest == null) return null;
for (int[] offset : new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}) {
if (target.getRelative(offset[0], 0, offset[1]).getType() == chest) return null;
}
return target;
}
private boolean startMeteor(Block target) {
if (falling) return false;
falling = true;
Location destination = target.getLocation().add(0.5, 0.5, 0.5);
broadcast("incoming", destination);
new BukkitRunnable() {
int step;
@Override
public void run() {
if (!target.getChunk().isLoaded() || !target.isEmpty()) {
falling = false;
cancel();
return;
}
Location trail = destination.clone().add(0, 25 - step * 2.5, 0);
particle("effects.particle", "FLAME", trail, 12);
if (++step < 11) return;
cancel();
falling = false;
Material chestMaterial = XMaterial.matchXMaterial("CHEST").map(XMaterial::parseMaterial).orElse(null);
if (chestMaterial == null || landingBlock(target.getWorld(), target.getX(), target.getZ()) == null) return;
target.setType(chestMaterial);
if (!(target.getState() instanceof Chest chest)) return;
fillChest(chest);
particle("effects.impact-particle", "EXPLOSION", destination, 3);
XSound.matchXSound(getConfig().getString("effects.sound", "ENTITY_GENERIC_EXPLODE"))
.ifPresent(sound -> sound.play(destination));
broadcast("landed", destination);
}
}.runTaskTimer(this, 0L, 2L);
return true;
}
private void particle(String path, String fallback, Location location, int count) {
XParticle.of(getConfig().getString(path, fallback)).ifPresent(p ->
location.getWorld().spawnParticle(p.get(), location, count, 0.3, 0.3, 0.3, 0));
}
private void fillChest(Chest chest) {
ConfigurationSection rewards = getConfig().getConfigurationSection("rewards");
if (rewards == null) return;
String path = chestPath(chest.getBlock());
int index = 0;
for (String key : rewards.getKeys(false)) {
ConfigurationSection reward = rewards.getConfigurationSection(key);
if (reward == null || ThreadLocalRandom.current().nextDouble(100) >= reward.getDouble("chance", 100)) continue;
String type = reward.getString("type", "item");
if (type.equalsIgnoreCase("item")) {
ItemStack item = XMaterial.matchXMaterial(reward.getString("material", "STONE"))
.map(XMaterial::parseItem).orElse(null);
if (item == null) {
getLogger().warning("Invalid material for reward " + key);
continue;
}
int min = Math.max(1, Math.min(item.getMaxStackSize(), reward.getInt("min", 1)));
int max = Math.max(min, Math.min(item.getMaxStackSize(), reward.getInt("max", min)));
item.setAmount(randomInt(min, max));
ItemMeta meta = item.getItemMeta();
if (meta != null) {
if (reward.isString("name")) meta.setDisplayName(color(reward.getString("name")));
if (reward.isList("lore")) meta.setLore(reward.getStringList("lore").stream().map(this::color).toList());
item.setItemMeta(meta);
}
chest.getBlockInventory().addItem(item).values().forEach(left -> chest.getWorld().dropItemNaturally(chest.getLocation(), left));
} else if (type.equalsIgnoreCase("money")) {
int min = Math.max(0, reward.getInt("min", 0));
int max = Math.max(min, reward.getInt("max", min));
data.set(path + "." + index + ".type", "money");
data.set(path + "." + index++ + ".amount", randomInt(min, max));
} else if (type.equalsIgnoreCase("command") && reward.isString("command")) {
data.set(path + "." + index + ".type", "command");
data.set(path + "." + index++ + ".command", reward.getString("command"));
}
}
if (index > 0) saveData();
}
private String chestPath(Block block) {
return "chests." + block.getWorld().getUID() + "." + block.getX() + "_" + block.getY() + "_" + block.getZ();
}
@EventHandler
public void onOpen(InventoryOpenEvent event) {
if (!(event.getPlayer() instanceof Player player) || !(event.getInventory().getHolder() instanceof Chest chest)) return;