168 lines
3.8 KiB
Java
168 lines
3.8 KiB
Java
package com.bernard.ptitPlugin.grenzen;
|
|
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.OptionalInt;
|
|
import java.util.Set;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.IntStream;
|
|
|
|
import org.bukkit.Location;
|
|
import org.bukkit.configuration.ConfigurationSection;
|
|
import org.bukkit.configuration.file.YamlConfiguration;
|
|
|
|
|
|
public class Stockeur {
|
|
|
|
public static final String FILENAME = "regions.yml";
|
|
|
|
List<Region> regions = new ArrayList<>();
|
|
|
|
public Stockeur() {
|
|
if(!new File(FILENAME).exists())
|
|
saveFile();
|
|
readFile();
|
|
}
|
|
|
|
public List<Boolean> regionsIn(Location l){
|
|
List<Boolean> poz = new ArrayList<Boolean>(regions.size());
|
|
|
|
for(Region r : regions) {
|
|
double 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);
|
|
}
|
|
|
|
return poz;
|
|
}
|
|
|
|
public String getDisplayName(int index) {
|
|
return regions.get(index).displayName;
|
|
}
|
|
|
|
public void readFile() {
|
|
|
|
ConfigurationSection cs = YamlConfiguration.loadConfiguration(new File(FILENAME));
|
|
Map<String,Object> regs = cs.getConfigurationSection("regions").getValues(false);
|
|
List<Region> regions = new ArrayList<Stockeur.Region>();
|
|
for(String nom : regs.keySet()) {
|
|
ConfigurationSection reg = cs.getConfigurationSection(nom);
|
|
List<Object[]> pts = (List<Object[]>) reg.get("points");
|
|
Set<PointPondere> ptz = pts.stream().map(p -> new PointPondere((int)p[0],(int)p[1],p.length>2?(int)p[2]:1)).collect(Collectors.toSet());
|
|
String dName = reg.getString("displayName");
|
|
long rayon = reg.getLong("rayon");
|
|
regions.add(new Region(ptz, nom, dName,rayon));
|
|
}
|
|
this.regions = regions;
|
|
|
|
|
|
}
|
|
|
|
public void saveFile() {
|
|
YamlConfiguration file = new YamlConfiguration();
|
|
ConfigurationSection regionsCS = file.createSection("regions");
|
|
for(Region r : regions) {
|
|
|
|
ConfigurationSection region = regionsCS.createSection(r.nom);
|
|
region.set("displayName", r.displayName);
|
|
region.set("rayon", r.rayon);
|
|
List<Object[]> lo = r.points.stream().map(pp -> new Object[] {pp.x,pp.z,pp.poids}).collect(Collectors.toList());
|
|
region.set("points", lo);
|
|
}
|
|
try {
|
|
file.save(new File(FILENAME));
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
|
|
|
|
public static class Region{
|
|
|
|
Set<PointPondere> points;
|
|
String nom;
|
|
String displayName;
|
|
long rayon;
|
|
|
|
public Region(Set<PointPondere> points, String nom, String displayName, long rayon) {
|
|
this.points = points;
|
|
this.nom = nom;
|
|
this.displayName = displayName;
|
|
this.rayon = rayon;
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
public static class PointPondere{
|
|
|
|
int x,z;
|
|
double poids;
|
|
public PointPondere(int x, int z, double poids) {
|
|
this.x = x;
|
|
this.z = z;
|
|
this.poids = poids;
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
public void newRegion(Region r) {
|
|
regions.add(r);
|
|
|
|
}
|
|
|
|
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());
|
|
return true;
|
|
}else
|
|
return false;
|
|
|
|
}
|
|
|
|
public boolean dnameRegion(String nom2, String displayName2) {
|
|
for(Region r : regions) {
|
|
if(r.nom.contentEquals(nom2)) {
|
|
r.displayName = displayName2;
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
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));
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public int delNearestPtRegion(String nom4, int x, int z) {
|
|
for(Region r : regions) {
|
|
if(r.nom.contentEquals(nom4)) {
|
|
|
|
int dcarre = 0;
|
|
for (PointPondere pp : r.points) {
|
|
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
}
|