Skip to content

Commit

Permalink
Starter Kits (#79)
Browse files Browse the repository at this point in the history
J'ai ajouté des starters kits en fonction des permissions ayw.levels.x0


![image](https://github.com/Margouta/PluginOpenMC/assets/56231137/02a81d01-b856-4a1c-a3fb-09f202cdb87b)
  • Loading branch information
MathiasDPX authored Jul 6, 2024
2 parents ba1c8dd + d2bd32b commit 1ed9539
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 28 deletions.
24 changes: 5 additions & 19 deletions src/main/java/fr/communaywen/core/AywenCraftPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import fr.communaywen.core.commands.*;
import fr.communaywen.core.listeners.*;
import fr.communaywen.core.staff.freeze.FreezeListener;
import fr.communaywen.core.teams.*;
import fr.communaywen.core.utils.*;

Expand All @@ -13,17 +12,14 @@
import fr.communaywen.core.economy.EconomyManager;
import dev.xernas.menulib.MenuLib;
import fr.communaywen.core.utils.database.DatabaseManager;
import fr.communaywen.core.staff.freeze.FreezeCommand;
import fr.communaywen.core.staff.freeze.FreezeListener;

import net.luckperms.api.LuckPerms;
import org.bukkit.Bukkit;
import fr.communaywen.core.commands.RTPCommand;
import fr.communaywen.core.utils.database.DatabaseManager;
import fr.communaywen.core.listeners.RTPWand;
import fr.communaywen.core.staff.freeze.FreezeCommand;
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.Listener;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -105,6 +101,9 @@ public void onEnable() {
this.getCommand("tpaccept").setExecutor(new CommandTpaccept());
this.getCommand("tpdeny").setExecutor(new CommandTpdeny());

this.getCommand("freeze").setExecutor(new FreezeCommand(this));
this.getCommand("unfreeze").setExecutor(new FreezeCommand(this));

PluginCommand teamCommand = this.getCommand("team");
teamCommand.setExecutor(new TeamCommand());
teamCommand.setTabCompleter(new TeamCommand());
Expand All @@ -123,20 +122,7 @@ public void onEnable() {
getServer().getPluginManager().registerEvents(new FreezeListener(this), this);
/* --------- */


// Initialiser EconomyManager et enregistrer la commande money
economyManager = new EconomyManager(getDataFolder());
this.getCommand("money").setExecutor(new MoneyCommand(economyManager));

// Commandes de freeze
this.getCommand("freeze").setExecutor(new FreezeCommand(this));
this.getCommand("unfreeze").setExecutor(new FreezeCommand(this));




saveDefaultConfig();

}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
try {
this.linkerAPI.setDatabase(player, args[0]);
} catch (SQLException e) {
System.out.println(e.getMessage());
throw new RuntimeException(e);
}

Expand Down
71 changes: 71 additions & 0 deletions src/main/java/fr/communaywen/core/listeners/StarterKits.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package fr.communaywen.core.listeners;

import fr.communaywen.core.utils.LinkerAPI;
import net.luckperms.api.LuckPerms;
import net.luckperms.api.model.user.User;
import net.luckperms.api.node.Node;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import java.sql.SQLException;

public class StarterKits implements Listener {

private LuckPerms luckPerms;
private LinkerAPI linkerAPI;

public StarterKits(LuckPerms luckPerms, LinkerAPI linkerAPI) {
this.luckPerms = luckPerms;
this.linkerAPI = linkerAPI;
}

private void addPermission(User user, String permission) {
// Add the permission
user.data().add(Node.builder(permission).build());

// Now we need to save changes.
luckPerms.getUserManager().saveUser(user);
}

@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) throws SQLException { // Donne une permissions en fonction du niveau
Player player = event.getPlayer();
User lpPlayer = this.luckPerms.getPlayerAdapter(Player.class).getUser(player);

if (player.hasPermission("ayw.starterpack.isclaimed")) { return; }
String uid = linkerAPI.getUserId(player);
if (uid.isBlank()){ return; }

Inventory pInv = player.getInventory();
if (player.hasPermission("ayw.levels.10")) {
System.out.println("Give lv10 starter kits to "+player.getName());
pInv.addItem(new ItemStack(Material.LEATHER_HELMET));
pInv.addItem(new ItemStack(Material.LEATHER_CHESTPLATE));
pInv.addItem(new ItemStack(Material.LEATHER_LEGGINGS));
pInv.addItem(new ItemStack(Material.LEATHER_BOOTS));
pInv.addItem(new ItemStack(Material.STONE_AXE));
pInv.addItem(new ItemStack(Material.STONE_PICKAXE));
pInv.addItem(new ItemStack(Material.STONE_SWORD));
pInv.addItem(new ItemStack(Material.COOKED_BEEF, 16));

}

if (player.hasPermission("ayw.levels.20")) {
System.out.println("Give lv20 starter kits to "+player.getName());
pInv.addItem(new ItemStack(Material.COOKED_BEEF, 16));

}

if (player.hasPermission("ayw.levels.30")) { // Level 30+
System.out.println("Give lv30 starter kits to "+player.getName());
pInv.addItem(new ItemStack(Material.COOKED_BEEF, 32));
}

addPermission(lpPlayer, "ayw.starterpack.isclaimed");
}
}
19 changes: 10 additions & 9 deletions src/main/java/fr/communaywen/core/utils/LinkerAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
import java.sql.ResultSet;
import java.sql.SQLException;

/*
Créer la DB: CREATE TABLE link (discord_id VARCHAR(100) NOT NULL, minecraft_uuid VARCHAR(100))
*/

public class LinkerAPI {

private final DatabaseManager dbmanager;
Expand All @@ -25,35 +21,40 @@ public boolean setDatabase(Player player, String uid) throws SQLException { // A
String playerUUID = player.getUniqueId().toString();
Connection connection = dbmanager.getConnection();

PreparedStatement statement = connection.prepareStatement("INSERT INTO mclink (discord_id, minecraft_uuid) VALUES (?, ?)");
PreparedStatement statement = connection.prepareStatement("INSERT INTO link (discord_id, minecraft_uuid) VALUES (?, ?)");

statement.setString(1, uid);
statement.setString(2, playerUUID);

statement.executeUpdate();

return true;
} catch (Exception e){ return false; }
} catch (Exception e){
System.out.println(e.toString());
return false;
}
}

public String getUserId(Player player) throws SQLException { // Lis la DB est retourne l'uid (discord) du joueur, "" si introuvable
try {
String uuid = player.getUniqueId().toString();
Connection connection = dbmanager.getConnection();

String sql = "SELECT * FROM mclink WHERE minecraft_uuid = ?";
String sql = "SELECT * FROM link WHERE minecraft_uuid = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, uuid);

ResultSet resultSet = statement.executeQuery();

if (resultSet.next()) {
System.out.println("Discord ID found for "+player.getName()+": "+resultSet.getString("discord_id"));
return resultSet.getString("discord_id");
} else {
return "";
}

} catch (Exception e){ return ""; }
} catch (Exception e) {
System.out.println(e.toString());
return "";
}
}
}

0 comments on commit 1ed9539

Please sign in to comment.