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 594d242..e3d3669 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 @@ -2,7 +2,6 @@ import de.fanta.challenge.Challenge; import de.fanta.challenge.events.TimerChangedEvent; -import de.fanta.challenge.schedular.CancellableTask; import de.fanta.challenge.scoreboard.ChallengePlayer; import de.fanta.challenge.scoreboard.Scorable; import de.fanta.challenge.scoreboard.ScoreManager; @@ -27,6 +26,7 @@ import org.bukkit.OfflinePlayer; import org.bukkit.Registry; import org.bukkit.Sound; +import org.bukkit.World; import org.bukkit.block.Biome; import org.bukkit.block.Block; import org.bukkit.enchantments.Enchantment; @@ -62,6 +62,8 @@ import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.event.player.PlayerRespawnEvent; import org.bukkit.event.player.PlayerSwapHandItemsEvent; +import org.bukkit.generator.structure.GeneratedStructure; +import org.bukkit.generator.structure.Structure; import org.bukkit.inventory.CraftingInventory; import org.bukkit.inventory.ItemFlag; import org.bukkit.inventory.ItemStack; @@ -70,6 +72,7 @@ import org.bukkit.persistence.PersistentDataType; import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffectType; +import org.bukkit.scheduler.BukkitTask; import org.kitteh.vanish.event.VanishStatusChangeEvent; import java.util.ArrayList; @@ -97,6 +100,7 @@ private static final ArrayList entityTypes = new ArrayList<>(); private static final ArrayList damageCauses = new ArrayList<>(); private static final ArrayList potionEffectTypes = new ArrayList<>(); + private static final ArrayList structureTypes = new ArrayList<>(); private static final HashMap itemByPlayerList = new HashMap<>(); private static final HashMap biomeByPlayerList = new HashMap<>(); @@ -105,17 +109,19 @@ private static final HashMap potionEffectTypeByPlayerList = new HashMap<>(); private static final HashMap locationByPlayerList = new HashMap<>(); private static final HashMap lastPlayerDamageCauseList = new HashMap<>(); + private static final HashMap structureTypeByPlayerList = new HashMap<>(); private static final HashMap> playerMaterials = new HashMap<>(); private static final HashMap> playerBiomes = new HashMap<>(); private static final HashMap> playerEntityTypes = new HashMap<>(); private static final HashMap> playerDamageCauses = new HashMap<>(); private static final HashMap> playerPotionEffectTypes = new HashMap<>(); + private static final HashMap> playerStructureTypes = new HashMap<>(); private static final ArrayList dismountAllowList = new ArrayList<>(); private static final HashMap bossBarMap = new HashMap<>(); - private final HashMap playerTasks = new HashMap<>(); + private BukkitTask playerTasks; private static final HashMap playerQuest = new HashMap<>(); private final Collection initPlayers = new ArrayList<>(); @@ -152,7 +158,7 @@ } for (EntityDamageEvent.DamageCause damageCause : EntityDamageEvent.DamageCause.values()) { - if (damageCause != EntityDamageEvent.DamageCause.KILL && damageCause != EntityDamageEvent.DamageCause.WORLD_BORDER && damageCause != EntityDamageEvent.DamageCause.CUSTOM && damageCause != EntityDamageEvent.DamageCause.DRYOUT && damageCause != EntityDamageEvent.DamageCause.ENTITY_SWEEP_ATTACK && damageCause != EntityDamageEvent.DamageCause.MELTING && damageCause != EntityDamageEvent.DamageCause.SUICIDE) { + if (damageCause != EntityDamageEvent.DamageCause.KILL && damageCause != EntityDamageEvent.DamageCause.WORLD_BORDER && damageCause != EntityDamageEvent.DamageCause.CUSTOM && damageCause != EntityDamageEvent.DamageCause.DRYOUT && damageCause != EntityDamageEvent.DamageCause.ENTITY_SWEEP_ATTACK && damageCause != EntityDamageEvent.DamageCause.MELTING && damageCause != EntityDamageEvent.DamageCause.SUICIDE && damageCause != EntityDamageEvent.DamageCause.POISON) { damageCauses.add(damageCause); } } @@ -163,10 +169,15 @@ } } + for (Structure structure : Registry.STRUCTURE) { + structureTypes.add(structure); + } + for (Player pp : plugin.getVanish().getPlayerListWithoutVanishPlayers()) { initPlayer(pp); } + startUpdateTask(); setRunning(true); } else { stopUpdateTask(); @@ -200,6 +211,7 @@ playerEntityTypes.put(player.getUniqueId(), new ArrayList<>(entityTypes)); playerDamageCauses.put(player.getUniqueId(), new ArrayList<>(damageCauses)); playerPotionEffectTypes.put(player.getUniqueId(), new ArrayList<>(potionEffectTypes)); + playerStructureTypes.put(player.getUniqueId(), new ArrayList<>(structureTypes)); player.getInventory().setItem(8, skipItem); initPlayers.add(player.getUniqueId()); @@ -211,7 +223,6 @@ addArmorStand(player); updateArmorStand(player); - startUpdateTask(player); } private static void createNewQuest(Player player) { @@ -261,7 +272,11 @@ locationByPlayerList.put(player.getUniqueId(), location); CoordsTargeter.addLocation(player.getUniqueId(), location, false); } - + case ForceStructure -> { + ArrayList itemList = playerStructureTypes.get(player.getUniqueId()); + Structure structureType = itemList.get(random.nextInt((itemList.size() - 1) + 1)); + structureTypeByPlayerList.put(player.getUniqueId(), structureType); + } } } @@ -316,6 +331,12 @@ case ForceLocation -> { return entry.getValue(); } + case ForceStructure -> { + if (playerStructureTypes.get(player.getUniqueId()).isEmpty()) { + return getRandomQuest(player); + } + return entry.getValue(); + } } } } @@ -330,10 +351,18 @@ 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(player.getWorld().getMinHeight() + 5, player.getWorld().getMaxHeight()); + double newY = random.nextDouble(minHeight, maxHeight); return new Location(player.getWorld(), newX, newY, newZ); } @@ -377,7 +406,6 @@ if (isRunning()) { Player player = e.getPlayer(); removeArmorStand(player); - stopUpdateTask(e.getPlayer()); } } @@ -476,6 +504,7 @@ ItemStack stack = p.getInventory().getItemInMainHand(); ItemMeta meta = stack.getItemMeta(); if (meta != null && meta.getPersistentDataContainer().has(skipKey)) { + plugin.getLogger().info(p.getOpenInventory().getType().name()); e.setCancelled(true); } } @@ -617,7 +646,7 @@ public void onRespawn(PlayerRespawnEvent e) { if (isRunning()) { Player player = e.getPlayer(); - plugin.getScheduler().runDelayedOnEntity(player, () -> addArmorStand(player), 1L); + plugin.getServer().getScheduler().runTaskLater(plugin, () -> addArmorStand(player), 1L); PlayerInventory inventory = e.getPlayer().getInventory(); int countSkipper = 0; @@ -688,6 +717,16 @@ selectBiome(player, player.getWorld().getBiome(player.getLocation())); } } + + if (questType == QuestType.ForceStructure) { + for (GeneratedStructure generatedStructure : player.getChunk().getStructures()) { + plugin.getLogger().info("Structure: " + generatedStructure.getStructure().key().asMinimalString()); + if (generatedStructure.getBoundingBox().overlaps(player.getBoundingBox())) { + plugin.getLogger().info("In Structure: " + generatedStructure.getStructure().key().asMinimalString()); + selectStructure(player, generatedStructure.getStructure()); + } + } + } } @EventHandler @@ -741,54 +780,46 @@ } } - public void startUpdateTask(Player player) { - playerTasks.put(player.getUniqueId(), plugin.getScheduler().runOnEntityAtFixedRate(player, () -> tick(player), 1L, 1L)); + public void startUpdateTask() { + playerTasks = plugin.getServer().getScheduler().runTaskTimer(plugin, () -> tick(), 1L, 1L); } public void stopUpdateTask() { - for (CancellableTask cancellableTask : playerTasks.values()) { - if (cancellableTask != null) { - cancellableTask.cancel(); - } - } - playerTasks.clear(); + playerTasks.cancel(); + playerTasks = null; } - public void stopUpdateTask(Player player) { - CancellableTask cancellableTask = playerTasks.get(player.getUniqueId()); - cancellableTask.cancel(); - playerTasks.remove(player.getUniqueId()); - } - - private void tick(Player player) { - if (player.getGameMode() == GameMode.SPECTATOR || plugin.getVanish().isVanish(player)) { - return; - } - - Location location = player.getLocation(); - Block block = location.getBlock(); - if (block.getType() == Material.NETHER_PORTAL || block.getType() == Material.END_PORTAL || block.getType() == Material.END_GATEWAY) { - removeArmorStand(player); - } else { - if (player.getPassengers().isEmpty()) { - addArmorStand(player); + private void tick() { + plugin.getVanish().getPlayerListWithoutVanishPlayers().forEach(player -> { + if (player.getGameMode() == GameMode.SPECTATOR) { + return; } - } - if (!player.getPassengers().isEmpty()) { - for (Entity entity : player.getPassengers()) { - entity.setRotation(location.getYaw(), location.getPitch()); - } - if (location.getPitch() < -50) { - for (Entity entity : player.getPassengers()) { - player.hideEntity(plugin, entity); - } + Location location = player.getLocation(); + Block block = location.getBlock(); + if (block.getType() == Material.NETHER_PORTAL || block.getType() == Material.END_PORTAL || block.getType() == Material.END_GATEWAY) { + removeArmorStand(player); } else { - for (Entity entity : player.getPassengers()) { - player.showEntity(plugin, entity); + if (player.getPassengers().isEmpty()) { + addArmorStand(player); } } - } + + if (!player.getPassengers().isEmpty()) { + for (Entity entity : player.getPassengers()) { + entity.setRotation(location.getYaw(), location.getPitch()); + } + if (location.getPitch() < -50) { + for (Entity entity : player.getPassengers()) { + player.hideEntity(plugin, entity); + } + } else { + for (Entity entity : player.getPassengers()) { + player.showEntity(plugin, entity); + } + } + } + }); } public static void mountArmorStand(Player player) { @@ -898,6 +929,10 @@ private static void selectLocation(Player player, Location location) { Location playerLocation = locationByPlayerList.get(player.getUniqueId()); + if (playerLocation.getWorld() != location.getWorld()) { + return; + } + if (location.clone().toCenterLocation().distance(playerLocation.clone().toCenterLocation()) <= 1) { Block block = location.getBlock(); String locationString = "X: " + block.getX() + ", Y: " + block.getY() + ", Z: " + block.getZ(); @@ -907,6 +942,19 @@ } } + private static void selectStructure(Player player, Structure structureType) { + Structure playerStructureTyp = structureTypeByPlayerList.get(player.getUniqueId()); + if (structureType.equals(playerStructureTyp)) { + structureTypeByPlayerList.remove(player.getUniqueId()); + ArrayList structureList = playerStructureTypes.get(player.getUniqueId()); + structureList.remove(structureType); + playerStructureTypes.put(player.getUniqueId(), structureList); + TextComponent textComponent = Component.text("Structure: ").color(Color.GREEN).append(Component.translatable(StringUtil.capitalizeFirstLetter(structureType.key().asString(), true)).color(Color.BLUE)).append(Component.text(" wurde Registriert").color(Color.GREEN)); + plugin.getComponentUtil().sendMessage(player, textComponent); + finishQuest(player); + } + } + private static void finishQuest(Player player) { plugin.getScoreManager().updateScore(new ChallengePlayer(player.getUniqueId()), 1); CubesideModUtils.sendFlashScreenToCubesideMod(plugin, player, 50, Color.GREEN); @@ -963,6 +1011,12 @@ bossBarTitle = getBossBarTitle(">> ForceLocation: ", Component.translatable(locationString), scoreManager.getPosition(new ChallengePlayer(player.getUniqueId()))); } } + case ForceStructure -> { + Structure structureType = structureTypeByPlayerList.get(player.getUniqueId()); + if (structureType != null) { + bossBarTitle = getBossBarTitle(">> StructureType: ", Component.translatable(StringUtil.capitalizeFirstLetter(structureType.key().asMinimalString(), true)), scoreManager.getPosition(new ChallengePlayer(player.getUniqueId()))); + } + } } BossBar bossBar = bossBarMap.computeIfAbsent(player.getUniqueId(), bossPlayer -> { @@ -991,7 +1045,7 @@ } public static void addArmorStand(Player player) { - plugin.getScheduler().runDelayedOnEntity(player, () -> { + plugin.getServer().getScheduler().runTaskLater(plugin, () -> { if (!player.isDead()) { mountArmorStand(player); updateArmorStand(player); @@ -1045,6 +1099,14 @@ int slot = event.getSlot(); if (slot == YES_INDEX) { + ItemStack skipStack = skipItem.clone(); + skipStack.setAmount(1); + HashMap removedItems = player.getInventory().removeItem(skipStack); + if (!removedItems.isEmpty()) { + plugin.getComponentUtil().sendErrorMessage(player, "Du hast keine Skipper im Inventar!!!"); + return; + } + QuestType questType = playerQuest.get(player.getUniqueId()); switch (questType) { case ForceItem -> { @@ -1077,12 +1139,12 @@ Location location = locationByPlayerList.get(player.getUniqueId()); selectLocation(player, location); } + case ForceStructure -> { + Structure structureType = structureTypeByPlayerList.get(player.getUniqueId()); + selectStructure(player, structureType); + } } - - ItemStack skipStack = skipItem.clone(); - skipStack.setAmount(1); - player.getInventory().removeItem(skipStack); player.closeInventory(); } if (slot == NO_INDEX) {