Files
src
build.gradle
Code
6 591
src/main/java/com/wardendrops/WardenDropsPlugin.java
package com.wardendrops;
import com.wardendrops.listeners.SculkShriekerListener;
import com.wardendrops.listeners.WardenDeathListener;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.logging.Level;
public class WardenDropsPlugin extends JavaPlugin {
private NamespacedKey shriekerKey;
private final Map<Location, Long> customShriekers = new HashMap<>();
private File dataFile;
private YamlConfiguration dataConfig;
@Override
public void onEnable() {
saveDefaultConfig();
shriekerKey = new NamespacedKey(this, "warden_shrieker");
dataFile = new File(getDataFolder(), "shriekers.yml");
getServer().getPluginManager().registerEvents(new WardenDeathListener(this), this);
getServer().getPluginManager().registerEvents(new SculkShriekerListener(this), this);
loadShriekers();
getLogger().info("WardenDrops v" + getDescription().getVersion() + " enabled!");
}
@Override
public void onDisable() {
saveShriekers();
}
public NamespacedKey getShriekerKey() {
return shriekerKey;
}
public Map<Location, Long> getCustomShriekers() {
return customShriekers;
}
public void addShrieker(Location loc) {
customShriekers.put(loc, System.currentTimeMillis());
saveShriekers();
}
public void removeShrieker(Location loc) {
customShriekers.remove(loc);
saveShriekers();
}
public boolean isCustomShrieker(Location loc) {
return customShriekers.containsKey(loc);
}
private void loadShriekers() {
if (!dataFile.exists()) return;
dataConfig = YamlConfiguration.loadConfiguration(dataFile);
for (String key : dataConfig.getKeys(false)) {
String worldName = dataConfig.getString(key + ".world");
double x = dataConfig.getDouble(key + ".x");
double y = dataConfig.getDouble(key + ".y");
double z = dataConfig.getDouble(key + ".z");
long placedAt = dataConfig.getLong(key + ".placed-at", System.currentTimeMillis());
if (worldName != null) {
Location loc = new Location(getServer().getWorld(worldName), x, y, z);
customShriekers.put(loc, placedAt);
}
}
getLogger().info("Loaded " + customShriekers.size() + " custom shriekers.");
}
private void saveShriekers() {
if (dataConfig == null) dataConfig = new YamlConfiguration();
for (String key : dataConfig.getKeys(false)) {
dataConfig.set(key, null);
}
int i = 0;
for (Map.Entry<Location, Long> entry : customShriekers.entrySet()) {
Location loc = entry.getKey();
String path = "shrieker-" + (i++);
dataConfig.set(path + ".world", loc.getWorld().getName());
dataConfig.set(path + ".x", loc.getBlockX());
dataConfig.set(path + ".y", loc.getBlockY());
dataConfig.set(path + ".z", loc.getBlockZ());
dataConfig.set(path + ".placed-at", entry.getValue());
}
try {
dataConfig.save(dataFile);
} catch (IOException e) {
getLogger().log(Level.WARNING, "Failed to save shrieker data", e);
}
}
}