Réorganisation du code du gestionnaire de cubo + remplacement de /cubo donner par /cubo vendre et /cubo acheter

This commit is contained in:
Marc Baloup 2015-02-28 05:15:04 -05:00
parent 16ca1b80b8
commit 10c3a40677
5 changed files with 1029 additions and 553 deletions

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<jardesc> <jardesc>
<jar path="PandacraftUtils/jar_export/PandacraftUtils-3.13.jar"/> <jar path="PandacraftUtils/jar_export/PandacraftUtils-3.14.jar"/>
<options buildIfNeeded="true" compress="true" descriptionLocation="/PandacraftUtils/make_jar.jardesc" exportErrors="true" exportWarnings="true" includeDirectoryEntries="false" overwrite="false" saveDescription="true" storeRefactorings="false" useSourceFolders="false"/> <options buildIfNeeded="true" compress="true" descriptionLocation="/PandacraftUtils/make_jar.jardesc" exportErrors="true" exportWarnings="true" includeDirectoryEntries="false" overwrite="false" saveDescription="true" storeRefactorings="false" useSourceFolders="false"/>
<storedRefactorings deprecationInfo="true" structuralOnly="false"/> <storedRefactorings deprecationInfo="true" structuralOnly="false"/>
<selectedProjects/> <selectedProjects/>

View File

@ -1,6 +1,6 @@
name: PandacraftUtils name: PandacraftUtils
main: net.mc_pandacraft.java.plugin.pandacraftutils.PandacraftUtils main: net.mc_pandacraft.java.plugin.pandacraftutils.PandacraftUtils
version: 3.13 version: 3.14

View File

@ -33,4 +33,13 @@ import net.mc_pandacraft.java.plugin.pandacraftutils.PandacraftUtils;
String[] args); String[] args);
public boolean isValidPlayerName(String name) {
return name.matches("[0-9a-zA-Z_]{2,16}");
}
} }

View File

