|
| 1 | +package github.nighter.smartspawner.hooks.protections.api; |
| 2 | + |
| 3 | +import com.google.common.eventbus.Subscribe; |
| 4 | +import com.plotsquared.core.events.PlotDeleteEvent; |
| 5 | +import com.plotsquared.core.events.Result; |
| 6 | +import com.plotsquared.core.plot.Plot; |
| 7 | +import github.nighter.smartspawner.SmartSpawner; |
| 8 | +import github.nighter.smartspawner.api.events.SpawnerPlaceEvent; |
| 9 | +import github.nighter.smartspawner.spawner.properties.SpawnerData; |
| 10 | +import org.bukkit.Location; |
| 11 | +import org.bukkit.entity.Player; |
| 12 | +import org.bukkit.event.EventHandler; |
| 13 | +import org.bukkit.event.EventPriority; |
| 14 | +import org.bukkit.event.Listener; |
| 15 | +import org.jetbrains.annotations.NotNull; |
| 16 | + |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.HashSet; |
| 19 | + |
| 20 | +public class PlotSquared implements Listener { |
| 21 | + private HashMap<Plot, HashSet<Location>> spawnersData = new HashMap<>(); |
| 22 | + |
| 23 | + public static boolean canInteract(@NotNull final Player player, @NotNull Location location) { |
| 24 | + Plot plot = Plot.getPlot(com.plotsquared.core.location.Location.at( |
| 25 | + location.getWorld().getName(), |
| 26 | + location.getBlockX(), |
| 27 | + location.getBlockY(), |
| 28 | + location.getBlockZ())); |
| 29 | + if (plot == null) return true; |
| 30 | + return plot.isAdded(player.getUniqueId()); |
| 31 | + } |
| 32 | + |
| 33 | + @Subscribe |
| 34 | + public void onPlotDelete(PlotDeleteEvent event) { |
| 35 | + if(event.getEventResult() == Result.DENY) return; |
| 36 | + Plot plot = event.getPlot(); |
| 37 | + if(!spawnersData.containsKey(plot)) return; |
| 38 | + for(Location loc : spawnersData.get(plot)) { |
| 39 | + if(loc == null) return; |
| 40 | + SpawnerData spawner = SmartSpawner.getInstance().getSpawnerManager().getSpawnerByLocation(loc); |
| 41 | + if (spawner == null) continue; |
| 42 | + SmartSpawner.getInstance().getSpawnerManager().removeGhostSpawner(spawner.getSpawnerId()); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @EventHandler (priority = EventPriority.HIGHEST) |
| 47 | + private void onSpawnerPlace(SpawnerPlaceEvent e) { |
| 48 | + Location location = e.getLocation(); |
| 49 | + if(location == null) return; |
| 50 | + Plot plot = Plot.getPlot(com.plotsquared.core.location.Location.at( |
| 51 | + location.getWorld().getName(), |
| 52 | + location.getBlockX(), |
| 53 | + location.getBlockY(), |
| 54 | + location.getBlockZ())); |
| 55 | + if(plot == null) return; |
| 56 | + if(!spawnersData.containsKey(plot)) spawnersData.put(plot, new HashSet<>()); |
| 57 | + spawnersData.get(plot).add(location); |
| 58 | + } |
| 59 | +} |
0 commit comments