Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajout du Météowand, qui permet de changer la météo du monde. #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 119 additions & 82 deletions src/main/java/fr/openmc/core/OMCPlugin.java
Original file line number Diff line number Diff line change
@@ -1,82 +1,119 @@
package fr.openmc.core;

import dev.xernas.menulib.MenuLib;
import fr.openmc.core.commands.CommandsManager;
import fr.openmc.core.features.ScoreboardManager;
import fr.openmc.core.features.city.CityManager;
import fr.openmc.core.features.contest.managers.ContestManager;
import fr.openmc.core.features.contest.managers.ContestPlayerManager;
import fr.openmc.core.features.economy.EconomyManager;
import fr.openmc.core.commands.utils.SpawnManager;
import fr.openmc.core.features.mailboxes.MailboxManager;
import fr.openmc.core.listeners.ListenersManager;
import fr.openmc.core.utils.LuckPermsAPI;
import fr.openmc.core.utils.PapiAPI;
import fr.openmc.core.utils.customitems.CustomItemRegistry;
import fr.openmc.core.utils.database.DatabaseManager;
import fr.openmc.core.utils.MotdUtils;
import lombok.Getter;
import net.luckperms.api.LuckPerms;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin;

import java.sql.SQLException;

public final class OMCPlugin extends JavaPlugin {
@Getter static OMCPlugin instance;
@Getter static FileConfiguration configs;
private DatabaseManager dbManager;

@Override
public void onEnable() {
instance = this;

/* CONFIG */
saveDefaultConfig();
configs = this.getConfig();

/* EXTERNALS */
MenuLib.init(this);
new LuckPermsAPI(this);
new PapiAPI();

/* MANAGERS */
dbManager = new DatabaseManager();
new CommandsManager();
CustomItemRegistry.init();
ContestManager contestManager = new ContestManager(this);
ContestPlayerManager contestPlayerManager = new ContestPlayerManager();
new SpawnManager(this);
new CityManager();
new ListenersManager();
new EconomyManager();
new MailboxManager();
contestPlayerManager.setContestManager(contestManager); // else ContestPlayerManager crash because ContestManager is null
contestManager.setContestPlayerManager(contestPlayerManager);
new MotdUtils(this);

getLogger().info("Plugin activé");
}

@Override
public void onDisable() {
ContestManager.getInstance().saveContestData();
ContestManager.getInstance().saveContestPlayerData();
if (dbManager != null) {
try {
dbManager.close();
} catch (SQLException e) {
getLogger().severe("Impossible de fermer la connexion à la base de données");
}
}

getLogger().info("Plugin désactivé");
}

public static void registerEvents(Listener... listeners) {
for (Listener listener : listeners) {
instance.getServer().getPluginManager().registerEvents(listener, instance);
}
}
}
package fr.openmc.core;

import dev.xernas.menulib.MenuLib;
import fr.openmc.core.commands.CommandsManager;
import fr.openmc.core.features.ScoreboardManager;
import fr.openmc.core.features.city.CityManager;
import fr.openmc.core.features.contest.managers.ContestManager;
import fr.openmc.core.features.contest.managers.ContestPlayerManager;
import fr.openmc.core.features.economy.EconomyManager;
import fr.openmc.core.commands.utils.SpawnManager;
import fr.openmc.core.features.mailboxes.MailboxManager;
import fr.openmc.core.listeners.ListenersManager;
import fr.openmc.core.utils.LuckPermsAPI;
import fr.openmc.core.utils.PapiAPI;
import fr.openmc.core.utils.customitems.CustomItemRegistry;
import fr.openmc.core.utils.database.DatabaseManager;
import fr.openmc.core.utils.MotdUtils;
import lombok.Getter;
import net.luckperms.api.LuckPerms;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.Listener;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.plugin.java.JavaPlugin;
import fr.openmc.core.features.weatherwand.WeatherGUIListener;
import fr.openmc.core.features.weatherwand.MeteoWand;

import java.sql.SQLException;

public final class OMCPlugin extends JavaPlugin {
@Getter static OMCPlugin instance;
@Getter static FileConfiguration configs;
private DatabaseManager dbManager;

@Override
public void onEnable() {
instance = this;

/* CONFIG */
saveDefaultConfig();
configs = this.getConfig();

/* EXTERNALS */
MenuLib.init(this);
new LuckPermsAPI(this);
new PapiAPI();

/* MANAGERS */
dbManager = new DatabaseManager();
new CommandsManager();
CustomItemRegistry.init();
ContestManager contestManager = new ContestManager(this);
ContestPlayerManager contestPlayerManager = new ContestPlayerManager();
new SpawnManager(this);
new CityManager();
new ListenersManager();
new EconomyManager();
new MailboxManager();
contestPlayerManager.setContestManager(contestManager); // else ContestPlayerManager crash because ContestManager is null
contestManager.setContestPlayerManager(contestPlayerManager);
new MotdUtils(this);

getServer().getPluginManager().registerEvents(new WeatherGUIListener(), this);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merci de le mettre dans ListenerManager stp

addCustomRecipes();

getLogger().info("Plugin activé");
}

@Override
public void onDisable() {
ContestManager.getInstance().saveContestData();
ContestManager.getInstance().saveContestPlayerData();
if (dbManager != null) {
try {
dbManager.close();
} catch (SQLException e) {
getLogger().severe("Impossible de fermer la connexion à la base de données");
}
}

getLogger().info("Plugin désactivé");
}

public static void registerEvents(Listener... listeners) {
for (Listener listener : listeners) {
instance.getServer().getPluginManager().registerEvents(listener, instance);
}
}

/**
* Méthode responsable d'ajouter les recettes personnalisées.
*/
private void addCustomRecipes() {
addMeteoWandRecipe();
}

/**
* Ajoute la recette pour fabriquer le Meteo Wand.
*/
private void addMeteoWandRecipe() {
ItemStack meteoWand = new MeteoWand().getVanilla();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On ne met pas tout dans le main stp met le dans MeteoWand et initialise le dans le constructeur MeteoWand


ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(this, "meteowand"), meteoWand);
recipe.shape(
" S ",
" B ",
" D "
);
recipe.setIngredient('S', Material.NETHER_STAR);
recipe.setIngredient('B', Material.STICK);
recipe.setIngredient('D', Material.DIAMOND);

Bukkit.addRecipe(recipe);
}
}
53 changes: 53 additions & 0 deletions src/main/java/fr/openmc/core/features/weatherwand/MeteoWand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package fr.openmc.core.features.weatherwand;

