Skip to content

Commit

Permalink
Phase 1: Implementing more events for DEVs
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
larryTheCoder committed Jul 4, 2020
1 parent e61fee6 commit bdc607b
Show file tree
Hide file tree
Showing 19 changed files with 289 additions and 68 deletions.
7 changes: 2 additions & 5 deletions src/main/java/com/larryTheCoder/ASkyBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/larryTheCoder/cache/FastCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/larryTheCoder/cache/IslandData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/larryTheCoder/cache/PlayerData.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/com/larryTheCoder/events/IslandTeleportEvent.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
39 changes: 30 additions & 9 deletions src/main/java/com/larryTheCoder/events/SkyBlockEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@

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;
import cn.nukkit.math.Vector2;
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();
Expand All @@ -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;
}


Expand Down Expand Up @@ -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();
}
}
47 changes: 47 additions & 0 deletions src/main/java/com/larryTheCoder/events/SkyBlockPlayerEvent.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
18 changes: 16 additions & 2 deletions src/main/java/com/larryTheCoder/island/GridManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
});
});
}

Expand Down
Loading

0 comments on commit bdc607b

Please sign in to comment.