initial commit

This commit is contained in:
nessar committed 2022-04-27 06:22:09 +02:00
commit b891600251
7 files changed
+490

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
bin/*
*.jar
lib/*
+13
View File
@@ -0,0 +1,13 @@
{
"java.project.sourcePaths": [
"src"
],
"java.project.outputPath": "bin",
"java.project.referencedLibraries": [
"lib/**/*.jar"
],
"java.project.exportJar.targetPath": "/home/nessar/plugin_mc/servermc/plugins",
"yaml.schemas": {
"https://json.schemastore.org/bukkit-plugin.json": "file:///home/nessar/plugin_mc/koth/src/plugin.yml"
}
}
+122
View File
@@ -0,0 +1,122 @@
package fr.nessar;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Location;
import org.bukkit.entity.Player;
public class ClaimedZone {
private List<String> playerList;
private double xStart, zStart, xEnd, zEnd;
private boolean active;
private String name, world;
public ClaimedZone(Location minimal, Location maximal, String name) {
try {
if (minimal.getX() > maximal.getX()) {
this.xStart = maximal.getX();
this.xEnd = minimal.getX();
} else {
this.xStart = minimal.getX();
this.xEnd = maximal.getX();
}
if (minimal.getZ() > maximal.getZ()) {
this.zStart = maximal.getZ();
this.zEnd = minimal.getZ();
} else {
this.zStart = minimal.getZ();
this.zEnd = maximal.getZ();
}
this.world = minimal.getWorld().getName();
this.name = name;
this.active = false;
this.playerList = new ArrayList<>();
} catch (Exception e) {
System.out.println("null pointer new ClaimedZone");
}
}
public List<String> getPlayerList() {
return playerList;
}
public boolean isPlayerInZone(Player p) {
for (int i = 0; i < playerList.size(); i++) {
if (playerList.get(i).equals(p.getName())) {
return true;
}
}
return false;
}
public String getFirstPlayer() {
if (playerList.size() == 0) {
return null;
}
return playerList.get(0);
}
public boolean addPlayer(String pName) {
if (pName != null) {
playerList.add(pName);
return true;
}
return false;
}
public boolean deletePlayer(String pName) {
if (pName != null) {
for (int i = 0; i < playerList.size(); i++) {
if (playerList.get(i).equals(pName)) {
playerList.remove(i);
return true;
}
}
}
return false;
}
public String getName() {
return this.name;
}
public String getWorld() {
return world;
}
public double getXStart() {
return xStart;
}
public double getZStart() {
return zStart;
}
public double getXEnd() {
return xEnd;
}
public double getZEnd() {
return zEnd;
}
public void setActive(boolean x) {
this.active = x;
}
public boolean isInZone(Location loc) {
return (loc.getWorld().getName().equals(this.world) && loc.getX() >= this.xStart && loc.getX() <= this.xEnd + 1
&& loc.getZ() >= this.zStart
&& loc.getZ() <= this.zEnd + 1);
}
public boolean isActive() {
return this.active;
}
}
+60
View File
@@ -0,0 +1,60 @@
package fr.nessar;
import java.io.File;
import java.io.IOException;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import net.md_5.bungee.api.ChatColor;
public class ConfigManager {
private koth plugin = koth.getPlugin(koth.class);
// Files & File Configs Here
public FileConfiguration playerscfg;
public File playersfile;
// --------------------------
public void setup() {
if (!plugin.getDataFolder().exists()) {
plugin.getDataFolder().mkdir();
}
playersfile = new File(plugin.getDataFolder(), "players.yml");
if (!playersfile.exists()) {
try {
playersfile.createNewFile();
Bukkit.getServer().getConsoleSender()
.sendMessage(ChatColor.GREEN + "The players.yml file has been created");
} catch (IOException e) {
Bukkit.getServer().getConsoleSender()
.sendMessage(ChatColor.RED + "Could not create the players.yml file");
}
}
playerscfg = YamlConfiguration.loadConfiguration(playersfile);
}
public FileConfiguration getPlayers() {
return playerscfg;
}
public void savePlayers() {
try {
playerscfg.save(playersfile);
Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.AQUA + "The players.yml file has been saved");
} catch (IOException e) {
Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.RED + "Could not save the players.yml file");
}
}
public void reloadPlayers() {
playerscfg = YamlConfiguration.loadConfiguration(playersfile);
Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.BLUE + "The players.yml file has been reload");
}
}
+80
View File
@@ -0,0 +1,80 @@
package fr.nessar;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.nio.file.Files;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import org.bukkit.Bukkit;
import net.md_5.bungee.api.ChatColor;
public class dataManager {
private koth plugin = koth.getPlugin(koth.class);
// Files & File Configs Here
public File zoneFile;
public dataManager() {
if (!plugin.getDataFolder().exists()) {
plugin.getDataFolder().mkdir();
}
this.zoneFile = new File(plugin.getDataFolder(), "data.json");
if (!this.zoneFile.exists()) {
try {
this.zoneFile.createNewFile();
Bukkit.getServer().getConsoleSender()
.sendMessage(ChatColor.GREEN + "The data.json file has been created");
} catch (IOException e) {
Bukkit.getServer().getConsoleSender()
.sendMessage(ChatColor.RED + "Could not create the data.json file");
}
} else {
Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "data.json found");
}
}
public void saveZones(List<ClaimedZone> zones) {
Gson gson = new Gson();
try {
Files.write(this.zoneFile.toPath(), gson.toJson(zones).getBytes());
} catch (IOException e) {
Bukkit.getServer().getConsoleSender()
.sendMessage(ChatColor.RED + "Couldn't save in the data.json file");
}
}
public List<ClaimedZone> loadZones() {
Gson gson = new Gson();
List<ClaimedZone> ret = new ArrayList<>();
Type listofClaimedZone = new TypeToken<ArrayList<ClaimedZone>>() {
}.getType();
String jsonString = null;
try {
byte[] bytes = Files.readAllBytes(zoneFile.toPath());
jsonString = new String(bytes);
} catch (IOException e1) {
Bukkit.getServer().getConsoleSender()
.sendMessage(ChatColor.RED + "Couldn't read the data.json file");
}
if (jsonString != null) {
Bukkit.getServer().getConsoleSender()
.sendMessage(ChatColor.GREEN + jsonString);
try {
ret = gson.fromJson(jsonString, listofClaimedZone);
} catch (JsonSyntaxException e) {
Bukkit.getServer().getConsoleSender()
.sendMessage(ChatColor.RED + "Couldn't convert to json data.json");
}
}
return ret;
}
}
+195
View File
@@ -0,0 +1,195 @@
package fr.nessar;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import com.sk89q.worldedit.bukkit.selections.Selection;
import net.md_5.bungee.api.ChatColor;
public class koth extends JavaPlugin implements Listener {
private static final String PREFIX = ChatColor.GRAY + "[" + ChatColor.GOLD + "KOTH" + ChatColor.GRAY + "] "
+ ChatColor.RESET;
// private ConfigManager cfgm;
private dataManager dManager;
private List<ClaimedZone> cZones;
private Selection sel;
@Override
public void onEnable() {
// loadConfigManager();
// loadConfig();
Bukkit.broadcastMessage(PREFIX + ChatColor.GREEN + "Loaded.");
this.dManager = new dataManager();
this.cZones = this.dManager.loadZones();
disableAllKOTH();
if (this.cZones != null) {
for (int i = 0; i < this.cZones.size(); i++) {
Bukkit.getServer().getConsoleSender()
.sendMessage(PREFIX + ChatColor.RED + this.cZones.get(i).getName());
}
}
getServer().getPluginManager().registerEvents(this, this);
}
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage(PREFIX + ChatColor.RED + "Seul les joueurs peuvent utiliser ces commandes.");
return true;
}
Player p = (Player) sender;
if (commandLabel.equalsIgnoreCase("koth")) {
if (args[0].equalsIgnoreCase("new") && args.length == 2) {
sel = getWorldEdit().getSelection(p);
if (sel == null || sel.getMinimumPoint() == null || sel.getMaximumPoint() == null) {
sender.sendMessage(
PREFIX + ChatColor.RED + "Vous devez d'abord selectionner une zone avec WorldEdit !");
return true;
}
ClaimedZone newZone = new ClaimedZone(sel.getMinimumPoint(), sel.getMaximumPoint(), args[1]);
p.sendMessage(PREFIX + ChatColor.BLUE + "Le KOTH " + ChatColor.BOLD + args[1] + ChatColor.BLUE
+ " à bien été créé.");
this.cZones.add(newZone);
this.dManager.saveZones(this.cZones);
return true;
} else if (args[0].equalsIgnoreCase("delete") && args.length == 2) {
for (int i = 0; i < this.cZones.size(); i++) {
if (this.cZones.get(i).getName().equalsIgnoreCase(args[1])) {
this.cZones.remove(i);
p.sendMessage(PREFIX + "Le KOTH " + args[1] + " a bien été supprimé.");
this.dManager.saveZones(this.cZones);
return true;
}
}
p.sendMessage(PREFIX + ChatColor.RED + "Le KOTH " + args[1] + " introuvable");
return true;
} else if (args[0].equalsIgnoreCase("list")) {
p.sendMessage(PREFIX + "Liste des KOTH :");
ClaimedZone act;
String actif = "";
for (int i = 0; i < this.cZones.size(); i++) {
act = this.cZones.get(i);
if (this.cZones.get(i).isActive())
actif = ChatColor.BOLD + "ACTIF";
else
actif = "";
p.sendMessage("- " + ChatColor.BOLD + act.getName() + ChatColor.RESET + ": x: "
+ String.valueOf(act.getXStart()) + " -> "
+ String.valueOf(act.getXEnd()) + ", z: " + String.valueOf(act.getZStart()) + " -> "
+ String.valueOf(act.getZEnd()) + ", monde: " + act.getWorld() + actif);
}
return true;
} else if (args[0].equalsIgnoreCase("start") && args.length == 2) {
String kothActive = isAnyKOTHActive();
if (kothActive != null) {
p.sendMessage(PREFIX + ChatColor.RED + "Le KOTH " + kothActive + " est deja en cours !");
return true;
}
for (int i = 0; i < this.cZones.size(); i++) {
if (this.cZones.get(i).getName().equalsIgnoreCase(args[1])) {
this.cZones.get(i).setActive(true);
this.dManager.saveZones(this.cZones);
Bukkit.broadcastMessage(PREFIX + "Le KOTH " + ChatColor.BOLD + args[1] + ChatColor.RESET
+ " à commencé !");
return true;
}
}
p.sendMessage(PREFIX +
ChatColor.RED + "Le KOTH " + ChatColor.BOLD + args[1] + ChatColor.RED + " n'existe pas.");
return true;
} else if (args[0].equalsIgnoreCase("stop") && args.length == 2) {
for (int i = 0; i < this.cZones.size(); i++) {
if (this.cZones.get(i).getName().equalsIgnoreCase(args[1])) {
if (!this.cZones.get(i).isActive()) {
p.sendMessage(PREFIX + ChatColor.RED + "Le KOTH " + args[1] + " n'a pas commencé !");
return true;
}
this.cZones.get(i).setActive(false);
this.dManager.saveZones(this.cZones);
Bukkit.broadcastMessage(PREFIX + "Le KOTH " + ChatColor.BOLD + args[1] + ChatColor.RESET
+ " à été arrété !");
return true;
}
}
p.sendMessage(PREFIX +
ChatColor.RED + "Le KOTH " + ChatColor.BOLD + args[1] + ChatColor.RED + " n'existe pas.");
return true;
}
}
p.sendMessage(PREFIX + ChatColor.RED + "Mauvais usage de la commande.");
return false;
}
public String isAnyKOTHActive() {
for (int i = 0; i < this.cZones.size(); i++) {
if (this.cZones.get(i).isActive()) {
return this.cZones.get(i).getName();
}
}
return null;
}
public void disableAllKOTH() {
for (int i = 0; i < this.cZones.size(); i++) {
this.cZones.get(i).setActive(false);
}
}
public WorldEditPlugin getWorldEdit() {
Plugin p = Bukkit.getServer().getPluginManager().getPlugin("WorldEdit");
if (p instanceof WorldEditPlugin)
return (WorldEditPlugin) p;
else
return null;
}
@EventHandler
public void onPlayerMove(PlayerMoveEvent event) {
Player p = event.getPlayer();
Location newLoc = event.getTo();
Location oldLoc = event.getFrom();
for (int i = 0; i < this.cZones.size(); i++) {
if (this.cZones.get(i).isInZone(newLoc) && !this.cZones.get(i).isInZone(oldLoc)) {
p.sendMessage("Vous êtes bien entré dans la zone " + this.cZones.get(i).getName());
this.cZones.get(i).addPlayer(p.getName());
} else if (!this.cZones.get(i).isInZone(newLoc) && this.cZones.get(i).isInZone(oldLoc)) {
this.cZones.get(i).deletePlayer(p.getName());
p.sendMessage("Vous êtes bien sorti de la zone " + this.cZones.get(i).getName());
}
}
}
// public void loadConfigManager() {
// cfgm = new ConfigManager();
// cfgm.setup();
// cfgm.savePlayers();
// cfgm.reloadPlayers();
// }
// public void loadConfig() {
// getConfig().options().copyDefaults(true);
// saveConfig();
// }
@Override
public void onDisable() {
this.dManager.saveZones(this.cZones);
Bukkit.broadcastMessage(PREFIX + ChatColor.RED + "Zones saved.");
Bukkit.broadcastMessage(PREFIX + ChatColor.RED + "Unloaded.");
}
}
+17
View File
@@ -0,0 +1,17 @@
name: koth
main: fr.nessar.koth
description: KOTH plugin made with hearth.
version: 1
api-version: 1.8.8
depend:
- WorldEdit
commands:
koth:
description: koth base command
permission: koth.base
usage: /<command> [new|delete|list] <name>
permissions:
koth.base:
description: allow base koth command
default: op