package me.km.plots; import java.io.File; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.UUID; import me.hammerle.snuviscript.code.ISnuviLogger; import me.km.plots.PlotMap.Plot; import me.km.utils.Utils; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.server.MinecraftServer; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; public class WorldPlotMap { private final HashMap maps = new HashMap<>(); private final static int PLACE_FLAG = (1 << 0); private final static int BREAK_FLAG = (1 << 1); private final static int BUCKET_FLAG = (1 << 2); private final static int HIT_AMBIENT_FLAG = (1 << 3); private final static int BLOCK_INTERACT_FLAG = (1 << 4); private final static int ENTITY_INTERACT_FLAG = (1 << 5); public boolean canDoSomething(World w, BlockPos pos, PlayerEntity p, int flag, boolean empty) { PlotMap map = maps.get(w); if(map == null) { return empty; } if(p == null) { return map.anyPlotMatches(pos.getX(), pos.getY(), pos.getZ(), empty, plot -> plot.hasFlags(flag)); } UUID uuid = p.getUniqueID(); return map.anyPlotMatches(pos.getX(), pos.getY(), pos.getZ(), empty, plot -> plot.hasFlags(flag) || plot.getOwners().contains(uuid)); } public boolean canPlaceBlock(World w, BlockPos pos, PlayerEntity p) { return canDoSomething(w, pos, p, PLACE_FLAG, true); } public boolean canBreakBlock(World w, BlockPos pos, PlayerEntity p) { return canDoSomething(w, pos, p, BREAK_FLAG, true); } public boolean canUseBucket(World w, BlockPos pos, PlayerEntity p) { return canDoSomething(w, pos, p, BUCKET_FLAG, true); } public boolean canHitAmbientEntity(World w, BlockPos pos, PlayerEntity p) { return canDoSomething(w, pos, p, HIT_AMBIENT_FLAG, true); } public boolean canInteractWithBlock(World w, BlockPos pos, PlayerEntity p) { return canDoSomething(w, pos, p, BLOCK_INTERACT_FLAG, true); } public boolean canInteractWithEntity(World w, BlockPos pos, PlayerEntity p) { return canDoSomething(w, pos, p, ENTITY_INTERACT_FLAG, true); } public List getPlots(World w, BlockPos pos) { PlotMap map = maps.get(w); if(map == null) { return new ArrayList<>(); } return map.getPlotAt(pos.getX(), pos.getY(), pos.getZ()); } private PlotMap getOrCreate(World w) { PlotMap map = maps.get(w); if(map == null) { map = new PlotMap(); maps.put(w, map); } return map; } public PlotMap.Plot add(World w, BlockPos pos1, BlockPos pos2) { return getOrCreate(w).add(pos1.getX(), pos1.getY(), pos1.getZ(), pos2.getX(), pos2.getY(), pos2.getZ()); } public PlotMap.Plot add(World w, BlockPos pos1, BlockPos pos2, int id) { return getOrCreate(w).add(pos1.getX(), pos1.getY(), pos1.getZ(), pos2.getX(), pos2.getY(), pos2.getZ(), id); } public void remove(World w, Plot p) { PlotMap map = maps.get(w); if(map != null) { map.remove(p); } } public Iterator getIterator(World w) { PlotMap map = maps.get(w); if(map != null) { return map.getIterator(); } return Collections.emptyList().iterator(); } public Iterator getIterator(World w, UUID uuid) { PlotMap map = maps.get(w); if(map != null) { return map.getIterator(uuid); } return Collections.emptyList().iterator(); } public List getIntersectingPlots(World w, int minX, int minY, int minZ, int maxX, int maxY, int maxZ) { PlotMap map = maps.get(w); if(map != null) { return map.getIntersectingPlots(minX, minY, minZ, maxX, maxY, maxZ); } return Collections.emptyList(); } public void save() { File f = new File("plot_storage"); f.mkdir(); maps.entrySet().forEach((entry) -> { String name = Utils.getWorldName(entry.getKey()); entry.getValue().save("plot_storage/" + name); }); } public void read(MinecraftServer ms, ISnuviLogger logger) { File dir = new File("plot_storage"); if(!dir.exists()) { return; } for(File f : dir.listFiles()) { String name = f.getName(); World sw = Utils.getWorldFromName(ms, f.getName()); if(sw == null) { logger.print(String.format("invalid world storage '%s'", name)); continue; } PlotMap pm = new PlotMap(); maps.put(sw, pm); pm.read(f); } } }