Files
src
build.gradle
settings.gradle
Code
9 400
src/main/java/com/kodari/effectloot/EffectLootPlugin.java
package com.kodari.effectloot;
import com.cryptomorin.xseries.XMaterial;
import com.cryptomorin.xseries.XPotion;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.ArrayList;
import java.util.List;
public final class EffectLootPlugin extends JavaPlugin {
private LootPickupListener pickupListener;
@Override
public void onEnable() {
saveDefaultConfig();
List<LootEntry> entries = loadLootEntries();
pickupListener = new LootPickupListener(entries);
getServer().getPluginManager().registerEvents(pickupListener, this);
if (getCommand("effectloot") != null) {
EffectLootCommand command = new EffectLootCommand(this);
getCommand("effectloot").setExecutor(command);
getCommand("effectloot").setTabCompleter(command);
}
getLogger().info("Loaded " + entries.size() + " effect loot entr" + (entries.size() == 1 ? "y" : "ies") + ".");
}
void reloadLootEntries() {
reloadConfig();
List<LootEntry> entries = loadLootEntries();
pickupListener.setEntries(entries);
getLogger().info("Loaded " + entries.size() + " effect loot entr" + (entries.size() == 1 ? "y" : "ies") + ".");
}
private List<LootEntry> loadLootEntries() {
List<LootEntry> entries = new ArrayList<>();
ConfigurationSection table = getConfig().getConfigurationSection("loot-table");
if (table == null) {
getLogger().warning("No loot-table section was configured.");
return entries;
}
for (String entryKey : table.getKeys(false)) {
ConfigurationSection entrySection = table.getConfigurationSection(entryKey);
if (entrySection == null) {
continue;
}
String materialName = entrySection.getString("material", "PLAYER_HEAD");
XMaterial material = XMaterial.matchXMaterial(materialName).orElse(null);
if (material == null) {
getLogger().warning("Skipping loot entry '" + entryKey + "': unknown material " + materialName + ".");
continue;
}
List<LootEffect> effects = loadEffects(entryKey, entrySection.getConfigurationSection("effects"));
if (effects.isEmpty()) {
getLogger().warning("Skipping loot entry '" + entryKey + "': no valid effects were configured.");
continue;
}
entries.add(new LootEntry(material, entrySection.getBoolean("consume", true), effects));
}
return entries;
}
private List<LootEffect> loadEffects(String entryKey, ConfigurationSection effectsSection) {
List<LootEffect> effects = new ArrayList<>();
if (effectsSection == null) {
return effects;
}
for (String effectKey : effectsSection.getKeys(false)) {
ConfigurationSection effectSection = effectsSection.getConfigurationSection(effectKey);
if (effectSection == null) {
continue;
}
String typeName = effectSection.getString("type", "SPEED");
XPotion type = XPotion.matchXPotion(typeName).orElse(null);
if (type == null) {
getLogger().warning("Skipping effect '" + effectKey + "' in loot entry '" + entryKey
+ "': unknown potion effect " + typeName + ".");
continue;
}
int durationSeconds = Math.max(1, effectSection.getInt("duration-seconds", 10));
int amplifier = Math.max(0, effectSection.getInt("amplifier", 0));
effects.add(new LootEffect(type, durationSeconds * 20, amplifier));
}
return effects;
}
}