@ -1,20 +1,33 @@
package net.mc_pandacraft.java.plugin.pandacraftutils.modules.protection; package net.mc_pandacraft.java.plugin.pandacraftutils.modules.protection;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Random;
import java.util.Set;
import net.mc_pandacraft.java.plugin.pandacraftutils.PandacraftUtils;
import net.mc_pandacraft.java.plugin.pandacraftutils.config.ConfigManager;
import net.mc_pandacraft.java.plugin.pandacraftutils.plugin_interface.EssentialsInterface;
import net.mc_pandacraft.java.plugin.pandacraftutils.plugin_interface.WorldGuardInterface; import net.mc_pandacraft.java.plugin.pandacraftutils.plugin_interface.WorldGuardInterface;
import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
import com.sk89q.worldedit.bukkit.selections.CuboidSelection;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.domains.DefaultDomain;
import com.sk89q.worldguard.protection.UnsupportedIntersectionException;
import com.sk89q.worldguard.protection.databases.ProtectionDatabaseException;
import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion; import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;
import com.sk89q.worldguard.protection.regions.ProtectedRegion; import com.sk89q.worldguard.protection.regions.ProtectedRegion;
public class SurvivalCuboManager { public class SurvivalCuboManager {
public WorldGuardPlugin wgPlugin = WorldGuardInterface.getPlugin(); public WorldGuardPlugin wgPlugin = WorldGuardInterface.getPlugin();
public PandacraftUtils plugin = PandacraftUtils.getInstance();
public final String regex_cubo_id = "cubo[0-9]+-[0-9A-Za-z_]+-[0-9A-Za-z_]+"; public final String regex_cubo_id = "cubo[0-9]+-[0-9A-Za-z_]+-[0-9A-Za-z_]+";
@ -28,7 +41,7 @@ public class SurvivalCuboManager {
* @param w le monde * @param w le monde
* @return tout les cubos d'un joueur donné sur un monde donné * @return tout les cubos d'un joueur donné sur un monde donné
*/ */
public List<ProtectedCuboidRegion> getPlayerCubo(Player p, World w) { public List<Cubo> getPlayerCubo(Player p, World w) {
return getPlayerCubo(p.getName(), w); return getPlayerCubo(p.getName(), w);
} }
@ -38,10 +51,10 @@ public class SurvivalCuboManager {
* @param w le monde * @param w le monde
* @return tout les cubos d'un joueur donné sur un monde donné * @return tout les cubos d'un joueur donné sur un monde donné
*/ */
public List<ProtectedCuboidRegion> getPlayerCubo(String p, World w) { public List<Cubo> getPlayerCubo(String p, World w) {
Map<String, ProtectedRegion> regions = wgPlugin.getRegionManager(w).getRegions(); Map<String, ProtectedRegion> regions = wgPlugin.getRegionManager(w).getRegions();
List<ProtectedCuboidRegion> playerRegions = new ArrayList<ProtectedCuboidRegion>(); List<Cubo> playerRegions = new ArrayList<Cubo>();
for (Map.Entry<String, ProtectedRegion> region : regions.entrySet()) { for (Map.Entry<String, ProtectedRegion> region : regions.entrySet()) {
// on ne prend en charge que les cuboides // on ne prend en charge que les cuboides
@ -52,7 +65,7 @@ public class SurvivalCuboManager {
continue; continue;
if (!region.getValue().isOwner(p)) if (!region.getValue().isOwner(p))
continue; continue;
playerRegions.add((ProtectedCuboidRegion)region.getValue()); playerRegions.add(new Cubo((ProtectedCuboidRegion)region.getValue(), w.getName()));
} }
return playerRegions; return playerRegions;
} }
@ -63,7 +76,7 @@ public class SurvivalCuboManager {
* @param w le monde * @param w le monde
* @return le volume total des cubos d'un joueur donné sur un monde donné * @return le volume total des cubos d'un joueur donné sur un monde donné
*/ */
public int getPlayerActualBlockCount(Player p, World w) { public long getPlayerActualBlockCount(Player p, World w) {
return getPlayerActualBlockCount(p.getName(), w); return getPlayerActualBlockCount(p.getName(), w);
} }
@ -73,11 +86,11 @@ public class SurvivalCuboManager {
* @param w le monde * @param w le monde
* @return le volume total des cubos d'un joueur donné sur un monde donné * @return le volume total des cubos d'un joueur donné sur un monde donné
*/ */
public int getPlayerActualBlockCount(String p, World w) { public long getPlayerActualBlockCount(String p, World w) {
List<ProtectedCuboidRegion> regions = getPlayerCubo(p, w); List<Cubo> regions = getPlayerCubo(p, w);
int nb = 0; long nb = 0;
for (ProtectedCuboidRegion region : regions){ for (Cubo region : regions){
nb += region.volume(); nb += region.getVolume();
} }
return nb; return nb;
} }
@ -87,7 +100,7 @@ public class SurvivalCuboManager {
* @param nb le volume * @param nb le volume
* @return le prix pour le volume donné * @return le prix pour le volume donné
*/ */
public double getVolumePrice(int nb) { public double getVolumePrice(long nb) {
if (nb <= 2000) if (nb <= 2000)
return 0; return 0;
@ -104,4 +117,271 @@ public class SurvivalCuboManager {
} }
public void saveRegionManager(World w) {
try {
wgPlugin.getRegionManager(w).save();
} catch (ProtectionDatabaseException e) {
e.printStackTrace();
}
}
public Cubo getCuboFromLocation(Location l) {
if (!isInCuboWorld(l.getWorld())) return null;
for(ProtectedRegion cubo : wgPlugin.getRegionManager(l.getWorld()).getApplicableRegions(l))
{
if (!(cubo instanceof ProtectedCuboidRegion)) continue;
if (!cubo.getId().matches(regex_cubo_id)) continue;
return new Cubo( (ProtectedCuboidRegion) cubo, l.getWorld().getName());
}
return null;
}
public boolean isInCuboWorld(String w) {
return ConfigManager.getInstance().defaultConfig.cubos_worlds.contains(w);
}
public boolean isInCuboWorld(World w) { return isInCuboWorld(w.getName()); }
/**
*
* @param selection la sélection WorldEdit concerné par cette vérification
* @return la liste des identifiants des régions qui interfèrent avec la sélection passé en paramètre. S'il y en a aucun, retourne une liste vide.
*/
public List<String> getCuboNameIntersectedBySelection(CuboidSelection selection) {
// on crée un cubo factice (qui ne sera pas enregistré)
ProtectedCuboidRegion cubo = new ProtectedCuboidRegion("null", selection.getNativeMinimumPoint().toBlockVector(), selection.getNativeMaximumPoint().toBlockVector());
// on récupère les régions de la map pour les comparer
List<ProtectedRegion> regions = new ArrayList<ProtectedRegion>(wgPlugin.getRegionManager(selection.getWorld()).getRegions().values());
for (ProtectedRegion reg : regions.toArray(new ProtectedRegion[regions.size()])) {
if (reg.getPriority() < 0)
regions.remove(reg);
}
List<String> intersected_str = new ArrayList<String>();
List<ProtectedRegion> intersect_region = new ArrayList<ProtectedRegion>();
try {
intersect_region = ((ProtectedRegion)cubo).getIntersectingRegions(regions);
} catch (UnsupportedIntersectionException e) { }
// on extrait les ID des régions intersecté
for (ProtectedRegion r : intersect_region) {
intersected_str.add(r.getId());
}
return intersected_str;
}
public Cubo addCubo(CuboidSelection selection, String ownerName, String nom_cubo) {
if (!isInCuboWorld(selection.getWorld()))
return null;
if (ownerName == null || !ownerName.matches("[0-9A-Za-z_]{2;16}"))
return null;
if (nom_cubo == null || !nom_cubo.matches("[0-9A-Za-z_]+"))
return null;
String id_cubo;
do
id_cubo = "cubo"+(new Random()).nextInt(1000000000)+"-"+ownerName+"-"+nom_cubo;
while(wgPlugin.getRegionManager(selection.getWorld()).hasRegion(id_cubo));
ProtectedCuboidRegion cubo = new ProtectedCuboidRegion(id_cubo, selection.getNativeMinimumPoint().toBlockVector(), selection.getNativeMaximumPoint().toBlockVector());
DefaultDomain owners = new DefaultDomain();
owners.addPlayer(ownerName);
cubo.setOwners(owners);
wgPlugin.getRegionManager(selection.getWorld()).addRegion(cubo);
plugin.survivalCuboManager.saveRegionManager(selection.getWorld());
return new Cubo(cubo, selection.getWorld().getName());
}
public void removeCubo(Cubo c) {
wgPlugin.getRegionManager(plugin.getServer().getWorld(c.getWorldName())).removeRegion(c.getId());
plugin.survivalCuboManager.saveRegionManager(plugin.getServer().getWorld(c.getWorldName()));
}
public class Cubo {
private ProtectedCuboidRegion wgCubo;
private String world;
public Cubo(ProtectedCuboidRegion cubo, String w) {
if (!cubo.getId().matches(regex_cubo_id))
throw new IllegalArgumentException("La commande /cubo ne gère pas la protection '"+cubo.getId()+"'");
wgCubo = cubo;
world = w;
}
public int getVolume() { return wgCubo.volume(); }
public String getId() { return wgCubo.getId(); }
public String getWorldName() { return world; }
public Vector getMinimumPoint() { return new Vector(wgCubo.getMinimumPoint().getX(), wgCubo.getMinimumPoint().getY(), wgCubo.getMinimumPoint().getZ()); }
public Vector getMaximumPoint() { return new Vector(wgCubo.getMaximumPoint().getX(), wgCubo.getMaximumPoint().getY(), wgCubo.getMaximumPoint().getZ()); }
public Vector getSize() {
return new Vector(Math.abs(getMinimumPoint().getBlockX() - getMaximumPoint().getBlockX()) + 1,
Math.abs(getMinimumPoint().getBlockY() - getMaximumPoint().getBlockY()) + 1,
Math.abs(getMinimumPoint().getBlockZ() - getMaximumPoint().getBlockZ()) + 1);
}
public CuboidSelection getWESelection() {
return new CuboidSelection(plugin.getServer().getWorld(world), wgCubo.getMinimumPoint(), wgCubo.getMaximumPoint());
}
public boolean isOwner(Player p) { return isOwner(p.getName()); }
public boolean isOwner(String p) { return getOwnerName().equals(p); }
public String getOwnerName() {
if (forSale()) return "";
Set<String> pls = wgCubo.getOwners().getPlayers();
for (String p : pls)
return p;
throw new RuntimeException("getOwnerName() devrait retourner au moins un Pseudo pour la protection '"+wgCubo.getId()+"'");
}
public Set<String> getMembersNames() {
if (forSale()) return new HashSet<String>();
return wgCubo.getMembers().getPlayers();
}
public String getUserFriendlyMembersNames() {
if (forSale()) return "";
return wgCubo.getMembers().toUserFriendlyString();
}
public void addMember(String p) {
if (forSale()) return;
wgCubo.getMembers().addPlayer(p);
}
public void removeMember(String p) {
if (forSale()) return;
wgCubo.getMembers().removePlayer(p);
}
public void remove() {
removeCubo(this);
}
public void sellTo(String p) {
if (forSale()) return;
wgCubo.setMembers(new DefaultDomain());
wgCubo.getMembers().addPlayer(p);
wgCubo.getMembers().addPlayer(getOwnerName());
wgCubo.setOwners(new DefaultDomain());
}
public boolean forSale() {
return wgCubo.getOwners().size() == 0;
}
public boolean buy(String p) {
if (!forSale()) return false;
if (!wgCubo.getMembers().contains(p)) return false;
wgCubo.setMembers(new DefaultDomain());
wgCubo.setOwners(new DefaultDomain());
wgCubo.getOwners().addPlayer(p);
return true;
}
public boolean possibleBuyer(String p) {
if (!forSale()) return false;
return wgCubo.getMembers().contains(p);
}
}
public StatsAchatCubo getStatsAchatCubo(CuboidSelection selection, Player p) {
return new StatsAchatCubo(selection, p);
}
public class StatsAchatCubo {
public final long volumeTotalCubosActuel;
public final double valeurTotaleCubosActuel;
public final double soldeCompteActuel;
public final int volumeCuboCree;
public final double valeurCuboCree;
public final long volumeTotalCubosApresCreation;
public final double valeurTotaleCubosApresCreation;
public final double soldeCompteApresCreation;
private StatsAchatCubo(CuboidSelection selection, Player p) {
soldeCompteActuel = EssentialsInterface.getPlayerMoney(p);
volumeTotalCubosActuel = plugin.survivalCuboManager.getPlayerActualBlockCount(p, selection.getWorld());
volumeCuboCree = selection.getArea();
volumeTotalCubosApresCreation = volumeTotalCubosActuel + volumeCuboCree;
valeurTotaleCubosActuel = plugin.survivalCuboManager.getVolumePrice(volumeTotalCubosActuel);
valeurTotaleCubosApresCreation = plugin.survivalCuboManager.getVolumePrice(volumeTotalCubosApresCreation);
valeurCuboCree = valeurTotaleCubosApresCreation - valeurTotaleCubosActuel;
soldeCompteApresCreation = soldeCompteActuel - valeurCuboCree;
}
}
} }