From e4e625dbfc17189502647e3afa129660adcb0c5c Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 30 Jul 2018 22:42:11 +0100 Subject: [PATCH] Code cleanup --- .../mineplex/core/gadget/GadgetManager.java | 4 +- .../{ItemSwortal.java => ItemSortal.java} | 144 +++++++++++++----- 2 files changed, 106 insertions(+), 42 deletions(-) rename Plugins/Mineplex.Core/src/mineplex/core/gadget/gadgets/item/{ItemSwortal.java => ItemSortal.java} (56%) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/gadget/GadgetManager.java b/Plugins/Mineplex.Core/src/mineplex/core/gadget/GadgetManager.java index 450c30fd5..a04b75a95 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/gadget/GadgetManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/gadget/GadgetManager.java @@ -157,7 +157,7 @@ import mineplex.core.gadget.gadgets.item.ItemPaintballGun; import mineplex.core.gadget.gadgets.item.ItemPaintbrush; import mineplex.core.gadget.gadgets.item.ItemPartyPopper; import mineplex.core.gadget.gadgets.item.ItemSnowball; -import mineplex.core.gadget.gadgets.item.ItemSwortal; +import mineplex.core.gadget.gadgets.item.ItemSortal; import mineplex.core.gadget.gadgets.item.ItemTNT; import mineplex.core.gadget.gadgets.item.ItemTrampoline; import mineplex.core.gadget.gadgets.kitselector.HalloweenKitSelector; @@ -584,7 +584,7 @@ public class GadgetManager extends MiniPlugin addGadget(new ItemConnect4(this)); addGadget(new ItemMaryPoppins(this)); addGadget(new ItemClacker(this)); - addGadget(new ItemSwortal(this)); + addGadget(new ItemSortal(this)); // Costume addGadget(new OutfitRaveSuitHelmet(this)); diff --git a/Plugins/Mineplex.Core/src/mineplex/core/gadget/gadgets/item/ItemSwortal.java b/Plugins/Mineplex.Core/src/mineplex/core/gadget/gadgets/item/ItemSortal.java similarity index 56% rename from Plugins/Mineplex.Core/src/mineplex/core/gadget/gadgets/item/ItemSwortal.java rename to Plugins/Mineplex.Core/src/mineplex/core/gadget/gadgets/item/ItemSortal.java index 5da90fc98..1b943a4ef 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/gadget/gadgets/item/ItemSwortal.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/gadget/gadgets/item/ItemSortal.java @@ -3,7 +3,10 @@ package mineplex.core.gadget.gadgets.item; import java.time.Month; import java.time.YearMonth; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; import org.bukkit.Location; import org.bukkit.Material; @@ -31,7 +34,7 @@ import mineplex.core.gadget.GadgetManager; import mineplex.core.gadget.types.ItemGadget; import mineplex.core.gadget.util.CostConstants; -public class ItemSwortal extends ItemGadget +public class ItemSortal extends ItemGadget { private static final Material[] SWORD_TYPES = @@ -40,13 +43,22 @@ public class ItemSwortal extends ItemGadget Material.IRON_SWORD, Material.DIAMOND_SWORD }; + private static final double DIST_FACTOR_TICK = 0.5; + private static final double DELTA_THETA = Math.PI / 10; + private static final int EXPLOSION_RADIUS = 8; + private static final int PARTICLE_RATE = 5; + private static final int MAX_SWORD_TICKS = 150; - public ItemSwortal(GadgetManager manager) + private final Set _sortals; + + public ItemSortal(GadgetManager manager) { - super(manager, "Swortal", new String[] + super(manager, "Gate of Babylon", new String[] { - }, CostConstants.POWERPLAY_BONUS, Material.BONE, (byte) 0, 50, null); + }, CostConstants.POWERPLAY_BONUS, Material.BONE, (byte) 0, TimeUnit.SECONDS.toMillis(1), null); + + _sortals = new HashSet<>(); // Free = false; setPPCYearMonth(YearMonth.of(2018, Month.AUGUST)); @@ -55,12 +67,13 @@ public class ItemSwortal extends ItemGadget @Override public void ActivateCustom(Player player) { - createAndFire(player.getLocation().add(0, 3, 0), 20); + createAndFire(player, player.getLocation().add(0, 3, 0), 20); } - private void createAndFire(Location location, int delay) + private void createAndFire(Player wielder, Location location, int delay) { - Swortal swortal = new Swortal(location); + Sortal sortal = new Sortal(wielder, location); + _sortals.add(sortal); Manager.runSyncTimer(new BukkitRunnable() { @@ -69,21 +82,22 @@ public class ItemSwortal extends ItemGadget @Override public void run() { - if (ticks % 5 == 0) + if (ticks % PARTICLE_RATE == 0) { - swortal.drawParticles(); + sortal.drawParticles(); } if (ticks > delay) { - swortal.throwSword(); + sortal.throwSword(); } ticks++; - if (ticks > 150 || !swortal.Sword.isValid()) + if (sortal.shouldClean()) { - swortal.remove(); + _sortals.remove(sortal); + sortal.remove(); cancel(); } } @@ -93,14 +107,16 @@ public class ItemSwortal extends ItemGadget @EventHandler public void command(PlayerCommandPreprocessEvent event) { - if (!event.getMessage().equalsIgnoreCase("/gil") || !event.getPlayer().isOp()) + Player player = event.getPlayer(); + + if (!event.getMessage().equalsIgnoreCase("/gil") || !player.isOp()) { return; } event.setCancelled(true); - Location center = event.getPlayer().getLocation().add(0, 0.5, 0); + Location center = player.getLocation().add(0, 0.5, 0); Manager.runSyncTimer(new BukkitRunnable() { @@ -115,28 +131,31 @@ public class ItemSwortal extends ItemGadget return; } - Location location = center.clone().add(UtilAlg.getTrajectory(UtilMath.r(360), -UtilMath.r(60) - 20).multiply(18)); - location.setDirection(UtilAlg.getTrajectory(location, center)); + Vector direction = UtilAlg.getTrajectory(UtilMath.r(360), -UtilMath.r(60) - 20); + Location location = center.clone().add(direction.multiply(18)); + location.setDirection(direction.multiply(-1)); - createAndFire(location, 80 - (i * 2) + UtilMath.r(25)); + createAndFire(player, location, 80 - (i * 2) + UtilMath.r(25)); } }, 20, 2); } - private class Swortal + private class Sortal { + final Player Wielder; final Location Point; final Vector Direction; final ArmorStand Sword; final DustSpellColor PortalColour = new DustSpellColor(240 + UtilMath.rRange(-5, 5), 205 + UtilMath.rRange(-20, 20), UtilMath.r(5)); - final DustSpellColor SwordColour = new DustSpellColor(255, UtilMath.r(20), UtilMath.r(20)); + final DustSpellColor SwordColour; final List PortalPoints, SwordPathPoints; double DistFactor; - Swortal(Location point) + Sortal(Player wielder, Location point) { + Wielder = wielder; Point = point; Direction = point.getDirection(); @@ -150,6 +169,15 @@ public class ItemSwortal extends ItemGadget Sword.setVisible(false); Sword.setRightArmPose(new EulerAngle(Math.toRadians(Point.getPitch() - 4), 0, 0)); + if (UtilPlayer.isSlimSkin(wielder.getUniqueId())) + { + SwordColour = new DustSpellColor(240 + UtilMath.r(15), UtilMath.r(20), UtilMath.r(20)); + } + else + { + SwordColour = new DustSpellColor(0, 30 + UtilMath.r(50), 255); + } + PortalPoints = new ArrayList<>(); SwordPathPoints = new ArrayList<>(); createPortalPoints(); @@ -162,7 +190,7 @@ public class ItemSwortal extends ItemGadget for (double radius = 0.2; radius < 1.1; radius += 0.2) { - for (double theta = 0; theta < 2 * Math.PI; theta += Math.PI / 10) + for (double theta = 0; theta < 2 * Math.PI; theta += DELTA_THETA) { Location location = Point.clone().add(Math.cos(theta) * radius, Math.sin(theta) * radius, 0); Vector vector = location.toVector().subtract(pointVector); @@ -174,18 +202,23 @@ public class ItemSwortal extends ItemGadget } } - UtilParticle.PlayParticleToAll(ParticleType.FLAME, Point, null, 0.1F, 20, ViewDist.LONGER); + UtilParticle.PlayParticleToAll(ParticleType.FLAME, Point, null, 0.1F, 20, ViewDist.LONG); Point.getWorld().playSound(Point, Sound.PORTAL, 1, 0.5F); } void drawParticles() { - PortalPoints.forEach(location -> new ColoredParticle(ParticleType.RED_DUST, PortalColour, location).display(ViewDist.LONGER)); - SwordPathPoints.forEach(location -> new ColoredParticle(ParticleType.RED_DUST, SwordColour, location).display(ViewDist.LONGER)); + PortalPoints.forEach(location -> new ColoredParticle(ParticleType.RED_DUST, PortalColour, location).display(ViewDist.LONG)); + SwordPathPoints.forEach(location -> new ColoredParticle(ParticleType.RED_DUST, SwordColour, location).display(ViewDist.LONG)); } void throwSword() { + if (shouldClean()) + { + return; + } + if (DistFactor == 0) { Material material = UtilMath.randomElement(SWORD_TYPES); @@ -194,15 +227,28 @@ public class ItemSwortal extends ItemGadget { Sword.setItemInHand(new ItemStack(material)); } + Point.getWorld().playSound(Point, Sound.ZOMBIE_REMEDY, 1, 0.5F); } - Vector direction = Direction.clone().multiply(DistFactor += 0.5); - Location newLocation = Sword.getLocation().add(Direction.clone().multiply(0.5)); + Vector direction = Direction.clone().multiply(DistFactor += DIST_FACTOR_TICK); + Location newLocation = Sword.getLocation().add(Direction.clone().multiply(DIST_FACTOR_TICK)); if (newLocation.getBlock().getType() != Material.AIR) { - explode(); + explode(false); + return; + } + + for (Sortal sortal : _sortals) + { + if (sortal.Wielder.equals(Wielder) || sortal.shouldClean() || !hasCollided(sortal)) + { + continue; + } + + explode(true); + sortal.explode(false); return; } @@ -210,24 +256,32 @@ public class ItemSwortal extends ItemGadget SwordPathPoints.add(Point.clone().add(direction)); } - void explode() + void explode(boolean swordCollision) { Location location = Sword.getLocation(); - UtilParticle.PlayParticleToAll(ParticleType.HUGE_EXPLOSION, location, null, 0, 1, ViewDist.LONGER); - location.getWorld().playSound(location, Sound.EXPLODE, 2, 0.5F); - - UtilPlayer.getInRadius(location, 8).forEach((player, scale) -> + if (swordCollision) { - if (!Manager.selectEntity(ItemSwortal.this, player)) - { - return; - } + UtilParticle.PlayParticleToAll(ParticleType.FIREWORKS_SPARK, location.add(0, 1, 0), null, 0.1F, 10, ViewDist.LONG); + Point.getWorld().playSound(Point, Sound.ZOMBIE_REMEDY, 1, 0.5F); + } + else + { + UtilParticle.PlayParticleToAll(ParticleType.HUGE_EXPLOSION, location.add(0, 1, 0), null, 0, 1, ViewDist.LONG); + location.getWorld().playSound(location, Sound.EXPLODE, 1.3F, 0.5F); - UtilAction.velocity(player, UtilAlg.getTrajectory(location, player.getLocation()) - .setY(0.8) - .multiply(scale * 2)); - }); + UtilPlayer.getInRadius(location, EXPLOSION_RADIUS).forEach((player, scale) -> + { + if (!Manager.selectEntity(ItemSortal.this, player)) + { + return; + } + + UtilAction.velocity(player, UtilAlg.getTrajectory(location, player.getLocation()) + .setY(0.8) + .multiply(scale * 2)); + }); + } remove(); } @@ -238,5 +292,15 @@ public class ItemSwortal extends ItemGadget PortalPoints.clear(); SwordPathPoints.clear(); } + + boolean shouldClean() + { + return !Sword.isValid() || Sword.getTicksLived() > MAX_SWORD_TICKS; + } + + boolean hasCollided(Sortal sortal) + { + return UtilMath.offsetSquared(Sword, sortal.Sword) < 3; + } } }