WorldPlotMap.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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, plot -> plot.hasFlags(flag));
  31. }
  32. UUID uuid = p.getUniqueID();
  33. return map.anyPlotMatches(pos.getX(), pos.getY(), pos.getZ(), empty,
  34. plot -> plot.hasFlags(flag) || plot.getOwners().contains(uuid));
  35. }
  36. public boolean canPlaceBlock(World w, BlockPos pos, PlayerEntity p) {
  37. return canDoSomething(w, pos, p, PLACE_FLAG, true);
  38. }
  39. public boolean canBreakBlock(World w, BlockPos pos, PlayerEntity p) {
  40. return canDoSomething(w, pos, p, BREAK_FLAG, true);
  41. }
  42. public boolean canUseBucket(World w, BlockPos pos, PlayerEntity p) {
  43. return canDoSomething(w, pos, p, BUCKET_FLAG, true);
  44. }
  45. public boolean canHitAmbientEntity(World w, BlockPos pos, PlayerEntity p) {
  46. return canDoSomething(w, pos, p, HIT_AMBIENT_FLAG, true);
  47. }
  48. public boolean canInteractWithBlock(World w, BlockPos pos, PlayerEntity p) {
  49. return canDoSomething(w, pos, p, BLOCK_INTERACT_FLAG, true);
  50. }
  51. public boolean canInteractWithEntity(World w, BlockPos pos, PlayerEntity p) {
  52. return canDoSomething(w, pos, p, ENTITY_INTERACT_FLAG, true);
  53. }
  54. public List<PlotMap.Plot> getPlots(World w, BlockPos pos) {
  55. PlotMap map = maps.get(w);
  56. if(map == null) {
  57. return new ArrayList<>();
  58. }
  59. return map.getPlotAt(pos.getX(), pos.getY(), pos.getZ());
  60. }
  61. public PlotMap.Plot add(World w, BlockPos pos1, BlockPos pos2) {
  62. PlotMap map = maps.get(w);
  63. if(map == null) {
  64. map = new PlotMap();
  65. maps.put(w, map);
  66. }
  67. return map.add(pos1.getX(), pos1.getY(), pos1.getZ(), pos2.getX(), pos2.getY(), pos2.getZ());
  68. }
  69. public void remove(World w, Plot p) {
  70. PlotMap map = maps.get(w);
  71. if(map != null) {
  72. map.remove(p);
  73. }
  74. }
  75. public Iterator<PlotMap.Plot> getIterator(World w) {
  76. PlotMap map = maps.get(w);
  77. if(map != null) {
  78. return map.getIterator();
  79. }
  80. return Collections.EMPTY_LIST.iterator();
  81. }
  82. public Iterator<PlotMap.Plot> getIterator(World w, UUID uuid) {
  83. PlotMap map = maps.get(w);
  84. if(map != null) {
  85. return map.getIterator(uuid);
  86. }
  87. return Collections.EMPTY_LIST.iterator();
  88. }
  89. public List<Plot> getIntersectingPlots(World w, int minX, int minY, int minZ, int maxX, int maxY, int maxZ) {
  90. PlotMap map = maps.get(w);
  91. if(map != null) {
  92. return map.getIntersectingPlots(minX, minY, minZ, maxX, maxY, maxZ);
  93. }
  94. return Collections.EMPTY_LIST;
  95. }
  96. public void save() {
  97. File f = new File("plot_storage");
  98. f.mkdir();
  99. maps.entrySet().forEach((entry) -> {
  100. String name = Utils.getWorldName(entry.getKey());
  101. entry.getValue().save("plot_storage/" + name);
  102. });
  103. }
  104. public void read(MinecraftServer ms, ISnuviLogger logger) {
  105. File dir = new File("plot_storage");
  106. if(!dir.exists()) {
  107. return;
  108. }
  109. for(File f : dir.listFiles()) {
  110. String name = f.getName();
  111. World sw = Utils.getWorldFromName(ms, f.getName());
  112. if(sw == null) {
  113. logger.print(String.format("invalid world storage '%s'", name));
  114. continue;
  115. }
  116. PlotMap pm = new PlotMap();
  117. maps.put(sw, pm);
  118. pm.read(f);
  119. }
  120. }
  121. }