Compare commits
15
Commits
a47376db3a
...
v1.0.1
@@ -0,0 +1,38 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
pull_request:
|
||||
branches: [main]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup JDK 17
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
distribution: temurin
|
||||
java-version: '17'
|
||||
cache: maven
|
||||
|
||||
- name: Install Maven
|
||||
run: |
|
||||
which mvn || (sudo apt-get update && sudo apt-get install -y maven)
|
||||
|
||||
- name: Build and test
|
||||
run: mvn -B clean verify
|
||||
|
||||
- name: Upload jars
|
||||
if: success()
|
||||
uses: actions/upload-artifact@v3
|
||||
with:
|
||||
name: votenetwork-jars
|
||||
path: |
|
||||
velocity/target/VoteNetwork-Velocity.jar
|
||||
spigot/target/VoteNetwork-Spigot.jar
|
||||
retention-days: 14
|
||||
@@ -1,6 +1,8 @@
|
||||
target/
|
||||
dependency-reduced-pom.xml
|
||||
dist/
|
||||
.idea/
|
||||
*.iml
|
||||
.vscode/
|
||||
.DS_Store
|
||||
MSP3/
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2026 Sar_Tron
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -1,5 +1,7 @@
|
||||
# VoteNetwork
|
||||
|
||||
Par **Sar_Tron**, sous licence [MIT](LICENSE).
|
||||
|
||||
Systeme de vote reseau en deux modules, pour une architecture **proxy Velocity + serveurs Spigot/Paper** (modes A, B, C, ...).
|
||||
|
||||
- `velocity/` — module proxy. Recoit `votenetwork vote <pseudo>` (console uniquement) et route le vote en direct ou le met en attente.
|
||||
@@ -32,6 +34,8 @@ Compatible **Java 17**, compile contre l'API Spigot **1.18.2** (additive, donc c
|
||||
- Serveur : `plugins/votenetwork/config.yml`
|
||||
4. Redémarrer.
|
||||
|
||||
> **⚠️ Même base de données partout.** `mysql.host` / `mysql.port` / `mysql.database` / `mysql.user` / `mysql.password` doivent être **identiques** dans `config.properties` (Velocity) et dans **chaque** `config.yml` (tous les serveurs Spigot : A, B, C...). C'est cette base commune qui permet à un joueur de voter hors ligne, puis de récupérer ses votes avec `/claim` sur n'importe lequel de vos serveurs. Des bases différentes = `/claim` ne retrouvera jamais les votes stockés par Velocity. Voir aussi [Stockage : MySQL ou YAML](#stockage--mysql-ou-yaml).
|
||||
|
||||
## Build depuis les sources
|
||||
|
||||
```
|
||||
@@ -40,6 +44,14 @@ mvn clean package
|
||||
|
||||
Jars produits dans `velocity/target/` et `spigot/target/`.
|
||||
|
||||
### Publier une release (Gitea)
|
||||
|
||||
```
|
||||
GITEA_TOKEN=xxxx ./scripts/release.sh 1.0.1 "Description de la release"
|
||||
```
|
||||
|
||||
Build les 2 jars, les zip dans `dist/VoteNetwork.zip`, crée le tag/release sur Gitea et y attache le zip. Nécessite un token Gitea (Paramètres → Applications → Générer un nouveau jeton, scope `repo`), passé uniquement en variable d'environnement — jamais commité.
|
||||
|
||||
## Configuration
|
||||
|
||||
Chaque composant a son propre fichier, tous deux dans un dossier **`votenetwork`** :
|
||||
@@ -97,10 +109,10 @@ messages:
|
||||
|
||||
## Stockage : MySQL ou YAML
|
||||
|
||||
Chaque composant choisit indépendamment son stockage via `storage.type` :
|
||||
Chaque composant choisit son stockage via `storage.type`, **mais ce choix doit être le même partout** (Velocity + tous les serveurs Spigot), avec les mêmes identifiants de connexion :
|
||||
|
||||
- **`mysql`** (recommandé pour un vrai réseau) : partagé entre le proxy et tous les serveurs. La table `nb_votes_attente` est créée automatiquement au démarrage si absente.
|
||||
- **`yaml`** : fichier local `pending-votes.yml` dans le dossier du plugin. Pratique en solo/test, **mais pas synchronisé automatiquement** entre le fichier du proxy et celui d'un serveur — chacun a sa propre copie locale. N'utilisez ce mode en réseau multi-machines que si les dossiers de plugins sont partagés sur le même disque (montage réseau, symlink). Sinon, un vote stocké côté proxy en YAML ne sera jamais vu par `/claim` sur un serveur Spigot séparé.
|
||||
- **`mysql`** (recommandé, seul mode fiable pour un vrai réseau) : Velocity et tous les serveurs Spigot pointent sur **la même base** (même `host`/`database`/`user`/`password`). La table `nb_votes_attente` est créée automatiquement au démarrage si absente. Un vote stocké par Velocity (joueur hors ligne) est donc visible par `/claim` sur n'importe quel serveur.
|
||||
- **`yaml`** : fichier local `pending-votes.yml` dans le dossier de **chaque** plugin (un par proxy, un par serveur). **Pas synchronisé automatiquement** entre eux. Si Velocity est en `yaml` et qu'un serveur Spigot vote pour lui-même en `mysql` (ou l'inverse), `/claim` ne trouvera jamais les votes stockés côté proxy. N'utilisez ce mode qu'en solo/test, ou si tous les dossiers de plugins partagent le même disque (montage réseau, symlink) — sinon utilisez `mysql` partout.
|
||||
|
||||
Testez la connexion/l'accès au stockage configuré côté Spigot avec :
|
||||
|
||||
@@ -108,6 +120,21 @@ Testez la connexion/l'accès au stockage configuré côté Spigot avec :
|
||||
/vote testdb
|
||||
```
|
||||
|
||||
### Portée des votes en attente : partagée (`global`) ou par serveur (`per-server`)
|
||||
|
||||
En mode `mysql` uniquement, `pending-votes.scope` (dans `config.properties` côté Velocity) contrôle si le compteur de votes en attente est **partagé** entre tous les serveurs ou **indépendant par serveur** :
|
||||
|
||||
- **`global`** (par défaut) : un seul compteur par joueur. Un `/claim` sur n'importe quel serveur le remet à 0 **partout** à la fois.
|
||||
- **`per-server`** : chaque serveur listé dans `direct-vote.servers` a son propre compteur. Un vote hors ligne incrémente le compteur de **chaque** serveur de la liste. Un `/claim` sur le serveur 1 ne remet à 0 **que** le serveur 1 — le joueur garde ses votes en attente sur le serveur 2 et peut les réclamer séparément là-bas.
|
||||
|
||||
> Exemple : le joueur vote 2 fois hors ligne → 2 votes en attente sur *chaque* serveur. `/claim` sur le serveur 1 → 2 récompenses, serveur 1 repasse à 0, serveur 2 reste à 2. S'il revote une fois → serveur 1 = 1, serveur 2 = 3.
|
||||
|
||||
Pour activer `per-server`, il faut **les deux** :
|
||||
1. `pending-votes.scope=per-server` + `direct-vote.servers` rempli dans `config.properties` (Velocity).
|
||||
2. `storage.pending-votes-scope: per-server` + `server-name: "gen1"` (nom exact tiré de `direct-vote.servers`) dans le `config.yml` de **chaque** serveur Spigot concerné.
|
||||
|
||||
Si ces réglages ne correspondent pas entre le proxy et un serveur (scope différent, ou `server-name` absent/mal orthographié), ce serveur ne retrouvera jamais les votes stockés par Velocity.
|
||||
|
||||
## Fonctionnement du vote
|
||||
|
||||
1. Un site de vote appelle la console du proxy : `votenetwork vote <pseudo>`.
|
||||
@@ -129,7 +156,7 @@ Testez la connexion/l'accès au stockage configuré côté Spigot avec :
|
||||
|
||||
## VoteParty
|
||||
|
||||
- Compteur indépendant **par serveur**, persistant localement (`plugins/votenetwork/voteparty.yml`), survit aux redémarrages.
|
||||
- Compteur indépendant **par serveur**, toujours persistant localement dans `plugins/votenetwork/voteparty.yml` (indépendant de `storage.type`, qui ne concerne que les votes en attente), survit aux redémarrages.
|
||||
- `voteparty.votes-requis` définit le seuil de déclenchement.
|
||||
- `voteparty.commands` s'exécutent une fois le seuil atteint, puis le compteur repart à 0 (avec report de l'excédent si plusieurs votes arrivent d'un coup, ex. via `/claim` de N votes).
|
||||
- Messages de progression et de déclenchement personnalisables (`voteparty.broadcast`).
|
||||
@@ -155,16 +182,40 @@ api.getPendingVotes(player.getUniqueId(), pending -> {
|
||||
- `getVotePartyCurrentVotes()` / `getVotePartyRequiredVotes()` : lecture directe en mémoire, thread principal uniquement.
|
||||
- `getPendingVotes(...)` : lecture asynchrone du stockage configuré (MySQL ou YAML), ne consomme pas les votes contrairement à `/claim`.
|
||||
|
||||
### Placeholders PlaceholderAPI (scoreboard/GUI sans coder de plugin)
|
||||
|
||||
Si [PlaceholderAPI](https://www.spigotmc.org/resources/placeholderapi.6245/) est installé, VoteNetwork enregistre automatiquement ses propres placeholders (aucune config nécessaire) :
|
||||
|
||||
| Placeholder | Valeur |
|
||||
|---|---|
|
||||
| `%votenetwork_voteparty_current%` | Votes cumulés sur ce serveur pour le VoteParty |
|
||||
| `%votenetwork_voteparty_required%` | Objectif VoteParty (`voteparty.votes-requis`) |
|
||||
| `%votenetwork_voteparty_remaining%` | Votes restants avant déclenchement |
|
||||
| `%votenetwork_maintenance%` | `true`/`false` |
|
||||
| `%votenetwork_pending%` | Votes en attente du joueur (rafraîchi en arrière-plan, pas de lag) |
|
||||
|
||||
Utilisez ces placeholders directement dans la config de votre plugin de scoreboard/tab/GUI habituel (FeatherBoard, TAB, DeluxeMenus, ...) — **pas** `%current%`/`%required%` bruts, qui n'existent pas côté PlaceholderAPI et doivent être définis dans la config du plugin de scoreboard lui-même.
|
||||
|
||||
## Structure de la base de données
|
||||
|
||||
Créée automatiquement si absente (mode `mysql`) :
|
||||
Créées automatiquement si absentes (mode `mysql`), les deux tables coexistent toujours — seule celle correspondant à `pending-votes.scope` est utilisée :
|
||||
|
||||
```sql
|
||||
-- scope = global (par defaut)
|
||||
CREATE TABLE IF NOT EXISTS nb_votes_attente (
|
||||
player_uuid VARCHAR(36) NOT NULL PRIMARY KEY,
|
||||
player_name VARCHAR(16) NOT NULL,
|
||||
nombre_votes INT DEFAULT 0
|
||||
);
|
||||
|
||||
-- scope = per-server
|
||||
CREATE TABLE IF NOT EXISTS nb_votes_attente_serveur (
|
||||
player_uuid VARCHAR(36) NOT NULL,
|
||||
server_name VARCHAR(64) NOT NULL,
|
||||
player_name VARCHAR(16) NOT NULL,
|
||||
nombre_votes INT DEFAULT 0,
|
||||
PRIMARY KEY (player_uuid, server_name)
|
||||
);
|
||||
```
|
||||
|
||||
---
|
||||
@@ -174,3 +225,4 @@ CREATE TABLE IF NOT EXISTS nb_votes_attente (
|
||||
- HikariCP + MySQL Connector/J sont shadés et relocalisés dans les deux jars (rien à installer manuellement sur le serveur). Le driver JDBC est chargé explicitement par son nom de classe relocalisé pour rester fiable après le shading.
|
||||
- Toutes les requêtes SQL et I/O fichier sont asynchrones — aucun accès réseau ou disque ne bloque le thread principal du serveur ni celui du proxy.
|
||||
- Si la connexion MySQL échoue au démarrage, le plugin reste actif (pas de crash) : corrigez `config.yml`/`config.properties` puis utilisez `/vote testdb`, ou redémarrez.
|
||||
- **Mise à jour de config** : à chaque démarrage, les nouvelles options apportées par une mise à jour du plugin (absentes de votre `config.yml`/`config.properties` existant) sont ajoutées automatiquement, **sans jamais toucher aux valeurs que vous avez déjà personnalisées**. Côté Spigot, la réécriture du `config.yml` fait perdre les commentaires (limitation de l'API Bukkit) ; côté Velocity, les nouvelles clés sont simplement ajoutées en bas du `config.properties`, les commentaires existants sont préservés.
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
[CENTER][SIZE=5][B]VoteNetwork[/B][/SIZE]
|
||||
Systeme de vote reseau complet pour Velocity + Spigot/Paper
|
||||
Par [B]Sar_Tron[/B] — Licence MIT — Code source : https://gitea.louconnect.fr/Sar_Tron/VoteNetwork[/CENTER]
|
||||
|
||||
[CENTER][B]⚠ Ce plugin necessite les DEUX modules du zip (proxy Velocity + serveurs Spigot) et une base MySQL. Il ne fonctionne PAS en le mettant seul sur un serveur solo sans proxy.[/B][/CENTER]
|
||||
|
||||
[HR][/HR]
|
||||
[SIZE=4][B]Pourquoi VoteNetwork ?[/B][/SIZE]
|
||||
|
||||
La plupart des plugins de vote forcent un choix : soit le joueur doit etre connecte au bon moment, soit tout passe par une base de donnees meme quand ce n'est pas necessaire. VoteNetwork fait les deux intelligemment :
|
||||
|
||||
[LIST]
|
||||
[*]Joueur connecte -> recompense [B]instantanee[/B] sur son serveur actuel, sans toucher a la base de donnees.
|
||||
[*]Joueur deconnecte -> vote stocke automatiquement, recuperable plus tard via [B]/claim[/B].
|
||||
[*]VoteParty [B]independant par serveur[/B] (chaque mode de jeu a son propre objectif et ses propres recompenses).
|
||||
[*]Mode maintenance pour bloquer /claim pendant un reset sans desinstaller le plugin.
|
||||
[/LIST]
|
||||
|
||||
[HR][/HR]
|
||||
[SIZE=4][B]Fonctionnalites[/B][/SIZE]
|
||||
|
||||
[LIST]
|
||||
[*][B]Vote direct en jeu[/B] via Plugin Messaging (canal custom, aucune latence de base de donnees si le joueur est en ligne).
|
||||
[*][B]Votes en attente[/B] stockes en MySQL (partage reseau) ou en YAML local, au choix.
|
||||
[*][B]Portee configurable[/B] des votes en attente : un compteur partage entre tous les serveurs, OU un compteur independant par serveur (le joueur peut claim ses votes du serveur A sans toucher a ceux du serveur B).
|
||||
[*][B]VoteParty local[/B] par serveur : seuil, commandes et messages 100% personnalisables, persistant au redemarrage.
|
||||
[*][B]Liste de commandes[/B] entierement configurable pour chaque vote et pour le declenchement du VoteParty.
|
||||
[*][B]Message broadcast[/B] optionnel a chaque vote/claim, personnalisable.
|
||||
[*][B]Mode maintenance[/B] (/vote stop|start) pour bloquer /claim proprement pendant un reset.
|
||||
[*][B]Commande de diagnostic[/B] (/vote testdb) pour verifier la connexion MySQL ou l'acces au fichier YAML sans redemarrer.
|
||||
[*][B]API Java publique[/B] (+ enregistrement ServicesManager Bukkit) pour lire l'etat du vote depuis un autre plugin.
|
||||
[*][B]Support PlaceholderAPI[/B] natif : %votenetwork_voteparty_current%, %votenetwork_voteparty_required%, %votenetwork_voteparty_remaining%, %votenetwork_maintenance%, %votenetwork_pending% — pour scoreboard, tab, GUI, sans coder une ligne.
|
||||
[*][B]Merge automatique de config[/B] : les nouvelles options ajoutees par une mise a jour n'ecrasent jamais tes reglages existants.
|
||||
[*]Compatible [B]Java 17[/B], du [B]1.18[/B] aux versions les plus recentes (API additive, testee en production).
|
||||
[/LIST]
|
||||
|
||||
[HR][/HR]
|
||||
[SIZE=4][B]Installation[/B][/SIZE]
|
||||
|
||||
[LIST=1]
|
||||
[*][B]VoteNetwork-Velocity.jar[/B] -> dossier plugins/ du proxy Velocity.
|
||||
[*][B]VoteNetwork-Spigot.jar[/B] -> dossier plugins/ de CHAQUE serveur de jeu (aucune limite de nombre).
|
||||
[*]Demarrer une fois pour generer les configs, puis editer :
|
||||
[LIST]
|
||||
[*]Proxy : plugins/votenetwork/config.properties
|
||||
[*]Serveur : plugins/votenetwork/config.yml
|
||||
[/LIST]
|
||||
[*]Renseigner [B]les memes identifiants MySQL[/B] partout (proxy + tous les serveurs) — c'est cette base commune qui permet a un joueur de voter hors ligne puis de recuperer ses votes n'importe ou.
|
||||
[*]Redemarrer. La table est creee automatiquement, aucun script SQL a lancer a la main.
|
||||
[/LIST]
|
||||
|
||||
[HR][/HR]
|
||||
[SIZE=4][B]Commandes & permissions[/B][/SIZE]
|
||||
|
||||
[TABLE]
|
||||
[TR][TD][B]Commande[/B][/TD][TD][B]Ou[/B][/TD][TD][B]Qui[/B][/TD][TD][B]Description[/B][/TD][/TR]
|
||||
[TR][TD]votenetwork vote <pseudo>[/TD][TD]Velocity[/TD][TD]Console uniquement[/TD][TD]Enregistre un vote pour le joueur[/TD][/TR]
|
||||
[TR][TD]/claim[/TD][TD]Spigot[/TD][TD]Joueurs[/TD][TD]Recupere les votes en attente[/TD][/TR]
|
||||
[TR][TD]/vote stop|start[/TD][TD]Spigot[/TD][TD]votenetwork.admin (op)[/TD][TD]Bascule le mode maintenance[/TD][/TR]
|
||||
[TR][TD]/vote testdb[/TD][TD]Spigot[/TD][TD]votenetwork.admin (op)[/TD][TD]Teste le stockage configure[/TD][/TR]
|
||||
[/TABLE]
|
||||
|
||||
[HR][/HR]
|
||||
[SIZE=4][B]Pre-requis[/B][/SIZE]
|
||||
|
||||
[LIST]
|
||||
[*]Un proxy [B]Velocity[/B] + au moins un serveur [B]Spigot/Paper 1.18+[/B].
|
||||
[*]Une base [B]MySQL[/B] accessible depuis le proxy ET tous les serveurs (ou stockage YAML local pour un usage solo/test uniquement).
|
||||
[*][B]Java 17[/B] minimum.
|
||||
[*][I]Optionnel[/I] : PlaceholderAPI pour les placeholders de scoreboard.
|
||||
[/LIST]
|
||||
|
||||
[HR][/HR]
|
||||
[CENTER]Code source complet et documentation detaillee (config avancee, portee per-server, API) disponibles sur le depot Git : [B]https://gitea.louconnect.fr/Sar_Tron/VoteNetwork[/B]
|
||||
|
||||
Bugs / suggestions -> ouvrez une issue sur le depot.
|
||||
|
||||
Licence MIT — libre d'utilisation, modification et redistribution avec attribution.[/CENTER]
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>fr.votenetwork</groupId>
|
||||
<artifactId>votenetwork-parent</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<version>1.0.1</version>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
@@ -14,12 +14,31 @@
|
||||
<module>spigot</module>
|
||||
</modules>
|
||||
|
||||
<name>VoteNetwork</name>
|
||||
<description>Systeme de vote reseau Velocity + Spigot/Paper</description>
|
||||
<url>https://gitea.louconnect.fr/Sar_Tron/VoteNetwork</url>
|
||||
|
||||
<licenses>
|
||||
<license>
|
||||
<name>MIT License</name>
|
||||
<url>https://opensource.org/licenses/MIT</url>
|
||||
</license>
|
||||
</licenses>
|
||||
|
||||
<developers>
|
||||
<developer>
|
||||
<name>Sar_Tron</name>
|
||||
</developer>
|
||||
</developers>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>17</maven.compiler.source>
|
||||
<maven.compiler.target>17</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<hikaricp.version>5.1.0</hikaricp.version>
|
||||
<mysql.version>8.0.33</mysql.version>
|
||||
<junit.version>5.10.2</junit.version>
|
||||
<mockito.version>5.11.0</mockito.version>
|
||||
</properties>
|
||||
|
||||
<repositories>
|
||||
@@ -46,6 +65,14 @@
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.5.1</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.2.5</version>
|
||||
<configuration>
|
||||
<argLine>-Dnet.bytebuddy.experimental=true</argLine>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
#!/usr/bin/env bash
|
||||
# Build les 2 jars, zip, puis cree une release Gitea avec le zip attache.
|
||||
#
|
||||
# Usage :
|
||||
# GITEA_TOKEN=xxxx ./scripts/release.sh 1.0.1 "Description de la release"
|
||||
#
|
||||
# Le token doit avoir le scope "repo" (Parametres -> Applications -> Generer un nouveau jeton).
|
||||
# Ne JAMAIS commit le token. Passe-le uniquement en variable d'environnement.
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
VERSION="${1:?Usage: release.sh <version> [notes]}"
|
||||
NOTES="${2:-Release ${VERSION}}"
|
||||
|
||||
GITEA_HOST="https://gitea.louconnect.fr"
|
||||
OWNER="Sar_Tron"
|
||||
REPO="VoteNetwork"
|
||||
TAG="v${VERSION}"
|
||||
|
||||
if [ -z "${GITEA_TOKEN:-}" ]; then
|
||||
echo "Erreur : variable GITEA_TOKEN non definie." >&2
|
||||
echo "Cree un token dans Gitea (Parametres > Applications, scope repo) puis :" >&2
|
||||
echo " GITEA_TOKEN=xxxx ./scripts/release.sh ${VERSION}" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
echo "==> Build Maven"
|
||||
mvn -q clean package
|
||||
|
||||
echo "==> Preparation du zip de distribution"
|
||||
rm -rf dist
|
||||
mkdir -p dist
|
||||
cp velocity/target/VoteNetwork-Velocity.jar dist/
|
||||
cp spigot/target/VoteNetwork-Spigot.jar dist/
|
||||
|
||||
if command -v zip >/dev/null 2>&1; then
|
||||
(cd dist && zip -q VoteNetwork.zip VoteNetwork-Velocity.jar VoteNetwork-Spigot.jar)
|
||||
else
|
||||
powershell -Command "Compress-Archive -Path dist/VoteNetwork-Velocity.jar,dist/VoteNetwork-Spigot.jar -DestinationPath dist/VoteNetwork.zip -Force"
|
||||
fi
|
||||
|
||||
echo "==> Creation de la release Gitea ${TAG}"
|
||||
RELEASE_JSON=$(curl -sf -X POST "${GITEA_HOST}/api/v1/repos/${OWNER}/${REPO}/releases" \
|
||||
-H "Authorization: token ${GITEA_TOKEN}" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "{\"tag_name\":\"${TAG}\",\"name\":\"VoteNetwork ${VERSION}\",\"body\":$(printf '%s' "$NOTES" | python3 -c 'import json,sys;print(json.dumps(sys.stdin.read()))' 2>/dev/null || printf '"%s"' "$NOTES"),\"draft\":false,\"prerelease\":false}")
|
||||
|
||||
RELEASE_ID=$(printf '%s' "$RELEASE_JSON" | grep -o '"id":[0-9]*' | head -1 | grep -o '[0-9]*')
|
||||
|
||||
if [ -z "$RELEASE_ID" ]; then
|
||||
echo "Erreur : creation de la release echouee." >&2
|
||||
echo "$RELEASE_JSON" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "==> Upload du zip (release id ${RELEASE_ID})"
|
||||
curl -sf -X POST "${GITEA_HOST}/api/v1/repos/${OWNER}/${REPO}/releases/${RELEASE_ID}/assets?name=VoteNetwork.zip" \
|
||||
-H "Authorization: token ${GITEA_TOKEN}" \
|
||||
-F "attachment=@dist/VoteNetwork.zip" > /dev/null
|
||||
|
||||
echo "==> Fait : ${GITEA_HOST}/${OWNER}/${REPO}/releases/tag/${TAG}"
|
||||
+23
-1
@@ -7,7 +7,7 @@
|
||||
<parent>
|
||||
<groupId>fr.votenetwork</groupId>
|
||||
<artifactId>votenetwork-parent</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<version>1.0.1</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>votenetwork-spigot</artifactId>
|
||||
@@ -36,6 +36,24 @@
|
||||
<artifactId>mysql-connector-j</artifactId>
|
||||
<version>${mysql.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>me.clip</groupId>
|
||||
<artifactId>placeholderapi</artifactId>
|
||||
<version>2.11.6</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>${mockito.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
@@ -47,6 +65,10 @@
|
||||
<id>sonatype</id>
|
||||
<url>https://oss.sonatype.org/content/groups/public/</url>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>placeholderapi</id>
|
||||
<url>https://repo.extendedclip.com/content/repositories/placeholderapi/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
package fr.votenetwork.spigot;
|
||||
|
||||
import fr.votenetwork.spigot.gui.VoteSite;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class VoteConfig {
|
||||
@@ -30,6 +35,15 @@ public class VoteConfig {
|
||||
return !"yaml".equalsIgnoreCase(getStorageType());
|
||||
}
|
||||
|
||||
/**
|
||||
* Doit correspondre exactement a pending-votes.scope cote Velocity :
|
||||
* global : /claim consomme le compteur partage entre tous les serveurs.
|
||||
* per-server : /claim ne consomme que le compteur de CE serveur (server-name).
|
||||
*/
|
||||
public boolean isPerServerScope() {
|
||||
return "per-server".equalsIgnoreCase(cfg().getString("storage.pending-votes-scope", "global"));
|
||||
}
|
||||
|
||||
public String getMysqlHost() {
|
||||
return cfg().getString("mysql.host", "127.0.0.1");
|
||||
}
|
||||
@@ -54,6 +68,14 @@ public class VoteConfig {
|
||||
return cfg().getInt("mysql.pool-size", 5);
|
||||
}
|
||||
|
||||
/**
|
||||
* Nom de CE serveur, tel que declare cote Velocity dans direct-vote.servers.
|
||||
* Uniquement necessaire si le proxy utilise pending-votes.scope=per-server.
|
||||
*/
|
||||
public String getServerName() {
|
||||
return cfg().getString("server-name", "");
|
||||
}
|
||||
|
||||
public List<String> getVoteRewardCommands() {
|
||||
return cfg().getStringList("rewards.vote-commands");
|
||||
}
|
||||
@@ -124,6 +146,57 @@ public class VoteConfig {
|
||||
"&cVous n'avez pas la permission d'utiliser cette commande."));
|
||||
}
|
||||
|
||||
public String getGuiTitle() {
|
||||
return colorize(cfg().getString("gui.title", "&8VoteNetwork"));
|
||||
}
|
||||
|
||||
public String getGuiProgressItemMaterial() {
|
||||
return cfg().getString("gui.progress-item.material", "NETHER_STAR");
|
||||
}
|
||||
|
||||
public String getGuiProgressItemName() {
|
||||
return colorize(cfg().getString("gui.progress-item.name", "&b&lVoteParty"));
|
||||
}
|
||||
|
||||
public List<String> getGuiProgressItemLore() {
|
||||
return colorizeList(cfg().getStringList("gui.progress-item.lore"));
|
||||
}
|
||||
|
||||
public String getGuiClickMessage() {
|
||||
return colorize(cfg().getString("gui.click-message", "&a[Vote] Clique sur le lien pour voter :"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses gui.vote-sites (a YAML list of maps: name/material/url/lore) into VoteSite
|
||||
* objects, one item per configured vote site.
|
||||
*/
|
||||
public List<VoteSite> getVoteSites() {
|
||||
List<VoteSite> sites = new ArrayList<>();
|
||||
for (Map<?, ?> raw : cfg().getMapList("gui.vote-sites")) {
|
||||
String name = raw.containsKey("name") ? String.valueOf(raw.get("name")) : "&aVoter";
|
||||
String materialName = raw.containsKey("material") ? String.valueOf(raw.get("material")) : "PAPER";
|
||||
String url = raw.containsKey("url") ? String.valueOf(raw.get("url")) : "";
|
||||
|
||||
List<String> lore = new ArrayList<>();
|
||||
Object loreObj = raw.get("lore");
|
||||
if (loreObj instanceof List<?> loreList) {
|
||||
for (Object line : loreList) {
|
||||
lore.add(String.valueOf(line));
|
||||
}
|
||||
}
|
||||
|
||||
Material material;
|
||||
try {
|
||||
material = Material.valueOf(materialName.toUpperCase(Locale.ROOT));
|
||||
} catch (IllegalArgumentException e) {
|
||||
material = Material.PAPER;
|
||||
}
|
||||
|
||||
sites.add(new VoteSite(colorize(name), material, url, colorizeList(lore)));
|
||||
}
|
||||
return sites;
|
||||
}
|
||||
|
||||
private String colorize(String input) {
|
||||
return input == null ? "" : ChatColor.translateAlternateColorCodes('&', input);
|
||||
}
|
||||
|
||||
@@ -5,14 +5,23 @@ import fr.votenetwork.spigot.api.VoteNetworkAPIHolder;
|
||||
import fr.votenetwork.spigot.api.VoteNetworkAPIImpl;
|
||||
import fr.votenetwork.spigot.command.ClaimCommand;
|
||||
import fr.votenetwork.spigot.command.VoteAdminCommand;
|
||||
import fr.votenetwork.spigot.command.VoteGuiCommand;
|
||||
import fr.votenetwork.spigot.gui.VotePartyGuiListener;
|
||||
import fr.votenetwork.spigot.listener.VoteMessageListener;
|
||||
import fr.votenetwork.spigot.placeholder.VoteNetworkPlaceholders;
|
||||
import fr.votenetwork.spigot.storage.MySqlPendingVoteStore;
|
||||
import fr.votenetwork.spigot.storage.PendingVoteStore;
|
||||
import fr.votenetwork.spigot.storage.YamlPendingVoteStore;
|
||||
import fr.votenetwork.spigot.voteparty.VoteParty;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.plugin.ServicePriority;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class VoteNetworkSpigot extends JavaPlugin {
|
||||
|
||||
public static final String CHANNEL = "votenetwork:vote";
|
||||
@@ -25,6 +34,7 @@ public class VoteNetworkSpigot extends JavaPlugin {
|
||||
@Override
|
||||
public void onEnable() {
|
||||
saveDefaultConfig();
|
||||
mergeMissingConfigKeys();
|
||||
|
||||
this.voteConfig = new VoteConfig(this);
|
||||
this.maintenanceState = new MaintenanceState();
|
||||
@@ -47,14 +57,58 @@ public class VoteNetworkSpigot extends JavaPlugin {
|
||||
|
||||
getCommand("claim").setExecutor(new ClaimCommand(this, voteConfig, pendingVoteStore, voteParty, maintenanceState));
|
||||
getCommand("vote").setExecutor(new VoteAdminCommand(this, voteConfig, pendingVoteStore, maintenanceState));
|
||||
getCommand("votegui").setExecutor(new VoteGuiCommand(voteConfig, voteParty));
|
||||
getServer().getPluginManager().registerEvents(new VotePartyGuiListener(voteConfig), this);
|
||||
|
||||
VoteNetworkAPI api = new VoteNetworkAPIImpl(this, voteConfig, pendingVoteStore, voteParty, maintenanceState);
|
||||
VoteNetworkAPIHolder.set(api);
|
||||
getServer().getServicesManager().register(VoteNetworkAPI.class, api, this, ServicePriority.Normal);
|
||||
|
||||
if (Bukkit.getPluginManager().isPluginEnabled("PlaceholderAPI")) {
|
||||
new VoteNetworkPlaceholders(this, voteConfig, pendingVoteStore, voteParty, maintenanceState).register();
|
||||
getLogger().info("Placeholders PlaceholderAPI enregistres (%votenetwork_...%).");
|
||||
}
|
||||
|
||||
getLogger().info("VoteNetwork (Spigot) demarre.");
|
||||
}
|
||||
|
||||
/**
|
||||
* Ajoute dans config.yml les cles presentes dans le config.yml embarque dans le jar mais
|
||||
* absentes du fichier existant (nouvelles options apportees par une mise a jour du plugin),
|
||||
* sans jamais modifier une valeur deja definie par l'admin.
|
||||
*
|
||||
* Limite connue : Bukkit ne preserve pas les commentaires lors de la reecriture du fichier.
|
||||
*/
|
||||
private void mergeMissingConfigKeys() {
|
||||
try (InputStreamReader reader = new InputStreamReader(getResource("config.yml"), StandardCharsets.UTF_8)) {
|
||||
YamlConfiguration defaults = YamlConfiguration.loadConfiguration(reader);
|
||||
YamlConfiguration current = YamlConfiguration.loadConfiguration(getConfigFile());
|
||||
|
||||
boolean changed = false;
|
||||
for (String key : defaults.getKeys(true)) {
|
||||
if (defaults.isConfigurationSection(key)) {
|
||||
continue;
|
||||
}
|
||||
if (!current.isSet(key)) {
|
||||
current.set(key, defaults.get(key));
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (changed) {
|
||||
current.save(getConfigFile());
|
||||
reloadConfig();
|
||||
getLogger().info("config.yml mis a jour avec les nouvelles options manquantes (valeurs existantes conservees).");
|
||||
}
|
||||
} catch (IOException | java.io.UncheckedIOException e) {
|
||||
getLogger().warning("Impossible de fusionner les nouvelles options de config.yml : " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private java.io.File getConfigFile() {
|
||||
return new java.io.File(getDataFolder(), "config.yml");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
getServer().getServicesManager().unregisterAll(this);
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package fr.votenetwork.spigot.command;
|
||||
|
||||
import fr.votenetwork.spigot.VoteConfig;
|
||||
import fr.votenetwork.spigot.gui.VotePartyGui;
|
||||
import fr.votenetwork.spigot.voteparty.VoteParty;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandExecutor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class VoteGuiCommand implements CommandExecutor {
|
||||
|
||||
private final VoteConfig config;
|
||||
private final VoteParty voteParty;
|
||||
|
||||
public VoteGuiCommand(VoteConfig config, VoteParty voteParty) {
|
||||
this.config = config;
|
||||
this.voteParty = voteParty;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
|
||||
if (!(sender instanceof Player player)) {
|
||||
sender.sendMessage(ChatColor.RED + "Commande reservee aux joueurs.");
|
||||
return true;
|
||||
}
|
||||
VotePartyGui.open(player, config, voteParty);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package fr.votenetwork.spigot.gui;
|
||||
|
||||
import fr.votenetwork.spigot.VoteConfig;
|
||||
import fr.votenetwork.spigot.voteparty.VoteParty;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Builds and opens the VoteParty progress GUI: a progress item (slot 4) plus one item per
|
||||
* configured vote site (starting at slot 9), each opening its URL when clicked.
|
||||
*/
|
||||
public final class VotePartyGui {
|
||||
|
||||
public static final int PROGRESS_SLOT = 4;
|
||||
public static final int SITES_START_SLOT = 9;
|
||||
|
||||
private VotePartyGui() {
|
||||
}
|
||||
|
||||
public static void open(Player player, VoteConfig config, VoteParty voteParty) {
|
||||
List<VoteSite> sites = config.getVoteSites();
|
||||
int size = ((SITES_START_SLOT + sites.size() + 8) / 9) * 9;
|
||||
size = Math.max(size, 18);
|
||||
|
||||
VotePartyGuiHolder holder = new VotePartyGuiHolder();
|
||||
Inventory inventory = Bukkit.createInventory(holder, size, config.getGuiTitle());
|
||||
holder.setInventory(inventory);
|
||||
|
||||
int required = config.getVotePartyVotesRequired();
|
||||
int current = Math.min(voteParty.getCurrentVotes(), required);
|
||||
|
||||
ItemStack progressItem = new ItemStack(materialOrFallback(config.getGuiProgressItemMaterial()));
|
||||
ItemMeta progressMeta = progressItem.getItemMeta();
|
||||
if (progressMeta != null) {
|
||||
progressMeta.setDisplayName(config.getGuiProgressItemName());
|
||||
progressMeta.setLore(config.getGuiProgressItemLore().stream()
|
||||
.map(line -> line.replace("%current%", String.valueOf(current))
|
||||
.replace("%required%", String.valueOf(required)))
|
||||
.toList());
|
||||
progressItem.setItemMeta(progressMeta);
|
||||
}
|
||||
inventory.setItem(PROGRESS_SLOT, progressItem);
|
||||
|
||||
int slot = SITES_START_SLOT;
|
||||
for (VoteSite site : sites) {
|
||||
ItemStack item = new ItemStack(site.getMaterial());
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
if (meta != null) {
|
||||
meta.setDisplayName(site.getName());
|
||||
meta.setLore(site.getLore());
|
||||
item.setItemMeta(meta);
|
||||
}
|
||||
inventory.setItem(slot, item);
|
||||
slot++;
|
||||
}
|
||||
|
||||
player.openInventory(inventory);
|
||||
}
|
||||
|
||||
private static org.bukkit.Material materialOrFallback(String name) {
|
||||
try {
|
||||
return org.bukkit.Material.valueOf(name.toUpperCase(java.util.Locale.ROOT));
|
||||
} catch (IllegalArgumentException e) {
|
||||
return org.bukkit.Material.NETHER_STAR;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package fr.votenetwork.spigot.gui;
|
||||
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.InventoryHolder;
|
||||
|
||||
/**
|
||||
* Marker holder used to recognize our own GUI inventory in InventoryClickEvent, so the
|
||||
* listener never touches clicks happening in unrelated inventories (chests, other plugins'
|
||||
* GUIs, etc.).
|
||||
*/
|
||||
public class VotePartyGuiHolder implements InventoryHolder {
|
||||
|
||||
private Inventory inventory;
|
||||
|
||||
@Override
|
||||
public Inventory getInventory() {
|
||||
return inventory;
|
||||
}
|
||||
|
||||
public void setInventory(Inventory inventory) {
|
||||
this.inventory = inventory;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package fr.votenetwork.spigot.gui;
|
||||
|
||||
import fr.votenetwork.spigot.VoteConfig;
|
||||
import net.md_5.bungee.api.chat.ClickEvent;
|
||||
import net.md_5.bungee.api.chat.TextComponent;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Bukkit cannot open a browser directly from an inventory click, so a click on a vote-site
|
||||
* item closes the GUI and sends a clickable chat message (ClickEvent OPEN_URL) instead.
|
||||
*/
|
||||
public class VotePartyGuiListener implements Listener {
|
||||
|
||||
private final VoteConfig config;
|
||||
|
||||
public VotePartyGuiListener(VoteConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onInventoryClick(InventoryClickEvent event) {
|
||||
if (!(event.getInventory().getHolder() instanceof VotePartyGuiHolder)) {
|
||||
return;
|
||||
}
|
||||
event.setCancelled(true);
|
||||
|
||||
int slot = event.getRawSlot();
|
||||
if (slot < VotePartyGui.SITES_START_SLOT || !(event.getWhoClicked() instanceof Player player)) {
|
||||
return;
|
||||
}
|
||||
|
||||
List<VoteSite> sites = config.getVoteSites();
|
||||
int index = slot - VotePartyGui.SITES_START_SLOT;
|
||||
if (index < 0 || index >= sites.size()) {
|
||||
return;
|
||||
}
|
||||
|
||||
VoteSite site = sites.get(index);
|
||||
if (site.getUrl() == null || site.getUrl().isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
player.closeInventory();
|
||||
TextComponent message = new TextComponent(config.getGuiClickMessage() + " " + site.getUrl());
|
||||
message.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, site.getUrl()));
|
||||
player.spigot().sendMessage(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package fr.votenetwork.spigot.gui;
|
||||
|
||||
import org.bukkit.Material;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A single vote-site entry configured in config.yml (gui.vote-sites): the display item and
|
||||
* the URL opened (via a clickable chat message) when the player clicks it in the GUI.
|
||||
*/
|
||||
public class VoteSite {
|
||||
|
||||
private final String name;
|
||||
private final Material material;
|
||||
private final String url;
|
||||
private final List<String> lore;
|
||||
|
||||
public VoteSite(String name, Material material, String url, List<String> lore) {
|
||||
this.name = name;
|
||||
this.material = material;
|
||||
this.url = url;
|
||||
this.lore = lore;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Material getMaterial() {
|
||||
return material;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public List<String> getLore() {
|
||||
return lore;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package fr.votenetwork.spigot.placeholder;
|
||||
|
||||
import fr.votenetwork.spigot.MaintenanceState;
|
||||
import fr.votenetwork.spigot.VoteConfig;
|
||||
import fr.votenetwork.spigot.VoteNetworkSpigot;
|
||||
import fr.votenetwork.spigot.storage.PendingVoteStore;
|
||||
import fr.votenetwork.spigot.voteparty.VoteParty;
|
||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* Placeholders PlaceholderAPI :
|
||||
* %votenetwork_voteparty_current%
|
||||
* %votenetwork_voteparty_required%
|
||||
* %votenetwork_voteparty_remaining%
|
||||
* %votenetwork_maintenance% -> "true"/"false"
|
||||
* %votenetwork_pending% -> votes en attente du joueur (rafraichi en arriere-plan)
|
||||
*
|
||||
* Utilisables dans n'importe quel plugin de scoreboard/GUI qui supporte PlaceholderAPI
|
||||
* (%votenetwork_...%, pas %current%/%required% bruts : ces derniers doivent etre configures
|
||||
* comme placeholders dans le plugin de scoreboard lui-meme).
|
||||
*/
|
||||
public class VoteNetworkPlaceholders extends PlaceholderExpansion {
|
||||
|
||||
private final VoteNetworkSpigot plugin;
|
||||
private final VoteConfig config;
|
||||
private final PendingVoteStore pendingVoteStore;
|
||||
private final VoteParty voteParty;
|
||||
private final MaintenanceState maintenanceState;
|
||||
|
||||
private final Map<String, Integer> pendingVotesCache = new ConcurrentHashMap<>();
|
||||
|
||||
public VoteNetworkPlaceholders(VoteNetworkSpigot plugin, VoteConfig config, PendingVoteStore pendingVoteStore,
|
||||
VoteParty voteParty, MaintenanceState maintenanceState) {
|
||||
this.plugin = plugin;
|
||||
this.config = config;
|
||||
this.pendingVoteStore = pendingVoteStore;
|
||||
this.voteParty = voteParty;
|
||||
this.maintenanceState = maintenanceState;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getIdentifier() {
|
||||
return "votenetwork";
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getAuthor() {
|
||||
return "VoteNetwork";
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getVersion() {
|
||||
return plugin.getDescription().getVersion();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean persist() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String onRequest(OfflinePlayer player, @NotNull String params) {
|
||||
switch (params.toLowerCase()) {
|
||||
case "voteparty_current":
|
||||
return String.valueOf(voteParty.getCurrentVotes());
|
||||
case "voteparty_required":
|
||||
return String.valueOf(config.getVotePartyVotesRequired());
|
||||
case "voteparty_remaining":
|
||||
return String.valueOf(Math.max(0, config.getVotePartyVotesRequired() - voteParty.getCurrentVotes()));
|
||||
case "maintenance":
|
||||
return String.valueOf(maintenanceState.isStopped());
|
||||
case "pending":
|
||||
if (player == null) {
|
||||
return "0";
|
||||
}
|
||||
return String.valueOf(getPendingVotesCached(player));
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private int getPendingVotesCached(OfflinePlayer player) {
|
||||
String key = player.getUniqueId().toString();
|
||||
Integer cached = pendingVotesCache.get(key);
|
||||
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () ->
|
||||
pendingVoteStore.peek(
|
||||
player.getUniqueId(),
|
||||
votes -> pendingVotesCache.put(key, votes),
|
||||
exception -> { /* on garde la derniere valeur en cache en cas d'erreur */ }
|
||||
)
|
||||
);
|
||||
|
||||
return cached != null ? cached : 0;
|
||||
}
|
||||
}
|
||||
@@ -73,16 +73,24 @@ public class MySqlPendingVoteStore implements PendingVoteStore {
|
||||
}
|
||||
|
||||
private void createTableIfNotExists() {
|
||||
String sql = "CREATE TABLE IF NOT EXISTS nb_votes_attente (" +
|
||||
String globalSql = "CREATE TABLE IF NOT EXISTS nb_votes_attente (" +
|
||||
"player_uuid VARCHAR(36) NOT NULL PRIMARY KEY," +
|
||||
"player_name VARCHAR(16) NOT NULL," +
|
||||
"nombre_votes INT DEFAULT 0" +
|
||||
")";
|
||||
String perServerSql = "CREATE TABLE IF NOT EXISTS nb_votes_attente_serveur (" +
|
||||
"player_uuid VARCHAR(36) NOT NULL," +
|
||||
"server_name VARCHAR(64) NOT NULL," +
|
||||
"player_name VARCHAR(16) NOT NULL," +
|
||||
"nombre_votes INT DEFAULT 0," +
|
||||
"PRIMARY KEY (player_uuid, server_name)" +
|
||||
")";
|
||||
try (Connection connection = dataSource.getConnection();
|
||||
Statement statement = connection.createStatement()) {
|
||||
statement.execute(sql);
|
||||
statement.execute(globalSql);
|
||||
statement.execute(perServerSql);
|
||||
} catch (SQLException e) {
|
||||
plugin.getLogger().severe("Impossible de creer la table nb_votes_attente: " + e.getMessage());
|
||||
plugin.getLogger().severe("Impossible de creer les tables de votes en attente: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,6 +100,14 @@ public class MySqlPendingVoteStore implements PendingVoteStore {
|
||||
onError.accept(new SQLException("Pool MySQL indisponible (echec de connexion au demarrage)"));
|
||||
return;
|
||||
}
|
||||
if (config.isPerServerScope()) {
|
||||
fetchAndClearPerServer(uuid, onResult, onError);
|
||||
} else {
|
||||
fetchAndClearGlobal(uuid, onResult, onError);
|
||||
}
|
||||
}
|
||||
|
||||
private void fetchAndClearGlobal(UUID uuid, IntConsumer onResult, Consumer<Exception> onError) {
|
||||
String selectSql = "SELECT nombre_votes FROM nb_votes_attente WHERE player_uuid = ?";
|
||||
String resetSql = "UPDATE nb_votes_attente SET nombre_votes = 0 WHERE player_uuid = ?";
|
||||
|
||||
@@ -120,16 +136,59 @@ public class MySqlPendingVoteStore implements PendingVoteStore {
|
||||
}
|
||||
}
|
||||
|
||||
private void fetchAndClearPerServer(UUID uuid, IntConsumer onResult, Consumer<Exception> onError) {
|
||||
if (config.getServerName().isEmpty()) {
|
||||
onError.accept(new SQLException("storage.pending-votes-scope=per-server mais server-name est vide dans config.yml"));
|
||||
return;
|
||||
}
|
||||
String selectSql = "SELECT nombre_votes FROM nb_votes_attente_serveur WHERE player_uuid = ? AND server_name = ?";
|
||||
String resetSql = "UPDATE nb_votes_attente_serveur SET nombre_votes = 0 WHERE player_uuid = ? AND server_name = ?";
|
||||
|
||||
try (Connection connection = dataSource.getConnection()) {
|
||||
int pendingVotes = 0;
|
||||
|
||||
try (PreparedStatement select = connection.prepareStatement(selectSql)) {
|
||||
select.setString(1, uuid.toString());
|
||||
select.setString(2, config.getServerName());
|
||||
try (ResultSet resultSet = select.executeQuery()) {
|
||||
if (resultSet.next()) {
|
||||
pendingVotes = resultSet.getInt("nombre_votes");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (pendingVotes > 0) {
|
||||
try (PreparedStatement reset = connection.prepareStatement(resetSql)) {
|
||||
reset.setString(1, uuid.toString());
|
||||
reset.setString(2, config.getServerName());
|
||||
reset.executeUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
onResult.accept(pendingVotes);
|
||||
} catch (SQLException e) {
|
||||
onError.accept(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void peek(UUID uuid, IntConsumer onResult, Consumer<Exception> onError) {
|
||||
if (!isConnected()) {
|
||||
onError.accept(new SQLException("Pool MySQL indisponible (echec de connexion au demarrage)"));
|
||||
return;
|
||||
}
|
||||
String sql = "SELECT nombre_votes FROM nb_votes_attente WHERE player_uuid = ?";
|
||||
|
||||
boolean perServer = config.isPerServerScope();
|
||||
String sql = perServer
|
||||
? "SELECT nombre_votes FROM nb_votes_attente_serveur WHERE player_uuid = ? AND server_name = ?"
|
||||
: "SELECT nombre_votes FROM nb_votes_attente WHERE player_uuid = ?";
|
||||
|
||||
try (Connection connection = dataSource.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(sql)) {
|
||||
statement.setString(1, uuid.toString());
|
||||
if (perServer) {
|
||||
statement.setString(2, config.getServerName());
|
||||
}
|
||||
try (ResultSet resultSet = statement.executeQuery()) {
|
||||
onResult.accept(resultSet.next() ? resultSet.getInt("nombre_votes") : 0);
|
||||
}
|
||||
|
||||
@@ -8,6 +8,14 @@
|
||||
# partage automatiquement avec le fichier YAML du proxy Velocity si vous etes en reseau.
|
||||
storage:
|
||||
type: mysql
|
||||
# Doit correspondre EXACTEMENT a pending-votes.scope cote Velocity (config.properties) :
|
||||
# global (par defaut) : /claim consomme le compteur partage entre tous les serveurs.
|
||||
# per-server : /claim ne consomme que le compteur de CE serveur (voir server-name ci-dessous).
|
||||
pending-votes-scope: global
|
||||
|
||||
# Nom de CE serveur, IDENTIQUE a celui utilise dans direct-vote.servers cote Velocity.
|
||||
# Uniquement necessaire si storage.pending-votes-scope=per-server.
|
||||
server-name: ""
|
||||
|
||||
mysql:
|
||||
host: 127.0.0.1
|
||||
@@ -39,6 +47,30 @@ voteparty:
|
||||
progress-message: "&b[Vote] &f%current%&7/&f%required% &fvotes avant le prochain VoteParty !"
|
||||
triggered-message: "&a[Vote] &fVoteParty declenche ! Profitez des recompenses !"
|
||||
|
||||
# GUI /votegui : affiche la progression du VoteParty et des items cliquables vers les sites
|
||||
# de vote (ouvre le lien via un message chat cliquable, Bukkit ne peut pas ouvrir un navigateur
|
||||
# directement depuis un clic d'inventaire).
|
||||
gui:
|
||||
title: "&8VoteNetwork"
|
||||
progress-item:
|
||||
material: NETHER_STAR
|
||||
name: "&b&lVoteParty"
|
||||
lore:
|
||||
- "&7Progression actuelle :"
|
||||
- "&f%current%&7/&f%required% &7votes"
|
||||
click-message: "&a[Vote] Clique sur le lien pour voter :"
|
||||
vote-sites:
|
||||
- name: "&aVoter sur le site 1"
|
||||
material: PAPER
|
||||
url: "https://example.com/vote/site1"
|
||||
lore:
|
||||
- "&7Clique pour voter !"
|
||||
- name: "&aVoter sur le site 2"
|
||||
material: BOOK
|
||||
url: "https://example.com/vote/site2"
|
||||
lore:
|
||||
- "&7Clique pour voter !"
|
||||
|
||||
messages:
|
||||
direct-vote: "&a[Vote] &fMerci pour ton vote !"
|
||||
claim-success: "&a[Vote] &fVous avez recupere &e%amount% &fvote(s) en attente !"
|
||||
|
||||
@@ -2,8 +2,9 @@ name: votenetwork
|
||||
main: fr.votenetwork.spigot.VoteNetworkSpigot
|
||||
version: ${project.version}
|
||||
api-version: 1.18
|
||||
author: VoteNetwork
|
||||
author: Sar_Tron
|
||||
description: Reception des votes reseau (direct ou en attente) avec VoteParty local.
|
||||
softdepend: [PlaceholderAPI]
|
||||
|
||||
commands:
|
||||
claim:
|
||||
@@ -13,6 +14,9 @@ commands:
|
||||
description: Administration du systeme de vote (maintenance, test du stockage).
|
||||
usage: /vote <stop|start|testdb>
|
||||
permission: votenetwork.admin
|
||||
votegui:
|
||||
description: Ouvre le GUI de progression du VoteParty avec les liens de vote.
|
||||
usage: /votegui
|
||||
|
||||
permissions:
|
||||
votenetwork.admin:
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
package fr.votenetwork.spigot;
|
||||
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class VoteConfigTest {
|
||||
|
||||
@Test
|
||||
void defaultsAreUsedWhenKeysAbsent() {
|
||||
VoteNetworkSpigot plugin = mock(VoteNetworkSpigot.class);
|
||||
when(plugin.getConfig()).thenReturn(new YamlConfiguration());
|
||||
|
||||
VoteConfig config = new VoteConfig(plugin);
|
||||
|
||||
assertTrue(config.isMysqlStorage());
|
||||
assertEquals("127.0.0.1", config.getMysqlHost());
|
||||
assertEquals(3306, config.getMysqlPort());
|
||||
assertEquals("votenetwork", config.getMysqlDatabase());
|
||||
assertEquals(100, config.getVotePartyVotesRequired());
|
||||
assertFalse(config.isPerServerScope());
|
||||
assertFalse(config.isVoteBroadcastEnabled());
|
||||
}
|
||||
|
||||
@Test
|
||||
void mysqlStorageIsFalseOnlyForYaml() {
|
||||
YamlConfiguration yaml = new YamlConfiguration();
|
||||
yaml.set("storage.type", "yaml");
|
||||
VoteNetworkSpigot plugin = mock(VoteNetworkSpigot.class);
|
||||
when(plugin.getConfig()).thenReturn(yaml);
|
||||
|
||||
VoteConfig config = new VoteConfig(plugin);
|
||||
|
||||
assertFalse(config.isMysqlStorage());
|
||||
assertEquals("yaml", config.getStorageType());
|
||||
}
|
||||
|
||||
@Test
|
||||
void perServerScopeReadFromConfig() {
|
||||
YamlConfiguration yaml = new YamlConfiguration();
|
||||
yaml.set("storage.pending-votes-scope", "per-server");
|
||||
yaml.set("server-name", "gen1");
|
||||
VoteNetworkSpigot plugin = mock(VoteNetworkSpigot.class);
|
||||
when(plugin.getConfig()).thenReturn(yaml);
|
||||
|
||||
VoteConfig config = new VoteConfig(plugin);
|
||||
|
||||
assertTrue(config.isPerServerScope());
|
||||
assertEquals("gen1", config.getServerName());
|
||||
}
|
||||
|
||||
@Test
|
||||
void messagesAreColorizedAndPlaceholdersPreserved() {
|
||||
YamlConfiguration yaml = new YamlConfiguration();
|
||||
yaml.set("messages.claim-success", "&aTu as recupere &e%amount% &avotes !");
|
||||
VoteNetworkSpigot plugin = mock(VoteNetworkSpigot.class);
|
||||
when(plugin.getConfig()).thenReturn(yaml);
|
||||
|
||||
VoteConfig config = new VoteConfig(plugin);
|
||||
String message = config.getMessageClaimSuccess();
|
||||
|
||||
assertFalse(message.contains("&a"), "les codes couleur '&' doivent etre traduits");
|
||||
assertTrue(message.contains("%amount%"), "le placeholder %amount% doit rester intact pour etre remplace plus tard");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package fr.votenetwork.spigot.storage;
|
||||
|
||||
import fr.votenetwork.spigot.VoteNetworkSpigot;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class YamlPendingVoteStoreTest {
|
||||
|
||||
@Test
|
||||
void peekReturnsZeroWhenNothingStored(@TempDir File dataFolder) {
|
||||
YamlPendingVoteStore store = newStore(dataFolder);
|
||||
UUID uuid = UUID.randomUUID();
|
||||
|
||||
AtomicInteger result = new AtomicInteger(-1);
|
||||
store.peek(uuid, result::set, e -> { throw new AssertionError(e); });
|
||||
|
||||
assertEquals(0, result.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void fetchAndClearResetsCounterToZeroAfterReading(@TempDir File dataFolder) throws Exception {
|
||||
YamlPendingVoteStore store = newStore(dataFolder);
|
||||
UUID uuid = UUID.randomUUID();
|
||||
|
||||
writeInitialVotes(dataFolder, uuid, 3);
|
||||
|
||||
AtomicInteger firstRead = new AtomicInteger(-1);
|
||||
store.fetchAndClear(uuid, firstRead::set, e -> { throw new AssertionError(e); });
|
||||
assertEquals(3, firstRead.get());
|
||||
|
||||
AtomicInteger secondRead = new AtomicInteger(-1);
|
||||
store.peek(uuid, secondRead::set, e -> { throw new AssertionError(e); });
|
||||
assertEquals(0, secondRead.get(), "fetchAndClear doit remettre le compteur a 0");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testConnectionReportsWritableDirectory(@TempDir File dataFolder) {
|
||||
YamlPendingVoteStore store = newStore(dataFolder);
|
||||
|
||||
AtomicReference<Boolean> success = new AtomicReference<>();
|
||||
store.testConnection((ok, message) -> success.set(ok));
|
||||
|
||||
assertEquals(Boolean.TRUE, success.get(), "un dossier temporaire accessible en ecriture doit etre rapporte comme fonctionnel");
|
||||
}
|
||||
|
||||
private YamlPendingVoteStore newStore(File dataFolder) {
|
||||
VoteNetworkSpigot plugin = mock(VoteNetworkSpigot.class);
|
||||
when(plugin.getDataFolder()).thenReturn(dataFolder);
|
||||
return new YamlPendingVoteStore(plugin);
|
||||
}
|
||||
|
||||
private void writeInitialVotes(File dataFolder, UUID uuid, int votes) throws Exception {
|
||||
org.bukkit.configuration.file.YamlConfiguration yaml = new org.bukkit.configuration.file.YamlConfiguration();
|
||||
yaml.set("players." + uuid + ".votes", votes);
|
||||
File file = new File(dataFolder, "pending-votes.yml");
|
||||
yaml.save(file);
|
||||
}
|
||||
}
|
||||
+7
-1
@@ -7,7 +7,7 @@
|
||||
<parent>
|
||||
<groupId>fr.votenetwork</groupId>
|
||||
<artifactId>votenetwork-parent</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<version>1.0.1</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>votenetwork-velocity</artifactId>
|
||||
@@ -36,6 +36,12 @@
|
||||
<version>2.10.1</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
|
||||
@@ -57,16 +57,24 @@ public class MySqlPendingVoteStore implements PendingVoteStore {
|
||||
}
|
||||
|
||||
private void createTableIfNotExists() {
|
||||
String sql = "CREATE TABLE IF NOT EXISTS nb_votes_attente (" +
|
||||
String globalSql = "CREATE TABLE IF NOT EXISTS nb_votes_attente (" +
|
||||
"player_uuid VARCHAR(36) NOT NULL PRIMARY KEY," +
|
||||
"player_name VARCHAR(16) NOT NULL," +
|
||||
"nombre_votes INT DEFAULT 0" +
|
||||
")";
|
||||
String perServerSql = "CREATE TABLE IF NOT EXISTS nb_votes_attente_serveur (" +
|
||||
"player_uuid VARCHAR(36) NOT NULL," +
|
||||
"server_name VARCHAR(64) NOT NULL," +
|
||||
"player_name VARCHAR(16) NOT NULL," +
|
||||
"nombre_votes INT DEFAULT 0," +
|
||||
"PRIMARY KEY (player_uuid, server_name)" +
|
||||
")";
|
||||
try (Connection connection = dataSource.getConnection();
|
||||
Statement statement = connection.createStatement()) {
|
||||
statement.execute(sql);
|
||||
statement.execute(globalSql);
|
||||
statement.execute(perServerSql);
|
||||
} catch (SQLException e) {
|
||||
logger.error("Impossible de creer la table nb_votes_attente", e);
|
||||
logger.error("Impossible de creer les tables de votes en attente", e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -77,20 +85,46 @@ public class MySqlPendingVoteStore implements PendingVoteStore {
|
||||
logger.error("Impossible d'enregistrer le vote en attente pour {} : pool MySQL indisponible", playerName);
|
||||
return;
|
||||
}
|
||||
String sql = "INSERT INTO nb_votes_attente (player_uuid, player_name, nombre_votes) " +
|
||||
"VALUES (?, ?, 1) " +
|
||||
"ON DUPLICATE KEY UPDATE nombre_votes = nombre_votes + 1, player_name = VALUES(player_name)";
|
||||
try (Connection connection = dataSource.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(sql)) {
|
||||
statement.setString(1, uuid.toString());
|
||||
statement.setString(2, playerName);
|
||||
statement.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
logger.error("Erreur lors de l'insertion du vote en attente pour {}", playerName, e);
|
||||
if (config.isPerServerScope()) {
|
||||
addPendingVotePerServer(uuid, playerName);
|
||||
} else {
|
||||
addPendingVoteGlobal(uuid, playerName);
|
||||
}
|
||||
}, asyncExecutor);
|
||||
}
|
||||
|
||||
private void addPendingVoteGlobal(UUID uuid, String playerName) {
|
||||
String sql = "INSERT INTO nb_votes_attente (player_uuid, player_name, nombre_votes) " +
|
||||
"VALUES (?, ?, 1) " +
|
||||
"ON DUPLICATE KEY UPDATE nombre_votes = nombre_votes + 1, player_name = VALUES(player_name)";
|
||||
try (Connection connection = dataSource.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(sql)) {
|
||||
statement.setString(1, uuid.toString());
|
||||
statement.setString(2, playerName);
|
||||
statement.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
logger.error("Erreur lors de l'insertion du vote en attente pour {}", playerName, e);
|
||||
}
|
||||
}
|
||||
|
||||
private void addPendingVotePerServer(UUID uuid, String playerName) {
|
||||
String sql = "INSERT INTO nb_votes_attente_serveur (player_uuid, server_name, player_name, nombre_votes) " +
|
||||
"VALUES (?, ?, ?, 1) " +
|
||||
"ON DUPLICATE KEY UPDATE nombre_votes = nombre_votes + 1, player_name = VALUES(player_name)";
|
||||
try (Connection connection = dataSource.getConnection();
|
||||
PreparedStatement statement = connection.prepareStatement(sql)) {
|
||||
for (String server : config.getDirectVoteServers()) {
|
||||
statement.setString(1, uuid.toString());
|
||||
statement.setString(2, server);
|
||||
statement.setString(3, playerName);
|
||||
statement.addBatch();
|
||||
}
|
||||
statement.executeBatch();
|
||||
} catch (SQLException e) {
|
||||
logger.error("Erreur lors de l'insertion du vote en attente (per-server) pour {}", playerName, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (dataSource != null) {
|
||||
|
||||
@@ -26,6 +26,7 @@ public class VoteConfig {
|
||||
private String password;
|
||||
private int poolSize;
|
||||
private Set<String> directVoteServers;
|
||||
private String pendingVotesScope;
|
||||
|
||||
public VoteConfig(Path dataDirectory, Logger logger) {
|
||||
this.dataDirectory = dataDirectory;
|
||||
@@ -45,6 +46,8 @@ public class VoteConfig {
|
||||
}
|
||||
}
|
||||
|
||||
mergeMissingConfigKeys(configFile);
|
||||
|
||||
try (InputStream in = Files.newInputStream(configFile)) {
|
||||
properties.load(in);
|
||||
}
|
||||
@@ -63,11 +66,69 @@ public class VoteConfig {
|
||||
.filter(s -> !s.isEmpty())
|
||||
.map(s -> s.toLowerCase(Locale.ROOT))
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
this.pendingVotesScope = properties.getProperty("pending-votes.scope", "global");
|
||||
if (isPerServerScope() && directVoteServers.isEmpty()) {
|
||||
logger.warn("pending-votes.scope=per-server mais direct-vote.servers est vide : bascule sur 'global'. "
|
||||
+ "Renseignez direct-vote.servers pour utiliser le mode per-server.");
|
||||
this.pendingVotesScope = "global";
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.error("Impossible de charger la configuration Velocity", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ajoute a la fin de config.properties les blocs (commentaires + cle=valeur) du fichier par
|
||||
* defaut du jar dont la cle est absente du fichier existant (nouvelles options apportees par
|
||||
* une mise a jour du plugin), sans jamais toucher aux lignes/valeurs deja presentes. Copie le
|
||||
* bloc entier tel quel (donc avec son explication en commentaire), pas juste "cle=valeur".
|
||||
*/
|
||||
private void mergeMissingConfigKeys(Path configFile) throws IOException {
|
||||
String defaultsText;
|
||||
try (InputStream in = getClass().getClassLoader().getResourceAsStream("config.properties")) {
|
||||
if (in == null) {
|
||||
return;
|
||||
}
|
||||
defaultsText = new String(in.readAllBytes(), java.nio.charset.StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
Properties existing = new Properties();
|
||||
try (InputStream in = Files.newInputStream(configFile)) {
|
||||
existing.load(in);
|
||||
}
|
||||
|
||||
String[] blocks = defaultsText.split("\\r?\\n\\s*\\r?\\n");
|
||||
StringBuilder toAppend = new StringBuilder();
|
||||
|
||||
for (String block : blocks) {
|
||||
java.util.List<String> keysInBlock = new java.util.ArrayList<>();
|
||||
for (String line : block.split("\\r?\\n")) {
|
||||
String trimmed = line.trim();
|
||||
if (trimmed.isEmpty() || trimmed.startsWith("#")) {
|
||||
continue;
|
||||
}
|
||||
int eq = trimmed.indexOf('=');
|
||||
if (eq > 0) {
|
||||
keysInBlock.add(trimmed.substring(0, eq).trim());
|
||||
}
|
||||
}
|
||||
|
||||
if (!keysInBlock.isEmpty() && keysInBlock.stream().noneMatch(existing::containsKey)) {
|
||||
toAppend.append(block.stripTrailing()).append(System.lineSeparator()).append(System.lineSeparator());
|
||||
}
|
||||
}
|
||||
|
||||
if (toAppend.length() > 0) {
|
||||
String addition = System.lineSeparator()
|
||||
+ "# --- nouvelles options ajoutees par une mise a jour du plugin (voir description ci-dessous) ---"
|
||||
+ System.lineSeparator() + System.lineSeparator()
|
||||
+ toAppend;
|
||||
Files.writeString(configFile, addition, java.nio.file.StandardOpenOption.APPEND);
|
||||
logger.info("config.properties mis a jour avec les nouvelles options manquantes (valeurs existantes conservees).");
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isMysqlStorage() {
|
||||
return !"yaml".equalsIgnoreCase(storageType);
|
||||
}
|
||||
@@ -81,6 +142,19 @@ public class VoteConfig {
|
||||
return directVoteServers.isEmpty() || directVoteServers.contains(serverName.toLowerCase(Locale.ROOT));
|
||||
}
|
||||
|
||||
/**
|
||||
* true si chaque serveur doit avoir son propre compteur de votes en attente independant
|
||||
* (un /claim sur un serveur ne remet a 0 que ce serveur-la). Sinon (par defaut), un seul
|
||||
* compteur est partage entre tous les serveurs : /claim le remet a 0 partout a la fois.
|
||||
*/
|
||||
public boolean isPerServerScope() {
|
||||
return "per-server".equalsIgnoreCase(pendingVotesScope);
|
||||
}
|
||||
|
||||
public Set<String> getDirectVoteServers() {
|
||||
return directVoteServers;
|
||||
}
|
||||
|
||||
public String getJdbcUrl() {
|
||||
return "jdbc:mysql://" + host + ":" + port + "/" + database
|
||||
+ "?useSSL=false&autoReconnect=true&characterEncoding=utf8";
|
||||
|
||||
@@ -14,9 +14,9 @@ import java.nio.file.Path;
|
||||
@Plugin(
|
||||
id = "votenetwork",
|
||||
name = "VoteNetwork",
|
||||
version = "1.0.0",
|
||||
version = "1.0.1",
|
||||
description = "Systeme de vote reseau Velocity <-> Spigot",
|
||||
authors = {"VoteNetwork"}
|
||||
authors = {"Sar_Tron"}
|
||||
)
|
||||
public class VoteNetworkVelocity {
|
||||
|
||||
|
||||
@@ -20,3 +20,17 @@ mysql.pool-size=5
|
||||
# Laisser vide = tous les serveurs sont consideres comme ayant le plugin (deconseille des
|
||||
# qu'un lobby/hub sans VoteNetwork-Spigot existe sur le reseau).
|
||||
direct-vote.servers=gen1,gen2
|
||||
|
||||
# Portee des votes en attente (joueur hors ligne au moment du vote), uniquement en storage.type=mysql :
|
||||
# global (par defaut) : un seul compteur partage entre tous les serveurs. Un /claim sur
|
||||
# N'IMPORTE QUEL serveur remet le compteur a 0 pour TOUS les serveurs a la fois.
|
||||
# per-server : chaque serveur de direct-vote.servers a son propre compteur independant. Un
|
||||
# vote hors ligne incremente le compteur de CHAQUE serveur de la liste. Un /claim
|
||||
# sur le serveur 1 ne remet a 0 QUE le compteur du serveur 1 : le joueur garde ses
|
||||
# votes en attente sur le serveur 2 et peut les reclamer separement la-bas.
|
||||
# Exemple concret en per-server : le joueur vote 2 fois hors ligne -> 2 votes en attente sur
|
||||
# CHAQUE serveur. Il fait /claim sur le serveur 1 -> recoit 2 recompenses, serveur 1 repasse a 0,
|
||||
# serveur 2 reste a 2. S'il revote une fois -> serveur 1 = 1, serveur 2 = 3.
|
||||
# Necessite direct-vote.servers rempli ET que chaque serveur Spigot declare son server-name
|
||||
# (config.yml) correspondant exactement a un nom de cette liste.
|
||||
pending-votes.scope=global
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
package fr.votenetwork.velocity;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.slf4j.helpers.NOPLogger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class VoteConfigTest {
|
||||
|
||||
@Test
|
||||
void loadCopiesDefaultsWhenFileMissing(@TempDir Path dataDir) {
|
||||
VoteConfig config = new VoteConfig(dataDir, NOPLogger.NOP_LOGGER);
|
||||
config.load();
|
||||
|
||||
assertTrue(config.isMysqlStorage());
|
||||
assertEquals("jdbc:mysql://127.0.0.1:3306/votenetwork?useSSL=false&autoReconnect=true&characterEncoding=utf8",
|
||||
config.getJdbcUrl());
|
||||
assertTrue(Files.exists(dataDir.resolve("config.properties")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void perServerScopeFallsBackToGlobalWhenServerListEmpty(@TempDir Path dataDir) throws IOException {
|
||||
writeConfig(dataDir, """
|
||||
storage.type=mysql
|
||||
mysql.host=127.0.0.1
|
||||
mysql.port=3306
|
||||
mysql.database=votenetwork
|
||||
mysql.user=root
|
||||
mysql.password=changeme
|
||||
mysql.pool-size=5
|
||||
direct-vote.servers=
|
||||
pending-votes.scope=per-server
|
||||
""");
|
||||
|
||||
VoteConfig config = new VoteConfig(dataDir, NOPLogger.NOP_LOGGER);
|
||||
config.load();
|
||||
|
||||
assertFalse(config.isPerServerScope(), "sans direct-vote.servers, per-server doit retomber sur global");
|
||||
}
|
||||
|
||||
@Test
|
||||
void perServerScopeAppliesWhenServersListed(@TempDir Path dataDir) throws IOException {
|
||||
writeConfig(dataDir, """
|
||||
storage.type=mysql
|
||||
mysql.host=127.0.0.1
|
||||
mysql.port=3306
|
||||
mysql.database=votenetwork
|
||||
mysql.user=root
|
||||
mysql.password=changeme
|
||||
mysql.pool-size=5
|
||||
direct-vote.servers=gen1, GEN2
|
||||
pending-votes.scope=per-server
|
||||
""");
|
||||
|
||||
VoteConfig config = new VoteConfig(dataDir, NOPLogger.NOP_LOGGER);
|
||||
config.load();
|
||||
|
||||
assertTrue(config.isPerServerScope());
|
||||
assertTrue(config.isDirectVoteServer("gen1"));
|
||||
assertTrue(config.isDirectVoteServer("Gen2"), "la comparaison doit ignorer la casse");
|
||||
assertFalse(config.isDirectVoteServer("lobby"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void isDirectVoteServerAllowsAllWhenListEmpty(@TempDir Path dataDir) throws IOException {
|
||||
writeConfig(dataDir, """
|
||||
storage.type=mysql
|
||||
direct-vote.servers=
|
||||
""");
|
||||
|
||||
VoteConfig config = new VoteConfig(dataDir, NOPLogger.NOP_LOGGER);
|
||||
config.load();
|
||||
|
||||
assertTrue(config.isDirectVoteServer("n_importe_quoi"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void mergeAddsMissingKeysWithoutTouchingExistingOnes(@TempDir Path dataDir) throws IOException {
|
||||
writeConfig(dataDir, """
|
||||
mysql.password=ne-pas-toucher
|
||||
""");
|
||||
|
||||
VoteConfig config = new VoteConfig(dataDir, NOPLogger.NOP_LOGGER);
|
||||
config.load();
|
||||
|
||||
String fileContent = Files.readString(dataDir.resolve("config.properties"), StandardCharsets.UTF_8);
|
||||
assertTrue(fileContent.contains("mysql.password=ne-pas-toucher"), "la valeur existante ne doit jamais etre ecrasee");
|
||||
assertTrue(fileContent.contains("pending-votes.scope="), "les cles manquantes doivent etre ajoutees");
|
||||
assertEquals("ne-pas-toucher", config.getPassword());
|
||||
}
|
||||
|
||||
private void writeConfig(Path dataDir, String content) throws IOException {
|
||||
Files.createDirectories(dataDir);
|
||||
Files.writeString(dataDir.resolve("config.properties"), content, StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package fr.votenetwork.velocity;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.slf4j.helpers.NOPLogger;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class YamlPendingVoteStoreTest {
|
||||
|
||||
@Test
|
||||
void addPendingVoteCreatesEntryAndIncrements(@TempDir Path dataDir)
|
||||
throws ExecutionException, InterruptedException, TimeoutException, IOException {
|
||||
YamlPendingVoteStore store = new YamlPendingVoteStore(dataDir, NOPLogger.NOP_LOGGER);
|
||||
UUID uuid = UUID.fromString("069a79f4-44e9-4726-a5be-fca90e38aaf5");
|
||||
|
||||
store.addPendingVote(uuid, "Notch").get(5, TimeUnit.SECONDS);
|
||||
|
||||
String content = Files.readString(dataDir.resolve("pending-votes.yml"), StandardCharsets.UTF_8);
|
||||
assertTrue(content.contains(uuid.toString()));
|
||||
assertTrue(content.contains("name: Notch"));
|
||||
assertTrue(content.contains("votes: 1"));
|
||||
|
||||
store.addPendingVote(uuid, "Notch").get(5, TimeUnit.SECONDS);
|
||||
|
||||
content = Files.readString(dataDir.resolve("pending-votes.yml"), StandardCharsets.UTF_8);
|
||||
assertTrue(content.contains("votes: 2"), "un deuxieme vote hors ligne doit incrementer le compteur existant");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addPendingVoteHandlesTwoDifferentPlayersIndependently(@TempDir Path dataDir)
|
||||
throws ExecutionException, InterruptedException, TimeoutException, IOException {
|
||||
YamlPendingVoteStore store = new YamlPendingVoteStore(dataDir, NOPLogger.NOP_LOGGER);
|
||||
UUID uuidA = UUID.fromString("069a79f4-44e9-4726-a5be-fca90e38aaf5");
|
||||
UUID uuidB = UUID.fromString("853c80ef-3c37-49fd-aa49-938b674adae6");
|
||||
|
||||
store.addPendingVote(uuidA, "Sar_Tron").get(5, TimeUnit.SECONDS);
|
||||
store.addPendingVote(uuidB, "Notch").get(5, TimeUnit.SECONDS);
|
||||
|
||||
String content = Files.readString(dataDir.resolve("pending-votes.yml"), StandardCharsets.UTF_8);
|
||||
assertTrue(content.contains(uuidA.toString()));
|
||||
assertTrue(content.contains(uuidB.toString()));
|
||||
assertTrue(content.contains("name: Sar_Tron"));
|
||||
assertTrue(content.contains("name: Notch"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user