From ef905f4d5b4ed8c87ece49335df418acc13681ef Mon Sep 17 00:00:00 2001 From: drfiveminusmint Date: Sun, 16 Jul 2023 08:57:56 -0400 Subject: [PATCH 01/20] Testing entity summon event --- .../movecraft/combat/features/tracking/TNTTracking.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/net/countercraft/movecraft/combat/features/tracking/TNTTracking.java b/src/main/java/net/countercraft/movecraft/combat/features/tracking/TNTTracking.java index 97aaa79a..133eb1ff 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/tracking/TNTTracking.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/tracking/TNTTracking.java @@ -22,6 +22,7 @@ import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.BlockDispenseEvent; +import org.bukkit.event.entity.EntitySpawnEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.metadata.FixedMetadataValue; @@ -146,4 +147,11 @@ public void onBlockDispense(@NotNull BlockDispenseEvent e) { CraftFireWeaponEvent event = new CraftFireWeaponEvent(playerCraft, new TNTCannon()); Bukkit.getPluginManager().callEvent(event); } + + @EventHandler + public void onEntitySpawn (@NotNull EntitySpawnEvent e) { + if(e.getEntityType().equals(EntityType.PRIMED_TNT)) { + Bukkit.getServer().broadcastMessage("TNT created!"); + } + } } From c15064b58fa0c480fa0690bf8f4f6818002b01e4 Mon Sep 17 00:00:00 2001 From: drfiveminusmint Date: Sun, 16 Jul 2023 10:38:30 -0400 Subject: [PATCH 02/20] Change TNTTracking event --- .../combat/features/tracking/TNTTracking.java | 55 +++++++------------ 1 file changed, 21 insertions(+), 34 deletions(-) diff --git a/src/main/java/net/countercraft/movecraft/combat/features/tracking/TNTTracking.java b/src/main/java/net/countercraft/movecraft/combat/features/tracking/TNTTracking.java index 133eb1ff..7ff31fdc 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/tracking/TNTTracking.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/tracking/TNTTracking.java @@ -14,6 +14,8 @@ import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.Sound; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; import org.bukkit.block.Dispenser; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; @@ -96,41 +98,32 @@ private boolean subtractItem(@NotNull Inventory inv, @NotNull ItemStack item) { return false; } - @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) - public void onBlockDispense(@NotNull BlockDispenseEvent e) { + + @EventHandler + public void onEntitySpawn (@NotNull EntitySpawnEvent e) { if (!DamageTracking.EnableTNTTracking) return; - if (e.getBlock().getType() != Material.DISPENSER || e.getItem().getType() != Material.TNT) + if(!e.getEntityType().equals(EntityType.PRIMED_TNT)) return; - - // Cancel dispense event - e.setCancelled(true); - - // Subtract item yourself - Dispenser d = (Dispenser) e.getBlock().getState(); - Inventory inv = d.getInventory(); - if (!subtractItem(inv, e.getItem())) { - Bukkit.getScheduler().runTask(MovecraftCombat.getInstance(), () -> { - subtractItem(inv, e.getItem()); - }); - } - - // Spawn TNT - Location l = e.getVelocity().toLocation(e.getBlock().getWorld()); - TNTPrimed tnt = (TNTPrimed) e.getBlock().getWorld().spawnEntity(l, EntityType.PRIMED_TNT); - Vector velocity = getTNTVector(); - tnt.setVelocity(velocity); - for (Player p : Bukkit.getServer().getOnlinePlayers()) { - p.playSound(l, Sound.ENTITY_TNT_PRIMED, 1.5f, 1.5f); - } + TNTPrimed tnt = (TNTPrimed)e.getEntity(); // Find nearest craft Craft craft = MathUtils.fastNearestCraftToLoc(CraftManager.getInstance().getCrafts(), - e.getBlock().getLocation()); + tnt.getLocation()); if (!(craft instanceof PlayerCraft)) return; - if (!craft.getHitBox().contains(MathUtils.bukkit2MovecraftLoc(e.getBlock().getLocation()))) - return; + if (!craft.getHitBox().contains(MathUtils.bukkit2MovecraftLoc(tnt.getLocation()))) { + //check adjacent blocks + boolean found = false; + Block center = tnt.getLocation().getBlock(); + for (BlockFace face : BlockFace.values()) { + if (craft.getHitBox().contains(MathUtils.bukkit2MovecraftLoc(center.getRelative(face).getLocation()))) { + found = true; + break; + } + } + if (!found) return; + } // Report to tracking PlayerCraft playerCraft = (PlayerCraft) craft; @@ -142,16 +135,10 @@ public void onBlockDispense(@NotNull BlockDispenseEvent e) { if (sender == null) return; + Bukkit.getServer().broadcastMessage("Debug: TNT Found!"); tnt.setMetadata("MCC-Sender", new FixedMetadataValue(MovecraftCombat.getInstance(), sender.getUniqueId().toString())); CraftFireWeaponEvent event = new CraftFireWeaponEvent(playerCraft, new TNTCannon()); Bukkit.getPluginManager().callEvent(event); } - - @EventHandler - public void onEntitySpawn (@NotNull EntitySpawnEvent e) { - if(e.getEntityType().equals(EntityType.PRIMED_TNT)) { - Bukkit.getServer().broadcastMessage("TNT created!"); - } - } } From af028045285325d6ca7aee267226cd89a54a8da8 Mon Sep 17 00:00:00 2001 From: drfiveminusmint Date: Sun, 16 Jul 2023 10:43:45 -0400 Subject: [PATCH 03/20] Remove debug message --- .../movecraft/combat/features/tracking/TNTTracking.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/java/net/countercraft/movecraft/combat/features/tracking/TNTTracking.java b/src/main/java/net/countercraft/movecraft/combat/features/tracking/TNTTracking.java index 7ff31fdc..c8a47338 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/tracking/TNTTracking.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/tracking/TNTTracking.java @@ -134,8 +134,7 @@ public void onEntitySpawn (@NotNull EntitySpawnEvent e) { sender = playerCraft.getPilot(); if (sender == null) return; - - Bukkit.getServer().broadcastMessage("Debug: TNT Found!"); + tnt.setMetadata("MCC-Sender", new FixedMetadataValue(MovecraftCombat.getInstance(), sender.getUniqueId().toString())); CraftFireWeaponEvent event = new CraftFireWeaponEvent(playerCraft, new TNTCannon()); From d8c64659d02412398404f40f4ede17dcf8ba9c68 Mon Sep 17 00:00:00 2001 From: drfiveminusmint Date: Sun, 16 Jul 2023 10:45:35 -0400 Subject: [PATCH 04/20] Remove unnecessary subtract item function --- .../combat/features/tracking/TNTTracking.java | 21 +------------------ 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/src/main/java/net/countercraft/movecraft/combat/features/tracking/TNTTracking.java b/src/main/java/net/countercraft/movecraft/combat/features/tracking/TNTTracking.java index c8a47338..89cbb86c 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/tracking/TNTTracking.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/tracking/TNTTracking.java @@ -80,25 +80,6 @@ private Vector getTNTVector() { return v; } - private boolean subtractItem(@NotNull Inventory inv, @NotNull ItemStack item) { - int count = item.getAmount(); - for (int i = 0; i < inv.getSize(); i++) { - ItemStack temp = inv.getItem(i); - if (temp == null || !temp.isSimilar(item)) - continue; - - if (temp.getAmount() <= count) { - count -= temp.getAmount(); - inv.remove(temp); - } else { - temp.setAmount(temp.getAmount() - count); - return true; - } - } - return false; - } - - @EventHandler public void onEntitySpawn (@NotNull EntitySpawnEvent e) { if (!DamageTracking.EnableTNTTracking) @@ -134,7 +115,7 @@ public void onEntitySpawn (@NotNull EntitySpawnEvent e) { sender = playerCraft.getPilot(); if (sender == null) return; - + tnt.setMetadata("MCC-Sender", new FixedMetadataValue(MovecraftCombat.getInstance(), sender.getUniqueId().toString())); CraftFireWeaponEvent event = new CraftFireWeaponEvent(playerCraft, new TNTCannon()); From 38cf6f4aa5152004a1f90a17f335badc4025506d Mon Sep 17 00:00:00 2001 From: goodroach Date: Fri, 7 Jul 2023 12:52:32 -1000 Subject: [PATCH 05/20] Make tnt tracers more configurable --- .../combat/features/tracers/TNTTracers.java | 46 +++++++++++++++---- src/main/resources/config.yml | 5 +- 2 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/main/java/net/countercraft/movecraft/combat/features/tracers/TNTTracers.java b/src/main/java/net/countercraft/movecraft/combat/features/tracers/TNTTracers.java index 8d47a79a..84170efa 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/tracers/TNTTracers.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/tracers/TNTTracers.java @@ -8,10 +8,15 @@ import org.bukkit.Material; import org.bukkit.Particle; import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.block.BlockState; +import org.bukkit.block.data.BlockData; +import org.bukkit.block.data.Waterlogged; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; +import org.bukkit.entity.Stray; import org.bukkit.entity.TNTPrimed; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -25,8 +30,14 @@ public class TNTTracers extends BukkitRunnable implements Listener { public static double TracerRateTicks = 5.0; public static long TracerMinDistanceSqrd = 360; + public static long TracerDelayTicks = 5; + public static long TracerExplosionDelayTicks = 5; public static Particle TracerParticle = null; public static Particle ExplosionParticle = null; + public static Material TracerBlock = null; + public static Material TracerExplosionBlock = null; + private static BlockData tracerBlockData; + private static BlockData tracerExplosionBlockData; @NotNull private final PlayerManager manager; private long lastUpdate = 0; @@ -38,9 +49,28 @@ public TNTTracers(@NotNull PlayerManager manager) { public static void load(@NotNull FileConfiguration config) { TracerRateTicks = config.getDouble("TracerRateTicks", 5.0); TracerMinDistanceSqrd = config.getLong("TracerMinDistance", 60); + TracerDelayTicks = config.getLong("TracerDelayTicks", 5); + TracerExplosionDelayTicks = config.getLong("TracerExplosionDelayTicks", 20); TracerMinDistanceSqrd *= TracerMinDistanceSqrd; TracerParticle = Particle.valueOf(config.getString("TracerParticles", "FIREWORKS_SPARK")); ExplosionParticle = Particle.valueOf(config.getString("ExplosionParticles", "VILLAGER_ANGRY")); + TracerBlock = Material.valueOf(config.getString("TracerBlock", "COBWEB")); + TracerExplosionBlock = Material.valueOf(config.getString("TracerExplosionBlock", "GLOWSTONE")); + System.out.println(TracerBlock.toString()); + System.out.println(TracerExplosionBlock.toString()); + tracerBlockData = setNonWaterLogged(TracerBlock); + tracerExplosionBlockData = setNonWaterLogged(TracerExplosionBlock); + System.out.println(tracerExplosionBlockData.getAsString()); + } + + private static BlockData setNonWaterLogged(Material material) { + BlockData blockData = material.createBlockData(); + if (blockData instanceof Waterlogged) { + Waterlogged waterloggedData = (Waterlogged) blockData; + waterloggedData.setWaterlogged(false); + return blockData; + } + return blockData; } @Override @@ -99,7 +129,7 @@ else if (setting == PlayerConfig.TNTSetting.MEDIUM) { public void run() { fp.spawnParticle(TracerParticle, tntLoc, 0, 0.0, 0.0, 0.0); } - }.runTaskLater(MovecraftCombat.getInstance(), 5); + }.runTaskLater(MovecraftCombat.getInstance(), TracerDelayTicks); break; case BLOCKS: default: @@ -109,16 +139,16 @@ public void run() { new BukkitRunnable() { @Override public void run() { - fp.sendBlockChange(tntLoc, Material.COBWEB.createBlockData()); + fp.sendBlockChange(tntLoc, tracerBlockData); } - }.runTaskLater(MovecraftCombat.getInstance(), 5); + }.runTaskLater(MovecraftCombat.getInstance(), TracerDelayTicks); // then restore it new BukkitRunnable() { @Override public void run() { fp.sendBlockChange(tntLoc, tntLoc.getBlock().getBlockData()); } - }.runTaskLater(MovecraftCombat.getInstance(), 160); + }.runTaskLater(MovecraftCombat.getInstance(), TracerDelayTicks + 160); break; } } @@ -161,7 +191,7 @@ public void entityExplodeEvent(@NotNull EntityExplodeEvent e) { public void run() { fp.spawnParticle(ExplosionParticle, loc, 9); } - }.runTaskLater(MovecraftCombat.getInstance(), 20); + }.runTaskLater(MovecraftCombat.getInstance(), TracerExplosionDelayTicks); break; case BLOCKS: default: @@ -169,16 +199,16 @@ public void run() { new BukkitRunnable() { @Override public void run() { - fp.sendBlockChange(loc, Material.GLOWSTONE.createBlockData()); + fp.sendBlockChange(loc, tracerExplosionBlockData); } - }.runTaskLater(MovecraftCombat.getInstance(), 5); + }.runTaskLater(MovecraftCombat.getInstance(),TracerExplosionDelayTicks); // then remove it new BukkitRunnable() { @Override public void run() { fp.sendBlockChange(loc, Material.AIR.createBlockData()); } - }.runTaskLater(MovecraftCombat.getInstance(), 160); + }.runTaskLater(MovecraftCombat.getInstance(), TracerExplosionDelayTicks + 160); break; } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index e52edce0..609155b5 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -44,9 +44,12 @@ FireballLifespan: 6 # Lifespan of fireballs in seconds # TNT Tracers TracerRateTicks: 5.0 # Rate at which tracers are spawned on the path of flying TNT TracerMinDistance: 60 # Minimum distance at which glowstone tracers are spawned on explosion +TracerExplosionDelayTicks: 20 #Delay for the tracer explosion to appear +TracerDelayTicks: 5 #Delay for the tracers to appear TracerParticles: FIREWORKS_SPARK # Particle to use for particle tracers See https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Particle.html for options. ExplosionParticles: VILLAGER_ANGRY # Particle to use for particle tracer explosions - +TracerBlock: COBWEB #Block to use for block tracers +TracerExplosionBlock: GLOWSTONE #Block to use to indicate tracer explosions ### Custom features From f5129590777ce049f4c75f6d0fa41854f02eedf1 Mon Sep 17 00:00:00 2001 From: goodroach Date: Mon, 24 Jul 2023 14:09:50 -1000 Subject: [PATCH 06/20] implement node directors --- .../features/directors/AADirectors.java | 4 +- .../features/directors/CannonDirectors.java | 70 ++++++++++++--- .../features/directors/DirectorData.java | 37 ++++++++ .../combat/features/directors/Directors.java | 90 ++++++++++++++----- .../features/tracking/FireballTracking.java | 6 +- .../combat/features/tracking/TNTTracking.java | 6 +- 6 files changed, 167 insertions(+), 46 deletions(-) create mode 100644 src/main/java/net/countercraft/movecraft/combat/features/directors/DirectorData.java diff --git a/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java b/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java index 084bb45a..e11e8b0a 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java @@ -75,7 +75,7 @@ private void processFireball(@NotNull SmallFireball fireball) { if (!(c instanceof PlayerCraft) || !hasDirector((PlayerCraft) c)) return; - Player p = getDirector((PlayerCraft) c); + Player p = null; MovecraftLocation midPoint = c.getHitBox().getMidPoint(); int distX = Math.abs(midPoint.getX() - fireball.getLocation().getBlockX()); @@ -178,7 +178,7 @@ public void onSignClick(@NotNull PlayerInteractEvent e) { } clearDirector(p); - addDirector(foundCraft, p); + addDirector(p, foundCraft, sign.getLine(1), sign.getLine(2), sign.getLine(3)); p.sendMessage(I18nSupport.getInternationalisedString("AADirector - Directing")); } } diff --git a/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java b/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java index f547f0ef..76caf747 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java @@ -1,7 +1,6 @@ package net.countercraft.movecraft.combat.features.directors; import it.unimi.dsi.fastutil.objects.Object2DoubleOpenHashMap; -import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.combat.features.tracking.DamageTracking; import net.countercraft.movecraft.combat.localisation.I18nSupport; import net.countercraft.movecraft.combat.utils.DirectorUtils; @@ -14,6 +13,7 @@ import net.countercraft.movecraft.util.MathUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.World; @@ -33,6 +33,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Arrays; +import java.util.HashSet; import java.util.List; import java.util.UUID; @@ -42,6 +44,7 @@ public class CannonDirectors extends Directors implements Listener { public static final NamespacedKey ALLOW_CANNON_DIRECTOR_SIGN = new NamespacedKey("movecraft-combat", "allow_cannon_director_sign"); private static final String HEADER = "Cannon Director"; public static int CannonDirectorDistance = 100; + public static int CannonDirectorNodeDistance = 3; public static int CannonDirectorRange = 120; private final Object2DoubleOpenHashMap tracking = new Object2DoubleOpenHashMap<>(); private long lastCheck = 0; @@ -56,7 +59,8 @@ public static void register() { } public static void load(@NotNull FileConfiguration config) { - CannonDirectorDistance = config.getInt("CannonDirectorsDistance", 100); + CannonDirectorDistance = config.getInt("CannonDirectorDistance", 100); + CannonDirectorNodeDistance = config.getInt("CannonDirectorNodeDistance", 3); CannonDirectorRange = config.getInt("CannonDirectorRange", 120); } @@ -98,16 +102,48 @@ private void processTNT(@NotNull TNTPrimed tnt) { if (!(c instanceof PlayerCraft)) return; - MovecraftLocation midpoint = c.getHitBox().getMidPoint(); - int distX = Math.abs(midpoint.getX() - tnt.getLocation().getBlockX()); - int distY = Math.abs(midpoint.getY() - tnt.getLocation().getBlockY()); - int distZ = Math.abs(midpoint.getZ() - tnt.getLocation().getBlockZ()); - if (!hasDirector((PlayerCraft) c) || distX >= CannonDirectorDistance - || distY >= CannonDirectorDistance || distZ >= CannonDirectorDistance) - return; + HashSet directorDataSet = getDirectorDataSet((PlayerCraft) c); + Player player = null; + Player dominantPlayer = null; + // Adjust the TNT location based on its velocity to make it closer to the firing point. + int ticks = 1; + Location correctedLocation = tnt.getLocation().clone().add(tnt.getVelocity().clone().multiply(-ticks)); + System.out.println("Corrected location at: " + correctedLocation.getBlockX() + ", " + correctedLocation.getBlockY() + ", " + correctedLocation.getBlockZ()); + + for (DirectorData data : directorDataSet) { + if (data.getSelectedSigns().isEmpty()) { + dominantPlayer = data.getPlayer(); + } + } + if (dominantPlayer != null) { + System.out.println("Dominant player is: " + dominantPlayer.getName()); + player = dominantPlayer; + } else { + System.out.println("Dominant player not found, finding others..."); + for (DirectorData data : directorDataSet) { + HashSet locations = getLocations(data); + for (Location location : locations) { + System.out.println("Location of sign: " + location.getBlockX() + ", " + location.getBlockY() + ", " + location.getBlockZ()); + int distX = Math.abs(location.getBlockX() - correctedLocation.getBlockX()); + int distY = Math.abs(location.getBlockY() - correctedLocation.getBlockY()); + int distZ = Math.abs(location.getBlockZ() - correctedLocation.getBlockZ()); + + System.out.println("Node distances are for xyz: " + distX + " , " + distY + " , " + distZ); + + if (distX <= CannonDirectorNodeDistance && distY <= CannonDirectorNodeDistance && distZ <= CannonDirectorNodeDistance) { + player = data.getPlayer(); + System.out.println("Non-dominant player is: " + data.getPlayer()); + break; + } + } + if (player != null) { + System.out.println("Non-dominant player is not null."); + break; + } + } + } - Player p = getDirector((PlayerCraft) c); - if (p == null || p.getInventory().getItemInMainHand().getType() != Directors.DirectorTool) + if (player == null || player.getInventory().getItemInMainHand().getType() != Directors.DirectorTool) return; // Store the speed to add it back in later, since all the values we will be using are "normalized", IE: have a speed of 1 @@ -117,10 +153,10 @@ private void processTNT(@NotNull TNTPrimed tnt) { double horizontalSpeed = tntVector.length(); tntVector = tntVector.normalize(); // you normalize it for comparison with the new direction to see if we are trying to steer too far - Block targetBlock = DirectorUtils.getDirectorBlock(p, CannonDirectorRange); + Block targetBlock = DirectorUtils.getDirectorBlock(player, CannonDirectorRange); Vector targetVector; if (targetBlock == null || targetBlock.getType().equals(Material.AIR)) // the player is looking at nothing, shoot in that general direction - targetVector = p.getLocation().getDirection(); + targetVector = player.getLocation().getDirection(); else // shoot directly at the block the player is looking at (IE: with convergence) targetVector = targetBlock.getLocation().toVector().subtract(tnt.getLocation().toVector()); @@ -208,7 +244,13 @@ public final void onSignClick(@NotNull PlayerInteractEvent e) { } clearDirector(p); - addDirector(foundCraft, p); + DirectorData data = addDirector(p, foundCraft, sign.getLine(1), sign.getLine(2), sign.getLine(3)); + + if (isNodesShared(data)) { + p.sendMessage(ERROR_PREFIX + " " + I18nSupport.getInternationalisedString("CannonDirector - Must Not Share Nodes")); + return; + } + p.sendMessage(I18nSupport.getInternationalisedString("CannonDirector - Directing")); } diff --git a/src/main/java/net/countercraft/movecraft/combat/features/directors/DirectorData.java b/src/main/java/net/countercraft/movecraft/combat/features/directors/DirectorData.java new file mode 100644 index 00000000..56781472 --- /dev/null +++ b/src/main/java/net/countercraft/movecraft/combat/features/directors/DirectorData.java @@ -0,0 +1,37 @@ +package net.countercraft.movecraft.combat.features.directors; + +import net.countercraft.movecraft.MovecraftLocation; +import net.countercraft.movecraft.craft.PlayerCraft; +import org.bukkit.Location; +import org.bukkit.block.Block; +import org.bukkit.block.Sign; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +public class DirectorData { + private Player player; + private PlayerCraft craft; + private List selectedSigns; + + public DirectorData(Player player, PlayerCraft craft, List selectedSigns) { + this.player = player; + this.craft = craft; + this.selectedSigns = selectedSigns; + } + + public Player getPlayer() { + return player; + } + + public PlayerCraft getCraft() { + return craft; + } + + public List getSelectedSigns() { + return selectedSigns; + } +} \ No newline at end of file diff --git a/src/main/java/net/countercraft/movecraft/combat/features/directors/Directors.java b/src/main/java/net/countercraft/movecraft/combat/features/directors/Directors.java index 5ff507f6..872d7e0b 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/directors/Directors.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/directors/Directors.java @@ -1,25 +1,33 @@ package net.countercraft.movecraft.combat.features.directors; import com.google.common.collect.HashBiMap; +import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.combat.MovecraftCombat; import net.countercraft.movecraft.craft.PlayerCraft; import net.countercraft.movecraft.util.Tags; +import org.bukkit.Location; import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.block.Sign; +import org.bukkit.block.data.type.WallSign; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; +import java.util.List; +import java.util.Map; import java.util.Set; public class Directors extends BukkitRunnable { private static final Set instances = new HashSet<>(); public static Material DirectorTool = null; public static Set Transparent = null; - private final HashBiMap directors = HashBiMap.create(); - + private Map directors = new HashMap<>(); public Directors() { instances.add(this); @@ -60,28 +68,50 @@ public void run() { } + public DirectorData addDirector(Player player, PlayerCraft craft, String line1, String line2, String line3) { + if (directors.containsValue(player)) directors.remove(player); + List selectedLines = new ArrayList<>(); + selectedLines.add(line1); + selectedLines.add(line2); + selectedLines.add(line3); + if (line1.isBlank() && line2.isBlank() && line3.isBlank()) selectedLines.clear(); + DirectorData data = new DirectorData(player, craft, selectedLines); + directors.put(player, data); + System.out.println("New director added: " + directors.get(player).getSelectedSigns()); + System.out.println("Director name: " + directors.get(player).getPlayer()); + return data; + } - public void addDirector(@NotNull PlayerCraft craft, @NotNull Player player) { - if (directors.containsValue(player)) - directors.inverse().remove(player); - - directors.put(craft, player); + public HashSet getDirectorDataSet(PlayerCraft craft) { + HashSet directorDataSet = new HashSet<>(); + for (DirectorData data : directors.values()) { + if (data.getCraft() == craft) directorDataSet.add(data); + } + return directorDataSet; } - public boolean isDirector(@NotNull Player player) { - return directors.containsValue(player); + public boolean hasDirector(PlayerCraft craft) { + if (craft == null) return false; + for (DirectorData data : directors.values()) { + if (data.getCraft() == craft) return true; + } + return false; } - public boolean hasDirector(@NotNull PlayerCraft craft) { - if (!directors.containsKey(craft)) - return false; + public boolean isNodesShared(DirectorData director) { + PlayerCraft craft = director.getCraft(); + List selectedSigns = director.getSelectedSigns(); + return directors.values().stream() + .filter(data -> data != director && data.getCraft() == craft) + .anyMatch(data -> data.getSelectedSigns().stream().anyMatch(selectedSigns::contains)); + } - Player director = directors.get(craft); - return director != null && director.isOnline(); + public boolean isDirector(@NotNull Player player) { + return directors.containsKey(player); } public void removeDirector(@NotNull Player player) { - directors.inverse().remove(player); + directors.remove(player); } public void clearDirector(@NotNull Player player) { @@ -89,12 +119,32 @@ public void clearDirector(@NotNull Player player) { instance.removeDirector(player); } - @Nullable - public Player getDirector(@NotNull PlayerCraft craft) { - Player director = directors.get(craft); - if (director == null || !director.isOnline()) + public HashSet getLocations(DirectorData data) { + if (data.getSelectedSigns().isEmpty() || data.getCraft() == null) { return null; + } + PlayerCraft craft = data.getCraft(); + + HashSet locations = new HashSet<>(); + for (MovecraftLocation location : craft.getHitBox()) { + Block block = craft.getWorld().getBlockAt(location.getX(), location.getY(), location.getZ()); + if (!(block.getState() instanceof Sign)) + continue; - return director; + Sign sign = (Sign) block.getState(); + + if (!sign.getLine(0).equalsIgnoreCase("subcraft rotate")) { + continue; + } + if (sign.getLine(3).isBlank()) { + System.out.println("Sign is blank"); + continue; + } + if (data.getSelectedSigns().contains(sign.getLine(3))) { + System.out.println("Sign found."); + locations.add(block.getLocation()); + } + } + return locations; } } diff --git a/src/main/java/net/countercraft/movecraft/combat/features/tracking/FireballTracking.java b/src/main/java/net/countercraft/movecraft/combat/features/tracking/FireballTracking.java index 4ff27e89..fe83bc06 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/tracking/FireballTracking.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/tracking/FireballTracking.java @@ -68,11 +68,7 @@ public void onProjectileLaunch(@NotNull ProjectileLaunchEvent e) { PlayerCraft playerCraft = (PlayerCraft) craft; - Player sender; - if (directors.hasDirector(playerCraft)) - sender = directors.getDirector(playerCraft); - else - sender = playerCraft.getPilot(); + Player sender = playerCraft.getPilot(); if (sender == null) return; diff --git a/src/main/java/net/countercraft/movecraft/combat/features/tracking/TNTTracking.java b/src/main/java/net/countercraft/movecraft/combat/features/tracking/TNTTracking.java index 97aaa79a..1fbef9d8 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/tracking/TNTTracking.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/tracking/TNTTracking.java @@ -133,11 +133,7 @@ public void onBlockDispense(@NotNull BlockDispenseEvent e) { // Report to tracking PlayerCraft playerCraft = (PlayerCraft) craft; - Player sender; - if (directors.hasDirector(playerCraft)) - sender = directors.getDirector(playerCraft); - else - sender = playerCraft.getPilot(); + Player sender = playerCraft.getPilot(); if (sender == null) return; From a15564c63a3d12b638eadf6d6cbf8a8597575d54 Mon Sep 17 00:00:00 2001 From: goodroach Date: Fri, 28 Jul 2023 23:09:09 -1000 Subject: [PATCH 07/20] oops forgot aa directors --- .../features/directors/AADirectors.java | 63 +++++++++++++++---- .../features/directors/CannonDirectors.java | 16 ++--- .../features/directors/DirectorData.java | 8 --- src/main/resources/config.yml | 2 + 4 files changed, 61 insertions(+), 28 deletions(-) diff --git a/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java b/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java index e11e8b0a..55ad6eef 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java @@ -11,6 +11,7 @@ import net.countercraft.movecraft.util.MathUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.World; @@ -27,12 +28,15 @@ import org.bukkit.util.Vector; import org.jetbrains.annotations.NotNull; +import java.util.HashSet; + import static net.countercraft.movecraft.util.ChatUtils.ERROR_PREFIX; public class AADirectors extends Directors implements Listener { public static final NamespacedKey ALLOW_AA_DIRECTOR_SIGN = new NamespacedKey("movecraft-combat", "allow_aa_director_sign"); private static final String HEADER = "AA Director"; public static int AADirectorDistance = 50; + public static int AADirectorNodeDistance = 3; public static int AADirectorRange = 120; private long lastCheck = 0; @@ -46,6 +50,7 @@ public static void register() { public static void load(@NotNull FileConfiguration config) { AADirectorDistance = config.getInt("AADirectorDistance", 50); + AADirectorNodeDistance = config.getInt("AADirectorNodeDistance", 3); AADirectorRange = config.getInt("AADirectorRange", 120); } @@ -75,28 +80,56 @@ private void processFireball(@NotNull SmallFireball fireball) { if (!(c instanceof PlayerCraft) || !hasDirector((PlayerCraft) c)) return; - Player p = null; + HashSet directorDataSet = getDirectorDataSet((PlayerCraft) c); + Player player = null; + Player dominantPlayer = null; - MovecraftLocation midPoint = c.getHitBox().getMidPoint(); - int distX = Math.abs(midPoint.getX() - fireball.getLocation().getBlockX()); - int distY = Math.abs(midPoint.getY() - fireball.getLocation().getBlockY()); - int distZ = Math.abs(midPoint.getZ() - fireball.getLocation().getBlockZ()); - if (distX > AADirectorDistance || distY > AADirectorDistance || distZ > AADirectorDistance) - return; + for (DirectorData data : directorDataSet) { + if (data.getSelectedSigns().isEmpty()) { + dominantPlayer = data.getPlayer(); + } + } + if (dominantPlayer != null) { + player = dominantPlayer; + } else { + for (DirectorData data : directorDataSet) { + HashSet locations = getLocations(data); + for (Location location : locations) { + int distX = Math.abs(location.getBlockX() - fireball.getLocation().getBlockX()); + int distY = Math.abs(location.getBlockY() - fireball.getLocation().getBlockY()); + int distZ = Math.abs(location.getBlockZ() - fireball.getLocation().getBlockZ()); + + if (distX <= AADirectorNodeDistance && distY <= AADirectorNodeDistance && distZ <= AADirectorNodeDistance) { + player = data.getPlayer(); + break; + } + } + if (player != null) { + break; + } + } + } - fireball.setShooter(p); + if (player == null || player.getInventory().getItemInMainHand().getType() != Directors.DirectorTool) + return; - if (p == null || p.getInventory().getItemInMainHand().getType() != Directors.DirectorTool) + MovecraftLocation midpoint = c.getHitBox().getMidPoint(); + int distX = Math.abs(midpoint.getX() - fireball.getLocation().getBlockX()); + int distY = Math.abs(midpoint.getY() - fireball.getLocation().getBlockY()); + int distZ = Math.abs(midpoint.getZ() - fireball.getLocation().getBlockZ()); + if (distX >= AADirectorDistance || distY >= AADirectorDistance || distZ >= AADirectorDistance) return; + fireball.setShooter(player); + Vector fireballVector = fireball.getVelocity(); double speed = fireballVector.length(); // store the speed to add it back in later, since all the values we will be using are "normalized", IE: have a speed of 1 fireballVector = fireballVector.normalize(); // you normalize it for comparison with the new direction to see if we are trying to steer too far - Block targetBlock = DirectorUtils.getDirectorBlock(p, AADirectorRange); + Block targetBlock = DirectorUtils.getDirectorBlock(player, AADirectorRange); Vector targetVector; if (targetBlock == null || targetBlock.getType().equals(Material.AIR)) // the player is looking at nothing, shoot in that general direction - targetVector = p.getLocation().getDirection(); + targetVector = player.getLocation().getDirection(); else { // shoot directly at the block the player is looking at (IE: with convergence) targetVector = targetBlock.getLocation().toVector().subtract(fireball.getLocation().toVector()); targetVector = targetVector.normalize(); @@ -178,7 +211,13 @@ public void onSignClick(@NotNull PlayerInteractEvent e) { } clearDirector(p); - addDirector(p, foundCraft, sign.getLine(1), sign.getLine(2), sign.getLine(3)); + DirectorData data = addDirector(p, foundCraft, sign.getLine(1), sign.getLine(2), sign.getLine(3)); + + if (isNodesShared(data)) { + p.sendMessage(ERROR_PREFIX + " " + I18nSupport.getInternationalisedString("CannonDirector - Must Not Share Nodes")); + return; + } + p.sendMessage(I18nSupport.getInternationalisedString("AADirector - Directing")); } } diff --git a/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java b/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java index 76caf747..4c0866f9 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java @@ -1,6 +1,7 @@ package net.countercraft.movecraft.combat.features.directors; import it.unimi.dsi.fastutil.objects.Object2DoubleOpenHashMap; +import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.combat.features.tracking.DamageTracking; import net.countercraft.movecraft.combat.localisation.I18nSupport; import net.countercraft.movecraft.combat.utils.DirectorUtils; @@ -108,7 +109,6 @@ private void processTNT(@NotNull TNTPrimed tnt) { // Adjust the TNT location based on its velocity to make it closer to the firing point. int ticks = 1; Location correctedLocation = tnt.getLocation().clone().add(tnt.getVelocity().clone().multiply(-ticks)); - System.out.println("Corrected location at: " + correctedLocation.getBlockX() + ", " + correctedLocation.getBlockY() + ", " + correctedLocation.getBlockZ()); for (DirectorData data : directorDataSet) { if (data.getSelectedSigns().isEmpty()) { @@ -116,28 +116,21 @@ private void processTNT(@NotNull TNTPrimed tnt) { } } if (dominantPlayer != null) { - System.out.println("Dominant player is: " + dominantPlayer.getName()); player = dominantPlayer; } else { - System.out.println("Dominant player not found, finding others..."); for (DirectorData data : directorDataSet) { HashSet locations = getLocations(data); for (Location location : locations) { - System.out.println("Location of sign: " + location.getBlockX() + ", " + location.getBlockY() + ", " + location.getBlockZ()); int distX = Math.abs(location.getBlockX() - correctedLocation.getBlockX()); int distY = Math.abs(location.getBlockY() - correctedLocation.getBlockY()); int distZ = Math.abs(location.getBlockZ() - correctedLocation.getBlockZ()); - System.out.println("Node distances are for xyz: " + distX + " , " + distY + " , " + distZ); - if (distX <= CannonDirectorNodeDistance && distY <= CannonDirectorNodeDistance && distZ <= CannonDirectorNodeDistance) { player = data.getPlayer(); - System.out.println("Non-dominant player is: " + data.getPlayer()); break; } } if (player != null) { - System.out.println("Non-dominant player is not null."); break; } } @@ -146,6 +139,13 @@ private void processTNT(@NotNull TNTPrimed tnt) { if (player == null || player.getInventory().getItemInMainHand().getType() != Directors.DirectorTool) return; + MovecraftLocation midpoint = c.getHitBox().getMidPoint(); + int distX = Math.abs(midpoint.getX() - correctedLocation.getBlockX()); + int distY = Math.abs(midpoint.getY() - correctedLocation.getBlockY()); + int distZ = Math.abs(midpoint.getZ() - correctedLocation.getBlockZ()); + if (distX >= CannonDirectorDistance || distY >= CannonDirectorDistance || distZ >= CannonDirectorDistance) + return; + // Store the speed to add it back in later, since all the values we will be using are "normalized", IE: have a speed of 1 // We're only interested in the horizontal speed for now since that's all directors *should* affect. Vector tntVector = tnt.getVelocity(); diff --git a/src/main/java/net/countercraft/movecraft/combat/features/directors/DirectorData.java b/src/main/java/net/countercraft/movecraft/combat/features/directors/DirectorData.java index 56781472..a1f41530 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/directors/DirectorData.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/directors/DirectorData.java @@ -1,16 +1,8 @@ package net.countercraft.movecraft.combat.features.directors; - -import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.craft.PlayerCraft; -import org.bukkit.Location; -import org.bukkit.block.Block; -import org.bukkit.block.Sign; import org.bukkit.entity.Player; -import java.util.ArrayList; -import java.util.HashSet; import java.util.List; -import java.util.Set; public class DirectorData { private Player player; diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index e52edce0..1bc53291 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -5,10 +5,12 @@ Locale: "en" # Language file to load, currently the plugin comes with en and no, # AA Directors AADirectorDistance: 50 # Max range at which fireballs will be redirected +AADirectorNodeDistance: # See node directors AADirectorRange: 120 # Max range at which it will direct to a block vs in the general direction # Cannon Directors CannonDirectorDistance: 100 # Max range at which TNT will be redirected +CannonDirectorNodeDistance: 3 # See node directors CannonDirectorRange: 120 # Max range at which it will direct to a block vs in the general direction # Directors From aa690f60809b10bef9ea4c78714740a192980da5 Mon Sep 17 00:00:00 2001 From: goodroach Date: Fri, 28 Jul 2023 23:24:15 -1000 Subject: [PATCH 08/20] removed debug messages --- .../movecraft/combat/features/tracers/TNTTracers.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/net/countercraft/movecraft/combat/features/tracers/TNTTracers.java b/src/main/java/net/countercraft/movecraft/combat/features/tracers/TNTTracers.java index 84170efa..df99f5be 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/tracers/TNTTracers.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/tracers/TNTTracers.java @@ -56,11 +56,8 @@ public static void load(@NotNull FileConfiguration config) { ExplosionParticle = Particle.valueOf(config.getString("ExplosionParticles", "VILLAGER_ANGRY")); TracerBlock = Material.valueOf(config.getString("TracerBlock", "COBWEB")); TracerExplosionBlock = Material.valueOf(config.getString("TracerExplosionBlock", "GLOWSTONE")); - System.out.println(TracerBlock.toString()); - System.out.println(TracerExplosionBlock.toString()); tracerBlockData = setNonWaterLogged(TracerBlock); tracerExplosionBlockData = setNonWaterLogged(TracerExplosionBlock); - System.out.println(tracerExplosionBlockData.getAsString()); } private static BlockData setNonWaterLogged(Material material) { From 3da8b3996c2949f1ad6a77483dcdb865b371d85a Mon Sep 17 00:00:00 2001 From: Nicole <52045376+drfiveminusmint@users.noreply.github.com> Date: Tue, 22 Aug 2023 09:26:37 -0400 Subject: [PATCH 09/20] Update config.yml --- src/main/resources/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index e52edce0..d0af6fea 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -52,7 +52,7 @@ ExplosionParticles: VILLAGER_ANGRY # Particle to use for particle tracer explosi # Damage Tracking EnableFireballTracking: false # Enable tracking and recording of fireballs (higher lag than other tracking methods) -EnableTNTTracking: false # Enable tracking and recording of TNT damage (please read the wiki page before enabling!) +EnableTNTTracking: false # Enable tracking and recording of TNT damage EnableTorpedoTracking: false # Enable tracking and recording of Torpedo damage DamageTimeout: 300 # Timeout for damages in seconds From d96dd3c50f91a3f8cd8e773121e9fed895ee74c8 Mon Sep 17 00:00:00 2001 From: goodroach Date: Fri, 1 Sep 2023 19:39:12 -1000 Subject: [PATCH 10/20] Provide additional checks for sign selection --- .../movecraft/combat/features/directors/AADirectors.java | 2 +- .../movecraft/combat/features/directors/CannonDirectors.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java b/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java index 55ad6eef..3dfefd18 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java @@ -85,7 +85,7 @@ private void processFireball(@NotNull SmallFireball fireball) { Player dominantPlayer = null; for (DirectorData data : directorDataSet) { - if (data.getSelectedSigns().isEmpty()) { + if (data.getSelectedSigns().isEmpty() || getLocations(data).isEmpty()) { dominantPlayer = data.getPlayer(); } } diff --git a/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java b/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java index 4c0866f9..a7298059 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java @@ -111,7 +111,7 @@ private void processTNT(@NotNull TNTPrimed tnt) { Location correctedLocation = tnt.getLocation().clone().add(tnt.getVelocity().clone().multiply(-ticks)); for (DirectorData data : directorDataSet) { - if (data.getSelectedSigns().isEmpty()) { + if (data.getSelectedSigns().isEmpty() || getLocations(data).isEmpty()) { dominantPlayer = data.getPlayer(); } } From 013722a31a2dc3e915af1c7091c009852cd12e32 Mon Sep 17 00:00:00 2001 From: goodroach Date: Sun, 3 Sep 2023 23:48:41 -1000 Subject: [PATCH 11/20] refactor and fixes --- .../combat/features/tracers/TNTTracers.java | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/main/java/net/countercraft/movecraft/combat/features/tracers/TNTTracers.java b/src/main/java/net/countercraft/movecraft/combat/features/tracers/TNTTracers.java index df99f5be..e2f2bd56 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/tracers/TNTTracers.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/tracers/TNTTracers.java @@ -34,10 +34,8 @@ public class TNTTracers extends BukkitRunnable implements Listener { public static long TracerExplosionDelayTicks = 5; public static Particle TracerParticle = null; public static Particle ExplosionParticle = null; - public static Material TracerBlock = null; - public static Material TracerExplosionBlock = null; - private static BlockData tracerBlockData; - private static BlockData tracerExplosionBlockData; + private static BlockData TracerBlockData; + private static BlockData TracerExplosionBlockData; @NotNull private final PlayerManager manager; private long lastUpdate = 0; @@ -54,10 +52,8 @@ public static void load(@NotNull FileConfiguration config) { TracerMinDistanceSqrd *= TracerMinDistanceSqrd; TracerParticle = Particle.valueOf(config.getString("TracerParticles", "FIREWORKS_SPARK")); ExplosionParticle = Particle.valueOf(config.getString("ExplosionParticles", "VILLAGER_ANGRY")); - TracerBlock = Material.valueOf(config.getString("TracerBlock", "COBWEB")); - TracerExplosionBlock = Material.valueOf(config.getString("TracerExplosionBlock", "GLOWSTONE")); - tracerBlockData = setNonWaterLogged(TracerBlock); - tracerExplosionBlockData = setNonWaterLogged(TracerExplosionBlock); + TracerBlockData = setNonWaterLogged(Material.valueOf(config.getString("TracerBlock", "COBWEB"))); + TracerExplosionBlockData = setNonWaterLogged(Material.valueOf(config.getString("TracerExplosionBlock", "GLOWSTONE"))); } private static BlockData setNonWaterLogged(Material material) { @@ -136,7 +132,7 @@ public void run() { new BukkitRunnable() { @Override public void run() { - fp.sendBlockChange(tntLoc, tracerBlockData); + fp.sendBlockChange(tntLoc, TracerBlockData); } }.runTaskLater(MovecraftCombat.getInstance(), TracerDelayTicks); // then restore it @@ -196,7 +192,7 @@ public void run() { new BukkitRunnable() { @Override public void run() { - fp.sendBlockChange(loc, tracerExplosionBlockData); + fp.sendBlockChange(loc, TracerExplosionBlockData); } }.runTaskLater(MovecraftCombat.getInstance(),TracerExplosionDelayTicks); // then remove it From 256cabca86275d8b0fce6bb57d7e48607e9b65b8 Mon Sep 17 00:00:00 2001 From: TylerS1066 Date: Tue, 5 Sep 2023 16:58:40 -0500 Subject: [PATCH 12/20] Squashed commit of the following: commit ac4e701adbbd37fcf13832b1e2a0a6b74a0e3162 Author: TylerS1066 Date: Tue Sep 5 16:58:25 2023 -0500 Rename config key commit e76ccdde4aee20ec85ae1af5c281d274bf3b1b00 Author: TylerS1066 Date: Tue Sep 5 16:57:18 2023 -0500 Clean up config commit 345accab59305c45891f384af0b07a9e6568b675 Author: TylerS1066 Date: Tue Sep 5 16:53:28 2023 -0500 Clean up formatting commit fd11ba94d032433f3cf23f13a285da7bd159ffc0 Author: TylerS1066 Date: Tue Sep 5 16:53:02 2023 -0500 Rename function commit fd69a0f570a088cb08a508d9ea9a5d889ebfe4fb Author: TylerS1066 Date: Tue Sep 5 16:52:21 2023 -0500 Fix format commit 1a0ad2a00b3d5599126c8dec693c6b4da9e0ba52 Author: TylerS1066 Date: Tue Sep 5 16:52:08 2023 -0500 Removed unused imports --- .../combat/features/tracers/TNTTracers.java | 26 +++++++++---------- src/main/resources/config.yml | 9 ++++--- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/main/java/net/countercraft/movecraft/combat/features/tracers/TNTTracers.java b/src/main/java/net/countercraft/movecraft/combat/features/tracers/TNTTracers.java index e2f2bd56..a5cfc2cd 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/tracers/TNTTracers.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/tracers/TNTTracers.java @@ -8,15 +8,12 @@ import org.bukkit.Material; import org.bukkit.Particle; import org.bukkit.World; -import org.bukkit.block.Block; -import org.bukkit.block.BlockState; import org.bukkit.block.data.BlockData; import org.bukkit.block.data.Waterlogged; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; -import org.bukkit.entity.Stray; import org.bukkit.entity.TNTPrimed; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -35,7 +32,7 @@ public class TNTTracers extends BukkitRunnable implements Listener { public static Particle TracerParticle = null; public static Particle ExplosionParticle = null; private static BlockData TracerBlockData; - private static BlockData TracerExplosionBlockData; + private static BlockData ExplosionBlockData; @NotNull private final PlayerManager manager; private long lastUpdate = 0; @@ -47,16 +44,16 @@ public TNTTracers(@NotNull PlayerManager manager) { public static void load(@NotNull FileConfiguration config) { TracerRateTicks = config.getDouble("TracerRateTicks", 5.0); TracerMinDistanceSqrd = config.getLong("TracerMinDistance", 60); + TracerMinDistanceSqrd *= TracerMinDistanceSqrd; TracerDelayTicks = config.getLong("TracerDelayTicks", 5); TracerExplosionDelayTicks = config.getLong("TracerExplosionDelayTicks", 20); - TracerMinDistanceSqrd *= TracerMinDistanceSqrd; TracerParticle = Particle.valueOf(config.getString("TracerParticles", "FIREWORKS_SPARK")); ExplosionParticle = Particle.valueOf(config.getString("ExplosionParticles", "VILLAGER_ANGRY")); - TracerBlockData = setNonWaterLogged(Material.valueOf(config.getString("TracerBlock", "COBWEB"))); - TracerExplosionBlockData = setNonWaterLogged(Material.valueOf(config.getString("TracerExplosionBlock", "GLOWSTONE"))); + TracerBlockData = createNonWaterLogged(Material.valueOf(config.getString("TracerBlock", "COBWEB"))); + ExplosionBlockData = createNonWaterLogged(Material.valueOf(config.getString("ExplosionBlock", "GLOWSTONE"))); } - private static BlockData setNonWaterLogged(Material material) { + private static BlockData createNonWaterLogged(Material material) { BlockData blockData = material.createBlockData(); if (blockData instanceof Waterlogged) { Waterlogged waterloggedData = (Waterlogged) blockData; @@ -103,7 +100,7 @@ else if (setting == PlayerConfig.TNTSetting.MEDIUM) { long seed = (long) (tntLoc.getX() * tntLoc.getY() * tntLoc.getZ() + (System.currentTimeMillis() >> 12)); int random = new Random(seed).nextInt(100); if (random < 50) - continue; // Medium merely spawns half the particles/cobwebs + continue; // Medium merely spawns half the particles/cobwebs } // is the TNT within the view distance (rendered world) of the player? @@ -147,7 +144,6 @@ public void run() { } } - @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) public void entityExplodeEvent(@NotNull EntityExplodeEvent e) { Entity tnt = e.getEntity(); @@ -165,7 +161,8 @@ public void entityExplodeEvent(@NotNull EntityExplodeEvent e) { if (setting == null || setting == PlayerConfig.TNTSetting.OFF) continue; - // is the TNT within the view distance (rendered world) of the player, yet further than TracerMinDistance blocks? + // is the TNT within the view distance (rendered world) of the player, yet + // further than TracerMinDistance blocks? double distance = p.getLocation().distanceSquared(tnt.getLocation()); if (distance >= maxDistSquared || distance < TracerMinDistanceSqrd) return; @@ -188,13 +185,14 @@ public void run() { break; case BLOCKS: default: - // then make a glowstone to look like the explosion, place it a little later so it isn't right in the middle of the volley + // then make a glowstone to look like the explosion, place it a little later so + // it isn't right in the middle of the volley new BukkitRunnable() { @Override public void run() { - fp.sendBlockChange(loc, TracerExplosionBlockData); + fp.sendBlockChange(loc, ExplosionBlockData); } - }.runTaskLater(MovecraftCombat.getInstance(),TracerExplosionDelayTicks); + }.runTaskLater(MovecraftCombat.getInstance(), TracerExplosionDelayTicks); // then remove it new BukkitRunnable() { @Override diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 609155b5..54afc8e1 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -44,12 +44,13 @@ FireballLifespan: 6 # Lifespan of fireballs in seconds # TNT Tracers TracerRateTicks: 5.0 # Rate at which tracers are spawned on the path of flying TNT TracerMinDistance: 60 # Minimum distance at which glowstone tracers are spawned on explosion -TracerExplosionDelayTicks: 20 #Delay for the tracer explosion to appear -TracerDelayTicks: 5 #Delay for the tracers to appear +TracerExplosionDelayTicks: 20 # Delay for the tracer explosion to appear +TracerDelayTicks: 5 # Delay for the tracers to appear +TracerBlock: COBWEB # Block to use for block tracers +ExplosionBlock: GLOWSTONE # Block to use to indicate tracer explosions TracerParticles: FIREWORKS_SPARK # Particle to use for particle tracers See https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Particle.html for options. ExplosionParticles: VILLAGER_ANGRY # Particle to use for particle tracer explosions -TracerBlock: COBWEB #Block to use for block tracers -TracerExplosionBlock: GLOWSTONE #Block to use to indicate tracer explosions + ### Custom features From 6e60692187334009c17ef8bfb1bd3a5dff598559 Mon Sep 17 00:00:00 2001 From: TylerS1066 Date: Tue, 5 Sep 2023 17:14:07 -0500 Subject: [PATCH 13/20] Revert default to previous value --- .../movecraft/combat/features/tracers/TNTTracers.java | 2 +- src/main/resources/config.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/net/countercraft/movecraft/combat/features/tracers/TNTTracers.java b/src/main/java/net/countercraft/movecraft/combat/features/tracers/TNTTracers.java index a5cfc2cd..14b914b7 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/tracers/TNTTracers.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/tracers/TNTTracers.java @@ -46,7 +46,7 @@ public static void load(@NotNull FileConfiguration config) { TracerMinDistanceSqrd = config.getLong("TracerMinDistance", 60); TracerMinDistanceSqrd *= TracerMinDistanceSqrd; TracerDelayTicks = config.getLong("TracerDelayTicks", 5); - TracerExplosionDelayTicks = config.getLong("TracerExplosionDelayTicks", 20); + TracerExplosionDelayTicks = config.getLong("TracerExplosionDelayTicks", 5); TracerParticle = Particle.valueOf(config.getString("TracerParticles", "FIREWORKS_SPARK")); ExplosionParticle = Particle.valueOf(config.getString("ExplosionParticles", "VILLAGER_ANGRY")); TracerBlockData = createNonWaterLogged(Material.valueOf(config.getString("TracerBlock", "COBWEB"))); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 54afc8e1..dbcfc3b9 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -44,7 +44,7 @@ FireballLifespan: 6 # Lifespan of fireballs in seconds # TNT Tracers TracerRateTicks: 5.0 # Rate at which tracers are spawned on the path of flying TNT TracerMinDistance: 60 # Minimum distance at which glowstone tracers are spawned on explosion -TracerExplosionDelayTicks: 20 # Delay for the tracer explosion to appear +TracerExplosionDelayTicks: 5 # Delay for the tracer explosion to appear TracerDelayTicks: 5 # Delay for the tracers to appear TracerBlock: COBWEB # Block to use for block tracers ExplosionBlock: GLOWSTONE # Block to use to indicate tracer explosions From 9366c8fdd315c2eaa0a7a477b287f5bffabb6157 Mon Sep 17 00:00:00 2001 From: TylerS1066 Date: Sat, 16 Sep 2023 16:04:09 -0500 Subject: [PATCH 14/20] Add more warnings, create interface for NMS I/O --- .../features/BlastResistanceOverride.java | 49 +++++++++++++------ 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/countercraft/movecraft/combat/features/BlastResistanceOverride.java b/src/main/java/net/countercraft/movecraft/combat/features/BlastResistanceOverride.java index 2238c361..7754e83f 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/BlastResistanceOverride.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/BlastResistanceOverride.java @@ -18,6 +18,7 @@ public class BlastResistanceOverride { public static Map BlastResistanceOverride = null; + private static BlastResistanceNMS nmsInterface; public static void load(@NotNull FileConfiguration config) { if (!config.contains("BlastResistanceOverride")) @@ -44,23 +45,12 @@ public static void load(@NotNull FileConfiguration config) { BlastResistanceOverride.put(m, value); } } + + nmsInterface = new BlastResistanceNMS_V1(); // TODO! } public static boolean setBlastResistance(Material m, float resistance) { - try { - String packageName = Bukkit.getServer().getClass().getPackage().getName(); - String version = packageName.substring(packageName.lastIndexOf('.') + 1); - Class clazz = Class.forName("org.bukkit.craftbukkit." + version + ".util.CraftMagicNumbers"); - Method method = clazz.getMethod("getBlock", Material.class); - Object block = method.invoke(null, m); - Field field = FieldUtils.getField(block.getClass(), "durability", true); - FieldUtils.writeField(field, block, resistance); - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException - | SecurityException | ClassNotFoundException e) { - e.printStackTrace(); - return false; - } - return true; + return nmsInterface.setBlastResistance(m, resistance); } public static boolean revertToVanilla(Material m) { @@ -69,13 +59,40 @@ public static boolean revertToVanilla(Material m) { public static void enable() { for (var entry : BlastResistanceOverride.entrySet()) { - setBlastResistance(entry.getKey(), entry.getValue()); + if (!setBlastResistance(entry.getKey(), entry.getValue())) + MovecraftCombat.getInstance().getLogger() + .warning("Unable to set " + entry.getKey().name() + ": " + entry.getValue()); } } public static void disable() { for (Material m : BlastResistanceOverride.keySet()) { - revertToVanilla(m); + if (!revertToVanilla(m)) + MovecraftCombat.getInstance().getLogger().warning("Unable to reset " + m.name()); + } + } + + private static interface BlastResistanceNMS { + public boolean setBlastResistance(Material m, float resistance); + } + + private static class BlastResistanceNMS_V1 implements BlastResistanceNMS { + public boolean setBlastResistance(Material m, float resistance) { + try { + String packageName = Bukkit.getServer().getClass().getPackage().getName(); + String version = packageName.substring(packageName.lastIndexOf('.') + 1); + Class clazz = Class.forName("org.bukkit.craftbukkit." + version + ".util.CraftMagicNumbers"); + Method method = clazz.getMethod("getBlock", Material.class); + Object block = method.invoke(null, m); + Field field = FieldUtils.getField(block.getClass(), "durability", true); + FieldUtils.writeField(field, block, resistance); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException + | NoSuchMethodException + | SecurityException | ClassNotFoundException e) { + e.printStackTrace(); + return false; + } + return true; } } } From 7c7a7fe9924175812b38cf8800a24356d52a4265 Mon Sep 17 00:00:00 2001 From: TylerS1066 Date: Sat, 16 Sep 2023 16:16:44 -0500 Subject: [PATCH 15/20] Add second NMS interface and switching code --- .../features/BlastResistanceOverride.java | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/countercraft/movecraft/combat/features/BlastResistanceOverride.java b/src/main/java/net/countercraft/movecraft/combat/features/BlastResistanceOverride.java index 7754e83f..93850afc 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/BlastResistanceOverride.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/BlastResistanceOverride.java @@ -46,7 +46,17 @@ public static void load(@NotNull FileConfiguration config) { } } - nmsInterface = new BlastResistanceNMS_V1(); // TODO! + String packageName = Bukkit.getServer().getClass().getPackage().getName(); + String version = packageName.substring(packageName.lastIndexOf('.') + 1); + int major_version = Integer.parseInt(version.split("_")[1]); + if (major_version < 17) { + nmsInterface = new BlastResistanceNMS_V1(); + MovecraftCombat.getInstance().getLogger().info("Loaded BlastResistanceOverride NMS v1"); + } + else { + nmsInterface = new BlastResistanceNMS_V2(); + MovecraftCombat.getInstance().getLogger().info("Loaded BlastResistanceOverride NMS v2"); + } } public static boolean setBlastResistance(Material m, float resistance) { @@ -95,4 +105,24 @@ public boolean setBlastResistance(Material m, float resistance) { return true; } } + + private static class BlastResistanceNMS_V2 implements BlastResistanceNMS { + public boolean setBlastResistance(Material m, float resistance) { + try { + String packageName = Bukkit.getServer().getClass().getPackage().getName(); + String version = packageName.substring(packageName.lastIndexOf('.') + 1); + Class clazz = Class.forName("org.bukkit.craftbukkit." + version + ".util.CraftMagicNumbers"); + Method method = clazz.getMethod("getBlock", Material.class); + Object block = method.invoke(null, m); + Field field = FieldUtils.getField(block.getClass(), "aH", true); // obfuscated explosionResistance + FieldUtils.writeField(field, block, resistance); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException + | NoSuchMethodException + | SecurityException | ClassNotFoundException e) { + e.printStackTrace(); + return false; + } + return true; + } + } } From 49c7c5d95cb54c83b1ac3a3df0ebd7f6571a97ed Mon Sep 17 00:00:00 2001 From: TylerS1066 Date: Sat, 16 Sep 2023 16:38:59 -0500 Subject: [PATCH 16/20] Add 1.20 specific handler, try combining reused code --- .../features/BlastResistanceOverride.java | 69 +++++++++++++------ 1 file changed, 47 insertions(+), 22 deletions(-) diff --git a/src/main/java/net/countercraft/movecraft/combat/features/BlastResistanceOverride.java b/src/main/java/net/countercraft/movecraft/combat/features/BlastResistanceOverride.java index 93850afc..2cb3fadd 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/BlastResistanceOverride.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/BlastResistanceOverride.java @@ -7,6 +7,7 @@ import java.util.HashMap; import java.util.Map; +import org.apache.commons.lang.NotImplementedException; import org.apache.commons.lang.reflect.FieldUtils; import org.bukkit.Bukkit; import org.bukkit.Material; @@ -49,13 +50,13 @@ public static void load(@NotNull FileConfiguration config) { String packageName = Bukkit.getServer().getClass().getPackage().getName(); String version = packageName.substring(packageName.lastIndexOf('.') + 1); int major_version = Integer.parseInt(version.split("_")[1]); + MovecraftCombat.getInstance().getLogger().info("Loaded BlastResistanceOverride NMS " + major_version); if (major_version < 17) { nmsInterface = new BlastResistanceNMS_V1(); - MovecraftCombat.getInstance().getLogger().info("Loaded BlastResistanceOverride NMS v1"); - } - else { + } else if (major_version == 20) { + nmsInterface = new BlastResistanceNMS_v1_20(); + } else { nmsInterface = new BlastResistanceNMS_V2(); - MovecraftCombat.getInstance().getLogger().info("Loaded BlastResistanceOverride NMS v2"); } } @@ -82,20 +83,49 @@ public static void disable() { } } - private static interface BlastResistanceNMS { - public boolean setBlastResistance(Material m, float resistance); + private static class BlastResistanceNMS { + public boolean setBlastResistance(Material m, float resistance) { + throw new NotImplementedException(); + } + + protected static Class getCraftMagicNumbersClass() throws ClassNotFoundException { + String packageName = Bukkit.getServer().getClass().getPackage().getName(); + String version = packageName.substring(packageName.lastIndexOf('.') + 1); + return Class.forName("org.bukkit.craftbukkit." + version + ".util.CraftMagicNumbers"); + } + + protected static Object getBlockClass(Class magicNumbers, Material m) + throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + Method method = magicNumbers.getMethod("getBlock", Material.class); + return method.invoke(null, m); + } + + protected static void writeField(Object block, String fieldName, float resistance) throws IllegalAccessException { + Field field = FieldUtils.getField(block.getClass(), fieldName, true); + FieldUtils.writeField(field, block, resistance); + } + } + + private static class BlastResistanceNMS_V1 extends BlastResistanceNMS { + public boolean setBlastResistance(Material m, float resistance) { + try { + Object block = getBlockClass(getCraftMagicNumbersClass(), m); + writeField(block, "durability", resistance); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException + | NoSuchMethodException + | SecurityException | ClassNotFoundException e) { + e.printStackTrace(); + return false; + } + return true; + } } - private static class BlastResistanceNMS_V1 implements BlastResistanceNMS { + private static class BlastResistanceNMS_V2 extends BlastResistanceNMS { public boolean setBlastResistance(Material m, float resistance) { try { - String packageName = Bukkit.getServer().getClass().getPackage().getName(); - String version = packageName.substring(packageName.lastIndexOf('.') + 1); - Class clazz = Class.forName("org.bukkit.craftbukkit." + version + ".util.CraftMagicNumbers"); - Method method = clazz.getMethod("getBlock", Material.class); - Object block = method.invoke(null, m); - Field field = FieldUtils.getField(block.getClass(), "durability", true); - FieldUtils.writeField(field, block, resistance); + Object block = getBlockClass(getCraftMagicNumbersClass(), m); + writeField(block, "aH", resistance); // obfuscated explosionResistance } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | ClassNotFoundException e) { @@ -106,16 +136,11 @@ public boolean setBlastResistance(Material m, float resistance) { } } - private static class BlastResistanceNMS_V2 implements BlastResistanceNMS { + private static class BlastResistanceNMS_v1_20 extends BlastResistanceNMS { public boolean setBlastResistance(Material m, float resistance) { try { - String packageName = Bukkit.getServer().getClass().getPackage().getName(); - String version = packageName.substring(packageName.lastIndexOf('.') + 1); - Class clazz = Class.forName("org.bukkit.craftbukkit." + version + ".util.CraftMagicNumbers"); - Method method = clazz.getMethod("getBlock", Material.class); - Object block = method.invoke(null, m); - Field field = FieldUtils.getField(block.getClass(), "aH", true); // obfuscated explosionResistance - FieldUtils.writeField(field, block, resistance); + Object block = getBlockClass(getCraftMagicNumbersClass(), m); + writeField(block, "aF", resistance); // obfuscated explosionResistance } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException | ClassNotFoundException e) { From ee50c9d8678179626aed6de974aea9976ba942cb Mon Sep 17 00:00:00 2001 From: TylerS1066 Date: Sat, 16 Sep 2023 16:59:57 -0500 Subject: [PATCH 17/20] Remove debug, add notes --- .../combat/features/BlastResistanceOverride.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/net/countercraft/movecraft/combat/features/BlastResistanceOverride.java b/src/main/java/net/countercraft/movecraft/combat/features/BlastResistanceOverride.java index 2cb3fadd..75d1fc3c 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/BlastResistanceOverride.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/BlastResistanceOverride.java @@ -50,13 +50,12 @@ public static void load(@NotNull FileConfiguration config) { String packageName = Bukkit.getServer().getClass().getPackage().getName(); String version = packageName.substring(packageName.lastIndexOf('.') + 1); int major_version = Integer.parseInt(version.split("_")[1]); - MovecraftCombat.getInstance().getLogger().info("Loaded BlastResistanceOverride NMS " + major_version); if (major_version < 17) { - nmsInterface = new BlastResistanceNMS_V1(); + nmsInterface = new BlastResistanceNMS_V1(); // Tested on 1.14.4 and 1.16.5 } else if (major_version == 20) { - nmsInterface = new BlastResistanceNMS_v1_20(); + nmsInterface = new BlastResistanceNMS_v1_20(); // Tested on 1.20.1 } else { - nmsInterface = new BlastResistanceNMS_V2(); + nmsInterface = new BlastResistanceNMS_V2(); // Tested on 1.19.4 and 1.18.2 } } @@ -100,7 +99,8 @@ protected static Object getBlockClass(Class magicNumbers, Material m) return method.invoke(null, m); } - protected static void writeField(Object block, String fieldName, float resistance) throws IllegalAccessException { + protected static void writeField(Object block, String fieldName, float resistance) + throws IllegalAccessException { Field field = FieldUtils.getField(block.getClass(), fieldName, true); FieldUtils.writeField(field, block, resistance); } From cec098d9446295a4e2069c1407d4e388306b98e8 Mon Sep 17 00:00:00 2001 From: TylerS1066 Date: Sat, 16 Sep 2023 17:43:05 -0500 Subject: [PATCH 18/20] Bump version for release --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index b90344b0..f500412a 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ java.net.countercraft.movecraft.combat Movecraft-Combat - 2.0.0_beta-2 + 2.0.0_beta-3 jar Movecraft-Combat From 71d32c14f26e66f503cc0974246ca4ac60a1bc24 Mon Sep 17 00:00:00 2001 From: goodroach Date: Sat, 7 Oct 2023 23:13:23 -1000 Subject: [PATCH 19/20] optimizations --- .../features/directors/AADirectors.java | 44 +++----- .../features/directors/CannonDirectors.java | 63 +++++------ .../features/directors/DirectorData.java | 58 ++++++++-- .../combat/features/directors/Directors.java | 104 +++++++++--------- src/main/resources/config.yml | 2 +- .../localisation/mcclang_en.properties | 2 + 6 files changed, 144 insertions(+), 129 deletions(-) diff --git a/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java b/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java index 3dfefd18..a40fc380 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/directors/AADirectors.java @@ -11,7 +11,6 @@ import net.countercraft.movecraft.util.MathUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; -import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.World; @@ -29,6 +28,7 @@ import org.jetbrains.annotations.NotNull; import java.util.HashSet; +import java.util.Set; import static net.countercraft.movecraft.util.ChatUtils.ERROR_PREFIX; @@ -80,34 +80,23 @@ private void processFireball(@NotNull SmallFireball fireball) { if (!(c instanceof PlayerCraft) || !hasDirector((PlayerCraft) c)) return; - HashSet directorDataSet = getDirectorDataSet((PlayerCraft) c); - Player player = null; + HashSet craftDirectors = getCraftDirectors((PlayerCraft) c); + Player player; Player dominantPlayer = null; - for (DirectorData data : directorDataSet) { - if (data.getSelectedSigns().isEmpty() || getLocations(data).isEmpty()) { + for (DirectorData data : craftDirectors) { + if (data.getSelectedNodes().isEmpty() || data.getSignLocations().isEmpty()) { dominantPlayer = data.getPlayer(); } } if (dominantPlayer != null) { player = dominantPlayer; } else { - for (DirectorData data : directorDataSet) { - HashSet locations = getLocations(data); - for (Location location : locations) { - int distX = Math.abs(location.getBlockX() - fireball.getLocation().getBlockX()); - int distY = Math.abs(location.getBlockY() - fireball.getLocation().getBlockY()); - int distZ = Math.abs(location.getBlockZ() - fireball.getLocation().getBlockZ()); - - if (distX <= AADirectorNodeDistance && distY <= AADirectorNodeDistance && distZ <= AADirectorNodeDistance) { - player = data.getPlayer(); - break; - } - } - if (player != null) { - break; - } - } + player = getClosestDirectorFromProjectile( + craftDirectors, + fireball.getLocation().toVector(), + AADirectorNodeDistance + ); } if (player == null || player.getInventory().getItemInMainHand().getType() != Directors.DirectorTool) @@ -117,7 +106,7 @@ private void processFireball(@NotNull SmallFireball fireball) { int distX = Math.abs(midpoint.getX() - fireball.getLocation().getBlockX()); int distY = Math.abs(midpoint.getY() - fireball.getLocation().getBlockY()); int distZ = Math.abs(midpoint.getZ() - fireball.getLocation().getBlockZ()); - if (distX >= AADirectorDistance || distY >= AADirectorDistance || distZ >= AADirectorDistance) + if (distX*distX + distY*distY + distZ*distZ >= AADirectorDistance*AADirectorDistance) return; fireball.setShooter(player); @@ -210,14 +199,15 @@ public void onSignClick(@NotNull PlayerInteractEvent e) { return; } - clearDirector(p); - DirectorData data = addDirector(p, foundCraft, sign.getLine(1), sign.getLine(2), sign.getLine(3)); - - if (isNodesShared(data)) { - p.sendMessage(ERROR_PREFIX + " " + I18nSupport.getInternationalisedString("CannonDirector - Must Not Share Nodes")); + Set selectedLines = processSign(sign); + if (isNodesShared(selectedLines, foundCraft, p)) { + p.sendMessage(ERROR_PREFIX + " " + I18nSupport.getInternationalisedString("AADirector - Must Not Share Nodes")); return; } + clearDirector(p); + addDirector(p, foundCraft, selectedLines); + p.sendMessage(I18nSupport.getInternationalisedString("AADirector - Directing")); } } diff --git a/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java b/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java index a7298059..0fc8610e 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/directors/CannonDirectors.java @@ -20,6 +20,7 @@ import org.bukkit.World; import org.bukkit.block.Block; import org.bukkit.block.Sign; +import org.bukkit.block.data.type.TNT; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.bukkit.entity.TNTPrimed; @@ -34,9 +35,9 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Arrays; import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.UUID; import static net.countercraft.movecraft.util.ChatUtils.ERROR_PREFIX; @@ -50,7 +51,6 @@ public class CannonDirectors extends Directors implements Listener { private final Object2DoubleOpenHashMap tracking = new Object2DoubleOpenHashMap<>(); private long lastCheck = 0; - public CannonDirectors() { super(); } @@ -103,47 +103,33 @@ private void processTNT(@NotNull TNTPrimed tnt) { if (!(c instanceof PlayerCraft)) return; - HashSet directorDataSet = getDirectorDataSet((PlayerCraft) c); + // Automatically calibrate the TNT location based on its velocity to make it closer to the firing point. + Location correctedLocation = tnt.getLocation().clone().add(tnt.getVelocity().clone().multiply(-1.2)); + Vector correctedPosition = correctedLocation.toVector(); + + HashSet craftDirectors = getCraftDirectors((PlayerCraft) c); Player player = null; - Player dominantPlayer = null; - // Adjust the TNT location based on its velocity to make it closer to the firing point. - int ticks = 1; - Location correctedLocation = tnt.getLocation().clone().add(tnt.getVelocity().clone().multiply(-ticks)); - - for (DirectorData data : directorDataSet) { - if (data.getSelectedSigns().isEmpty() || getLocations(data).isEmpty()) { - dominantPlayer = data.getPlayer(); + for (DirectorData data : craftDirectors) { + if (data.getSelectedNodes().isEmpty()) { + player = data.getPlayer(); } } - if (dominantPlayer != null) { - player = dominantPlayer; - } else { - for (DirectorData data : directorDataSet) { - HashSet locations = getLocations(data); - for (Location location : locations) { - int distX = Math.abs(location.getBlockX() - correctedLocation.getBlockX()); - int distY = Math.abs(location.getBlockY() - correctedLocation.getBlockY()); - int distZ = Math.abs(location.getBlockZ() - correctedLocation.getBlockZ()); - - if (distX <= CannonDirectorNodeDistance && distY <= CannonDirectorNodeDistance && distZ <= CannonDirectorNodeDistance) { - player = data.getPlayer(); - break; - } - } - if (player != null) { - break; - } - } + if (player == null) { + player = getClosestDirectorFromProjectile( + craftDirectors, + correctedPosition, + CannonDirectorNodeDistance + ); } if (player == null || player.getInventory().getItemInMainHand().getType() != Directors.DirectorTool) return; MovecraftLocation midpoint = c.getHitBox().getMidPoint(); - int distX = Math.abs(midpoint.getX() - correctedLocation.getBlockX()); - int distY = Math.abs(midpoint.getY() - correctedLocation.getBlockY()); - int distZ = Math.abs(midpoint.getZ() - correctedLocation.getBlockZ()); - if (distX >= CannonDirectorDistance || distY >= CannonDirectorDistance || distZ >= CannonDirectorDistance) + int distX = Math.abs(midpoint.getX() - correctedPosition.getBlockX()); + int distY = Math.abs(midpoint.getY() - correctedPosition.getBlockY()); + int distZ = Math.abs(midpoint.getZ() - correctedPosition.getBlockZ()); + if (distX*distX + distY*distY + distZ*distZ >= CannonDirectorDistance*CannonDirectorDistance) return; // Store the speed to add it back in later, since all the values we will be using are "normalized", IE: have a speed of 1 @@ -243,14 +229,15 @@ public final void onSignClick(@NotNull PlayerInteractEvent e) { return; } - clearDirector(p); - DirectorData data = addDirector(p, foundCraft, sign.getLine(1), sign.getLine(2), sign.getLine(3)); - - if (isNodesShared(data)) { + Set selectedLines = processSign(sign); + if (isNodesShared(selectedLines, foundCraft, p)) { p.sendMessage(ERROR_PREFIX + " " + I18nSupport.getInternationalisedString("CannonDirector - Must Not Share Nodes")); return; } + clearDirector(p); + addDirector(p, foundCraft, selectedLines); + p.sendMessage(I18nSupport.getInternationalisedString("CannonDirector - Directing")); } diff --git a/src/main/java/net/countercraft/movecraft/combat/features/directors/DirectorData.java b/src/main/java/net/countercraft/movecraft/combat/features/directors/DirectorData.java index a1f41530..e0bb8fcd 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/directors/DirectorData.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/directors/DirectorData.java @@ -1,29 +1,71 @@ package net.countercraft.movecraft.combat.features.directors; + +import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.craft.PlayerCraft; +import org.bukkit.block.Block; +import org.bukkit.block.Sign; import org.bukkit.entity.Player; +import org.bukkit.util.Vector; +import org.jetbrains.annotations.NotNull; -import java.util.List; +import java.util.HashSet; +import java.util.Set; public class DirectorData { - private Player player; - private PlayerCraft craft; - private List selectedSigns; + private final Player player; + private final PlayerCraft craft; + private final Set selectedNodes; + private Set signLocations = new HashSet<>(); + private MovecraftLocation currentLocation; - public DirectorData(Player player, PlayerCraft craft, List selectedSigns) { + public DirectorData(Player player, PlayerCraft craft, Set selectedNodes) { this.player = player; this.craft = craft; - this.selectedSigns = selectedSigns; + this.selectedNodes = selectedNodes; + } + + @NotNull + public Set getSignLocations() { + if (selectedNodes.isEmpty()) return signLocations; + //If the craft stays in the same position, return the already known sign locations. + MovecraftLocation midpoint = craft.getHitBox().getMidPoint(); + if (currentLocation == null) currentLocation = midpoint; + if (currentLocation.equals(midpoint) && !signLocations.isEmpty()) { + return signLocations; + } + + currentLocation = midpoint; + for (MovecraftLocation location : craft.getHitBox()) { + Block block = craft.getWorld().getBlockAt( + location.getX(), + location.getY(), + location.getZ() + ); + if (!(block.getState() instanceof Sign)) continue; + Sign sign = (Sign) block.getState(); + + if (!sign.getLine(0).equalsIgnoreCase("subcraft rotate")) continue; + if (sign.getLine(3).isBlank()) continue; + if (!selectedNodes.contains(sign.getLine(3))) continue; + + Vector relativeVector = sign.getLocation().toVector(); + signLocations.add(relativeVector); + } + return signLocations; } + @NotNull public Player getPlayer() { return player; } + @NotNull public PlayerCraft getCraft() { return craft; } - public List getSelectedSigns() { - return selectedSigns; + @NotNull + public Set getSelectedNodes() { + return selectedNodes; } } \ No newline at end of file diff --git a/src/main/java/net/countercraft/movecraft/combat/features/directors/Directors.java b/src/main/java/net/countercraft/movecraft/combat/features/directors/Directors.java index 872d7e0b..fd6b7042 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/directors/Directors.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/directors/Directors.java @@ -1,25 +1,19 @@ package net.countercraft.movecraft.combat.features.directors; -import com.google.common.collect.HashBiMap; -import net.countercraft.movecraft.MovecraftLocation; import net.countercraft.movecraft.combat.MovecraftCombat; import net.countercraft.movecraft.craft.PlayerCraft; import net.countercraft.movecraft.util.Tags; -import org.bukkit.Location; import org.bukkit.Material; -import org.bukkit.block.Block; import org.bukkit.block.Sign; -import org.bukkit.block.data.type.WallSign; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.entity.Player; import org.bukkit.scheduler.BukkitRunnable; +import org.bukkit.util.Vector; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Set; @@ -68,29 +62,23 @@ public void run() { } - public DirectorData addDirector(Player player, PlayerCraft craft, String line1, String line2, String line3) { + public void addDirector(Player player, PlayerCraft craft, Set selectedLines) { if (directors.containsValue(player)) directors.remove(player); - List selectedLines = new ArrayList<>(); - selectedLines.add(line1); - selectedLines.add(line2); - selectedLines.add(line3); - if (line1.isBlank() && line2.isBlank() && line3.isBlank()) selectedLines.clear(); DirectorData data = new DirectorData(player, craft, selectedLines); directors.put(player, data); - System.out.println("New director added: " + directors.get(player).getSelectedSigns()); - System.out.println("Director name: " + directors.get(player).getPlayer()); - return data; } - public HashSet getDirectorDataSet(PlayerCraft craft) { + @NotNull + public HashSet getCraftDirectors(@NotNull PlayerCraft craft) { HashSet directorDataSet = new HashSet<>(); - for (DirectorData data : directors.values()) { - if (data.getCraft() == craft) directorDataSet.add(data); + for (DirectorData directorData : directors.values()) { + if (directorData.getCraft() == craft) directorDataSet.add(directorData); } return directorDataSet; } - public boolean hasDirector(PlayerCraft craft) { + + public boolean hasDirector(@Nullable PlayerCraft craft) { if (craft == null) return false; for (DirectorData data : directors.values()) { if (data.getCraft() == craft) return true; @@ -98,12 +86,46 @@ public boolean hasDirector(PlayerCraft craft) { return false; } - public boolean isNodesShared(DirectorData director) { - PlayerCraft craft = director.getCraft(); - List selectedSigns = director.getSelectedSigns(); - return directors.values().stream() - .filter(data -> data != director && data.getCraft() == craft) - .anyMatch(data -> data.getSelectedSigns().stream().anyMatch(selectedSigns::contains)); + //This ensures that no two director nodes are shared between the director players. + public boolean isNodesShared(Set selectedStrings, PlayerCraft craft, Player player) { + for (DirectorData directorData : getCraftDirectors(craft)) { + if (directorData.getPlayer() == player) continue; + if (selectedStrings.isEmpty()) return false; + Set stringsCopy = new HashSet<>(selectedStrings); + stringsCopy.retainAll(directorData.getSelectedNodes()); + if (!stringsCopy.isEmpty()) return true; + } + return false; + } + + @NotNull + public Set processSign(Sign sign) { + String[] lines = sign.getLines(); + Set selectedLines = new HashSet<>(); + + for (int i = 1; i < lines.length ; i++) { + String line = lines[i].trim(); + if (!line.isBlank()) selectedLines.add(line); + } + + return selectedLines; + } + + @Nullable + public Player getClosestDirectorFromProjectile( + Set directorDataSet, + Vector projectile, + int nodeDistance + ) { + for (DirectorData directorData : directorDataSet) { + for (Vector signLocation : directorData.getSignLocations()) { + // Calculate squared distance. + if (signLocation.distanceSquared(projectile) <= (nodeDistance * nodeDistance)) { + return directorData.getPlayer(); + } + } + } + return null; } public boolean isDirector(@NotNull Player player) { @@ -114,37 +136,9 @@ public void removeDirector(@NotNull Player player) { directors.remove(player); } + //This clears all DirectorData that might be already assigned to the player. public void clearDirector(@NotNull Player player) { for (var instance : instances) instance.removeDirector(player); } - - public HashSet getLocations(DirectorData data) { - if (data.getSelectedSigns().isEmpty() || data.getCraft() == null) { - return null; - } - PlayerCraft craft = data.getCraft(); - - HashSet locations = new HashSet<>(); - for (MovecraftLocation location : craft.getHitBox()) { - Block block = craft.getWorld().getBlockAt(location.getX(), location.getY(), location.getZ()); - if (!(block.getState() instanceof Sign)) - continue; - - Sign sign = (Sign) block.getState(); - - if (!sign.getLine(0).equalsIgnoreCase("subcraft rotate")) { - continue; - } - if (sign.getLine(3).isBlank()) { - System.out.println("Sign is blank"); - continue; - } - if (data.getSelectedSigns().contains(sign.getLine(3))) { - System.out.println("Sign found."); - locations.add(block.getLocation()); - } - } - return locations; - } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 1bc53291..27f7e2db 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -5,7 +5,7 @@ Locale: "en" # Language file to load, currently the plugin comes with en and no, # AA Directors AADirectorDistance: 50 # Max range at which fireballs will be redirected -AADirectorNodeDistance: # See node directors +AADirectorNodeDistance: 3 # See node directors AADirectorRange: 120 # Max range at which it will direct to a block vs in the general direction # Cannon Directors diff --git a/src/main/resources/localisation/mcclang_en.properties b/src/main/resources/localisation/mcclang_en.properties index 3cfbad1a..29d037eb 100644 --- a/src/main/resources/localisation/mcclang_en.properties +++ b/src/main/resources/localisation/mcclang_en.properties @@ -1,11 +1,13 @@ AADirector\ -\ Directing=You are now directing the AA of this craft AADirector\ -\ No\ Longer\ Directing=You are no longer directing the AA of this craft AADirector\ -\ Not\ Allowed\ On\ Craft=AA Director signs are not allowed on this type of craft +AADirector\ -\ Must\ Not\ Share\ Nodes=You must not share nodes with other AA directors Combat\ Release=Combat Release\! Combat\ Release\ Message=Combat releasing is not allowed\! CannonDirector\ -\ Directing=You are now directing the cannons of this craft CannonDirector\ -\ No\ Longer\ Directing=You are no longer directing the cannons of this craft CannonDirector\ -\ Not\ Allowed\ On\ Craft=Cannon Director signs are not allowed on this type of craft +CannonDirector\ -\ Must\ Not\ Share\ Nodes=You must not share nodes with other cannon directors Command\ -\ Must\ Be\ Player=You must be a player to use that command. Command\ -\ Current\ Mode=Current mode Command\ -\ Current\ Setting=Current setting From 526503f1c4853558dcfcc8a5b1fd4f38a1ddbec4 Mon Sep 17 00:00:00 2001 From: goodroach Date: Mon, 23 Oct 2023 02:15:40 -1000 Subject: [PATCH 20/20] togglable bleed fix --- .../movecraft/combat/features/DurabilityOverride.java | 4 +++- src/main/resources/config.yml | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/countercraft/movecraft/combat/features/DurabilityOverride.java b/src/main/java/net/countercraft/movecraft/combat/features/DurabilityOverride.java index 51f3dc94..76335b40 100644 --- a/src/main/java/net/countercraft/movecraft/combat/features/DurabilityOverride.java +++ b/src/main/java/net/countercraft/movecraft/combat/features/DurabilityOverride.java @@ -21,6 +21,7 @@ public class DurabilityOverride implements Listener { public static Map DurabilityOverride = null; + public static boolean EnableBleedFix = true; public static void load(@NotNull FileConfiguration config) { if (!config.contains("DurabilityOverride")) @@ -29,6 +30,7 @@ public static void load(@NotNull FileConfiguration config) { if (section == null) return; + EnableBleedFix = config.getBoolean("EnableBleedFix", true); DurabilityOverride = new HashMap<>(); for (var entry : section.getValues(false).entrySet()) { EnumSet materials = Tags.parseMaterials(entry.getKey()); @@ -55,7 +57,7 @@ public void onEntityExplode(@NotNull EntityExplodeEvent e) { Set removeList = new HashSet<>(); for (Block b : e.blockList()) { // remove the block if no adjacent blocks are air (IE: the explosion skipped a block) - if (!nextToAir(b)) { + if (!nextToAir(b) && EnableBleedFix) { removeList.add(b); continue; } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index b1137f69..a0506ce6 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -36,6 +36,7 @@ ContactExplosivesMaxImpulseFactor: 10.0 # The maximum fraction of its velocity t ContactExplosivesMinImpulse: 0.35 # Minimum velocity for contact explosives to track a TNT entity # Durability Override +EnableBleedFix: true # Ensures only surface blocks gets damaged by TNT DurabilityOverride: # List of block IDs: ignore percent END_STONE: 80 END_STONE_BRICKS: 80