Skip to content

Commit

Permalink
Début d'essaie du systeme en cache
Browse files Browse the repository at this point in the history
Ajout du SkyblockCreateEvent
Création de l'annotation pour préciser quand c'est experimental
  • Loading branch information
Euphillya committed Jan 16, 2024
1 parent c158b47 commit e30be67
Show file tree
Hide file tree
Showing 19 changed files with 310 additions and 67 deletions.
20 changes: 19 additions & 1 deletion .idea/sonarlint/issuestore/index.pb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 19 additions & 1 deletion .idea/sonarlint/securityhotspotstore/index.pb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package fr.euphyllia.skyfolia.api.annotation;


import java.lang.annotation.*;

@Inherited
public @interface Experimental {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package fr.euphyllia.skyfolia.api.annotation;

import java.lang.annotation.Inherited;

@Inherited
public @interface Information {

String value();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package fr.euphyllia.skyfolia.api.event;

import fr.euphyllia.skyfolia.api.skyblock.Island;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;

public class SkyblockCreateEvent extends Event {

private static final HandlerList handlerList = new HandlerList();
private final Island island;

public SkyblockCreateEvent(Island islandCreate) {
super(true);
this.island = islandCreate;
}


public static HandlerList getHandlerList() {
return handlerList;
}

@Override
public @NotNull HandlerList getHandlers() {
return getHandlerList();
}

public Island getIsland() {
return this.island;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
package fr.euphyllia.skyfolia.api.skyblock.model;

public record Position(int regionX, int regionZ) {

@Override
public String toString() {
return "x=%s;z=%s".formatted(regionX, regionZ);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public enum PermissionsIsland implements Permissions {
KILL_ANIMAL(64),
DROP_ITEMS(128),
TAKE_ITEMS(256),
USE_PORTAL(5012),
USE_PORTAL(512),
INTERACT_ENTITIES(1_024),
;

Expand Down
27 changes: 25 additions & 2 deletions plugin/src/main/java/fr/euphyllia/skyfolia/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,22 @@
import fr.euphyllia.skyfolia.api.InterneAPI;
import fr.euphyllia.skyfolia.api.exceptions.DatabaseException;
import fr.euphyllia.skyfolia.commands.SkyFoliaCommand;
import fr.euphyllia.skyfolia.listeners.bukkitevents.BlockEvent;
import fr.euphyllia.skyfolia.listeners.bukkitevents.JoinEvent;
import fr.euphyllia.skyfolia.listeners.skyblockevents.SkyblockEvent;
import fr.euphyllia.skyfolia.managers.Managers;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Main extends JavaPlugin {

Expand Down Expand Up @@ -45,9 +51,9 @@ public void onEnable() {

this.interneAPI.setManagers(new Managers(interneAPI));
this.interneAPI.getManagers().init();
this.interneAPI.loadCachePlugin();
this.setupCommands();
this.loadListener();
this.runCache();
}

@Override
Expand All @@ -74,6 +80,23 @@ private void setupCommands() {
}

private void loadListener() {
getServer().getPluginManager().registerEvents(new JoinEvent(this.interneAPI), this);
PluginManager pluginManager = getServer().getPluginManager();
// Bukkit Events
pluginManager.registerEvents(new JoinEvent(this.interneAPI), this);
pluginManager.registerEvents(new BlockEvent(this.interneAPI), this);

// Skyblock Event
pluginManager.registerEvents(new SkyblockEvent(this.interneAPI), this);
}

private void runCache() {
ScheduledExecutorService executors = Executors.newScheduledThreadPool(2);
executors.scheduleAtFixedRate(() -> {
logger.log(Level.FATAL, "Update en cours");
Bukkit.getOnlinePlayers().forEach(player -> {
this.interneAPI.updateCache(player);
});
}, 0, 10, TimeUnit.SECONDS);

}
}
12 changes: 10 additions & 2 deletions plugin/src/main/java/fr/euphyllia/skyfolia/api/InterneAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import fr.euphyllia.skyfolia.Main;
import fr.euphyllia.skyfolia.api.exceptions.DatabaseException;
import fr.euphyllia.skyfolia.cache.CacheManager;
import fr.euphyllia.skyfolia.configuration.ConfigToml;
import fr.euphyllia.skyfolia.configuration.LanguageToml;
import fr.euphyllia.skyfolia.database.DatabaseLoader;
Expand All @@ -15,6 +16,7 @@
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -33,11 +35,13 @@ public class InterneAPI {
private MariaDBTransactionQuery transaction;
private DatabaseLoader databaseLoader;
private Managers managers;
private CacheManager cacheManager;

public InterneAPI(Main plugin) {
this.plugin = plugin;
this.logger = LogManager.getLogger("fr.euphyllia.skyfolia.api.InterneAPI");
this.skyblockManager = new SkyblockManager(this.plugin);
this.cacheManager = new CacheManager(this.skyblockManager);
}


Expand Down Expand Up @@ -131,7 +135,11 @@ public SkyblockManager getSkyblockManager() {
return MiniMessage.miniMessage();
}

public void loadCachePlugin() {
this.skyblockManager.loadCache();
public void updateCache(Player player) {
this.cacheManager.updateCache(skyblockManager, player);
}

public CacheManager getCacheManager() {
return this.cacheManager;
}
}
60 changes: 60 additions & 0 deletions plugin/src/main/java/fr/euphyllia/skyfolia/cache/CacheManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package fr.euphyllia.skyfolia.cache;

import fr.euphyllia.skyfolia.api.annotation.Experimental;
import fr.euphyllia.skyfolia.api.annotation.Information;
import fr.euphyllia.skyfolia.api.skyblock.Island;
import fr.euphyllia.skyfolia.api.skyblock.model.PermissionRoleIsland;
import fr.euphyllia.skyfolia.api.skyblock.model.RoleType;
import fr.euphyllia.skyfolia.api.skyblock.model.permissions.PermissionsType;
import fr.euphyllia.skyfolia.managers.skyblock.SkyblockManager;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bukkit.entity.Player;

import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;

@Experimental
@Information("Je n'ai pas tester encore cette fonctionnalité")
public class CacheManager {

private final Logger logger = LogManager.getLogger(CacheManager.class);
private final SkyblockManager skyblockManager;

public CacheManager(SkyblockManager skyblockManager) {
this.skyblockManager = skyblockManager;
}

public void updateCache(SkyblockManager skyblockManager, Player bPlayer) {
Island pIsland = skyblockManager.getIslandByOwner(bPlayer.getUniqueId()).join();
if (pIsland == null) {
// ========= remove player
PlayersInIslandCache.getIslandIdByPlayerId().remove(bPlayer.getUniqueId());
return;
}
this.updateCacheIsland(pIsland, bPlayer.getUniqueId());
}

public void updateCacheIsland(Island island, UUID playerId) {
Executors.newSingleThreadScheduledExecutor().execute(() -> {
// ============= player cache
PlayersInIslandCache.getIslandIdByPlayerId().put(playerId, island.getId());
PlayersInIslandCache.getListPlayersInIsland().put(island.getId(), island.getMembers());
// ============= position island cache
PositionIslandCache.getPositionIslandId().put(island.getPosition().toString(), island.getId());
// ============= permission role cache
ConcurrentHashMap<RoleType, Integer> permissionByRole = new ConcurrentHashMap<>();
for (PermissionsType permissionsType : PermissionsType.values()) {
for (RoleType roleType : RoleType.values()) {
PermissionRoleIsland permissionRoleIsland = skyblockManager.getPermissionIsland(island.getId(), permissionsType, roleType).join();
permissionByRole.put(roleType, permissionRoleIsland.permission());
}
}
PermissionRoleInIslandCache.getListPermissionsInIsland().put(island.getId(), permissionByRole);

logger.log(Level.INFO, island.getId() + " est mis à jour.");
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package fr.euphyllia.skyfolia.cache;

import fr.euphyllia.skyfolia.api.skyblock.Players;
import fr.euphyllia.skyfolia.api.skyblock.model.RoleType;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

public class PermissionRoleInIslandCache {
private static final Logger logger = LogManager.getLogger(PermissionRoleInIslandCache.class);
private static final ConcurrentHashMap<UUID, ConcurrentHashMap<RoleType, Integer>> listPermissionsInIsland = new ConcurrentHashMap<>();


public static ConcurrentHashMap<UUID, ConcurrentHashMap<RoleType, Integer>> getListPermissionsInIsland() {
return listPermissionsInIsland;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package fr.euphyllia.skyfolia.cache;

import fr.euphyllia.skyfolia.api.skyblock.Island;
import fr.euphyllia.skyfolia.api.skyblock.Players;
import fr.euphyllia.skyfolia.api.skyblock.model.RoleType;
import fr.euphyllia.skyfolia.managers.skyblock.SkyblockManager;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;

public class PlayersInIslandCache {

private static final Logger logger = LogManager.getLogger(PlayersInIslandCache.class);
private static final ConcurrentHashMap<UUID, CopyOnWriteArrayList<Players>> listPlayersInIsland = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<UUID, UUID> islandIdByPlayerId = new ConcurrentHashMap<>();

public static Players getPlayers(UUID islandId, UUID playerId) {
List<Players> playersInIsland = listPlayersInIsland.getOrDefault(islandId, new CopyOnWriteArrayList<>());
if (playersInIsland.isEmpty()) {
return new Players(playerId, null, islandId, RoleType.VISITOR);
}
for (Players players : playersInIsland) {
if (players.getMojangId() == playerId) {
return players;
}
}
return new Players(playerId, null, islandId, RoleType.VISITOR);
}

public static ConcurrentMap<UUID, CopyOnWriteArrayList<Players>> getListPlayersInIsland() {
return listPlayersInIsland;
}

public static ConcurrentMap<UUID, UUID> getIslandIdByPlayerId() {
return islandIdByPlayerId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package fr.euphyllia.skyfolia.cache;

import fr.euphyllia.skyfolia.api.skyblock.model.Position;
import org.jetbrains.annotations.Nullable;

import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class PositionIslandCache {

private static final ConcurrentHashMap<String, UUID> positionIslandId = new ConcurrentHashMap<>();

public @Nullable UUID getIslandId(Position position) {
return positionIslandId.getOrDefault(position.toString(), null);
}

public static ConcurrentMap<String, UUID> getPositionIslandId() {
return positionIslandId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package fr.euphyllia.skyfolia.listeners.bukkitevents;

import fr.euphyllia.skyfolia.api.InterneAPI;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;

public class BlockEvent implements Listener {

private final InterneAPI api;
private final Logger logger = LogManager.getLogger(BlockEvent.class);

public BlockEvent(InterneAPI interneAPI) {
this.api = interneAPI;
}

public void onBlockBreakOnIsland(final BlockBreakEvent event) {

}

}
Loading

0 comments on commit e30be67

Please sign in to comment.