diff --git a/plugin.yml b/plugin.yml index f16c57f..4e2f5ca 100644 --- a/plugin.yml +++ b/plugin.yml @@ -1,8 +1,34 @@ -name: Le Ptit Plugin +name: LePtitPlugin main: com.bernard.ptitPlugin.LePtitPlugin -version: beta -api-version: 1.13 +version: alpha +description: Un plugin fait avec amour pour les besoins des P'tits Potes +author: Mysaa(bernard) +website: opnutz.bernard.com.de +api-version: 1.15 commands: + grenze new: + aliases: [grenze créer, grenze nouvelle] + usage: "Usage : /grenze new" + description: Permet de gérer les frontières + permission: lpp.grenze.manage + permission-message: Seul le président de GOOGLEMAPZ est autorisé à lancer cette commande + default: false + grenze remove: + usage: /grenze remove + description: Permet de gérer les frontières + grenze addpt: + usage: /grenze addpt + description: Permet de gérer les frontières grenze: - usage: /grenze list - description: Permet de gérer les frontières \ No newline at end of file + usage: /grenze help + description: Permet de gérer les frontières + fisc: + usage: /fisc help + description: Permet de gérer l'unicité des monnaies crées + s: + usage: /s + description: Switch survie/spec + +permissions: + lpp.grenze.manage: + \ No newline at end of file diff --git a/src/com/bernard/ptitPlugin/LePtitPlugin.java b/src/com/bernard/ptitPlugin/LePtitPlugin.java index 4387d6d..48f6bcf 100644 --- a/src/com/bernard/ptitPlugin/LePtitPlugin.java +++ b/src/com/bernard/ptitPlugin/LePtitPlugin.java @@ -1,14 +1,32 @@ package com.bernard.ptitPlugin; -import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.java.JavaPlugin; +import com.bernard.ptitPlugin.construcation.Construcation; +import com.bernard.ptitPlugin.grenzen.Grenzen; +import com.bernard.ptitPlugin.mobiseur.Mobiseur; +import com.bernard.ptitPlugin.unicitax.Unicitax; + public class LePtitPlugin extends JavaPlugin { + Mobiseur mobiseur; + Grenzen grenzen; + Unicitax unicitax; + Construcation construcation; + @Override public void onEnable() { super.onEnable(); - + grenzen = new Grenzen(this); + mobiseur = new Mobiseur(); + unicitax = new Unicitax(this); + construcation = new Construcation(); + getServer().getPluginManager().registerEvents(grenzen, this); + getServer().getPluginManager().registerEvents(mobiseur, this); + getServer().getPluginManager().registerEvents(unicitax, this); + this.getCommand("grenze").setExecutor(grenzen.gmod);; + this.getCommand("fisc").setExecutor(unicitax.fisc);; + this.getCommand("s").setExecutor(construcation);; } @@ -20,8 +38,6 @@ public class LePtitPlugin extends JavaPlugin { public void setupConfig() { - FileConfiguration config = this.getConfig(); - this.saveDefaultConfig(); } diff --git a/src/com/bernard/ptitPlugin/construcation/Construcation.java b/src/com/bernard/ptitPlugin/construcation/Construcation.java index 3c2d9c4..c42509d 100644 --- a/src/com/bernard/ptitPlugin/construcation/Construcation.java +++ b/src/com/bernard/ptitPlugin/construcation/Construcation.java @@ -1,5 +1,39 @@ package com.bernard.ptitPlugin.construcation; -public class Construcation { +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import org.bukkit.GameMode; +import org.bukkit.Location; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +public class Construcation implements CommandExecutor{ + + Map lastPoz = new HashMap(); + + + @Override + public boolean onCommand( CommandSender sender, Command command, String cmd, String[] args) { + + if(!(sender instanceof Player)) { + sender.sendMessage("Cette commande doit être envoyée par un joueur !"); + return true; + } + Player pp = (Player)sender; + if(pp.getGameMode() == GameMode.SPECTATOR) { + if(lastPoz.containsKey(pp.getUniqueId()))pp.teleport(lastPoz.get(pp.getUniqueId())); + pp.setGameMode(GameMode.SURVIVAL); + }else { + lastPoz.put(pp.getUniqueId(), pp.getLocation()); + pp.setGameMode(GameMode.SPECTATOR); + } + return true; + } + + } diff --git a/src/com/bernard/ptitPlugin/grenzen/GrenzeModif.java b/src/com/bernard/ptitPlugin/grenzen/GrenzeModif.java index ecdb849..4f2c4eb 100644 --- a/src/com/bernard/ptitPlugin/grenzen/GrenzeModif.java +++ b/src/com/bernard/ptitPlugin/grenzen/GrenzeModif.java @@ -1,32 +1,37 @@ package com.bernard.ptitPlugin.grenzen; import java.util.HashSet; +import java.util.List; import java.util.regex.Pattern; import org.bukkit.Location; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.command.TabCompleter; import org.bukkit.entity.Player; +import com.bernard.ptitPlugin.grenzen.Stockeur.PointPondere; import com.bernard.ptitPlugin.grenzen.Stockeur.Region; -public class GrenzeModif implements CommandExecutor{ - +public class GrenzeModif implements CommandExecutor, TabCompleter{ + + Stockeur stck; - Pattern REGNAME_PATTERN = Pattern.compile("^[a-zA-Z0-9_][a-zA-Z0-9.-_]*$"); - Pattern REGDNAME_PATTERN = Pattern.compile("^[a-zA-Z0-9_][a-zA-Z0-9.-_ :&|$#~’;']*$"); + Pattern REGNAME_PATTERN = Pattern.compile("^[a-zA-Z0-9_][a-zA-Z0-9._-]*$"); + Pattern REGDNAME_PATTERN = Pattern.compile("^[a-zA-Z0-9_][a-zA-Z0-9._ :&|$#~’;'-]*$"); + public GrenzeModif(Stockeur stck) { + super(); + this.stck = stck; + } + @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { - if(!(sender instanceof Player)) { - sender.sendMessage("Cette commande doit être envoyée par un joueur !"); - return true; - } if(args.length==0) return false; @@ -34,7 +39,7 @@ public class GrenzeModif implements CommandExecutor{ switch(subcommand) { - case "new": // nom [displayName] [rayon] + case "new": // nom [displayName [rayon]] // Crée une nouvelle région if(args.length<2) return false; @@ -55,13 +60,13 @@ public class GrenzeModif implements CommandExecutor{ return true; } - if(REGNAME_PATTERN.matcher(nom).matches()){ + if(!REGNAME_PATTERN.matcher(nom).matches()){ sender.sendMessage("Ce nom n'est pas légal !"); return true; } - if(REGDNAME_PATTERN.matcher(displayName).matches()){ + if(!REGDNAME_PATTERN.matcher(displayName).matches()){ sender.sendMessage("Ce nom d'affichage n'est pas légal !"); return true; } @@ -70,6 +75,7 @@ public class GrenzeModif implements CommandExecutor{ stck.newRegion(r); + sender.sendMessage("La région "+nom+ " a bien été créée !"); break; @@ -96,7 +102,7 @@ public class GrenzeModif implements CommandExecutor{ if(args.length>=3) displayName = args[2]; - if(REGDNAME_PATTERN.matcher(displayName2).matches()){ + if(!REGDNAME_PATTERN.matcher(displayName2).matches()){ sender.sendMessage("Ce nom d'affichage n'est pas légal !"); return true; } @@ -112,6 +118,10 @@ public class GrenzeModif implements CommandExecutor{ case "list": // Liste les régions enregistrées, et d'autres infos (nbre points, region) + if(stck.regions.isEmpty()) { + sender.sendMessage("Il n'y a pas de région enregistrée ... :-/"); + } + sender.sendMessage("Voici la liste des régions enregistrées"); for (Region reg : stck.regions) { sender.sendMessage(" - "+reg.nom+" as "+reg.displayName+" : "+reg.points.size()+ " points enregistrés"); @@ -163,7 +173,7 @@ public class GrenzeModif implements CommandExecutor{ sender.sendMessage("Je ne connais pas la région "+nom3+" !"); return true; } - + sender.sendMessage("Le point a été enregistré !"); break; case "delpt": // nom [x z/*exact*/] // sinon nearest @@ -177,44 +187,185 @@ public class GrenzeModif implements CommandExecutor{ return false; } Location loc = ((Player)sender).getLocation(); - x=loc.getBlockX(); - z=loc.getBlockZ(); + x4=loc.getBlockX(); + z4=loc.getBlockZ(); }else { try { - x = Integer.parseInt(args[3]); - z = Integer.parseInt(args[4]); + x4 = Integer.parseInt(args[3]); + z4 = Integer.parseInt(args[4]); }catch(NumberFormatException e) { sender.sendMessage("Je ne saurais pas interpréter "+args[3]+" "+args[4]+" comme des coordonées entières …"); return true; } } - if(!stck.delNearestPtRegion(nom4,x,z)) { - sender.sendMessage("Je ne connais pas la région "+nom3+" !"); + try { + PointPondere pp = stck.delNearestPtRegion(nom4,x4,z4); + if(pp==null) { + sender.sendMessage("Il n'y a pas de points dans la région "+nom4+" !"); + return true; + } + sender.sendMessage("Le point en "+pp.x+" "+pp.z+" a été supprimé de la région "+nom4+" !"); + }catch(IllegalArgumentException exc) { + sender.sendMessage("Je ne connais pas la région "+nom4+" !"); return true; } + sender.sendMessage("Le point a été supprimé !"); + break; - case "modpt": // nom nx ny nz/*nouvelle*/ [ox oy oz] // sinon nearest + case "modpt": // nom nx nz/*nouvelle*/ [poids |ox oz [poids]] // sinon nearest + + if(args.length<4) + return false; + String nom5 = args[1]; + int nx, nz; + try { + nx = Integer.parseInt(args[2]); + nz = Integer.parseInt(args[3]); + }catch(NumberFormatException e) { + sender.sendMessage("Je ne saurais pas interpréter "+args[2]+" "+args[3]+" comme des coordonées entières …"); + return true; + } + + int ox,oz; + double poids5 = 1.; + if(args.length<6) { + if(!(sender instanceof Player)) { + sender.sendMessage("Si vous n'envoyez cette commande en tant que joueur, vous devez préciser les coordonées d'origine!"); + return false; + } + Location loc = ((Player)sender).getLocation(); + ox=loc.getBlockX(); + oz=loc.getBlockZ(); + if(args.length>=5) { + try { + poids = Double.parseDouble(args[4]); + }catch(NumberFormatException e) { + sender.sendMessage("Je ne saurais pas interpréter "+args[4]+" comme un flottant …"); + return true; + } + } + }else { + try { + ox = Integer.parseInt(args[4]); + oz = Integer.parseInt(args[5]); + }catch(NumberFormatException e) { + sender.sendMessage("Je ne saurais pas interpréter "+args[4]+" "+args[5]+" comme des coordonées entières …"); + return true; + } + + if(args.length>=7) { + try { + poids = Double.parseDouble(args[6]); + }catch(NumberFormatException e) { + sender.sendMessage("Je ne saurais pas interpréter "+args[6]+" comme un flottant …"); + return true; + } + } + } + try { + PointPondere pp = stck.modNearestPtRegion(nom5,nx,nz,poids5,ox,oz); + if(pp==null) { + sender.sendMessage("Il n'y a pas de points dans la région "+nom5+" !"); + return true; + } + sender.sendMessage("Le point en "+pp.x+" "+pp.z+" a été déplacé en "+nx+" "+nz+" avec un poids de "+poids5+" dans la région "+nom5+" !"); + }catch(IllegalArgumentException exc) { + sender.sendMessage("Je ne connais pas la région "+nom5+" !"); + return true; + } + + sender.sendMessage("Le point a été modifié !"); + + break; case "clearpts": // nom - // Supprime tous les points de la région séléctionnée + if(args.length<2) + return false; + String nom7 = args[1]; + + if(!stck.emptyRegion(nom7)) { + sender.sendMessage("Je ne connais pas la région "+nom7+" !"); + return true; + } + sender.sendMessage("Région vidée !"); + break; case "listpts": // nom - // Liste les points de la région, avec le rayon + // Liste les points de la région, avec le rayon + if(args.length<2) + return false; + String nom9 = args[1]; + Region reg = stck.getRegion(nom9); + if(nom9==null) { + sender.sendMessage("Je ne connais pas la région "+nom9+" !"); + return true; + } + if(reg.points.isEmpty()) { + sender.sendMessage("Il n'y a pas de points dans la région "+nom9+" ou "+reg.displayName); + return true; + } + sender.sendMessage("Liste des points de la région "+nom9+" ou "+reg.displayName); + reg.points.stream().forEach(p -> sender.sendMessage(" - "+p.x+" "+p.z+" de poids "+p.poids)); + + + break; case "load": // Recharge le fichier des régions - break; + stck.readFile(); + sender.sendMessage("Fichier rechargé !"); - case "modrayon": // rayon + break; + case "save": + // Recharge le fichier des régions + stck.saveFile(); + sender.sendMessage("Fichier enregistré !"); + break; + + case "modrayon": // nom rayon // Redéfinit le rayon + + if(args.length<3) + return false; + String nom10 = args[1]; + long rayon10; + try { + rayon10 = Integer.parseInt(args[2]); + }catch(NumberFormatException e) { + sender.sendMessage("Je ne saurais pas interpréter "+args[2]+" comme un entier …"); + return true; + } + if(rayon10 < 0) { + sender.sendMessage("Je ne suis pas sur que "+rayon10+" soit un entier strictement positif"); + return true; + } + if(!stck.modRayon(nom10,rayon10)) { + sender.sendMessage("Je ne connais pas la région "+nom10+" !"); + return true; + } + sender.sendMessage("Rayon changé !"); + break; default: + sender.sendMessage("Subcommands :"); + sender.sendMessage(" - new nom [displayName [rayon]]"); + sender.sendMessage(" - remove nom"); + sender.sendMessage(" - list"); + sender.sendMessage(" - name nom displayName"); + sender.sendMessage(" - addpt nom [x z]"); + sender.sendMessage(" - delpt nom [x z]"); + sender.sendMessage(" - modpt nom nx nz [poids |ox oz [poids]]"); + sender.sendMessage(" - clearpts nom"); + sender.sendMessage(" - listpts nom"); + sender.sendMessage(" - load"); + sender.sendMessage(" - save"); + sender.sendMessage(" - modrayon nom rayon"); // Afficher l'aide break; @@ -223,8 +374,17 @@ public class GrenzeModif implements CommandExecutor{ return true; } + + public Region getReg(String name) { return stck.regions.stream().filter(r->r.nom.equals(name)).findAny().orElse(null); } + @Override + public List onTabComplete( CommandSender sender, Command command, String cmd, String[] args) { + + + return null; + } + } diff --git a/src/com/bernard/ptitPlugin/grenzen/Grenzen.java b/src/com/bernard/ptitPlugin/grenzen/Grenzen.java index da13640..95346ea 100644 --- a/src/com/bernard/ptitPlugin/grenzen/Grenzen.java +++ b/src/com/bernard/ptitPlugin/grenzen/Grenzen.java @@ -4,26 +4,39 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; +import java.util.stream.Collectors; import org.bukkit.Location; import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.plugin.Plugin; public class Grenzen implements Listener{ public static final double PLAYER_UPDATE_DIST2 = 64.; // distance 8 Stockeur stockeur; + public GrenzeModif gmod; - Map lastPlayerPos; - Map> whereis; - public Grenzen() { + Map lastPlayerPos = new HashMap<>(); + Map> whereis = new HashMap<>(); + + public Grenzen(Plugin p) { lastPlayerPos = new HashMap(); - stockeur = new Stockeur(); + stockeur = new Stockeur(p); + gmod = new GrenzeModif(stockeur); } + @EventHandler + public void onJoin(PlayerJoinEvent event) { + whereis.put(event.getPlayer().getUniqueId(),stockeur.regions.stream().map(r -> false).collect(Collectors.toList())); + } + + @EventHandler public void onMoved(PlayerMoveEvent event) { Player p = event.getPlayer(); if(!lastPlayerPos.containsKey(p.getUniqueId()) || p.getLocation().distanceSquared(lastPlayerPos.get(p.getUniqueId())) > PLAYER_UPDATE_DIST2) { @@ -42,7 +55,9 @@ public class Grenzen implements Listener{ } } } - + System.out.println(pWhereIs); + whereis.put(p.getUniqueId(), pWhereIs); + System.out.println("ouch"); } } } diff --git a/src/com/bernard/ptitPlugin/grenzen/Stockeur.java b/src/com/bernard/ptitPlugin/grenzen/Stockeur.java index 34c3210..87b296d 100644 --- a/src/com/bernard/ptitPlugin/grenzen/Stockeur.java +++ b/src/com/bernard/ptitPlugin/grenzen/Stockeur.java @@ -10,18 +10,23 @@ import java.util.Set; import java.util.stream.Collectors; import java.util.stream.IntStream; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.plugin.Plugin; public class Stockeur { public static final String FILENAME = "regions.yml"; + boolean shouldSave = false; + Plugin plugin; List regions = new ArrayList<>(); - public Stockeur() { + public Stockeur(Plugin p) { + plugin = p; if(!new File(FILENAME).exists()) saveFile(); readFile(); @@ -31,10 +36,11 @@ public class Stockeur { List poz = new ArrayList(regions.size()); for(Region r : regions) { - double d2 = 0; + long d2 = 0; for(PointPondere p : r.points) - d2 += (l.getBlockX()-p.x)*(l.getBlockX()-p.x)+(l.getBlockZ()-p.z)*(l.getBlockZ()-p.z); - poz.add(d2<=r.rayon); + d2 += (l.getBlockX()-p.x)*(l.getBlockX()-p.x)+(l.getBlockZ()-p.z)*(l.getBlockZ()-p.z)*p.poids; + poz.add(d2<=r.rayon*r.rayon); + System.out.println(r.points+","+l+"->"+d2+"càd"+r.rayon); } return poz; @@ -47,12 +53,17 @@ public class Stockeur { public void readFile() { ConfigurationSection cs = YamlConfiguration.loadConfiguration(new File(FILENAME)); - Map regs = cs.getConfigurationSection("regions").getValues(false); + ConfigurationSection rgs = cs.getConfigurationSection("regions"); + Map regs = rgs.getValues(false); List regions = new ArrayList(); for(String nom : regs.keySet()) { - ConfigurationSection reg = cs.getConfigurationSection(nom); - List pts = (List) reg.get("points"); - Set ptz = pts.stream().map(p -> new PointPondere((int)p[0],(int)p[1],p.length>2?(int)p[2]:1)).collect(Collectors.toSet()); + ConfigurationSection reg = rgs.getConfigurationSection(nom); + System.out.println(reg); + System.out.println(reg.getKeys(true)); + List pts = new ArrayList(); + pts = ((List) reg.getList("points",pts)); + System.out.println(pts); + Set ptz = pts.stream().map(p -> PointPondere.parseString(p)).collect(Collectors.toSet()); String dName = reg.getString("displayName"); long rayon = reg.getLong("rayon"); regions.add(new Region(ptz, nom, dName,rayon)); @@ -63,6 +74,7 @@ public class Stockeur { } public void saveFile() { + shouldSave=false; YamlConfiguration file = new YamlConfiguration(); ConfigurationSection regionsCS = file.createSection("regions"); for(Region r : regions) { @@ -70,7 +82,7 @@ public class Stockeur { ConfigurationSection region = regionsCS.createSection(r.nom); region.set("displayName", r.displayName); region.set("rayon", r.rayon); - List lo = r.points.stream().map(pp -> new Object[] {pp.x,pp.z,pp.poids}).collect(Collectors.toList()); + List lo = r.points.stream().map(PointPondere::toString).collect(Collectors.toList()); region.set("points", lo); } try { @@ -110,19 +122,70 @@ public class Stockeur { this.poids = poids; } - - - } + public int dcarre(int x, int z) { + return (this.x-x)*(this.x-x)+(this.z-z)*(this.z-z); + } - public void newRegion(Region r) { - regions.add(r); + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + long temp; + temp = Double.doubleToLongBits(poids); + result = prime * result + (int) (temp ^ (temp >>> 32)); + result = prime * result + x; + result = prime * result + z; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + PointPondere other = (PointPondere) obj; + if (Double.doubleToLongBits(poids) != Double.doubleToLongBits(other.poids)) + return false; + if (x != other.x) + return false; + if (z != other.z) + return false; + return true; + } + + @Override + public String toString() { + return Integer.toString(x)+"/"+Integer.toString(z)+"/"+Double.toString(poids); + } + + public static PointPondere parseString(String in) { + String[] args = in.split("/"); + return new PointPondere(Integer.parseInt(args[0]), Integer.parseInt(args[1]), args.length>2?Double.parseDouble(args[2]):1.); + } } + private void shouldSave() { + if(!shouldSave) { + shouldSave=true; + Bukkit.getScheduler().runTaskLater(plugin, () -> saveFile(),5000); + } + } + + public void newRegion(Region r) { + regions.add(r); + shouldSave(); + } + + public boolean delRegion(String name) { OptionalInt index = IntStream.range(0, regions.size()).filter(i-> regions.get(i).nom.contentEquals(name)).findAny(); if(index.isPresent()) { regions.remove(index.getAsInt()); + shouldSave(); return true; }else return false; @@ -133,35 +196,97 @@ public class Stockeur { for(Region r : regions) { if(r.nom.contentEquals(nom2)) { r.displayName = displayName2; + shouldSave(); return true; } } return false; } + + public Region getRegion(String nom2) { + for(Region r : regions) { + if(r.nom.contentEquals(nom2)) { + return r; + } + } + return null; + } + public boolean addPtRegion(String nom3, int x, int z, double poids) { for(Region r : regions) { if(r.nom.contentEquals(nom3)) { r.points.add(new PointPondere(x, z, poids)); + shouldSave(); return true; } } return false; } - public int delNearestPtRegion(String nom4, int x, int z) { + public PointPondere delNearestPtRegion(String nom4, int x, int z) throws IllegalArgumentException{ for(Region r : regions) { if(r.nom.contentEquals(nom4)) { - - int dcarre = 0; - for (PointPondere pp : r.points) { - + if(r.points.isEmpty()) + return null; + int minddcarre = Integer.MAX_VALUE; + PointPondere mpt = null; + for (PointPondere p : r.points) { + int dst = p.dcarre(x, z); + if (dst < minddcarre) { + minddcarre = dst; + mpt = p; + } } - + r.points.remove(mpt); + shouldSave(); + return mpt; + } + } + throw new IllegalArgumentException(); + } + + public PointPondere modNearestPtRegion(String nom5, int nx, int nz, double poids, int ox, int oz) { + for(Region r : regions) { + if(r.nom.contentEquals(nom5)) { + if(r.points.isEmpty()) + return null; + int minddcarre = Integer.MAX_VALUE; + PointPondere mpt = null; + for (PointPondere p : r.points) { + int dst = p.dcarre(ox, oz); + if (dst < minddcarre) { + minddcarre = dst; + mpt = p; + } + } + r.points.remove(mpt); + r.points.add(new PointPondere(nx, nz, poids)); + shouldSave(); + return mpt; + } + } + throw new IllegalArgumentException(); + } + + public boolean emptyRegion(String name) { + OptionalInt index = IntStream.range(0, regions.size()).filter(i-> regions.get(i).nom.contentEquals(name)).findAny(); + if(index.isPresent()) { + regions.get(index.getAsInt()).points.clear(); + shouldSave(); + return true; + }else + return false; + } + + public boolean modRayon(String nom7, long rayon10) { + for(Region r : regions) { + if(r.nom.contentEquals(nom7)) { + r.rayon = rayon10; + shouldSave(); return true; } } - return -1; + return false; } - -} +} \ No newline at end of file diff --git a/src/com/bernard/ptitPlugin/mobiseur/Mobiseur.java b/src/com/bernard/ptitPlugin/mobiseur/Mobiseur.java index b6b4ec0..e6c925f 100644 --- a/src/com/bernard/ptitPlugin/mobiseur/Mobiseur.java +++ b/src/com/bernard/ptitPlugin/mobiseur/Mobiseur.java @@ -1,5 +1,23 @@ package com.bernard.ptitPlugin.mobiseur; -public class Mobiseur { +import org.bukkit.Particle; +import org.bukkit.Sound; +import org.bukkit.SoundCategory; +import org.bukkit.entity.EntityType; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityExplodeEvent; + +public class Mobiseur implements Listener{ + + @EventHandler + public void onExplode(EntityExplodeEvent event) { + if(event.getEntityType().equals(EntityType.CREEPER)) { + event.setCancelled(true); + event.getLocation().getWorld().spawnParticle(Particle.EXPLOSION_HUGE,event.getLocation(),3); + event.getLocation().getWorld().playSound(event.getLocation(), Sound.ENTITY_GENERIC_EXPLODE, SoundCategory.HOSTILE, 1.f, 1.f); + } + + } } diff --git a/src/com/bernard/ptitPlugin/unicitax/Fisc.java b/src/com/bernard/ptitPlugin/unicitax/Fisc.java new file mode 100644 index 0000000..9b87694 --- /dev/null +++ b/src/com/bernard/ptitPlugin/unicitax/Fisc.java @@ -0,0 +1,197 @@ +package com.bernard.ptitPlugin.unicitax; + +import java.util.UUID; +import java.util.regex.Pattern; + +import org.bukkit.Bukkit; +import org.bukkit.NamespacedKey; +import org.bukkit.command.Command; +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import com.bernard.ptitPlugin.unicitax.GardeMonetaire.GardeSceaux; + +public class Fisc implements CommandExecutor{ + + GardeMonetaire gm; + + Pattern ASSNAME_PATTERN = Pattern.compile("^[a-zA-Z0-9_][a-zA-Z0-9.-_]*$"); + + + + public Fisc(GardeMonetaire gm) { + this.gm = gm; + } + + + + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if(!(sender instanceof Player)) { + sender.sendMessage("Cette commande doit être envoyée par un joueur !"); + return true; + } + if(args.length==0) + return false; + + String subcommand = args[0]; + + switch(subcommand) { + + case "new": // assign item Player nom + // Crée un nouveau garde + if(args.length<5) + return false; + String assign = args[1]; + String itemS = args[2]; + String playerS = args[3]; + String nom = args[4]; + NamespacedKey item; + UUID player; + try { + item = NamespacedKey.minecraft(itemS); + }catch(Exception e) {//TODO trouver la bonne exception + sender.sendMessage("Je ne saurais pas interpréter "+args[2]+" comme un item …"); + return true; + } + + try { + player = Bukkit.getServer().getPlayer(playerS).getUniqueId(); + }catch(Exception e) {//TODO trouver la bonne exception + sender.sendMessage("Je ne saurais pas interpréter "+args[3]+" comme un joueur …"); + return true; + } + + if(!ASSNAME_PATTERN.matcher(nom).matches()){ + sender.sendMessage("Ce nom n'est pas légal !"); + return true; + } + + + GardeSceaux gs = gm.new GardeSceaux(assign, item, player, nom); + + gm.newGarde(gs); + + sender.sendMessage("Le garde a bien été créé !"); + + break; + + case "remove":// nom + // Supprime un garde + if(args.length<2) + return false; + String nom1 = args[1]; + + if(!gm.delGarde(nom1)) { + sender.sendMessage("Je ne connais pas le garde "+nom1+" !"); + return true; + } + sender.sendMessage("Garde supprimée !"); + + break; + + case "item":// nom item + //Définit l'item d'un garde + if(args.length<3) + return false; + String nom2 = args[1]; + String item2 = args[2]; + + try { + item = NamespacedKey.minecraft(item2); + }catch(Exception e) {//TODO trouver la bonne exception + sender.sendMessage("Je ne saurais pas interpréter "+args[2]+" comme un item …"); + return true; + } + + if(!gm.itemGarde(nom2,item)) { + sender.sendMessage("Je ne connais pas le garde "+nom2+" !"); + return true; + } + sender.sendMessage("Item changé !"); + + break; + + case "player":// nom Player + //Définit l'item d'un garde + if(args.length<3) + return false; + String nom3 = args[1]; + String player3 = args[2]; + + try { + player = Bukkit.getServer().getPlayer(player3).getUniqueId(); + }catch(Exception e) {//TODO trouver la bonne exception + sender.sendMessage("Je ne saurais pas interpréter "+args[3]+" comme un joueur …"); + return true; + } + + if(!gm.playerGarde(nom3,player)) { + sender.sendMessage("Je ne connais pas le garde "+nom3+" !"); + return true; + } + sender.sendMessage("Joueur changé !"); + + break; + + case "name":// nom name + //Définit le nom d'un garde + if(args.length<3) + return false; + String nom4 = args[1]; + String name4 = args[2]; + + + if(!gm.nameGarde(nom4,name4)) { + sender.sendMessage("Je ne connais pas le garde "+nom4+" !"); + return true; + } + sender.sendMessage("Nom changé !"); + + break; + + case "list": + // Liste les gardes enregistrées, et d'autres infos + + if(gm.gardes.isEmpty()) { + sender.sendMessage("Il n'y a pas de garde enregistré !"); + } + + sender.sendMessage("Voici la liste des gardes enregistrées"); + for (GardeSceaux reg : gm.gardes) { + sender.sendMessage(" - "+reg.assignationID+" : "+reg.player.toString()+" peut renommer "+reg.item.toString()+ " en "+reg.exactName); + } + + break; + + case "load": + // Recharge le fichier des régions + gm.readFile(); + sender.sendMessage("Fichier rechargé !"); + + break; + case "save": + // Recharge le fichier des régions + gm.saveFile(); + sender.sendMessage("Fichier enregistré !"); + break; + + default: + // Afficher l'aide + sender.sendMessage("Subcommands :"); + sender.sendMessage(" - new assign item Player nom"); + sender.sendMessage(" - remove assign"); + sender.sendMessage(" - item assign item"); + sender.sendMessage(" - player assign Player"); + sender.sendMessage(" - name assign name"); + sender.sendMessage(" - list"); + sender.sendMessage(" - load"); + sender.sendMessage(" - save"); + break; + + } + + return true; + } +} diff --git a/src/com/bernard/ptitPlugin/unicitax/GardeMonetaire.java b/src/com/bernard/ptitPlugin/unicitax/GardeMonetaire.java new file mode 100644 index 0000000..9766e7a --- /dev/null +++ b/src/com/bernard/ptitPlugin/unicitax/GardeMonetaire.java @@ -0,0 +1,188 @@ +package com.bernard.ptitPlugin.unicitax; + +import java.io.File; +import java.io.IOException; +import java.util.HashSet; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.UUID; + +import org.bukkit.Bukkit; +import org.bukkit.NamespacedKey; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.plugin.Plugin; + +public class GardeMonetaire { + +public static final String FILENAME = "gardes.yml"; + + Set gardes = new HashSet(); + + Plugin plugin; + + boolean shouldSave = false; + + public GardeMonetaire(Plugin p) { + plugin = p; + if(!new File(FILENAME).exists()) + saveFile(); + readFile(); + } + + + public void readFile() { + + ConfigurationSection cs = YamlConfiguration.loadConfiguration(new File(FILENAME)); + Map gds = cs.getConfigurationSection("gardes").getValues(false); + Set gardes = new HashSet<>(); + for(String nom : gds.keySet()) { + ConfigurationSection gd = cs.getConfigurationSection(nom); + String name = gd.getString("name"); + UUID player = UUID.fromString(gd.getString("userUUID")); + NamespacedKey item = NamespacedKey.minecraft(gd.getString("item")); + gardes.add(new GardeSceaux(nom,item,player,name)); + } + this.gardes = gardes; + + + } + + public void saveFile() { + YamlConfiguration file = new YamlConfiguration(); + ConfigurationSection cs = file.createSection("gardes"); + for(GardeSceaux g : gardes) { + ConfigurationSection garde = cs.createSection(g.assignationID); + garde.set("item", g.item); + garde.set("userUUID", g.player.toString()); + garde.set("name", g.exactName); + } + try { + file.save(new File(FILENAME)); + } catch (IOException e) { + e.printStackTrace(); + } + + } + + + public class GardeSceaux { + + public String assignationID; + public NamespacedKey item; + public UUID player; + public String exactName; + + public GardeSceaux(String assignationID, NamespacedKey item, UUID player, String exactName) { + super(); + this.assignationID = assignationID; + this.item = item; + this.player = player; + this.exactName = exactName; + } + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((assignationID == null) ? 0 : assignationID.hashCode()); + result = prime * result + ((exactName == null) ? 0 : exactName.hashCode()); + result = prime * result + ((item == null) ? 0 : item.hashCode()); + result = prime * result + ((player == null) ? 0 : player.hashCode()); + return result; + } + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + GardeSceaux other = (GardeSceaux) obj; + if (assignationID == null) { + if (other.assignationID != null) + return false; + } else if (!assignationID.equals(other.assignationID)) + return false; + if (exactName == null) { + if (other.exactName != null) + return false; + } else if (!exactName.equals(other.exactName)) + return false; + if (item == null) { + if (other.item != null) + return false; + } else if (!item.equals(other.item)) + return false; + if (player == null) { + if (other.player != null) + return false; + } else if (!player.equals(other.player)) + return false; + return true; + } + + + + } + + private void shouldSave() { + if(!shouldSave) { + shouldSave=true; + Bukkit.getScheduler().runTaskLater(plugin, () -> saveFile(),5000); + } + } + + + + public void newGarde(GardeSceaux gs) { + gardes.add(gs); + shouldSave(); + } + + public boolean delGarde(String ass) { + Optional gd = gardes.stream().filter(g-> g.assignationID.contentEquals(ass)).findAny(); + if(gd.isPresent()) { + gardes.remove(gd.get()); + shouldSave(); + return true; + }else + return false; + + } + + + public boolean itemGarde(String nom2, NamespacedKey item) { + for(GardeSceaux gs : gardes) { + if(gs.assignationID.contentEquals(nom2)) { + gs.item = item; + shouldSave(); + return true; + } + } + return false; + } + public boolean playerGarde(String nom2, UUID player) { + for(GardeSceaux gs : gardes) { + if(gs.assignationID.contentEquals(nom2)) { + gs.player = player; + shouldSave(); + return true; + } + } + return false; + } + public boolean nameGarde(String nom2, String name) { + for(GardeSceaux gs : gardes) { + if(gs.assignationID.contentEquals(nom2)) { + gs.exactName = name; + shouldSave(); + return true; + } + } + return false; + } + + +} diff --git a/src/com/bernard/ptitPlugin/unicitax/Unicitax.java b/src/com/bernard/ptitPlugin/unicitax/Unicitax.java index 56260c0..b7cf68a 100644 --- a/src/com/bernard/ptitPlugin/unicitax/Unicitax.java +++ b/src/com/bernard/ptitPlugin/unicitax/Unicitax.java @@ -1,5 +1,37 @@ package com.bernard.ptitPlugin.unicitax; -public class Unicitax { +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.inventory.AnvilInventory; +import org.bukkit.plugin.Plugin; + +public class Unicitax implements Listener{ + + GardeMonetaire gmot; + public Fisc fisc; + + public Unicitax(Plugin p) { + gmot = new GardeMonetaire(p); + fisc = new Fisc(gmot); + } + + @EventHandler(priority = EventPriority.HIGH) + public void onRename(InventoryClickEvent ev) { + if(!(ev.getInventory() instanceof AnvilInventory) || ev.isCancelled() || !(ev.getWhoClicked() instanceof Player)) + return; + AnvilInventory aiv = (AnvilInventory) ev.getInventory(); + if(gmot.gardes.stream().anyMatch(g -> aiv.getItem(0/* slot de gauche*/).getType().getKey().equals(g.item) && + ((Player)ev.getWhoClicked()).getUniqueId().equals(g.player) && + aiv.getRenameText().contentEquals(g.exactName))) { + ev.setCancelled(true); + } + } + + + + }