Refactoring 2
This commit is contained in:
parent
c1a1978e44
commit
b3916fb312
9 changed files with 311 additions and 222 deletions
|
@ -1,130 +0,0 @@
|
|||
package eu.m724.giants;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class Configuration {
|
||||
private final File file;
|
||||
private final Logger logger;
|
||||
|
||||
String updater;
|
||||
|
||||
boolean ai;
|
||||
|
||||
double attackDamage;
|
||||
int attackDelay;
|
||||
Vector attackReach;
|
||||
int jumpMode;
|
||||
int jumpCondition;
|
||||
int jumpDelay;
|
||||
double jumpHeight = -1;
|
||||
|
||||
double chance;
|
||||
List<String> worldBlacklist;
|
||||
Set<PotionEffect> effects = new HashSet<>();
|
||||
Set<Drop> drops = new HashSet<>();
|
||||
|
||||
public Configuration(Plugin plugin, File file) {
|
||||
this.file = file;
|
||||
this.logger = Logger.getLogger(plugin.getLogger().getName() + ".Configuration");
|
||||
}
|
||||
|
||||
public void load() {
|
||||
YamlConfiguration config = YamlConfiguration.loadConfiguration(file);
|
||||
|
||||
updater = config.getString("updater", "true");
|
||||
if (updater.equalsIgnoreCase("true")) {
|
||||
updater = "release";
|
||||
} else if (updater.equalsIgnoreCase("false")) {
|
||||
updater = null;
|
||||
}
|
||||
|
||||
ai = config.getBoolean("ai");
|
||||
|
||||
chance = config.getDouble("chance");
|
||||
attackDamage = config.getDouble("attackDamage");
|
||||
attackDelay = config.getInt("attackDelay");
|
||||
|
||||
jumpMode = config.getInt("jumpMode");
|
||||
jumpCondition = config.getInt("jumpCondition");
|
||||
jumpDelay = config.getInt("jumpDelay");
|
||||
jumpHeight = config.getDouble("jumpHeight", defaultJumpHeight());
|
||||
|
||||
double _attackReach = config.getDouble("attackReach");
|
||||
double _attackVerticalReach = config.getDouble("attackVerticalReach");
|
||||
attackReach = new Vector(_attackReach, _attackVerticalReach, _attackReach);
|
||||
|
||||
worldBlacklist = config.getStringList("blacklist");
|
||||
|
||||
for (String line : config.getStringList("effects")) {
|
||||
String[] parts = line.split(":");
|
||||
|
||||
try {
|
||||
PotionEffectType effectType = PotionEffectType.getByName(parts[0]);
|
||||
if (effectType == null) {
|
||||
throw new IllegalArgumentException("Invalid PotionEffectType");
|
||||
}
|
||||
int amplifier = Integer.parseInt(parts[1]);
|
||||
effects.add(new PotionEffect(effectType, Integer.MAX_VALUE, amplifier));
|
||||
} catch (IllegalArgumentException e) {
|
||||
logger.warning("Parsing a potion effect failed:");
|
||||
logger.warning(line);
|
||||
logger.warning(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
for (Map<?, ?> dropMap : config.getMapList("drops")) {
|
||||
try {
|
||||
ItemStack itemStack;
|
||||
if (dropMap.containsKey("itemStack")) {
|
||||
itemStack = (ItemStack) dropMap.get("itemStack");
|
||||
} else {
|
||||
Material material = Material.getMaterial((String) dropMap.get("material"));
|
||||
if (material == null) {
|
||||
throw new IllegalArgumentException("Invalid Material");
|
||||
}
|
||||
itemStack = new ItemStack(material, 1);
|
||||
}
|
||||
|
||||
int min = (int) dropMap.get("quantityMin");
|
||||
int max = (int) dropMap.get("quantityMax");
|
||||
double chance;
|
||||
try {
|
||||
chance = (double) dropMap.get("chance");
|
||||
} catch (ClassCastException e) { // pointlessest error ever
|
||||
chance = ((Integer) dropMap.get("chance")).doubleValue();
|
||||
}
|
||||
|
||||
drops.add(new Drop(itemStack, min, max, chance));
|
||||
} catch (IllegalArgumentException e) {
|
||||
logger.warning("Parsing a drop failed:");
|
||||
logger.warning(e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public double defaultJumpHeight() {
|
||||
switch (jumpMode) {
|
||||
case 1:
|
||||
case 2:
|
||||
return 0.42;
|
||||
case 3:
|
||||
case 4:
|
||||
return 1.2;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
package eu.m724.giants;
|
||||
|
||||
import eu.m724.giants.ai.GiantProcessor;
|
||||
import eu.m724.giants.configuration.Configuration;
|
||||
import eu.m724.giants.updater.PluginUpdater;
|
||||
import eu.m724.giants.updater.UpdateCommand;
|
||||
import eu.m724.jarupdater.verify.VerificationException;
|
||||
|
@ -16,7 +18,7 @@ import java.io.InputStream;
|
|||
public class GiantsPlugin extends JavaPlugin implements CommandExecutor {
|
||||
private final File configFile = new File(getDataFolder(), "config.yml");
|
||||
|
||||
private final Configuration configuration = new Configuration(this, configFile);
|
||||
private static Configuration configuration;
|
||||
private final GiantProcessor giantProcessor = new GiantProcessor(this, configuration);
|
||||
|
||||
@Override
|
||||
|
@ -25,19 +27,18 @@ public class GiantsPlugin extends JavaPlugin implements CommandExecutor {
|
|||
saveResource("config.yml", false);
|
||||
}
|
||||
|
||||
configuration.load();
|
||||
configuration = Configuration.load(this, configFile);
|
||||
|
||||
giantProcessor.start();
|
||||
|
||||
// updater
|
||||
PluginUpdater updater = null;
|
||||
|
||||
if (configuration.updater != null) {
|
||||
UpdateCommand updateCommand = null;
|
||||
if (configuration.updaterEnabled()) {
|
||||
try (InputStream keyInputStream = getResource("verifies_downloaded_jars.pem")) {
|
||||
updater = PluginUpdater.build(this, getFile(), configuration.updater, keyInputStream);
|
||||
updater = PluginUpdater.build(this, getFile(), configuration.updaterChannel(), keyInputStream);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
getLogger().severe("Failed to load updater");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
if (updater != null) {
|
||||
|
@ -52,20 +53,24 @@ public class GiantsPlugin extends JavaPlugin implements CommandExecutor {
|
|||
|
||||
}
|
||||
updater.initNotifier();
|
||||
updateCommand = new UpdateCommand(updater);
|
||||
}
|
||||
}
|
||||
|
||||
UpdateCommand updateCommand = new UpdateCommand(updater);
|
||||
}
|
||||
|
||||
getCommand("giants").setExecutor(new GiantsCommand(this, updateCommand));
|
||||
|
||||
Metrics metrics = new Metrics(this, 14131);
|
||||
metrics.addCustomChart(new SimplePie("jump_mode", () -> String.valueOf(configuration.jumpMode)));
|
||||
metrics.addCustomChart(new SimplePie("jump_condition", () -> String.valueOf(configuration.jumpCondition)));
|
||||
metrics.addCustomChart(new SimplePie("jump_delay", () -> String.valueOf(configuration.jumpDelay)));
|
||||
metrics.addCustomChart(new SimplePie("jump_height", () -> String.valueOf(configuration.jumpHeight)));
|
||||
metrics.addCustomChart(new SimplePie("jump_mode", () -> String.valueOf(configuration.jumpMode())));
|
||||
metrics.addCustomChart(new SimplePie("jump_condition", () -> String.valueOf(configuration.jumpCondition())));
|
||||
metrics.addCustomChart(new SimplePie("jump_delay", () -> String.valueOf(configuration.jumpDelay())));
|
||||
metrics.addCustomChart(new SimplePie("jump_height", () -> String.valueOf(configuration.jumpHeight())));
|
||||
}
|
||||
|
||||
public static Configuration getConfiguration() {
|
||||
return configuration;
|
||||
}
|
||||
|
||||
// TODO api, untested
|
||||
|
||||
/**
|
||||
|
|
51
src/main/java/eu/m724/giants/ai/GiantJumper.java
Normal file
51
src/main/java/eu/m724/giants/ai/GiantJumper.java
Normal file
|
@ -0,0 +1,51 @@
|
|||
package eu.m724.giants.ai;
|
||||
|
||||
import eu.m724.giants.GiantsPlugin;
|
||||
import eu.m724.giants.configuration.Configuration;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class GiantJumper {
|
||||
private final Configuration configuration = GiantsPlugin.getConfiguration();
|
||||
|
||||
private final Map<Entity, Long> giantLastJump = new HashMap<>();
|
||||
|
||||
void processJump(Entity giant, Location prevLocation, Location location, Location targetLocation) {
|
||||
long now = System.currentTimeMillis();
|
||||
if (now - giantLastJump.getOrDefault(giant, 0L) < configuration.jumpDelay()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (giant.isOnGround()) {
|
||||
giantLastJump.put(giant, now);
|
||||
if (configuration.jumpCondition() == 0) {
|
||||
if (targetLocation.subtract(location).getY() > 0) {
|
||||
jump(giant);
|
||||
}
|
||||
} else if (configuration.jumpCondition() == 1) {
|
||||
Location delta = prevLocation.subtract(location);
|
||||
if (targetLocation.subtract(location).getY() > 0 && (delta.getX() == 0 || delta.getZ() == 0)) {
|
||||
jump(giant);
|
||||
}
|
||||
} else if (configuration.jumpCondition() == 2) {
|
||||
Location delta = prevLocation.subtract(location);
|
||||
if (delta.getX() == 0 || delta.getZ() == 0) {
|
||||
jump(giant);
|
||||
}
|
||||
|
||||
} // I could probably simplify that code
|
||||
}
|
||||
}
|
||||
|
||||
private void jump(Entity giant) {
|
||||
if (configuration.jumpMode() == 1) {
|
||||
giant.setVelocity(new Vector(0, configuration.jumpHeight(), 0));
|
||||
} else if (configuration.jumpMode() == 2) {
|
||||
giant.teleport(giant.getLocation().add(0, configuration.jumpHeight(), 0));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
package eu.m724.giants;
|
||||
package eu.m724.giants.ai;
|
||||
|
||||
import eu.m724.giants.Drop;
|
||||
import eu.m724.giants.configuration.Configuration;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.entity.*;
|
||||
|
@ -43,9 +45,9 @@ public class GiantProcessor implements Listener {
|
|||
this.huskKey = new NamespacedKey(plugin, "husk");
|
||||
}
|
||||
|
||||
void start() {
|
||||
if (configuration.ai) {
|
||||
new GiantRunnable(this).runTaskTimer(plugin, 0, configuration.attackDelay);
|
||||
public void start() {
|
||||
if (configuration.aiEnabled()) {
|
||||
new GiantTicker(this).schedule(plugin);
|
||||
}
|
||||
|
||||
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
||||
|
@ -95,8 +97,8 @@ public class GiantProcessor implements Listener {
|
|||
}
|
||||
|
||||
public void applyGiantsLogic(Giant giant) {
|
||||
if (configuration.ai) {
|
||||
// the husk basically moves the giant
|
||||
if (configuration.aiEnabled()) {
|
||||
// The husk moves the giant. That's the magic.
|
||||
LivingEntity passenger = (LivingEntity) giant.getWorld().spawnEntity(giant.getLocation(), EntityType.HUSK);
|
||||
new PotionEffect(PotionEffectType.INVISIBILITY, Integer.MAX_VALUE, 1).apply(passenger);
|
||||
passenger.setInvulnerable(true);
|
||||
|
@ -108,7 +110,7 @@ public class GiantProcessor implements Listener {
|
|||
trackedHusks.add((Husk) passenger);
|
||||
}
|
||||
|
||||
configuration.effects.forEach(giant::addPotionEffect);
|
||||
configuration.potionEffects().forEach(giant::addPotionEffect);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
|
@ -121,11 +123,11 @@ public class GiantProcessor implements Listener {
|
|||
applyGiantsLogic(giant);
|
||||
}
|
||||
|
||||
if (configuration.worldBlacklist.contains(e.getLocation().getWorld().getName()))
|
||||
if (configuration.worldBlacklist().contains(e.getLocation().getWorld().getName()))
|
||||
return;
|
||||
|
||||
if (e.getEntityType() == EntityType.ZOMBIE) {
|
||||
if (configuration.chance > random.nextDouble()) {
|
||||
if (configuration.spawnChance() > random.nextDouble()) {
|
||||
logger.fine("Trying to spawn a Giant by chance at " + e.getLocation());
|
||||
if (isSpawnableAt(e.getLocation())) {
|
||||
logger.fine("Spawned a Giant by chance at " + e.getLocation());
|
||||
|
@ -144,7 +146,7 @@ public class GiantProcessor implements Listener {
|
|||
Location location = entity.getLocation();
|
||||
logger.fine("A Giant died at " + location);
|
||||
|
||||
for (Drop drop : configuration.drops) {
|
||||
for (Drop drop : configuration.drops()) {
|
||||
logger.fine("Rolling a drop: " + drop.itemStack().toString());
|
||||
|
||||
drop.dropAt(location);
|
|
@ -1,26 +1,29 @@
|
|||
package eu.m724.giants;
|
||||
package eu.m724.giants.ai;
|
||||
|
||||
import eu.m724.giants.GiantsPlugin;
|
||||
import eu.m724.giants.configuration.Configuration;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.*;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Ticks giants
|
||||
*/
|
||||
public class GiantRunnable extends BukkitRunnable {
|
||||
public class GiantTicker extends BukkitRunnable {
|
||||
private final Configuration configuration = GiantsPlugin.getConfiguration();
|
||||
private final GiantJumper jumper = new GiantJumper();
|
||||
|
||||
private final GiantProcessor giantProcessor;
|
||||
private final Configuration configuration;
|
||||
|
||||
private final Map<Entity, Long> giantLastJump = new HashMap<>();
|
||||
|
||||
public GiantRunnable(GiantProcessor giantProcessor) {
|
||||
public GiantTicker(GiantProcessor giantProcessor) {
|
||||
this.giantProcessor = giantProcessor;
|
||||
this.configuration = giantProcessor.configuration;
|
||||
}
|
||||
|
||||
void schedule(Plugin plugin) {
|
||||
this.runTaskTimer(plugin, 0, configuration.attackDelay());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -33,13 +36,13 @@ public class GiantRunnable extends BukkitRunnable {
|
|||
if (giant instanceof Giant) {
|
||||
if (giant.isValid()) { // TODO reconsider
|
||||
giant.getWorld().getNearbyEntities(
|
||||
giant.getBoundingBox().expand(configuration.attackReach),
|
||||
giant.getBoundingBox().expand(configuration.attackReach()),
|
||||
e -> (e instanceof Player && !e.isInvulnerable())
|
||||
).forEach(p -> ((Player) p).damage(configuration.attackDamage, giant));
|
||||
).forEach(p -> ((Player) p).damage(configuration.attackDamage(), giant));
|
||||
giant.setRotation(huskLocation.getYaw(), huskLocation.getPitch());
|
||||
|
||||
// jumping
|
||||
if (configuration.jumpMode != 0) {
|
||||
// TODO move whole into that class?
|
||||
if (configuration.jumpMode() != 0) {
|
||||
// tracking location is only required for jumping
|
||||
Location prevLocation = giantProcessor.giantLocationMap.get(giant);
|
||||
Location location = giant.getLocation();
|
||||
|
@ -50,7 +53,7 @@ public class GiantRunnable extends BukkitRunnable {
|
|||
|
||||
LivingEntity target = husk.getTarget();
|
||||
if (target != null) {
|
||||
processJump(giant, prevLocation, location, target.getLocation());
|
||||
jumper.processJump(giant, prevLocation, location, target.getLocation());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -68,38 +71,5 @@ public class GiantRunnable extends BukkitRunnable {
|
|||
}
|
||||
}
|
||||
|
||||
private void processJump(Entity giant, Location prevLocation, Location location, Location targetLocation) {
|
||||
long now = System.currentTimeMillis();
|
||||
if (now - giantLastJump.getOrDefault(giant, 0L) < configuration.jumpDelay) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (giant.isOnGround()) {
|
||||
giantLastJump.put(giant, now);
|
||||
if (configuration.jumpCondition == 0) {
|
||||
if (targetLocation.subtract(location).getY() > 0) {
|
||||
jump(giant);
|
||||
}
|
||||
} else if (configuration.jumpCondition == 1) {
|
||||
Location delta = prevLocation.subtract(location);
|
||||
if (targetLocation.subtract(location).getY() > 0 && (delta.getX() == 0 || delta.getZ() == 0)) {
|
||||
jump(giant);
|
||||
}
|
||||
} else if (configuration.jumpCondition == 2) {
|
||||
Location delta = prevLocation.subtract(location);
|
||||
if (delta.getX() == 0 || delta.getZ() == 0) {
|
||||
jump(giant);
|
||||
}
|
||||
|
||||
} // I could probably simplify that code
|
||||
}
|
||||
}
|
||||
|
||||
private void jump(Entity giant) {
|
||||
if (configuration.jumpMode == 1) {
|
||||
giant.setVelocity(new Vector(0, configuration.jumpHeight, 0));
|
||||
} else if (configuration.jumpMode == 2) {
|
||||
giant.teleport(giant.getLocation().add(0, configuration.jumpHeight, 0));
|
||||
}
|
||||
}
|
||||
}
|
128
src/main/java/eu/m724/giants/configuration/Configuration.java
Normal file
128
src/main/java/eu/m724/giants/configuration/Configuration.java
Normal file
|
@ -0,0 +1,128 @@
|
|||
package eu.m724.giants.configuration;
|
||||
|
||||
import eu.m724.giants.Drop;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
import org.bukkit.util.Vector;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public record Configuration(
|
||||
boolean updaterEnabled,
|
||||
String updaterChannel,
|
||||
|
||||
boolean aiEnabled,
|
||||
|
||||
double attackDamage,
|
||||
int attackDelay,
|
||||
Vector attackReach,
|
||||
|
||||
int jumpMode,
|
||||
int jumpCondition,
|
||||
int jumpDelay,
|
||||
double jumpHeight,
|
||||
|
||||
double spawnChance,
|
||||
List<String> worldBlacklist,
|
||||
List<PotionEffect> potionEffects,
|
||||
List<Drop> drops
|
||||
|
||||
) {
|
||||
// TODO not use logger here
|
||||
private static Logger LOGGER;
|
||||
|
||||
public static Configuration load(Plugin plugin, File file) {
|
||||
LOGGER = Logger.getLogger(plugin.getLogger().getName() + ".Configuration");
|
||||
|
||||
YamlConfiguration config = YamlConfiguration.loadConfiguration(file);
|
||||
|
||||
boolean updaterEnabled = true;
|
||||
String updaterChannel = config.getString("updater", "release");
|
||||
|
||||
if (updaterChannel.equalsIgnoreCase("false")) {
|
||||
updaterEnabled = false;
|
||||
}
|
||||
|
||||
|
||||
boolean aiEnabled = config.getBoolean("ai");
|
||||
|
||||
double attackDamage = config.getDouble("attackDamage");
|
||||
int attackDelay = config.getInt("attackDelay");
|
||||
Vector attackReach = new Vector(
|
||||
config.getDouble("attackReach"),
|
||||
config.getDouble("attackVerticalReach"),
|
||||
config.getDouble("attackReach")
|
||||
);
|
||||
|
||||
int jumpMode = config.getInt("jumpMode");
|
||||
int jumpCondition = config.getInt("jumpCondition");
|
||||
int jumpDelay = config.getInt("jumpDelay");
|
||||
double jumpHeight = config.getDouble("jumpHeight", defaultJumpHeight(jumpMode));
|
||||
|
||||
double spawnChance = config.getDouble("chance");
|
||||
|
||||
List<String> worldBlacklist = config.getStringList("blacklist");
|
||||
|
||||
List<PotionEffect> potionEffects = config.getStringList("effects").stream()
|
||||
.map(Configuration::makePotionEffect)
|
||||
.filter(Objects::nonNull)
|
||||
.toList();
|
||||
|
||||
List<Drop> drops = config.getMapList("drops").stream()
|
||||
.map(Configuration::makeDrop)
|
||||
.filter(Objects::nonNull)
|
||||
.toList();
|
||||
|
||||
return new Configuration(
|
||||
updaterEnabled, updaterChannel, aiEnabled, attackDamage, attackDelay, attackReach, jumpMode, jumpCondition, jumpDelay, jumpHeight, spawnChance, worldBlacklist, potionEffects, drops
|
||||
);
|
||||
}
|
||||
|
||||
private static double defaultJumpHeight(int jumpMode) {
|
||||
return switch (jumpMode) {
|
||||
case 1, 2 -> 0.42;
|
||||
case 3, 4 -> 1.2;
|
||||
default -> -1;
|
||||
};
|
||||
}
|
||||
|
||||
private static Drop makeDrop(Map<?, ?> dropMap) {
|
||||
try {
|
||||
return ListParsers.makeDrop(dropMap);
|
||||
} catch (ParseException e) {
|
||||
LOGGER.warning("Failed to parse drop:");
|
||||
LOGGER.warning(" At: " + dropMap);
|
||||
LOGGER.warning(" " + e.getMessage());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static PotionEffect makePotionEffect(String line) {
|
||||
try {
|
||||
return ListParsers.makePotionEffect(line);
|
||||
} catch (ParseException e) {
|
||||
LOGGER.warning("Failed to parse potion effect:");
|
||||
LOGGER.warning(" At line: " + line);
|
||||
LOGGER.warning(" " + e.getMessage());
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
static void assertParse(boolean assertion, String message) throws ParseException {
|
||||
if (!assertion) throw new ParseException(message);
|
||||
}
|
||||
|
||||
public static class ParseException extends Exception {
|
||||
public ParseException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
}
|
69
src/main/java/eu/m724/giants/configuration/ListParsers.java
Normal file
69
src/main/java/eu/m724/giants/configuration/ListParsers.java
Normal file
|
@ -0,0 +1,69 @@
|
|||
package eu.m724.giants.configuration;
|
||||
|
||||
import eu.m724.giants.Drop;
|
||||
import eu.m724.giants.configuration.Configuration.ParseException;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.potion.PotionEffect;
|
||||
import org.bukkit.potion.PotionEffectType;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import static eu.m724.giants.configuration.Configuration.assertParse;
|
||||
|
||||
public class ListParsers {
|
||||
public static PotionEffect makePotionEffect(String line) throws ParseException {
|
||||
if (line == null || line.trim().isEmpty()) {
|
||||
throw new ParseException("Cannot parse empty or null line into a PotionEffect.");
|
||||
}
|
||||
|
||||
String[] parts = line.split(":", 2);
|
||||
assertParse(parts.length == 2, "Invalid PotionEffect format (expected 'TYPE:AMPLIFIER')");
|
||||
|
||||
PotionEffectType effectType = PotionEffectType.getByName(parts[0].trim());
|
||||
assertParse(effectType != null, "Unknown PotionEffectType: " + parts[0].trim());
|
||||
|
||||
int amplifier;
|
||||
try {
|
||||
amplifier = Integer.parseInt(parts[1].trim());
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new ParseException("Invalid amplifier format (expected integer): " + parts[1].trim());
|
||||
}
|
||||
|
||||
assertParse(amplifier > 0, "Amplifier must be bigger than 0, is: " + amplifier);
|
||||
assertParse(amplifier < 256, "Amplifier must be at most 255, is: " + amplifier);
|
||||
|
||||
return new PotionEffect(effectType, Integer.MAX_VALUE, amplifier);
|
||||
}
|
||||
|
||||
// TODO refactor this
|
||||
public static Drop makeDrop(Map<?, ?> dropMap) throws ParseException {
|
||||
ItemStack itemStack;
|
||||
if (dropMap.containsKey("itemStack")) {
|
||||
itemStack = (ItemStack) dropMap.get("itemStack");
|
||||
} else {
|
||||
Material material = Material.getMaterial((String) dropMap.get("material"));
|
||||
assertParse(material != null, "Invalid Material: " + dropMap.get("material"));
|
||||
|
||||
itemStack = new ItemStack(material, 1);
|
||||
}
|
||||
|
||||
int min = (int) dropMap.get("quantityMin");
|
||||
int max = (int) dropMap.get("quantityMax");
|
||||
assertParse(min > 0, "Minimum quantity must be more than 0, is: " + min);
|
||||
assertParse(max > 0, "Maximum quantity must be more than 0, is: " + max);
|
||||
assertParse(min < 100, "Minimum quantity must be less than 100, is: " + min);
|
||||
assertParse(max < 100, "Maximum quantity must be less than 100, is: " + max);
|
||||
|
||||
double chance = ((Number) dropMap.get("chance")).doubleValue();
|
||||
|
||||
if (chance > 1 && chance <= 100) {
|
||||
chance /= 100; // user might have misunderstood
|
||||
}
|
||||
|
||||
assertParse(chance > 0, "Chance must be more than 0, is: " + chance);
|
||||
assertParse(chance <= 1, "Chance must be at most 1, is: " + chance);
|
||||
|
||||
return new Drop(itemStack, min, max, chance);
|
||||
}
|
||||
}
|
|
@ -9,7 +9,6 @@ import org.bukkit.event.player.PlayerJoinEvent;
|
|||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.scheduler.BukkitRunnable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.CompletionException;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
|
@ -36,11 +35,7 @@ public class UpdateNotifier extends BukkitRunnable implements Listener { // TODO
|
|||
try {
|
||||
latestVersion = updater.getLatestVersion().join();
|
||||
} catch (CompletionException e) {
|
||||
Throwable ex = e.getCause();
|
||||
|
||||
if (ex instanceof IOException)
|
||||
plugin.getLogger().info("error trying to contact update server: " + ex.getMessage());
|
||||
else e.printStackTrace();
|
||||
plugin.getLogger().info("Error trying to contact update server: " + e.getCause().getMessage());
|
||||
}
|
||||
|
||||
if (latestVersion == null) return;
|
||||
|
@ -53,7 +48,6 @@ public class UpdateNotifier extends BukkitRunnable implements Listener { // TODO
|
|||
}
|
||||
|
||||
updateConsumer.accept(latestVersion);
|
||||
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
|
|
|
@ -10,6 +10,20 @@ updater: true
|
|||
# If disabled, the giant will not move or attack
|
||||
ai: true
|
||||
|
||||
# In hearts, 0.5 is half a heart
|
||||
attackDamage: 1.0
|
||||
|
||||
# Attack delay / speed, in ticks
|
||||
# 20 is 1 second
|
||||
attackDelay: 20
|
||||
|
||||
# Self-explanatory, 0 will attack only colliding (touching) entities
|
||||
# There's no wall check yet, so it will hit through walls
|
||||
attackReach: 2
|
||||
|
||||
# Vertical reach
|
||||
attackVerticalReach: 1
|
||||
|
||||
# Makes giants jump. Very experimental.
|
||||
# I prefer velocity mode
|
||||
# 0 - disabled
|
||||
|
@ -27,20 +41,6 @@ jumpDelay: 200
|
|||
# -1: auto
|
||||
jumpHeight: -1
|
||||
|
||||
# In hearts, 0.5 is half a heart
|
||||
attackDamage: 1.0
|
||||
|
||||
# Attack delay / speed, in ticks
|
||||
# 20 is 1 second
|
||||
attackDelay: 20
|
||||
|
||||
# Self-explanatory, 0 will attack only colliding (touching) entities
|
||||
# There's no wall check yet, so it will hit through walls
|
||||
attackReach: 2
|
||||
|
||||
# Vertical reach
|
||||
attackVerticalReach: 1
|
||||
|
||||
###
|
||||
|
||||
# Chance of a zombie becoming a giant. This is per each zombie spawn, natural or not.
|
||||
|
|
Loading…
Add table
Reference in a new issue