import fr.openmc.core.utils.customitems.CustomItem;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.List;

public class MeteoWand extends CustomItem {

public MeteoWand() {
super("meteo_wand");
}

@Override
public ItemStack getVanilla() {
ItemStack item = new ItemStack(Material.BLAZE_ROD);
ItemMeta meta = item.getItemMeta();
if (meta != null) {
meta.setDisplayName("§6Meteo Wand");
meta.setLore(List.of("§7Changer la météo du monde !"));
item.setItemMeta(meta);
}
return item;
}

@Override
public ItemStack getItemsAdder() {
return null;
}

/**
* Gère l'utilisation du bâton Meteo Wand par le joueur
*/
public void onClick(PlayerInteractEvent event) {
Player player = event.getPlayer();

openWeatherMenu(player);
}

/**
* Crée et ouvre une interface utilisateur pour que le joueur change la météo
*/
private void openWeatherMenu(Player player) {
Bukkit.getScheduler().runTask(fr.openmc.core.OMCPlugin.getInstance(), () -> {
WeatherGUI gui = new WeatherGUI();
player.openInventory(gui.getInventory());
});
}
}
88 changes: 88 additions & 0 deletions src/main/java/fr/openmc/core/features/weatherwand/WeatherGUI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package fr.openmc.core.features.weatherwand;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.HashMap;
import java.util.UUID;

public class WeatherGUI {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Utilise l'API de Xernas. Y'a un exemple dans features/contest/menu

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Donc je ne mets pas beaucoup de commentaire sur celui ci j'attends que tu fasses avec l'api de Xernas

private final Inventory inventory;

private final HashMap<UUID, Long> playerCooldowns = new HashMap<>();
private static final long COOLDOWN_TIME = 30 * 1000;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

met un commentaire pour dire sa valeur en heurs ou min


public WeatherGUI() {
this.inventory = Bukkit.createInventory(null, 9, "§9Changer la météo");

addWeatherItem(0, Material.SUNFLOWER, "§eSoleil");
addWeatherItem(2, Material.WATER_BUCKET, "§bPluie");
addWeatherItem(4, Material.CREEPER_HEAD, "§8Orage");
addWeatherItem(6, Material.SNOWBALL, "§fNeige");
}

private void addWeatherItem(int slot, Material material, String name) {
ItemStack item = new ItemStack(material);
ItemMeta meta = item.getItemMeta();
if (meta != null) {
meta.setDisplayName(name);
item.setItemMeta(meta);
}
inventory.setItem(slot, item);
}

public Inventory getInventory() {
return inventory;
}

/**
* Gère la sélection d'une météo par l'utilisateur avec gestion de cooldown.
*/
public void handleInventoryClick(Player player, ItemStack clickedItem) {
if (clickedItem == null || !clickedItem.hasItemMeta()) return;

UUID playerId = player.getUniqueId();
long currentTime = System.currentTimeMillis();
if (playerCooldowns.containsKey(playerId)) {
long lastUseTime = playerCooldowns.get(playerId);
if ((currentTime - lastUseTime) < COOLDOWN_TIME) {
long timeLeft = (COOLDOWN_TIME - (currentTime - lastUseTime)) / 1000;
player.sendMessage("§cVous devez attendre encore " + timeLeft + " secondes avant de changer la météo !");
return;
}
}

String displayName = clickedItem.getItemMeta().getDisplayName();

switch (displayName) {
case "§eSoleil":
player.getWorld().setWeatherDuration(0);
player.getWorld().setStorm(false);
break;
case "§bPluie":
player.getWorld().setStorm(true);
player.getWorld().setThundering(false);
break;
case "§8Orage":
player.getWorld().setStorm(true);
player.getWorld().setThundering(true);
break;
case "§fNeige":
player.getWorld().setStorm(true);
player.getWorld().setThundering(false);
player.getWorld().setWeatherDuration(6000);
player.sendMessage("§bVous avez invoqué une tempête de neige !");
break;
default:
return;
}

player.closeInventory();
player.sendMessage("§aVous avez changé la météo en : " + displayName);
playerCooldowns.put(playerId, currentTime);
}
}
Loading
Loading