diff --git a/Challenge/src/main/java/de/fanta/challenge/challenges/ChallengeEvents/forcequest/ForceQuestChallengeEvent.java b/Challenge/src/main/java/de/fanta/challenge/challenges/ChallengeEvents/forcequest/ForceQuestChallengeEvent.java index 131c144..cc638a5 100644 --- a/Challenge/src/main/java/de/fanta/challenge/challenges/ChallengeEvents/forcequest/ForceQuestChallengeEvent.java +++ b/Challenge/src/main/java/de/fanta/challenge/challenges/ChallengeEvents/forcequest/ForceQuestChallengeEvent.java @@ -11,6 +11,7 @@ import de.fanta.challenge.utils.CubesideModUtils; import de.fanta.challengeutils.ItemUtils; import de.fanta.challengeutils.Color; +import de.fanta.challengeutils.LocationUtils; import de.iani.cubesideutils.StringUtil; import de.iani.cubesideutils.bukkit.inventory.AbstractWindow; import de.iani.cubesideutils.bukkit.items.ItemGroups; @@ -65,6 +66,7 @@ import org.bukkit.event.player.PlayerSwapHandItemsEvent; import org.bukkit.generator.structure.GeneratedStructure; import org.bukkit.generator.structure.Structure; +import org.bukkit.generator.structure.StructurePiece; import org.bukkit.inventory.CraftingInventory; import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; @@ -74,6 +76,7 @@ import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; import org.bukkit.scheduler.BukkitTask; +import org.bukkit.util.BoundingBox; import org.bukkit.util.Vector; import org.kitteh.vanish.event.VanishStatusChangeEvent; @@ -130,6 +133,7 @@ public static final Collection activeQuestTypes = new ArrayList<>(); private final HashMap playerStructureMessage = new HashMap<>(); + private final HashMap boundingBoxCache = new HashMap<>(); @EventHandler public void onActivation(TimerChangedEvent event) { @@ -280,7 +284,7 @@ damageCauseByPlayerList.put(player.getUniqueId(), damageCause); } case ForceLocation -> { - Location location = getRandomLocationAroundPlayer(player, 350); + Location location = LocationUtils.getRandomLocationAroundPlayer(player, 350); locationByPlayerList.put(player.getUniqueId(), location); CoordsTargeter.addLocation(player.getUniqueId(), location, false); } @@ -355,29 +359,6 @@ throw new IllegalStateException("Fehler bei Ermittlung eines zufälligen Quest Typs. Ermittelter wert: " + result + " | Maximaler Wert: " + MAX_PROBABILITY); } - public static Location getRandomLocationAroundPlayer(Player player, double radius) { - double angle = random.nextDouble() * 2 * Math.PI; - double distance = random.nextDouble() * radius; - - double xOffset = Math.cos(angle) * distance; - double zOffset = Math.sin(angle) * distance; - - World world = player.getWorld(); - int maxHeight = world.getMaxHeight(); - int minHeight = world.getMinHeight() + 5; //+5 Weil Bedrock - - if (world.getEnvironment() == World.Environment.NETHER) { - maxHeight = 122; //Max Logical height in Nether - } - - Location playerLocation = player.getLocation(); - double newX = playerLocation.getX() + xOffset; - double newZ = playerLocation.getZ() + zOffset; - double newY = random.nextDouble(minHeight, maxHeight); - - return new Location(player.getWorld(), newX, newY, newZ); - } - @EventHandler public void onGameModeSwitch(PlayerGameModeChangeEvent e) { if (isRunning()) { @@ -782,17 +763,29 @@ if (questType == QuestType.ForceStructure) { for (GeneratedStructure generatedStructure : player.getChunk().getStructures()) { - generatedStructure.getPieces().forEach(structurePiece -> { + + BoundingBox boundingBox = boundingBoxCache.get(generatedStructure); + + if (boundingBox == null) { + List piecesBoxes = new ArrayList<>(); + for (StructurePiece piece : generatedStructure.getPieces()) { + piecesBoxes.add(piece.getBoundingBox()); + } + boundingBox = LocationUtils.calculateEnclosingBoundingBox(piecesBoxes); + boundingBoxCache.put(generatedStructure, boundingBox); + } + + for (StructurePiece structurePiece : generatedStructure.getPieces()) { if (structurePiece.getBoundingBox().overlaps(player.getBoundingBox())) { selectStructure(player, generatedStructure.getStructure()); Vector lastStructure = playerStructureMessage.getOrDefault(player.getUniqueId(), new Vector(0, 1000, 0)); - if (!lastStructure.equals(generatedStructure.getBoundingBox().getCenter())) { + if (!lastStructure.equals(boundingBox.getCenter())) { plugin.getComponentUtil().sendTitleToPlayer(player, Component.empty(), Component.translatable(generatedStructure.getStructure().key().asMinimalString(), Color.GREEN), 10, 100, 10, false); } - playerStructureMessage.put(player.getUniqueId(), generatedStructure.getBoundingBox().getCenter()); + playerStructureMessage.put(player.getUniqueId(), boundingBox.getCenter()); } - }); + } } } }