WorldPlotMap.java 4.8 KB

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