From ff9f712d65db94d87d905175dedfceaee5b4cb39 Mon Sep 17 00:00:00 2001 From: md678685 <1917406+md678685@users.noreply.github.com> Date: Mon, 5 Aug 2019 16:12:22 +0100 Subject: [PATCH] Add per-projectile permissions and speed argument to /fireball --- .../essentials/commands/Commandfireball.java | 78 ++++++++++++------- Essentials/src/plugin.yml | 2 +- 2 files changed, 52 insertions(+), 28 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandfireball.java b/Essentials/src/com/earth2me/essentials/commands/Commandfireball.java index 19d579119..029f1cb9f 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandfireball.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandfireball.java @@ -1,6 +1,8 @@ package com.earth2me.essentials.commands; import com.earth2me.essentials.User; +import com.earth2me.essentials.utils.FloatUtil; +import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; import org.bukkit.Server; import org.bukkit.entity.*; @@ -8,50 +10,72 @@ import org.bukkit.util.Vector; import java.util.Collections; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +import static com.earth2me.essentials.I18n.tl; public class Commandfireball extends EssentialsCommand { + private static final Map> types = ImmutableMap.>builder() + .put("fireball", Fireball.class) + .put("small", SmallFireball.class) + .put("large", LargeFireball.class) + .put("dragon", DragonFireball.class) + .put("arrow", Arrow.class) + .put("skull", WitherSkull.class) + .put("egg", Egg.class) + .put("snowball", Snowball.class) + .put("expbottle", ThrownExpBottle.class) + .put("splashpotion", SplashPotion.class) + .put("lingeringpotion", LingeringPotion.class) + .build(); + public Commandfireball() { super("fireball"); } @Override protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { - Class type = Fireball.class; - Projectile projectile; - int speed = 2; - if (args.length > 0) { - if (args[0].equalsIgnoreCase("small")) { - type = SmallFireball.class; - } else if (args[0].equalsIgnoreCase("arrow")) { - type = Arrow.class; - } else if (args[0].equalsIgnoreCase("skull")) { - type = WitherSkull.class; - } else if (args[0].equalsIgnoreCase("egg")) { - type = Egg.class; - } else if (args[0].equalsIgnoreCase("snowball")) { - type = Snowball.class; - } else if (args[0].equalsIgnoreCase("expbottle")) { - type = ThrownExpBottle.class; - } else if (args[0].equalsIgnoreCase("large")) { - type = LargeFireball.class; - } else if (args[0].equalsIgnoreCase("splashpotion")) { - type = SplashPotion.class; - } else if (args[0].equalsIgnoreCase("lingeringpotion")) { - type = LingeringPotion.class; - } else if (args[0].equalsIgnoreCase("dragon")) { - type = DragonFireball.class; - } + String type = "fireball"; + double speed = 2; + boolean ride = false; + + if (args.length > 0 && types.containsKey(args[0])) { + type = args[0]; } + + if (args.length > 1) { + try { + speed = FloatUtil.parseDouble(args[1]); + } catch (Exception ignored) {} + } + + if (args.length > 2 && args[2].equalsIgnoreCase("ride")) { + ride = true; + } + + if (!user.isAuthorized("essentials.fireball." + type)) { + throw new Exception(tl("noPerm", "essentials.fireball." + type)); + } + final Vector direction = user.getBase().getEyeLocation().getDirection().multiply(speed); - projectile = (Projectile) user.getWorld().spawn(user.getBase().getEyeLocation().add(direction.getX(), direction.getY(), direction.getZ()), type); + Projectile projectile = (Projectile) user.getWorld().spawn(user.getBase().getEyeLocation().add(direction.getX(), direction.getY(), direction.getZ()), types.get(type)); projectile.setShooter(user.getBase()); projectile.setVelocity(direction); + + if (ride) { + projectile.addPassenger(user.getBase()); + } } @Override protected List getTabCompleteOptions(final Server server, final User user, final String commandLabel, final String[] args) { if (args.length == 1) { - return Lists.newArrayList("small", "arrow", "skull", "egg", "snowball", "expbottle", "large", "splashpotion", "lingeringpotion", "dragon"); + return types.keySet().stream() + .filter(type -> user.isAuthorized("essentials.fireball." + type)) + .collect(Collectors.toList()); + } else if (args.length == 2) { + return Lists.newArrayList("1", "2", "3", "4", "5"); } else { return Collections.emptyList(); } diff --git a/Essentials/src/plugin.yml b/Essentials/src/plugin.yml index c2470c774..26add785a 100644 --- a/Essentials/src/plugin.yml +++ b/Essentials/src/plugin.yml @@ -146,7 +146,7 @@ commands: aliases: [efly] fireball: description: Throw a fireball. - usage: / [small|skull] + usage: / [small|large|dragon|arrow|skull|egg|snowball|expbottle|splashpotion|lingeringpotion] [speed] aliases: [efireball,fireentity,efireentity,fireskull,efireskull] firework: description: Allows you to modify a stack of fireworks.