Skip to content

Commit

Permalink
Improved Island level calculations checks.
Browse files Browse the repository at this point in the history
Added admin option for Cooperative islands.
Probably fixes an issue relating to creating an island.
Fixes MySQL dialects, most of the tables are falsely configured.
  • Loading branch information
larryTheCoder committed Jun 24, 2020
1 parent ca85d99 commit 899431d
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 466 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/larryTheCoder/ASkyBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import com.larryTheCoder.listener.invitation.InvitationHandler;
import com.larryTheCoder.locales.ASlocales;
import com.larryTheCoder.schematic.SchematicHandler;
import com.larryTheCoder.task.LevelCalcTask;
import com.larryTheCoder.task.TaskManager;
import com.larryTheCoder.updater.Updater;
import com.larryTheCoder.utils.ConfigManager;
Expand Down Expand Up @@ -159,6 +160,7 @@ public void onDisable() {
getDatabase().shutdownDB();
getMessages().saveMessages();
LavaCheck.clearStats();
getLevelCalcThread().shutdown();
//TopTen.topTenSave();
}

Expand Down Expand Up @@ -209,7 +211,7 @@ private void initIslands() {
// This should be loaded first
messages = new Messages(this);
messages.loadMessages();
// new LevelCalcTask(this);
levelCalcThread = new LevelCalcTask(this);
//TopTen.topTenLoad();

pm.registerEvents(chatHandler, this);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/larryTheCoder/ASkyBlockAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.larryTheCoder.listener.ChatHandler;
import com.larryTheCoder.listener.invitation.InvitationHandler;
import com.larryTheCoder.schematic.SchematicHandler;
import com.larryTheCoder.task.LevelCalcTask;
import lombok.Getter;

public class ASkyBlockAPI extends PluginBase {
Expand All @@ -63,4 +64,6 @@ public class ASkyBlockAPI extends PluginBase {
protected FastCache fastCache;
@Getter
protected SchematicHandler schematics;
@Getter
protected LevelCalcTask levelCalcThread;
}
4 changes: 4 additions & 0 deletions src/main/java/com/larryTheCoder/cache/CoopData.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,8 @@ public int hashCode() {

return i + super.hashCode();
}

public boolean isAdmin(Player p) {
return admins.stream().anyMatch(i -> i.equalsIgnoreCase(p.getName()));
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/larryTheCoder/command/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private void registerCategories() {
commandCategory.add(new GenericCategory(getPlugin()));
commandCategory.add(new IslandCategory(getPlugin()));
commandCategory.add(new ChatCategory(getPlugin()));
//commandCategory.add(new CoopCategory(getPlugin()));
commandCategory.add(new CoopCategory(getPlugin()));
commandCategory.add(new OperatorCategory(getPlugin()));

// Then we add them into the array.
Expand Down
45 changes: 22 additions & 23 deletions src/main/java/com/larryTheCoder/command/category/CoopCategory.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,37 +147,36 @@ public void execute(CommandSender sender, String commandLabel, String[] args) {
// Player cannot invite other players when he have no island
getPlugin().getFastCache().getRelations(p.getPosition(), data -> {
if (data == null) {
p.sendMessage(getPrefix() + "You must be on your island to invite other members.");
p.sendMessage(getPrefix() + "The location from where you standing is not your island.");
return;
}

if(!data.isAdmin(p)){
p.sendMessage(getPrefix() + "You are not an admin in this island.");
if (!data.isAdmin(p)) {
p.sendMessage(getPrefix() + "You are not an admin in this server.");
return;
}

// Get the island data of this Coop island.
getPlugin().getFastCache().getIslandData(data.getIslandUniqueId(), pd -> {
if (!pd.getPlotOwner().equalsIgnoreCase(p.getName())) {
p.sendMessage(getPrefix() + "That is not your island!");
return;
}

Player inviter = p.getServer().getPlayer(args[1]);
if (inviter == null) {
p.sendMessage(getPrefix() + getLocale(p).errorOfflinePlayer);
return;
}

// Now check if this player is already in a coop island.
getPlugin().getFastCache().getRelations(p.getName(), relation -> {
if (relation == null) {
getPlugin().getInvitationHandler().addInvitation(p, inviter, data);
} else {
sender.sendMessage(getPrefix() + getLocale(p).errorInTeam.replace("[player]", args[1]));
}
});

});
// if (!data.getPlotOwner().equalsIgnoreCase(p.getName())) {
// p.sendMessage(getPrefix() + "That is not your island!");
// return;
// }
//
// Player inviter = p.getServer().getPlayer(args[1]);
// if (inviter == null) {
// p.sendMessage(getPrefix() + getLocale(p).errorOfflinePlayer);
// return;
// }
//
// getPlugin().getFastCache().getRelations(p.getName(), relation -> {
// if (relation == null) {
// //getPlugin().getInvitationHandler().addInvitation(p, inviter, );
// } else {
// sender.sendMessage(getPrefix() + getLocale(p).errorInTeam.replace("[player]", args[1]));
// }
// });
});
break;
}
Expand Down
28 changes: 13 additions & 15 deletions src/main/java/com/larryTheCoder/database/DatabaseManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,11 @@ public class DatabaseManager {
public static final String DB_VERSION = "0.1.7";
public static String currentCacheId;

// Notes: During SELECT, sqlite database can read its database easily without effecting the thread,
// But when we are using mysql as our database, it effects the thread uptime. So do we need to
// Optimize our code so it can be async-compatible?
private final AbstractConfig database;
private final Connection connection;

private AbstractConfig database;
private Connection connection;

private AtomicBoolean isClosed = new AtomicBoolean(false);
private ConcurrentLinkedQueue<DatabaseImpl> requestQueue = new ConcurrentLinkedQueue<>();
private final AtomicBoolean isClosed = new AtomicBoolean(false);
private final ConcurrentLinkedQueue<DatabaseImpl> requestQueue = new ConcurrentLinkedQueue<>();

public static boolean isMysql = false;

Expand Down Expand Up @@ -109,7 +105,6 @@ private void startConnection() {
TableSet[] tableSets = {
METADATA_TABLE,
WORLD_TABLE,
ISLAND_DATA,
PLAYER_TABLE,
PLAYER_CHALLENGES,
ISLAND_TABLE,
Expand All @@ -130,7 +125,6 @@ private void startConnection() {
}

// Update database credentials.
// TODO: Versioning database stuffs.
Table result = connection.createQuery(TABLE_FETCH_CACHE.getQuery()).executeAndFetchTable();

if (result.rows().isEmpty()) {
Expand All @@ -154,7 +148,7 @@ private void startConnection() {
case "0.1.6":
connection.createQuery("ALTER TABLE islandRelations ADD COLUMN islandAdmins TEXT DEFAULT ''").executeUpdate();
case "0.1.7":
// TODO: Future updates?
break;
}

connection.createQuery(TABLE_CACHE_UPDATE.getQuery())
Expand Down Expand Up @@ -211,6 +205,7 @@ private void startAsyncPool(int maxPool) {
Thread asyncThread = new Thread(() -> {
while (!isClosed.get()) {
try {
long lastTick = System.currentTimeMillis();
DatabaseImpl consumer;
while ((consumer = requestQueue.poll()) != null) {
Exception result = null;
Expand All @@ -231,8 +226,13 @@ private void startAsyncPool(int maxPool) {
}
});
}
long nowTick = System.currentTimeMillis() - lastTick;

Thread.sleep(50);
// The execution took 50ms to execute, do not bother sleeping this thread.
// Continue execution.
if (nowTick >= 50) continue;

Thread.sleep(50 - nowTick);
} catch (Throwable e) {
// Sync up with console.

Expand All @@ -246,9 +246,7 @@ private void startAsyncPool(int maxPool) {

currentPoolSize++;

if (currentPoolSize < maxPool) {
startAsyncPool(maxPool);
}
if (currentPoolSize < maxPool) startAsyncPool(maxPool);
}

public abstract static class DatabaseImpl {
Expand Down
132 changes: 0 additions & 132 deletions src/main/java/com/larryTheCoder/events/ASkyBlockEvent.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import cn.nukkit.command.CommandSender;
import cn.nukkit.utils.TextFormat;
import com.larryTheCoder.ASkyBlock;
import com.larryTheCoder.cache.CoopData;
import com.larryTheCoder.cache.IslandData;
import com.larryTheCoder.utils.Settings;

/**
Expand All @@ -42,7 +42,7 @@ public class Invitation {
private final CommandSender sender;
private final Player receiver;
private final ASkyBlock plugin;
private final CoopData coopData;
private final IslandData islandData;

private int time;

Expand All @@ -53,11 +53,11 @@ public class Invitation {
* @param sender The sender of this invite
* @param receiver The receiver of this invite
*/
Invitation(InvitationHandler handler, CommandSender sender, Player receiver, CoopData pd) {
Invitation(InvitationHandler handler, CommandSender sender, Player receiver, IslandData islandData) {
this.handler = handler;
this.sender = sender;
this.receiver = receiver;
this.coopData = pd;
this.islandData = islandData;
this.time = Settings.memberTimeOut;
this.plugin = ASkyBlock.get();
}
Expand All @@ -84,8 +84,17 @@ public void acceptInvitation() {
sender.sendMessage(plugin.getPrefix() + plugin.getLocale(sender.isPlayer() ? (Player) sender : null).acceptedTo.replace("[player]", receiver.getName()));
receiver.sendMessage(plugin.getPrefix() + plugin.getLocale(receiver).acceptedFrom.replace("[player]", sender.getName()));

coopData.addMember(sender.getName());
handler.removeInvitation(this);
// TODO: Compliance under Co-Op regulatory and settings.
// List<IslandData> dataList = plugin.getIslandsInfo(receiver.getName());
// // Check if the player has an island
// if (!dataList.isEmpty()) {
// receiver.sendMessage(plugin.getPrefix() + "Deleting all of your islands");
// //dataList.forEach((island) -> plugin.getDatabase().deleteIsland(island));
// }
//
// // Set the team from the sender and the receiver.
// plugin.getTManager().setTeam(sender.getName(), receiver.getName());
// handler.removeInvitation(this);
}

public void denyInvitation() {
Expand Down
Loading

0 comments on commit 899431d

Please sign in to comment.