From bdc607bfe0ee5264a2ecc647b2cb55398991b2c6 Mon Sep 17 00:00:00 2001 From: Adam Matthew Date: Sat, 4 Jul 2020 22:17:21 +0800 Subject: [PATCH] Phase 1: Implementing more events for DEVs Accidentally did an oopsies with Caching... Bump API core to 31, v0.5.2-BETA Fixed a probable memory leak, perhaps. IslandLevelCalculations are now LIVE! GET MORE BLOCKS. --- .../java/com/larryTheCoder/ASkyBlock.java | 7 +-- .../com/larryTheCoder/cache/FastCache.java | 8 ++- .../com/larryTheCoder/cache/IslandData.java | 6 +-- .../com/larryTheCoder/cache/PlayerData.java | 4 +- .../events/IslandCalculateFinishEvent.java | 53 +++++++++++++++++++ .../events/IslandCalculateLevelEvent.java | 41 ++++++++++++++ .../events/IslandCreateEvent.java | 2 +- .../events/IslandEnterEvent.java | 2 +- .../larryTheCoder/events/IslandExitEvent.java | 2 +- .../events/IslandTeleportEvent.java | 51 ++++++++++++++++++ .../larryTheCoder/events/SkyBlockEvent.java | 39 ++++++++++---- .../events/SkyBlockPlayerEvent.java | 47 ++++++++++++++++ .../com/larryTheCoder/island/GridManager.java | 18 ++++++- .../larryTheCoder/island/IslandManager.java | 11 +--- .../larryTheCoder/island/TeleportLogic.java | 27 ++++------ .../listener/IslandListener.java | 13 ++--- .../com/larryTheCoder/task/LevelCalcTask.java | 19 +++++-- .../java/com/larryTheCoder/utils/Utils.java | 4 -- src/main/resources/plugin.yml | 3 ++ 19 files changed, 289 insertions(+), 68 deletions(-) create mode 100644 src/main/java/com/larryTheCoder/events/IslandCalculateFinishEvent.java create mode 100644 src/main/java/com/larryTheCoder/events/IslandCalculateLevelEvent.java create mode 100644 src/main/java/com/larryTheCoder/events/IslandTeleportEvent.java create mode 100644 src/main/java/com/larryTheCoder/events/SkyBlockPlayerEvent.java diff --git a/src/main/java/com/larryTheCoder/ASkyBlock.java b/src/main/java/com/larryTheCoder/ASkyBlock.java index 9ad8df7..ea602de 100644 --- a/src/main/java/com/larryTheCoder/ASkyBlock.java +++ b/src/main/java/com/larryTheCoder/ASkyBlock.java @@ -69,10 +69,7 @@ import org.sql2o.data.Table; import java.io.File; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; +import java.util.*; import static com.larryTheCoder.database.TableSet.FETCH_WORLDS; import static com.larryTheCoder.database.TableSet.WORLDS_INSERT; @@ -136,7 +133,7 @@ public void onEnable() { try { // Wohooo! Fast! Unique and Colorful! generateLevel(); - getServer().getLogger().info(getPrefix() + "§7Loading ASkyBlock - Bedrock Edition (API 30)"); + getServer().getLogger().info(getPrefix() + "§7Loading ASkyBlock - Bedrock Edition (API 31)"); // Only defaults initIslands(); diff --git a/src/main/java/com/larryTheCoder/cache/FastCache.java b/src/main/java/com/larryTheCoder/cache/FastCache.java index 2f95fcb..8aae076 100644 --- a/src/main/java/com/larryTheCoder/cache/FastCache.java +++ b/src/main/java/com/larryTheCoder/cache/FastCache.java @@ -81,8 +81,12 @@ public void executeQuery(Connection connection) { if (stmt.rows().size() == 0) return; for (Row plData : stmt.rows()) { - Date date = Date.from(Timestamp.valueOf(plData.getString("lastLogin")).toInstant()); - if (date.compareTo(Date.from(Instant.now())) >= Settings.loadCacheBefore) continue; + // Just an oopsies. + try { + Date date = Date.from(Timestamp.valueOf(plData.getString("lastLogin")).toInstant()); + if (date.compareTo(Date.from(Instant.now())) >= Settings.loadCacheBefore) continue; + } catch (Throwable ignored) { + } String userName = plData.getString("playerName"); diff --git a/src/main/java/com/larryTheCoder/cache/IslandData.java b/src/main/java/com/larryTheCoder/cache/IslandData.java index 2e69e9f..60277d5 100644 --- a/src/main/java/com/larryTheCoder/cache/IslandData.java +++ b/src/main/java/com/larryTheCoder/cache/IslandData.java @@ -72,11 +72,9 @@ public class IslandData implements Cloneable { @Setter private int protectionRange = 0; - @Getter - @Setter + @Getter @Setter private int levelHandicap = 0; - @Getter - @Setter + @Getter @Setter private int islandLevel = 0; // CoopDatabase diff --git a/src/main/java/com/larryTheCoder/cache/PlayerData.java b/src/main/java/com/larryTheCoder/cache/PlayerData.java index 877e545..c1527b7 100644 --- a/src/main/java/com/larryTheCoder/cache/PlayerData.java +++ b/src/main/java/com/larryTheCoder/cache/PlayerData.java @@ -35,7 +35,7 @@ import org.sql2o.Connection; import org.sql2o.data.Row; -import java.sql.Date; +import java.sql.Timestamp; import java.time.Instant; import java.util.ArrayList; import java.util.Arrays; @@ -189,7 +189,7 @@ public void executeQuery(Connection connection) { .addParameter("locale", locale) .addParameter("banList", Utils.arrayToString(banList)) .addParameter("resetLeft", resetLeft) - .addParameter("lastLogin", Date.from(Instant.now())) + .addParameter("lastLogin", Timestamp.from(Instant.now()).toString()) .executeUpdate(); connection.createQuery(PLAYER_UPDATE_DATA.getQuery()) diff --git a/src/main/java/com/larryTheCoder/events/IslandCalculateFinishEvent.java b/src/main/java/com/larryTheCoder/events/IslandCalculateFinishEvent.java new file mode 100644 index 0000000..50bb5e3 --- /dev/null +++ b/src/main/java/com/larryTheCoder/events/IslandCalculateFinishEvent.java @@ -0,0 +1,53 @@ +/* + * Adapted from the Wizardry License + * + * Copyright (c) 2016-2020 larryTheCoder and contributors + * + * Permission is hereby granted to any persons and/or organizations + * using this software to copy, modify, merge, publish, and distribute it. + * Said persons and/or organizations are not allowed to use the software or + * any derivatives of the work for commercial use or any other means to generate + * income, nor are they allowed to claim this software as their own. + * + * The persons and/or organizations are also disallowed from sub-licensing + * and/or trademarking this software without explicit permission from larryTheCoder. + * + * Any persons and/or organizations using this software must disclose their + * source code and have it publicly available, include this license, + * provide sufficient credit to the original authors of the project (IE: larryTheCoder), + * as well as provide a link to the original project. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR + * PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + * USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package com.larryTheCoder.events; + +import cn.nukkit.event.Cancellable; +import com.larryTheCoder.cache.IslandData; +import lombok.Getter; +import lombok.Setter; + +/** + * An event in which the island has successfully calculates their island + * level, this event will be called so that the developers can modify the amount of + * scores that the player's island will get. + */ +public class IslandCalculateFinishEvent extends SkyBlockEvent implements Cancellable { + + @Getter + private final int lastLevel; + @Getter @Setter + private int islandLevel; + + public IslandCalculateFinishEvent(IslandData island, int islandLevel, int lastLevel) { + super(island); + + this.islandLevel = islandLevel; + this.lastLevel = lastLevel; + } +} diff --git a/src/main/java/com/larryTheCoder/events/IslandCalculateLevelEvent.java b/src/main/java/com/larryTheCoder/events/IslandCalculateLevelEvent.java new file mode 100644 index 0000000..7ec5830 --- /dev/null +++ b/src/main/java/com/larryTheCoder/events/IslandCalculateLevelEvent.java @@ -0,0 +1,41 @@ +/* + * Adapted from the Wizardry License + * + * Copyright (c) 2016-2020 larryTheCoder and contributors + * + * Permission is hereby granted to any persons and/or organizations + * using this software to copy, modify, merge, publish, and distribute it. + * Said persons and/or organizations are not allowed to use the software or + * any derivatives of the work for commercial use or any other means to generate + * income, nor are they allowed to claim this software as their own. + * + * The persons and/or organizations are also disallowed from sub-licensing + * and/or trademarking this software without explicit permission from larryTheCoder. + * + * Any persons and/or organizations using this software must disclose their + * source code and have it publicly available, include this license, + * provide sufficient credit to the original authors of the project (IE: larryTheCoder), + * as well as provide a link to the original project. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR + * PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + * USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package com.larryTheCoder.events; + +import cn.nukkit.event.Cancellable; +import com.larryTheCoder.cache.IslandData; + +/** + * An island level calculation event. This event will be called before + * the island is being calculated, you can cancel this event if its needed. + */ +public class IslandCalculateLevelEvent extends SkyBlockEvent implements Cancellable { + public IslandCalculateLevelEvent(IslandData island) { + super(island); + } +} diff --git a/src/main/java/com/larryTheCoder/events/IslandCreateEvent.java b/src/main/java/com/larryTheCoder/events/IslandCreateEvent.java index 8c6ee3f..312500f 100644 --- a/src/main/java/com/larryTheCoder/events/IslandCreateEvent.java +++ b/src/main/java/com/larryTheCoder/events/IslandCreateEvent.java @@ -36,7 +36,7 @@ * @author larryTheCoder * @author tastybento */ -public class IslandCreateEvent extends SkyBlockEvent implements Cancellable { +public class IslandCreateEvent extends SkyBlockPlayerEvent implements Cancellable { private static final HandlerList HANDLERS = new HandlerList(); diff --git a/src/main/java/com/larryTheCoder/events/IslandEnterEvent.java b/src/main/java/com/larryTheCoder/events/IslandEnterEvent.java index 4a7860e..1ac657c 100644 --- a/src/main/java/com/larryTheCoder/events/IslandEnterEvent.java +++ b/src/main/java/com/larryTheCoder/events/IslandEnterEvent.java @@ -37,7 +37,7 @@ * @author larryTheCoder * @author tastybento */ -public class IslandEnterEvent extends SkyBlockEvent { +public class IslandEnterEvent extends SkyBlockPlayerEvent { /** * Location of where the player entered the island or tried to enter diff --git a/src/main/java/com/larryTheCoder/events/IslandExitEvent.java b/src/main/java/com/larryTheCoder/events/IslandExitEvent.java index 14a4116..5164e85 100644 --- a/src/main/java/com/larryTheCoder/events/IslandExitEvent.java +++ b/src/main/java/com/larryTheCoder/events/IslandExitEvent.java @@ -37,7 +37,7 @@ * @author larryTheCoder * @author tastybento */ -public class IslandExitEvent extends SkyBlockEvent { +public class IslandExitEvent extends SkyBlockPlayerEvent { /** * Location of where the player exited the island's protected area. diff --git a/src/main/java/com/larryTheCoder/events/IslandTeleportEvent.java b/src/main/java/com/larryTheCoder/events/IslandTeleportEvent.java new file mode 100644 index 0000000..b1bb61f --- /dev/null +++ b/src/main/java/com/larryTheCoder/events/IslandTeleportEvent.java @@ -0,0 +1,51 @@ +/* + * Adapted from the Wizardry License + * + * Copyright (c) 2016-2020 larryTheCoder and contributors + * + * Permission is hereby granted to any persons and/or organizations + * using this software to copy, modify, merge, publish, and distribute it. + * Said persons and/or organizations are not allowed to use the software or + * any derivatives of the work for commercial use or any other means to generate + * income, nor are they allowed to claim this software as their own. + * + * The persons and/or organizations are also disallowed from sub-licensing + * and/or trademarking this software without explicit permission from larryTheCoder. + * + * Any persons and/or organizations using this software must disclose their + * source code and have it publicly available, include this license, + * provide sufficient credit to the original authors of the project (IE: larryTheCoder), + * as well as provide a link to the original project. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR + * PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + * USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package com.larryTheCoder.events; + +import cn.nukkit.Player; +import cn.nukkit.event.Cancellable; +import cn.nukkit.level.Location; +import com.larryTheCoder.cache.IslandData; +import lombok.Getter; +import lombok.Setter; + +/** + * This event will be called when the player are attempting to teleport to the + * desired island location. The island can be the player's island or another island. + */ +public class IslandTeleportEvent extends SkyBlockPlayerEvent implements Cancellable { + + @Getter @Setter + private Location teleportLocation; + + public IslandTeleportEvent(Player player, IslandData island, Location targetLocation) { + super(player, island); + + this.teleportLocation = targetLocation; + } +} diff --git a/src/main/java/com/larryTheCoder/events/SkyBlockEvent.java b/src/main/java/com/larryTheCoder/events/SkyBlockEvent.java index aa9f0eb..0ff2444 100644 --- a/src/main/java/com/larryTheCoder/events/SkyBlockEvent.java +++ b/src/main/java/com/larryTheCoder/events/SkyBlockEvent.java @@ -27,6 +27,7 @@ import cn.nukkit.Player; import cn.nukkit.Server; +import cn.nukkit.event.Cancellable; import cn.nukkit.event.Event; import cn.nukkit.event.HandlerList; import cn.nukkit.level.Location; @@ -34,8 +35,10 @@ import com.larryTheCoder.ASkyBlock; import com.larryTheCoder.cache.IslandData; import lombok.Getter; +import lombok.extern.log4j.Log4j2; -public class SkyBlockEvent extends Event { +@Log4j2 +public abstract class SkyBlockEvent extends Event { @Getter private static final HandlerList handlers = new HandlerList(); @@ -46,15 +49,8 @@ public class SkyBlockEvent extends Event { @Getter private final IslandData island; - /** - * The player class who exited the island's protected area. - */ - @Getter - private final Player player; - - public SkyBlockEvent(Player player, IslandData island) { + public SkyBlockEvent(IslandData island) { this.island = island; - this.player = player; } @@ -104,4 +100,29 @@ public Location getIslandLocation() { return new Location(cartesianPlane.getFloorX(), 0, cartesianPlane.getFloorY(), Server.getInstance().getLevelByName(island.getLevelName())); } + + /** + * Calls an event statically. This is function is to cut off the amount + * of code required just to call this event. + * + * @param event The SkyBlock event. + */ + public static void eventCall(SkyBlockEvent event) { + log.debug("Calling an event..."); + + Server.getInstance().getPluginManager().callEvent(event); + } + + /** + * Calls an event in which the event is an instanceof {@linkplain cn.nukkit.event.Cancellable} class. + * As the above, it is just a convenience tool to cut off the amount of code required to call + * this event. + * + * @return {@code true} if the event is cancelled. + */ + public static boolean eventCancellableCall(SkyBlockEvent event) { + eventCall(event); + + return event instanceof Cancellable && event.isCancelled(); + } } diff --git a/src/main/java/com/larryTheCoder/events/SkyBlockPlayerEvent.java b/src/main/java/com/larryTheCoder/events/SkyBlockPlayerEvent.java new file mode 100644 index 0000000..0dcb2a5 --- /dev/null +++ b/src/main/java/com/larryTheCoder/events/SkyBlockPlayerEvent.java @@ -0,0 +1,47 @@ +/* + * Adapted from the Wizardry License + * + * Copyright (c) 2016-2020 larryTheCoder and contributors + * + * Permission is hereby granted to any persons and/or organizations + * using this software to copy, modify, merge, publish, and distribute it. + * Said persons and/or organizations are not allowed to use the software or + * any derivatives of the work for commercial use or any other means to generate + * income, nor are they allowed to claim this software as their own. + * + * The persons and/or organizations are also disallowed from sub-licensing + * and/or trademarking this software without explicit permission from larryTheCoder. + * + * Any persons and/or organizations using this software must disclose their + * source code and have it publicly available, include this license, + * provide sufficient credit to the original authors of the project (IE: larryTheCoder), + * as well as provide a link to the original project. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, + * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR + * PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE + * USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +package com.larryTheCoder.events; + +import cn.nukkit.Player; +import com.larryTheCoder.cache.IslandData; +import lombok.Getter; + +public class SkyBlockPlayerEvent extends SkyBlockEvent { + + /** + * The player class + */ + @Getter + private final Player player; + + public SkyBlockPlayerEvent(Player player, IslandData island) { + super(island); + + this.player = player; + } +} diff --git a/src/main/java/com/larryTheCoder/island/GridManager.java b/src/main/java/com/larryTheCoder/island/GridManager.java index 23b6404..4385fdd 100644 --- a/src/main/java/com/larryTheCoder/island/GridManager.java +++ b/src/main/java/com/larryTheCoder/island/GridManager.java @@ -35,6 +35,10 @@ import cn.nukkit.utils.TextFormat; import com.larryTheCoder.ASkyBlock; import com.larryTheCoder.cache.IslandData; +import com.larryTheCoder.events.IslandTeleportEvent; +import com.larryTheCoder.events.SkyBlockEvent; +import com.larryTheCoder.task.SimpleFancyTitle; +import com.larryTheCoder.task.TaskManager; import com.larryTheCoder.utils.BlockUtil; import com.larryTheCoder.utils.Utils; @@ -260,8 +264,18 @@ public void homeTeleport(Player player, int number) { return; } - plugin.getTeleportLogic().safeTeleport(player, home, false, number); - plugin.getIslandManager().showFancyTitle(player); + plugin.getFastCache().getIslandData(player.getName(), number, island -> { + // ???? + IslandTeleportEvent event = null; + if (island != null && SkyBlockEvent.eventCancellableCall(event = new IslandTeleportEvent(player, island, home))) { + return; + } + + plugin.getTeleportLogic().safeTeleport(player, event == null ? home : event.getTeleportLocation(), false, number); + plugin.getLevelCalcThread().addUpdateQueue(island); + + TaskManager.runTaskLater(new SimpleFancyTitle(plugin, player), 20); + }); }); } diff --git a/src/main/java/com/larryTheCoder/island/IslandManager.java b/src/main/java/com/larryTheCoder/island/IslandManager.java index 7b1d5b0..1d0aad2 100644 --- a/src/main/java/com/larryTheCoder/island/IslandManager.java +++ b/src/main/java/com/larryTheCoder/island/IslandManager.java @@ -44,8 +44,8 @@ import com.larryTheCoder.database.DatabaseManager; import com.larryTheCoder.database.TableSet; import com.larryTheCoder.events.IslandCreateEvent; +import com.larryTheCoder.events.SkyBlockEvent; import com.larryTheCoder.task.DeleteIslandTask; -import com.larryTheCoder.task.SimpleFancyTitle; import com.larryTheCoder.task.TaskManager; import com.larryTheCoder.utils.IslandAwaitStore; import com.larryTheCoder.utils.Settings; @@ -85,12 +85,6 @@ public void handleIslandCommand(Player pl, boolean reset) { } } - void showFancyTitle(Player p) { - // The problem solved. The task `while` are pushing the CPU far more to load makes it - // Glitching and corrupted half chunk data. #20 Cannot teleport to island - TaskManager.runTaskLater(new SimpleFancyTitle(plugin, p), 20); - } - public void kickPlayerByName(final Player pOwner, final String victimName) { final Location loc = pOwner.getLocation(); final IslandData pd = plugin.getFastCache().getIslandData(loc); @@ -212,8 +206,7 @@ public void createIsland(Player pl, int templateId, String levelName, String hom TaskManager.runTask(() -> { // Call an event IslandCreateEvent event = new IslandCreateEvent(pl, templateId, resultData); - plugin.getServer().getPluginManager().callEvent(event); - if (event.isCancelled()) { + if (SkyBlockEvent.eventCancellableCall(event)) { // The plugin should notify why the action is cancelled. return; } diff --git a/src/main/java/com/larryTheCoder/island/TeleportLogic.java b/src/main/java/com/larryTheCoder/island/TeleportLogic.java index 63d20d6..9c35d1b 100644 --- a/src/main/java/com/larryTheCoder/island/TeleportLogic.java +++ b/src/main/java/com/larryTheCoder/island/TeleportLogic.java @@ -35,7 +35,6 @@ import cn.nukkit.scheduler.TaskHandler; import cn.nukkit.utils.TextFormat; import com.google.common.collect.Lists; -import com.google.common.collect.Maps; import com.larryTheCoder.ASkyBlock; import com.larryTheCoder.utils.Settings; import com.larryTheCoder.utils.Utils; @@ -44,6 +43,7 @@ import java.util.Map; import java.util.UUID; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; /** * Responsible for teleporting (and canceling teleporting) of players. @@ -53,30 +53,27 @@ */ public class TeleportLogic implements Listener { - private static final List list = Lists.newArrayList(); - private static final Map time = Maps.newHashMap(); private static int teleportDelay; private final ASkyBlock plugin; private final Map pendingTPs = new ConcurrentHashMap<>(); private final double cancelDistance; + private static final List movedPlayers = Lists.newArrayList(); + public TeleportLogic(ASkyBlock plugin) { this.plugin = plugin; teleportDelay = plugin.getConfig().getInt("general.islandTeleportDelay", 2); cancelDistance = plugin.getConfig().getDouble("options.island.teleportCancelDistance", 0.6); + plugin.getServer().getPluginManager().registerEvents(this, plugin); } public static boolean isPlayerMoved(String p) { - return list.contains(p); - } - - public static int getPlayerTeleport(String p) { - return time.get(p); + return movedPlayers.remove(p); // Memory leak } public void safeTeleport(final Player player, final Location homeSweetHome, boolean force, int home) { - final Location targetLoc = homeSweetHome.clone().add(0.5, 0, 0.5); + Location targetLoc = homeSweetHome.clone().add(0.5, 0, 0.5); Utils.loadChunkAt(targetLoc); if (plugin.getPermissionHandler().hasPermission(player, "is.bypass.wait") || (teleportDelay == 0) || force) { @@ -94,13 +91,11 @@ public void safeTeleport(final Player player, final Location homeSweetHome, bool } else { player.sendMessage(plugin.getPrefix() + TextFormat.GREEN + "Teleported to your island #" + home); } - player.teleport(targetLoc.add(0, 0.35)); // Adjust spawn hieght + player.teleport(targetLoc.add(0, 0.35)); // Adjust spawn heights // Teleport in default gameMode - if (Settings.gameMode != -1) { + if (Settings.gameMode != -1 && plugin.getPermissionHandler().hasPermission(player, "is.gamemode.bypass")) player.setGamemode(Settings.gameMode); - } - }, Utils.secondsAsMillis(teleportDelay)); - time.put(player.getName(), Utils.secondsAsMillis(teleportDelay)); + }, (int) TimeUnit.SECONDS.toMillis(teleportDelay)); pendingTPs.put(player.getUniqueId(), new PendingTeleport(player.getLocation(), task)); } } @@ -141,9 +136,9 @@ void playerMoved(Player player) { double distance = location.distance(newLocation); if (distance > cancelDistance) { task.cancel(); - pendingTPs.remove(player.getUniqueId()); player.sendMessage(plugin.getPrefix() + plugin.getLocale(player).teleportCancelled); - list.add(player.getName()); + movedPlayers.add(player.getName()); + pendingTPs.remove(player.getUniqueId()); } } } diff --git a/src/main/java/com/larryTheCoder/listener/IslandListener.java b/src/main/java/com/larryTheCoder/listener/IslandListener.java index 80dfbd1..a19198a 100644 --- a/src/main/java/com/larryTheCoder/listener/IslandListener.java +++ b/src/main/java/com/larryTheCoder/listener/IslandListener.java @@ -46,6 +46,7 @@ import com.larryTheCoder.cache.IslandData; import com.larryTheCoder.events.IslandEnterEvent; import com.larryTheCoder.events.IslandExitEvent; +import com.larryTheCoder.events.SkyBlockEvent; import com.larryTheCoder.utils.Settings; import com.larryTheCoder.utils.SettingsFlag; import com.larryTheCoder.utils.Utils; @@ -173,8 +174,7 @@ public void onPlayerMove(PlayerMoveEvent e) { } // Fire entry event - final IslandEnterEvent event = new IslandEnterEvent(p, islandTo, e.getTo()); - plugin.getServer().getPluginManager().callEvent(event); + SkyBlockEvent.eventCall(new IslandEnterEvent(p, islandTo, e.getTo())); } else if (islandTo == null && islandFrom != null && (islandFrom.getPlotOwner() != null)) { // Leaving if (islandFrom.getIgsSettings().getIgsFlag(SettingsFlag.ENTER_EXIT_MESSAGES)) { @@ -182,15 +182,12 @@ public void onPlayerMove(PlayerMoveEvent e) { } // Fire exit event - final IslandExitEvent event = new IslandExitEvent(p, islandFrom, e.getFrom()); - plugin.getServer().getPluginManager().callEvent(event); + SkyBlockEvent.eventCall(new IslandExitEvent(p, islandFrom, e.getFrom())); } else if (islandTo != null && islandFrom != null && !islandTo.equals(islandFrom)) { // Fire exit event - final IslandExitEvent event = new IslandExitEvent(p, islandFrom, e.getFrom()); - plugin.getServer().getPluginManager().callEvent(event); + SkyBlockEvent.eventCall(new IslandExitEvent(p, islandFrom, e.getFrom())); // Fire entry event - final IslandEnterEvent event2 = new IslandEnterEvent(p, islandTo, e.getTo()); - plugin.getServer().getPluginManager().callEvent(event2); + SkyBlockEvent.eventCall(new IslandEnterEvent(p, islandTo, e.getTo())); } else if (islandTo != null && (islandTo.getPlotOwner() != null)) { // Lock check if (islandTo.isLocked()) { diff --git a/src/main/java/com/larryTheCoder/task/LevelCalcTask.java b/src/main/java/com/larryTheCoder/task/LevelCalcTask.java index 33dd236..e8fb886 100644 --- a/src/main/java/com/larryTheCoder/task/LevelCalcTask.java +++ b/src/main/java/com/larryTheCoder/task/LevelCalcTask.java @@ -37,6 +37,9 @@ import com.larryTheCoder.ASkyBlock; import com.larryTheCoder.cache.IslandData; import com.larryTheCoder.cache.settings.WorldSettings; +import com.larryTheCoder.events.IslandCalculateFinishEvent; +import com.larryTheCoder.events.IslandCalculateLevelEvent; +import com.larryTheCoder.events.SkyBlockEvent; import com.larryTheCoder.utils.Settings; import com.larryTheCoder.utils.Utils; import lombok.SneakyThrows; @@ -85,6 +88,8 @@ public void shutdown() { * @param islandDb The player island data that will be checked */ public void addUpdateQueue(IslandData islandDb) { + if (SkyBlockEvent.eventCancellableCall(new IslandCalculateLevelEvent(islandDb))) return; + levelUpdateQueue.add(islandDb); synchronized (this) { @@ -170,7 +175,6 @@ private void updateList() { } int levels = levelMultiplier; - int finalLevelMultiplier = levelMultiplier; // I wonder why I bother trying to pass these execution into an async worker lmao File logFile; @@ -334,10 +338,17 @@ private void updateList() { reportLines.clear(); } - log.debug("RESULT THREAD: " + score); + // Return back to the main thread, call an event there + final IslandData finalIsland = pd; + TaskManager.runTask(() -> plugin.getFastCache().getIslandData(finalIsland.getPlotOwner(), island -> { + IslandCalculateFinishEvent event; + if (SkyBlockEvent.eventCancellableCall(event = new IslandCalculateFinishEvent(island, score, island.getIslandLevel()))) { + return; + } - pd.setIslandLevel(score); - pd.saveIslandData(); + island.setIslandLevel(event.getIslandLevel()); + island.saveIslandData(); + })); } } diff --git a/src/main/java/com/larryTheCoder/utils/Utils.java b/src/main/java/com/larryTheCoder/utils/Utils.java index ee13746..d74e332 100644 --- a/src/main/java/com/larryTheCoder/utils/Utils.java +++ b/src/main/java/com/larryTheCoder/utils/Utils.java @@ -265,10 +265,6 @@ private static String convertTimer(long ms) { return String.format("%02dm %02ds", min, secs); } - public static int secondsAsMillis(int sec) { - return (sec * 60); - } - /** * Check physical activity of the player * diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index ab81409..c8d41a7 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -26,6 +26,9 @@ permissions: is.command: default: true description: "SkyBlock main command" + is.gamemode.bypass: + default: op + description: "Enables gamemode bypass to teleport into the SkyBlock world." # Island team chatting command is.command.teamChat: default: true