Skip to content

Commit

Permalink
Progression de la mise à a jours vers mon nouveau scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
Euphillya committed Feb 25, 2024
1 parent cab980f commit bf48684
Show file tree
Hide file tree
Showing 41 changed files with 973 additions and 1,123 deletions.
9 changes: 9 additions & 0 deletions api/src/main/java/fr/euphyllia/skyllia/api/SkylliaAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,13 @@ public static void setImplementation(SkylliaImplementation skylliaImplementation
public static @Nullable Island getIslandByChunk(Chunk chunk) {
return implementation.getIslandByChunk(chunk);
}

public static boolean isFolia() {
try {
Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
return true;
} catch (ClassNotFoundException ignored) {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.euphyllia.skyllia.api.utils.scheduler;

import fr.euphyllia.skyllia.api.SkylliaAPI;
import fr.euphyllia.skyllia.api.utils.scheduler.executors.ExecutorsScheduler;
import fr.euphyllia.skyllia.api.utils.scheduler.folia.FoliaScheduler;
import fr.euphyllia.skyllia.api.utils.scheduler.legacy.LegacyScheduler;
Expand All @@ -21,14 +22,18 @@ public SchedulerTask(Plugin javaPlugin) {
}

public Scheduler getScheduler(SchedulerSoft schedulerSoft) {
return switch (schedulerSoft) {
case NATIVE -> this.executorsScheduler;
case LEGACY -> this.legacyScheduler;
case FOLIA -> this.foliaScheduler;
};
if (schedulerSoft == SchedulerSoft.NATIVE) {
return this.executorsScheduler;
} else if (schedulerSoft == SchedulerSoft.MINECRAFT) {
if (SkylliaAPI.isFolia()) {
return this.foliaScheduler;
}
return this.legacyScheduler;
}
throw new UnsupportedOperationException();
}

public enum SchedulerSoft {
NATIVE, LEGACY, FOLIA
NATIVE, MINECRAFT
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package fr.euphyllia.skyllia.api.utils.scheduler.folia;

import fr.euphyllia.skyllia.api.utils.scheduler.model.Scheduler;
import fr.euphyllia.skyllia.api.utils.scheduler.model.SchedulerCallBack;
import fr.euphyllia.skyllia.api.utils.scheduler.model.SchedulerTaskInter;
import fr.euphyllia.skyllia.api.utils.scheduler.model.SchedulerType;
import fr.euphyllia.skyllia.api.utils.scheduler.model.*;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
Expand Down Expand Up @@ -148,6 +145,9 @@ public void runDelayed(@NotNull SchedulerType schedulerType, @Nullable Object ch

@Override
public void execute(@NotNull SchedulerType schedulerType, SchedulerCallBack callBack) {
if (schedulerType.equals(SchedulerType.ENTITY) || schedulerType.equals(SchedulerType.REGION)) {
throw new UnsupportedOperationException();
}
this.execute(schedulerType, null, null, callBack);
}

Expand All @@ -173,6 +173,10 @@ public void execute(@NotNull SchedulerType schedulerType, @Nullable Object chunk
Bukkit.getRegionScheduler().execute(this.plugin, chunk.getWorld(), chunk.getX(), chunk.getZ(), () -> {
callBack.run(null);
});
} else if (chunkOrLocOrEntity instanceof MultipleRecords.WorldChunk worldChunk) {
Bukkit.getRegionScheduler().execute(this.plugin, worldChunk.world(), worldChunk.chunkX(), worldChunk.chunkZ(), () -> {
callBack.run(null);
});
} else {
throw new RuntimeException("Object can only be Location or Chunk");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package fr.euphyllia.skyllia.api.utils.scheduler.model;

import org.bukkit.World;

public class MultipleRecords {
public record WorldChunk(World world, int chunkX, int chunkZ) {
}
}
18 changes: 9 additions & 9 deletions plugin/src/main/java/fr/euphyllia/skyllia/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import fr.euphyllia.skyllia.api.InterneAPI;
import fr.euphyllia.skyllia.api.SkylliaAPI;
import fr.euphyllia.skyllia.api.exceptions.DatabaseException;
import fr.euphyllia.skyllia.api.exceptions.UnsupportedMinecraftVersionException;
import fr.euphyllia.skyllia.api.utils.scheduler.SchedulerTask;
Expand Down Expand Up @@ -69,26 +70,25 @@ public void onEnable() {
Bukkit.getPluginManager().disablePlugin(this);
return;
}

this.interneAPI.loadAPI();
this.interneAPI.setManagers(new Managers(interneAPI));
this.interneAPI.getManagers().init();
this.setupCommands();
this.setupAdminCommands();
this.loadListener();
this.runCache();
this.disabledConfig();
this.interneAPI.loadAPI();
new Metrics(this, 20874);

new Metrics(this.interneAPI, 20874);
}

@Override
public void onDisable() {
this.logger.log(Level.INFO, "Plugin Off");
this.interneAPI.getSchedulerTask().getScheduler(SchedulerTask.SchedulerSoft.NATIVE).cancelAllTask();
this.interneAPI.getSchedulerTask().getScheduler(SchedulerTask.SchedulerSoft.MINECRAFT).cancelAllTask();
getServer().getGlobalRegionScheduler().cancelTasks(this);
getServer().getAsyncScheduler().cancelTasks(this);
this.interneAPI.getSchedulerTask().getScheduler(SchedulerTask.SchedulerSoft.NATIVE).cancelAllTask();
this.interneAPI.getSchedulerTask().getScheduler(SchedulerTask.SchedulerSoft.LEGACY).cancelAllTask();
this.interneAPI.getSchedulerTask().getScheduler(SchedulerTask.SchedulerSoft.FOLIA).cancelAllTask();
if (this.interneAPI.getDatabaseLoader() != null) {
this.interneAPI.getDatabaseLoader().closeDatabase();
}
Expand Down Expand Up @@ -129,9 +129,9 @@ private void loadListener() {
pluginManager.registerEvents(new PlayerEvent(this.interneAPI), this);
pluginManager.registerEvents(new DamageEvent(this.interneAPI), this);
pluginManager.registerEvents(new InteractEvent(this.interneAPI), this);
pluginManager.registerEvents(new TeleportEvent(this.interneAPI), this); // Todo Don't work with folia 1.19.4-1.20.2
pluginManager.registerEvents(new TeleportEvent(this.interneAPI), this); // Todo Don't work with folia 1.19.4-1.20.4
pluginManager.registerEvents(new PistonEvent(this.interneAPI), this);
if (this.interneAPI.isFolia()) {
if (SkylliaAPI.isFolia()) {
pluginManager.registerEvents(new PortailAlternativeFoliaEvent(this.interneAPI), this);
}
// GameRule Events
Expand All @@ -154,7 +154,7 @@ private void runCache() {
}

private void disabledConfig() {
if (this.getInterneAPI().isFolia()) {
if (SkylliaAPI.isFolia()) {
if (Bukkit.getAllowNether()) {
logger.log(Level.WARN, "Disable nether in server.properties to disable nether portals!");
}
Expand Down
12 changes: 0 additions & 12 deletions plugin/src/main/java/fr/euphyllia/skyllia/api/InterneAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public class InterneAPI {
private MariaDBTransactionQuery transaction;
private DatabaseLoader databaseLoader;
private Managers managers;
private boolean useFolia = false;
private WorldNMS worldNMS;
private PlayerNMS playerNMS;

Expand All @@ -55,19 +54,8 @@ public InterneAPI(Main plugin) throws UnsupportedMinecraftVersionException {
this.setVersionNMS();
this.skyblockManager = new SkyblockManager(this.plugin);
this.cacheManager = new CacheManager(this.skyblockManager);
try {
Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
this.useFolia = true;
} catch (ClassNotFoundException ignored) {
this.useFolia = false;
}
}

public boolean isFolia() {
return this.useFolia;
}


public @Nullable DatabaseLoader getDatabaseLoader() {
return this.database;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.Executors;
Expand All @@ -42,7 +41,6 @@ public boolean onCommand(@NotNull Main plugin, @NotNull CommandSender sender, @N
String playerName = args[0];
String changeValue = args[1];
String confirm = args[2];
System.out.println(Arrays.toString(args));
if (!confirm.equalsIgnoreCase("confirm")) {
LanguageToml.sendMessage(plugin, player, LanguageToml.messageASetSizeNotConfirmedArgs);
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package fr.euphyllia.skyllia.commands.common;

import fr.euphyllia.skyllia.Main;
import fr.euphyllia.skyllia.api.utils.scheduler.SchedulerTask;
import fr.euphyllia.skyllia.api.utils.scheduler.model.SchedulerType;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
Expand Down Expand Up @@ -29,7 +31,12 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
if (subCommands == null) {
return false;
}
return subCommands.getSubCommandInterface().onCommand(this.plugin, sender, command, label, listArgs);
plugin.getInterneAPI()
.getSchedulerTask()
.getScheduler(SchedulerTask.SchedulerSoft.NATIVE)
.execute(SchedulerType.ASYNC, schedulerTask -> {
subCommands.getSubCommandInterface().onCommand(this.plugin, sender, command, label, listArgs);
});
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import fr.euphyllia.skyllia.managers.skyblock.SkyblockManager;
import fr.euphyllia.skyllia.utils.PlayerUtils;
import fr.euphyllia.skyllia.utils.RegionUtils;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bukkit.Bukkit;
Expand All @@ -27,8 +26,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

public class AccessSubCommand implements SubCommandInterface {

Expand All @@ -44,56 +41,44 @@ public boolean onCommand(@NotNull Main plugin, @NotNull CommandSender sender, @N
return true;
}

ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
try {
executor.execute(() -> {
try {
SkyblockManager skyblockManager = plugin.getInterneAPI().getSkyblockManager();
Island island = skyblockManager.getIslandByPlayerId(player.getUniqueId()).join();
SkyblockManager skyblockManager = plugin.getInterneAPI().getSkyblockManager();
Island island = skyblockManager.getIslandByPlayerId(player.getUniqueId()).join();

if (island == null) {
LanguageToml.sendMessage(plugin, player, LanguageToml.messagePlayerHasNotIsland);
return;
}
if (island == null) {
LanguageToml.sendMessage(plugin, player, LanguageToml.messagePlayerHasNotIsland);
return true;
}

Players executorPlayer = island.getMember(player.getUniqueId());
if (!executorPlayer.getRoleType().equals(RoleType.OWNER)) {
PermissionRoleIsland permissionRoleIsland = skyblockManager.getPermissionIsland(island.getId(), PermissionsType.COMMANDS, executorPlayer.getRoleType()).join();
Players executorPlayer = island.getMember(player.getUniqueId());
if (!executorPlayer.getRoleType().equals(RoleType.OWNER)) {
PermissionRoleIsland permissionRoleIsland = skyblockManager.getPermissionIsland(island.getId(), PermissionsType.COMMANDS, executorPlayer.getRoleType()).join();

PermissionManager permissionManager = new PermissionManager(permissionRoleIsland.permission());
if (!permissionManager.hasPermission(PermissionsCommandIsland.ACCESS)) {
LanguageToml.sendMessage(plugin, player, LanguageToml.messagePlayerPermissionDenied);
return;
}
}
PermissionManager permissionManager = new PermissionManager(permissionRoleIsland.permission());
if (!permissionManager.hasPermission(PermissionsCommandIsland.ACCESS)) {
LanguageToml.sendMessage(plugin, player, LanguageToml.messagePlayerPermissionDenied);
return true;
}
}

boolean statusAccessUpdate = !island.isPrivateIsland();
boolean statusAccessUpdate = !island.isPrivateIsland();

boolean isUpdate = island.setPrivateIsland(statusAccessUpdate);
if (isUpdate) {
if (statusAccessUpdate) {
LanguageToml.sendMessage(plugin, player, LanguageToml.messageAccessIslandClose);
ConfigToml.worldConfigs.forEach(worldConfig -> {
RegionUtils.getEntitiesInRegion(plugin, EntityType.PLAYER, Bukkit.getWorld(worldConfig.name()), island.getPosition().x(), island.getPosition().z(), entity -> {
Player playerInIsland = (Player) entity;
if (playerInIsland.hasPermission("skyllia.island.command.access.bypass")) return;
Players players = island.getMember(playerInIsland.getUniqueId());
if (players == null || players.getRoleType().equals(RoleType.BAN) || players.getRoleType().equals(RoleType.VISITOR)) {
PlayerUtils.teleportPlayerSpawn(plugin, playerInIsland);
}
});
});
} else {
LanguageToml.sendMessage(plugin, player, LanguageToml.messageAccessIslandOpen);
boolean isUpdate = island.setPrivateIsland(statusAccessUpdate);
if (isUpdate) {
if (statusAccessUpdate) {
LanguageToml.sendMessage(plugin, player, LanguageToml.messageAccessIslandClose);
ConfigToml.worldConfigs.forEach(worldConfig -> {
RegionUtils.getEntitiesInRegion(plugin, EntityType.PLAYER, Bukkit.getWorld(worldConfig.name()), island.getPosition().x(), island.getPosition().z(), entity -> {
Player playerInIsland = (Player) entity;
if (playerInIsland.hasPermission("skyllia.island.command.access.bypass")) return;
Players players = island.getMember(playerInIsland.getUniqueId());
if (players == null || players.getRoleType().equals(RoleType.BAN) || players.getRoleType().equals(RoleType.VISITOR)) {
PlayerUtils.teleportPlayerSpawn(plugin, playerInIsland);
}
}
} catch (Exception e) {
logger.log(Level.FATAL, e.getMessage(), e);
LanguageToml.sendMessage(plugin, player, LanguageToml.messageError);
}
});
} finally {
executor.shutdown();
});
});
} else {
LanguageToml.sendMessage(plugin, player, LanguageToml.messageAccessIslandOpen);
}
}
return true;
}
Expand Down
Loading

0 comments on commit bf48684

Please sign in to comment.