WorldPlotMap.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package me.km.plots;
  2. import java.io.File;
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import java.util.HashMap;
  6. import java.util.Iterator;
  7. import java.util.List;
  8. import java.util.UUID;
  9. import me.hammerle.snuviscript.code.ISnuviLogger;
  10. import me.km.plots.PlotMap.Plot;
  11. import me.km.utils.Utils;
  12. import net.minecraft.entity.player.PlayerEntity;
  13. import net.minecraft.server.MinecraftServer;
  14. import net.minecraft.util.math.BlockPos;
  15. import net.minecraft.world.World;
  16. public class WorldPlotMap {
  17. private final HashMap<World, PlotMap> maps = new HashMap<>();
  18. private final static int PLACE_FLAG = (1 << 0);
  19. private final static int BREAK_FLAG = (1 << 1);
  20. private final static int BUCKET_FLAG = (1 << 2);
  21. private final static int HIT_AMBIENT_FLAG = (1 << 3);
  22. private final static int BLOCK_INTERACT_FLAG = (1 << 4);
  23. private final static int ENTITY_INTERACT_FLAG = (1 << 5);
  24. public boolean canDoSomething(World w, BlockPos pos, PlayerEntity p, int flag, boolean empty) {
  25. PlotMap map = maps.get(w);
  26. if(map == null) {
  27. return empty;
  28. }
  29. if(p == null) {
  30. return map.anyPlotMatches(pos.getX(), pos.getY(), pos.getZ(), empty,
  31. plot -> plot.hasFlags(flag));
  32. }
  33. UUID uuid = p.getUniqueID();
  34. return map.anyPlotMatches(pos.getX(), pos.getY(), pos.getZ(), empty,
  35. plot -> plot.hasFlags(flag) || plot.getOwners().contains(uuid));
  36. }
  37. public boolean canPlaceBlock(World w, BlockPos pos, PlayerEntity p) {
  38. return canDoSomething(w, pos, p, PLACE_FLAG, true);
  39. }
  40. public boolean canBreakBlock(World w, BlockPos pos, PlayerEntity p) {
  41. return canDoSomething(w, pos, p, BREAK_FLAG, true);
  42. }
  43. public boolean canUseBucket(World w, BlockPos pos, PlayerEntity p) {
  44. return canDoSomething(w, pos, p, BUCKET_FLAG, true);
  45. }
  46. public boolean canHitAmbientEntity(World w, BlockPos pos, PlayerEntity p) {
  47. return canDoSomething(w, pos, p, HIT_AMBIENT_FLAG, true);
  48. }
  49. public boolean canInteractWithBlock(World w, BlockPos pos, PlayerEntity p) {
  50. return canDoSomething(w, pos, p, BLOCK_INTERACT_FLAG, true);
  51. }
  52. public boolean canInteractWithEntity(World w, BlockPos pos, PlayerEntity p) {
  53. return canDoSomething(w, pos, p, ENTITY_INTERACT_FLAG, true);
  54. }
  55. public List<PlotMap.Plot> getPlots(World w, BlockPos pos) {
  56. PlotMap map = maps.get(w);
  57. if(map == null) {
  58. return new ArrayList<>();
  59. }
  60. return map.getPlotAt(pos.getX(), pos.getY(), pos.getZ());
  61. }
  62. private PlotMap getOrCreate(World w) {
  63. PlotMap map = maps.get(w);
  64. if(map == null) {
  65. map = new PlotMap();
  66. maps.put(w, map);
  67. }
  68. return map;
  69. }
  70. public PlotMap.Plot add(World w, BlockPos pos1, BlockPos pos2) {
  71. return getOrCreate(w).add(pos1.getX(), pos1.getY(), pos1.getZ(), pos2.getX(), pos2.getY(),
  72. pos2.getZ());
  73. }
  74. public PlotMap.Plot add(World w, BlockPos pos1, BlockPos pos2, int id) {
  75. return getOrCreate(w).add(pos1.getX(), pos1.getY(), pos1.getZ(), pos2.getX(), pos2.getY(),
  76. pos2.getZ(), id);
  77. }
  78. public void remove(World w, Plot p) {
  79. PlotMap map = maps.get(w);
  80. if(map != null) {
  81. map.remove(p);
  82. }
  83. }
  84. public Iterator<PlotMap.Plot> getIterator(World w) {
  85. PlotMap map = maps.get(w);
  86. if(map != null) {
  87. return map.getIterator();
  88. }
  89. return Collections.<PlotMap.Plot>emptyList().iterator();
  90. }
  91. public Iterator<PlotMap.Plot> getIterator(World w, UUID uuid) {
  92. PlotMap map = maps.get(w);
  93. if(map != null) {
  94. return map.getIterator(uuid);
  95. }
  96. return Collections.<PlotMap.Plot>emptyList().iterator();
  97. }
  98. public List<Plot> getIntersectingPlots(World w, int minX, int minY, int minZ, int maxX,
  99. int maxY, int maxZ) {
  100. PlotMap map = maps.get(w);
  101. if(map != null) {
  102. return map.getIntersectingPlots(minX, minY, minZ, maxX, maxY, maxZ);
  103. }
  104. return Collections.<Plot>emptyList();
  105. }
  106. public void save() {
  107. File f = new File("plot_storage");
  108. f.mkdir();
  109. maps.entrySet().forEach((entry) -> {
  110. String name = Utils.getWorldName(entry.getKey());
  111. entry.getValue().save("plot_storage/" + name);
  112. });
  113. }
  114. public void read(MinecraftServer ms, ISnuviLogger logger) {
  115. File dir = new File("plot_storage");
  116. if(!dir.exists()) {
  117. return;
  118. }
  119. for(File f : dir.listFiles()) {
  120. String name = f.getName();
  121. World sw = Utils.getWorldFromName(ms, f.getName());
  122. if(sw == null) {
  123. logger.print(String.format("invalid world storage '%s'", name));
  124. continue;
  125. }
  126. PlotMap pm = new PlotMap();
  127. maps.put(sw, pm);
  128. pm.read(f);
  129. }
  130. }
  131. }