From f6cb90bdf04c2e2d3ea894062eee1d6cc07d8fa5 Mon Sep 17 00:00:00 2001 From: Torrie Fischer Date: Thu, 10 Jun 2021 22:50:57 -0700 Subject: [PATCH] after a big rewrite and tons of new features, rc1 for 0.3 --- .gitignore | 2 + pom.xml | 137 +++++++++- .../us/camin/regions/BukkitEventHandler.java | 69 ----- .../us/camin/regions/CityRegionCommand.java | 54 ---- .../us/camin/regions/DynmapEventRelay.java | 107 +++++++- .../us/camin/regions/HomeRegionCommand.java | 55 ---- .../java/us/camin/regions/PlayerNotifier.java | 179 +++++++++++++ .../java/us/camin/regions/PlayerWatcher.java | 189 ++++++++++++- src/main/java/us/camin/regions/Plugin.java | 127 +++++---- src/main/java/us/camin/regions/Region.java | 252 +++++++++++++++++- .../java/us/camin/regions/RegionManager.java | 185 ++++--------- .../regions/RegionPostInteractionWatcher.java | 134 ++++++++++ .../camin/regions/RegionPostItemWatcher.java | 179 +++++++++++++ .../us/camin/regions/RegionPostManager.java | 141 ++++++++++ .../us/camin/regions/TestRegionGenerator.java | 29 ++ .../regions/{ => commands}/MoveinCommand.java | 7 +- .../regions/{ => commands}/RegionCommand.java | 73 +++-- .../regions/commands/RegionOpCommand.java | 99 +++++++ .../regions/commands/RegionTabComplete.java | 37 +++ .../regions/commands/RegionsCommand.java | 70 +++++ .../regions/config/RegionConfiguration.java | 75 ++++++ .../regions/config/WorldConfiguration.java | 26 ++ .../events/PlayerAddRegionChargeEvent.java | 45 ++++ .../regions/events/PlayerMoveInEvent.java | 49 ++++ .../events/PlayerNearRegionPostEvent.java | 47 ++++ .../PlayerPostInteractEvent.java} | 31 ++- .../{ => events}/PlayerRegionChangeEvent.java | 5 +- .../{ => events}/RegionCreateEvent.java | 5 +- .../regions/{ => events}/RegionEvent.java | 4 +- .../{ => events}/RegionRemoveEvent.java | 5 +- .../us/camin/regions/geometry/BorderMesh.java | 248 +++++++++++++++++ .../us/camin/regions/geometry/RegionSet.java | 59 ++++ src/main/java/us/camin/regions/ui/Colors.java | 72 +++++ .../regions/ui/PlayerInventoryTeleporter.java | 210 +++++++++++++++ .../us/camin/regions/ui/PlayerTeleporter.java | 88 ++++++ .../camin/regions/ui/RegionPostBuilder.java | 231 ++++++++++++++++ src/main/resources/plugin.yml | 66 +++-- 37 files changed, 2947 insertions(+), 444 deletions(-) delete mode 100644 src/main/java/us/camin/regions/BukkitEventHandler.java delete mode 100644 src/main/java/us/camin/regions/CityRegionCommand.java delete mode 100644 src/main/java/us/camin/regions/HomeRegionCommand.java create mode 100644 src/main/java/us/camin/regions/PlayerNotifier.java create mode 100644 src/main/java/us/camin/regions/RegionPostInteractionWatcher.java create mode 100644 src/main/java/us/camin/regions/RegionPostItemWatcher.java create mode 100644 src/main/java/us/camin/regions/RegionPostManager.java create mode 100644 src/main/java/us/camin/regions/TestRegionGenerator.java rename src/main/java/us/camin/regions/{ => commands}/MoveinCommand.java (90%) rename src/main/java/us/camin/regions/{ => commands}/RegionCommand.java (57%) create mode 100644 src/main/java/us/camin/regions/commands/RegionOpCommand.java create mode 100644 src/main/java/us/camin/regions/commands/RegionTabComplete.java create mode 100644 src/main/java/us/camin/regions/commands/RegionsCommand.java create mode 100644 src/main/java/us/camin/regions/config/RegionConfiguration.java create mode 100644 src/main/java/us/camin/regions/config/WorldConfiguration.java create mode 100644 src/main/java/us/camin/regions/events/PlayerAddRegionChargeEvent.java create mode 100644 src/main/java/us/camin/regions/events/PlayerMoveInEvent.java create mode 100644 src/main/java/us/camin/regions/events/PlayerNearRegionPostEvent.java rename src/main/java/us/camin/regions/{RegionAPI.java => events/PlayerPostInteractEvent.java} (52%) rename src/main/java/us/camin/regions/{ => events}/PlayerRegionChangeEvent.java (95%) rename src/main/java/us/camin/regions/{ => events}/RegionCreateEvent.java (94%) rename src/main/java/us/camin/regions/{ => events}/RegionEvent.java (94%) rename src/main/java/us/camin/regions/{ => events}/RegionRemoveEvent.java (94%) create mode 100644 src/main/java/us/camin/regions/geometry/BorderMesh.java create mode 100644 src/main/java/us/camin/regions/geometry/RegionSet.java create mode 100644 src/main/java/us/camin/regions/ui/Colors.java create mode 100644 src/main/java/us/camin/regions/ui/PlayerInventoryTeleporter.java create mode 100644 src/main/java/us/camin/regions/ui/PlayerTeleporter.java create mode 100644 src/main/java/us/camin/regions/ui/RegionPostBuilder.java diff --git a/.gitignore b/.gitignore index eb5a316..467f22c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ target +test-server +bin diff --git a/pom.xml b/pom.xml index e495654..a7ed1c6 100644 --- a/pom.xml +++ b/pom.xml @@ -4,23 +4,46 @@ us.camin.regions Regions jar - 0.1 - bukkitplugin + 0.2.99-rc1 + regions http://maven.apache.org UTF-8 + + com.comphenix.protocol + ProtocolLib + 4.6.0 + + + com.gmail.filoghost.holographicdisplays + holographicdisplays-api + 2.4.0 + provided + + + io.papermc + paperlib + 1.0.6 + compile + - org.bukkit - bukkit - 1.2.2-R0.1-SNAPSHOT + com.destroystokyo.paper + paper-api + 1.16.4-R0.1-SNAPSHOT provided + + com.github.jdiemke.delaunay-triangulator + DelaunayTriangulator + 1.0.0 + org.dynmap DynmapCoreAPI - 0.38 + 2.0 + provided commons-codec @@ -35,6 +58,13 @@ + + + org.apache.maven.wagon + wagon-ssh-external + 1.0-beta-6 + + src/main/resources @@ -45,17 +75,104 @@ org.apache.maven.plugins maven-compiler-plugin + 3.8.1 - 1.6 - 1.6 + 1.8 + 1.8 + + org.apache.maven.plugins + maven-shade-plugin + 3.1.1 + + ${project.build.directory}/dependency-reduced-pom.xml + + + io.papermc.lib + us.camin.regions.paperlib + + + + + + package + + shade + + + + + + + + org.eclipse.m2e + lifecycle-mapping + 1.0.0 + + + + + + org.apache.maven.plugins + maven-compiler-plugin + [1.0.0,) + + compile + + + + + + + + + org.apache.maven.plugins + maven-shade-plugin + [1.0.0,) + + shade + + + + + + + + + org.apache.maven.plugins + maven-jar-plugin + [1.0.0,) + + jar + + + + + + + + + + + + + + dmulloy2-repo + https://repo.dmulloy2.net/repository/public/ + + + codemc-repo + https://repo.codemc.io/repository/maven-public/ + - bukkit-repo - http://repo.bukkit.org/service/local/repositories/snapshots/content/ + papermc + http://papermc.io/repo/repository/maven-public/ + dynmap-repohttp://repo.mikeprimm.com/ + imagejhttp://maven.imagej.net/content/repositories/public/ diff --git a/src/main/java/us/camin/regions/BukkitEventHandler.java b/src/main/java/us/camin/regions/BukkitEventHandler.java deleted file mode 100644 index 31bf3d2..0000000 --- a/src/main/java/us/camin/regions/BukkitEventHandler.java +++ /dev/null @@ -1,69 +0,0 @@ -package us.camin.regions; - -/** - * This file is part of Regions - * - * Regions is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Regions is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Regions. If not, see . - * - */ - -import org.bukkit.event.Listener; -import org.bukkit.event.EventHandler; -import org.bukkit.event.player.PlayerTeleportEvent; -import org.bukkit.event.player.PlayerJoinEvent; -import org.bukkit.event.player.PlayerRespawnEvent; -import org.bukkit.event.player.PlayerChangedWorldEvent; -import org.bukkit.entity.Player; - -public class BukkitEventHandler implements Listener { - RegionManager m_manager; - public BukkitEventHandler(RegionManager manager) { - m_manager = manager; - } - - @EventHandler - public void onTeleport(PlayerTeleportEvent event) { - m_manager.recalculatePlayerRegions(); - } - - @EventHandler - public void onJoin(PlayerJoinEvent event) { - m_manager.recalculatePlayerRegions(); - } - - @EventHandler - public void onRespawn(PlayerRespawnEvent event) { - m_manager.recalculatePlayerRegions(); - } - - @EventHandler - public void onWorldChange(PlayerChangedWorldEvent event) { - m_manager.recalculatePlayerRegions(); - } - - @EventHandler - public void onPlayerRegionChanged(PlayerRegionChangeEvent event) { - if (event.oldRegion != null) { - for (Player p : m_manager.playersInRegion(event.oldRegion)) { - p.sendMessage(event.player.getName()+" has left the region."); - } - } - for (Player p : m_manager.playersInRegion(event.newRegion)) { - if (p != event.player) { - p.sendMessage(event.player.getName()+" has entered the region."); - } - } - event.player.sendMessage("Now entering region: "+event.newRegion.name()); - } -} diff --git a/src/main/java/us/camin/regions/CityRegionCommand.java b/src/main/java/us/camin/regions/CityRegionCommand.java deleted file mode 100644 index 8611d15..0000000 --- a/src/main/java/us/camin/regions/CityRegionCommand.java +++ /dev/null @@ -1,54 +0,0 @@ -package us.camin.regions; - -/** - * This file is part of Regions - * - * Regions is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Regions is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Regions. If not, see . - * - */ - -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.command.Command; -import org.bukkit.entity.Player; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; - -public class CityRegionCommand implements CommandExecutor { - - Plugin m_plugin; - - public CityRegionCommand(Plugin p) { - m_plugin = p; - } - - public boolean onCommand(CommandSender sender, Command command, String label, String[] split) { - if (!(sender instanceof Player)) { - sender.sendMessage("Region command is only available to players."); - return true; - } - Player p = (Player)sender; - Region city = m_plugin.regionManager().cityRegion(p.getLocation().getWorld().getName()); - Region nearest = m_plugin.regionManager().nearestRegion(p.getLocation()); - if (city != null) { - if (p.getLocation().distance(nearest.teleportLocation()) <= 5) { - p.teleport(city.teleportLocation(), TeleportCause.COMMAND); - } else { - sender.sendMessage("You must be within 5 blocks of a region center."); - } - } else { - sender.sendMessage("There is no city region defined."); - } - return true; - } -} diff --git a/src/main/java/us/camin/regions/DynmapEventRelay.java b/src/main/java/us/camin/regions/DynmapEventRelay.java index 80bcacf..540299c 100644 --- a/src/main/java/us/camin/regions/DynmapEventRelay.java +++ b/src/main/java/us/camin/regions/DynmapEventRelay.java @@ -21,30 +21,123 @@ package us.camin.regions; import org.bukkit.event.Listener; import org.dynmap.markers.MarkerSet; import org.dynmap.markers.MarkerIcon; +import org.dynmap.markers.AreaMarker; +import org.dynmap.markers.GenericMarker; +import org.dynmap.markers.PolyLineMarker; +import org.dynmap.markers.CircleMarker; import org.dynmap.markers.MarkerAPI; import org.dynmap.markers.Marker; import org.bukkit.event.EventHandler; import org.bukkit.Location; +import org.bukkit.World; +import java.util.logging.Logger; +import java.util.List; +import java.util.Collection; +import java.util.ArrayList; +import java.util.Map; +import java.util.HashMap; + +import us.camin.regions.events.RegionCreateEvent; +import us.camin.regions.events.RegionRemoveEvent; +import us.camin.regions.geometry.BorderMesh; +import us.camin.regions.geometry.RegionSet; +import us.camin.regions.ui.Colors; public class DynmapEventRelay implements Listener { - private MarkerSet m_set; + Logger log = Logger.getLogger("Regions.DynmapEventRelay"); + private MarkerSet m_borderSet; + private MarkerSet m_centerSet; + private MarkerSet m_routesSet; private MarkerAPI m_api; + private Map> m_borderMarkers; + Plugin m_plugin; - public DynmapEventRelay(MarkerAPI markerAPI) { + public DynmapEventRelay(Plugin plugin, MarkerAPI markerAPI) { + m_plugin = plugin; m_api = markerAPI; - m_set = m_api.createMarkerSet("Regions", "Regions", null, false); + m_centerSet = m_api.createMarkerSet("region-centers", "Region Posts", null, false); + m_borderSet = m_api.createMarkerSet("region-borders", "Region Borders", null, false); + m_routesSet = m_api.createMarkerSet("region-routes", "Region Routes", null, false); + m_borderSet.setHideByDefault(true); + m_routesSet.setHideByDefault(true); + m_borderMarkers = new HashMap>(); + + for(World world : plugin.getServer().getWorlds()) { + Collection regions = m_plugin.regionManager().regionsForWorld(world); + for (Region region : regions) { + createMarkerForRegion(region); + } + updatePolygons(world); + } + } + + public void updatePolygons(World world) { + List oldMarkers = m_borderMarkers.get(world); + if (oldMarkers != null) { + for(GenericMarker marker : oldMarkers) { + marker.deleteMarker(); + } + } else { + m_borderMarkers.put(world, new ArrayList()); + } + log.info("Triangulating mesh for world..."); + + RegionSet regions = m_plugin.regionManager().regionsForWorld(world); + BorderMesh geom = regions.borders(); + + for(Region region : regions) { + BorderMesh.Polygon polygon = geom.polygonForRegion(region); + if (polygon == null) { + log.info("Could not generate polygon for region " + region.name()); + continue; + } + boolean isFrontier = geom.isFrontier(region); + if (!isFrontier) { + AreaMarker marker = m_borderSet.createAreaMarker(null, region.name(), false, world.getName(), polygon.x, polygon.z, false); + marker.setFillStyle(0.7, region.color().getColor().asRGB()); + marker.setLineStyle(2, 0.8, region.color().getColor().asRGB()); + m_borderMarkers.get(world).add(marker); + } else { + PolyLineMarker marker = m_borderSet.createPolyLineMarker(null, region.name(), false, world.getName(), polygon.x, polygon.y, polygon.z, false); + marker.setLineStyle(2, 0.5, region.color().getColor().asRGB()); + m_borderMarkers.get(world).add(marker); + } + + // Add a line between each region, for teleportations + double thickness = Math.max(1, Math.log(geom.neighbors(region).size()) * 2.75); + for(Region neighbor : geom.neighbors(region)) { + double x[] = { neighbor.location().getBlockX(), region.location().getBlockX() }; + double y[] = { 64, 64 }; + double z[] = { neighbor.location().getBlockZ(), region.location().getBlockZ() }; + PolyLineMarker marker = m_routesSet.createPolyLineMarker(null, null, false, world.getName(), x, y, z, false); + marker.setLineStyle((int)Math.ceil(thickness), 0.5, region.color().getColor().asRGB()); + m_borderMarkers.get(world).add(marker); + } + } + } + + private void createMarkerForRegion(Region region) { + Location loc = region.location(); + MarkerIcon icon = m_api.getMarkerIcon("compass"); + CircleMarker circleMarker = m_routesSet.createCircleMarker(null, region.name(), false, loc.getWorld().getName(), loc.getX(), loc.getY(), loc.getZ(), 60, 60, false); + Marker marker = m_centerSet.createMarker(null, region.name(), loc.getWorld().getName(), loc.getX(), loc.getY(), loc.getZ(), icon, false); + circleMarker.setFillStyle(0.75, region.color().getColor().asRGB()); + circleMarker.setLineStyle(0, 0, 0); + String desc = "

" + region.name() + "

"; + marker.setDescription(desc); + circleMarker.setDescription(desc); } @EventHandler public void onRegionEvent(RegionRemoveEvent event) { - Marker marker = m_set.findMarkerByLabel(event.region.name()); + Marker marker = m_centerSet.findMarkerByLabel(event.region.name()); marker.deleteMarker(); + updatePolygons(event.region.location().getWorld()); } @EventHandler public void onRegionEvent(RegionCreateEvent event) { - Location loc = event.region.location(); - MarkerIcon icon = m_api.getMarkerIcon("default"); - m_set.createMarker(null, event.region.name(), loc.getWorld().getName(), loc.getX(), loc.getY(), loc.getZ(), icon, false); + createMarkerForRegion(event.region); + updatePolygons(event.region.location().getWorld()); } } diff --git a/src/main/java/us/camin/regions/HomeRegionCommand.java b/src/main/java/us/camin/regions/HomeRegionCommand.java deleted file mode 100644 index 30c5d5f..0000000 --- a/src/main/java/us/camin/regions/HomeRegionCommand.java +++ /dev/null @@ -1,55 +0,0 @@ -package us.camin.regions; - -/** - * This file is part of Regions - * - * Regions is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Regions is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Regions. If not, see . - * - */ - -import org.bukkit.command.CommandExecutor; -import org.bukkit.command.CommandSender; -import org.bukkit.command.Command; -import org.bukkit.entity.Player; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; - -public class HomeRegionCommand implements CommandExecutor { - - Plugin m_plugin; - - public HomeRegionCommand(Plugin p) { - m_plugin = p; - } - - public boolean onCommand(CommandSender sender, Command command, String label, String[] split) { - if (!(sender instanceof Player)) { - sender.sendMessage("Region command is only available to players."); - return true; - } - Player p = (Player)sender; - Region home = m_plugin.regionManager().homeRegion(p.getName()); - Region nearest = m_plugin.regionManager().nearestRegion(p.getLocation()); - - if (home != null) { - if (p.getLocation().distance(nearest.teleportLocation()) <= 5) { - p.teleport(home.teleportLocation(), TeleportCause.COMMAND); - } else { - sender.sendMessage("You must be within 5 blocks of a region center."); - } - } else { - sender.sendMessage("You have no home region."); - } - return true; - } -} diff --git a/src/main/java/us/camin/regions/PlayerNotifier.java b/src/main/java/us/camin/regions/PlayerNotifier.java new file mode 100644 index 0000000..c2d3eb9 --- /dev/null +++ b/src/main/java/us/camin/regions/PlayerNotifier.java @@ -0,0 +1,179 @@ +package us.camin.regions; + +/** + * This file is part of Regions + * + * Regions is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Regions is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Regions. If not, see . + * + */ + +import org.bukkit.event.Listener; +import org.bukkit.event.EventHandler; +import org.bukkit.entity.Player; +import org.bukkit.Location; +import org.bukkit.Particle; +import org.bukkit.Sound; +import org.bukkit.World; +import org.bukkit.scheduler.BukkitScheduler; +import org.bukkit.scheduler.BukkitTask; + +import com.comphenix.protocol.ProtocolManager; +import com.comphenix.protocol.ProtocolLibrary; +import com.comphenix.protocol.events.PacketContainer; +import com.comphenix.protocol.PacketType; +import com.comphenix.protocol.wrappers.EnumWrappers.TitleAction; +import com.comphenix.protocol.wrappers.WrappedChatComponent; + +import com.destroystokyo.paper.Title; +import java.util.logging.Logger; + +import us.camin.regions.events.PlayerMoveInEvent; +import us.camin.regions.events.PlayerNearRegionPostEvent; +import us.camin.regions.events.PlayerRegionChangeEvent; +import us.camin.regions.events.PlayerAddRegionChargeEvent; +import us.camin.regions.ui.RegionPostBuilder; + +import java.util.Collection; + +public class PlayerNotifier implements Listener { + Logger log = Logger.getLogger("Regions.PlayerNotifier"); + RegionManager m_manager; + Plugin m_plugin; + public PlayerNotifier(Plugin plugin, RegionManager manager) { + m_manager = manager; + m_plugin = plugin; + } + + @EventHandler + public void onPlayerRegionChanged(PlayerRegionChangeEvent event) { + if (event.oldRegion != null) { + for (Player p : m_plugin.playerWatcher().playersInRegion(event.oldRegion)) { + p.sendMessage(event.player.getName()+" has left the region."); + } + } + for (Player p : m_plugin.playerWatcher().playersInRegion(event.newRegion)) { + if (p != event.player) { + p.sendMessage(event.player.getName()+" has entered the region."); + } + } + event.newRegion.addVisit(); + event.player.sendMessage("Now entering region: "+event.newRegion.coloredName()); + int pop = m_plugin.playerWatcher().playersInRegion(event.newRegion).size(); + Location center = event.newRegion.location(); + int altitude = center.getBlockY(); + ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager(); + if (protocolManager != null) { + PacketContainer chatMessage = protocolManager.createPacket(PacketType.Play.Server.TITLE); + String colorHex = "#" + String.format("%06X", event.newRegion.color().getColor().asRGB()); + chatMessage.getChatComponents().write(0, WrappedChatComponent.fromJson("{text:\"" + event.newRegion.name() + "\", color: \""+colorHex+"\"}")); + try { + protocolManager.sendServerPacket(event.player, chatMessage); + } catch (Exception e) { + } + chatMessage = protocolManager.createPacket(PacketType.Play.Server.TITLE); + chatMessage.getChatComponents().write(0, WrappedChatComponent.fromJson("{text:\"Population: " + pop + " Altitude: "+ altitude + "\", color: \"#ffffff\"}")); + chatMessage.getTitleActions().write(0, TitleAction.SUBTITLE); + try { + protocolManager.sendServerPacket(event.player, chatMessage); + } catch (Exception e) { + } + chatMessage = protocolManager.createPacket(PacketType.Play.Server.TITLE); + chatMessage.getChatComponents().write(0, WrappedChatComponent.fromJson("{text:\"Now entering " + event.newRegion.name() + "\", color: \""+colorHex+"\"}")); + chatMessage.getTitleActions().write(0, TitleAction.ACTIONBAR); + try { + protocolManager.sendServerPacket(event.player, chatMessage); + } catch (Exception e) { + } + } else { + Title title = new Title.Builder().title(event.newRegion.name()).subtitle("Population: "+pop+" Altitude: " + altitude).build(); + event.player.sendTitle(title); + } + } + + @EventHandler + public void onPlayerNear(PlayerNearRegionPostEvent event) { + Collection nearby = m_manager.neighborsForRegion(event.region); + StringBuilder nearbyText = new StringBuilder(); + if (event.region.markSeenByPlayer(event.player)) { + event.player.playSound(event.region.location(), Sound.UI_TOAST_CHALLENGE_COMPLETE, (float)1, (float)1); + + ProtocolManager protocolManager = ProtocolLibrary.getProtocolManager(); + + if (protocolManager != null) { + PacketContainer chatMessage = protocolManager.createPacket(PacketType.Play.Server.TITLE); + String colorHex = "#" + String.format("%06X", event.region.color().getColor().asRGB()); + chatMessage.getChatComponents().write(0, WrappedChatComponent.fromJson("{text:\"Region discovered\", color: \""+colorHex+"\"}")); + try { + protocolManager.sendServerPacket(event.player, chatMessage); + } catch (Exception e) { + } + chatMessage = protocolManager.createPacket(PacketType.Play.Server.TITLE); + chatMessage.getChatComponents().write(0, WrappedChatComponent.fromJson("{text:\"You discovered the region " + event.region.name() + "\", color: \""+colorHex+"\"}")); + chatMessage.getTitleActions().write(0, TitleAction.SUBTITLE); + try { + protocolManager.sendServerPacket(event.player, chatMessage); + } catch (Exception e) { + } + } else { + //FIXME: also show pop/alt subtitle + Title title = new Title.Builder().title("Region Discovered").subtitle(event.region.name()).build(); + event.player.sendMessage("You discovered the region " + event.region.name()); + event.player.sendTitle(title); + } + } + for(Region region : nearby) { + nearbyText.append(" "); + nearbyText.append(region.coloredName()); + } + + event.player.playSound(event.region.location(), Sound.BLOCK_STONE_PRESSURE_PLATE_CLICK_ON, (float)0.5, (float)0.6); + event.player.sendMessage("Nearby regions:" + nearbyText.toString()); + + + World w = event.player.getLocation().getWorld(); + BukkitScheduler scheduler = m_plugin.getServer().getScheduler(); + BukkitTask puffGenerator = scheduler.runTaskTimer(m_plugin, () -> { + w.spawnParticle(Particle.REDSTONE, event.region.teleportLocation().add(0, -0.5, 0), 15, 0.1, 0.1, 0.1, event.region.dustOptions()); + }, 0, 15); + + scheduler.runTaskLater(m_plugin, () -> { + puffGenerator.cancel(); + }, 20 * 7); + } + + @EventHandler + public void onPlayerAddCharge(PlayerAddRegionChargeEvent event) { + if (event.region.charges() == 1) { + for (Player p : m_plugin.playerWatcher().playersInRegion(event.region)) { + if (p != event.player) { + p.sendMessage(event.player.getName()+" re-charged the region post."); + } + } + } + event.player.playSound(event.region.location(), Sound.BLOCK_RESPAWN_ANCHOR_CHARGE, (float)0.5, (float)0.6); + event.player.sendMessage("You re-charged the region post. It has " + event.region.charges() + " charges remaining."); + event.player.getWorld().spawnParticle(Particle.REDSTONE, event.region.teleportLocation(), 100, 1, 1, 1, event.region.dustOptions()); + } + + @EventHandler + public void onPlayerMoveIn(PlayerMoveInEvent event) { + /*RegionPostBuilder builder = new RegionPostBuilder(event.region); + builder.fireworks(); + for (Player p : m_plugin.playerWatcher().playersInRegion(event.region)) { + if (p != event.player) { + p.sendMessage(event.player.getName()+" has moved in to the region."); + } + }*/ + } +} diff --git a/src/main/java/us/camin/regions/PlayerWatcher.java b/src/main/java/us/camin/regions/PlayerWatcher.java index 0a4ac96..fafb7e8 100644 --- a/src/main/java/us/camin/regions/PlayerWatcher.java +++ b/src/main/java/us/camin/regions/PlayerWatcher.java @@ -18,16 +18,195 @@ package us.camin.regions; * */ -import java.lang.Runnable; +import org.bukkit.event.Listener; +import org.bukkit.event.Event; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.player.PlayerTeleportEvent; +import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.event.player.PlayerRespawnEvent; +import org.bukkit.event.player.PlayerChangedWorldEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.server.ServerLoadEvent; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.inventory.meta.CompassMeta; +import org.bukkit.inventory.meta.BannerMeta; +import org.bukkit.inventory.ItemStack; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.scheduler.BukkitTask; -public class PlayerWatcher implements Runnable { +import us.camin.regions.events.PlayerNearRegionPostEvent; +import us.camin.regions.events.PlayerPostInteractEvent; +import us.camin.regions.events.PlayerRegionChangeEvent; +import us.camin.regions.events.PlayerAddRegionChargeEvent; +import us.camin.regions.events.RegionCreateEvent; +import us.camin.regions.events.RegionRemoveEvent; +import us.camin.regions.ui.RegionPostBuilder; + +import java.util.logging.Logger; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Map; +import java.util.Collection; +import java.util.Collections; + +public class PlayerWatcher implements Listener { + Logger log = Logger.getLogger("Regions.PlayerWatcher"); private RegionManager m_manager; + private Plugin m_plugin; + private BukkitTask m_recalculateTask = null; + private PlayerTracker m_tracker; - public PlayerWatcher(RegionManager manager) { + private static int WATCH_INTERVAL = 3 * 20; // Every 3 seconds + + public PlayerWatcher(Plugin plugin, RegionManager manager) { m_manager = manager; + m_plugin = plugin; + m_tracker = new PlayerTracker(); } - public void run() { - m_manager.recalculatePlayerRegions(); + private void recalculateAndReschedule() { + if (m_recalculateTask != null) { + m_recalculateTask.cancel(); + } + m_tracker.recalculatePlayerRegions(); + m_recalculateTask = m_plugin.getServer().getScheduler().runTaskLater(m_plugin, () -> recalculateAndReschedule(), WATCH_INTERVAL); } + + @EventHandler + public void onReload(ServerLoadEvent event) { + recalculateAndReschedule(); + } + + @EventHandler + public void onTeleport(PlayerTeleportEvent event) { + recalculateAndReschedule(); + } + + @EventHandler + public void onJoin(PlayerJoinEvent event) { + recalculateAndReschedule(); + Region homeRegion = m_manager.homeRegion(event.getPlayer().getName()); + if (homeRegion != null) { + //event.getPlayer().setSubtitle(TextComponent.fromLegacyText(homeRegion.coloredName())); + //event.getPlayer().setCompassTarget(homeRegion.location()); + } + } + + @EventHandler + public void onRespawn(PlayerRespawnEvent event) { + recalculateAndReschedule(); + } + + @EventHandler + public void onWorldChange(PlayerChangedWorldEvent event) { + recalculateAndReschedule(); + } + + @EventHandler + public void onRegionCreate(RegionCreateEvent event) { + recalculateAndReschedule(); + } + + @EventHandler + public void onRegionCreate(RegionRemoveEvent event) { + m_tracker.forgetRegion(event.region); + recalculateAndReschedule(); + } + + public Collection playersInRegion(Region r) { + return m_tracker.playersInRegion(r); + } + + public void recalculatePlayerRegions(boolean b) { + m_tracker.recalculatePlayerRegions(b); + } + + public void clear() { + m_tracker.clear(); + } + + private class PlayerTracker { + private Map> m_regionPlayerLists; + private Map m_lastKnownRegions; + + public PlayerTracker() { + m_lastKnownRegions = new HashMap(); + m_regionPlayerLists = new HashMap>(); + } + + public synchronized Collection playersInRegion(Region r) { + if (m_regionPlayerLists.get(r) == null) { + return Collections.unmodifiableCollection(new ArrayList()); + } + return Collections.unmodifiableCollection(m_regionPlayerLists.get(r)); + } + + public synchronized void clear() { + m_lastKnownRegions = new HashMap(); + m_regionPlayerLists = new HashMap>(); + } + + public synchronized void forgetRegion(Region r) { + m_regionPlayerLists.remove(r); + } + + public synchronized void recalculatePlayerRegions() { + recalculatePlayerRegions(false); + } + + private HashMap m_playerIsNearby = new HashMap<>(); + + public boolean playerIsNearby(Player p) { + if (!m_playerIsNearby.containsKey(p)) { + m_playerIsNearby.put(p, false); + } + return m_playerIsNearby.get(p); + } + + public synchronized void recalculatePlayerRegions(boolean quiet) { + ArrayList updateEvents = new ArrayList(); + Collection allPlayers = m_plugin.getServer().getOnlinePlayers(); + for (Player p : allPlayers) { + Location loc = p.getLocation(); + Region nearest = m_manager.nearestRegion(loc); + if (nearest != null) { + log.finest("Current region for "+p.getName()+": "+nearest.name()); + Region last = m_lastKnownRegions.get(p); + if (nearest != last) { + log.fine("Player "+p.getName()+" entered region "+nearest.name()); + if (m_regionPlayerLists.get(nearest) == null) { + m_regionPlayerLists.put(nearest, new ArrayList()); + } + m_regionPlayerLists.get(nearest).add(p); + if (m_regionPlayerLists.get(last) != null) { + m_regionPlayerLists.get(last).remove(p); + } + m_lastKnownRegions.put(p, nearest); + m_playerIsNearby.put(p, false); + if (!quiet) { + updateEvents.add(new PlayerRegionChangeEvent(p, last, nearest)); + } + } + boolean isNearby = loc.distance(nearest.location()) <= 10; + if (!m_playerIsNearby.containsKey(p)) { + m_playerIsNearby.put(p, isNearby); + } + if (isNearby != m_playerIsNearby.get(p)) { + m_playerIsNearby.put(p, isNearby); + if (isNearby && !quiet) { + updateEvents.add(new PlayerNearRegionPostEvent(p, nearest)); + } + } + } + } + for (Event e : updateEvents) { + m_plugin.getServer().getScheduler().runTask(m_plugin, () -> m_plugin.getServer().getPluginManager().callEvent(e)); + } + } + } + + } diff --git a/src/main/java/us/camin/regions/Plugin.java b/src/main/java/us/camin/regions/Plugin.java index 2921665..bfa1e92 100644 --- a/src/main/java/us/camin/regions/Plugin.java +++ b/src/main/java/us/camin/regions/Plugin.java @@ -19,112 +19,109 @@ package us.camin.regions; */ import org.bukkit.plugin.java.JavaPlugin; -import org.bukkit.Location; -import org.bukkit.plugin.PluginManager; -import org.bukkit.World; -import org.bukkit.Material; -import org.bukkit.block.Block; -import org.bukkit.command.CommandExecutor; +import org.bukkit.configuration.Configuration; import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.plugin.ServicePriority; -import org.bukkit.plugin.ServicesManager; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.configuration.serialization.ConfigurationSerialization; import org.dynmap.markers.MarkerAPI; + +import us.camin.regions.commands.RegionCommand; +import us.camin.regions.commands.RegionOpCommand; +import us.camin.regions.commands.RegionsCommand; +import us.camin.regions.config.RegionConfiguration; +import us.camin.regions.config.WorldConfiguration; +import us.camin.regions.ui.PlayerInventoryTeleporter; + import org.dynmap.DynmapCommonAPI; +import java.io.File; +import java.io.IOException; +import java.util.logging.Level; import java.util.logging.Logger; -import java.util.Random; +import javax.security.auth.login.ConfigurationSpi; -public class Plugin extends JavaPlugin implements RegionAPI { +public class Plugin extends JavaPlugin { Logger log = Logger.getLogger("Regions"); RegionManager m_regions; PlayerWatcher m_playerWatcher; + RegionPostManager m_regionPosts; public RegionManager regionManager() { return m_regions; } + public PlayerWatcher playerWatcher() { + return m_playerWatcher; + } + public void onEnable() { - log.info("[Regions] Enabling Regions"); - m_regions = new RegionManager(getServer()); - - m_playerWatcher = new PlayerWatcher(m_regions); + log.info("Enabling Regions"); + ConfigurationSerialization.registerClass(RegionConfiguration.class); + m_regions = new RegionManager(this, getServer()); + m_playerWatcher = new PlayerWatcher(this, m_regions); - getServer().getScheduler().scheduleAsyncRepeatingTask(this, m_playerWatcher, 0, 5*20); + getCommand("region").setExecutor(new RegionCommand(this)); + getCommand("regions").setExecutor(new RegionsCommand(this)); + getCommand("regionop").setExecutor(new RegionOpCommand(this)); - CommandExecutor regionCommand = new RegionCommand(this); - getCommand("region").setExecutor(regionCommand); - getCommand("cityregion").setExecutor(new CityRegionCommand(this)); - getCommand("homeregion").setExecutor(new HomeRegionCommand(this)); - getCommand("movein").setExecutor(new MoveinCommand(this)); - - getServer().getPluginManager().registerEvents(new BukkitEventHandler(m_regions), this); + boolean useHolograms = getServer().getPluginManager().isPluginEnabled("HolographicDisplays"); + if (!useHolograms) { + log.info("HolographicDisplays not enabled. Region posts will not have holograms."); + } + m_regionPosts = new RegionPostManager(m_regions, this, useHolograms); + // TODO: Make holograms configurable. Disabled by default for now. + //getServer().getPluginManager().registerEvents(m_regionPosts, this); + loadRegions(); + m_playerWatcher.recalculatePlayerRegions(true); org.bukkit.plugin.Plugin mapPlugin = getServer().getPluginManager().getPlugin("dynmap"); - if (mapPlugin instanceof DynmapCommonAPI) { + if (mapPlugin instanceof DynmapCommonAPI && mapPlugin != null) { DynmapCommonAPI mapAPI = (DynmapCommonAPI)mapPlugin; MarkerAPI markerAPI = mapAPI.getMarkerAPI(); if (markerAPI != null) { - DynmapEventRelay regionHandler = new DynmapEventRelay (markerAPI); + DynmapEventRelay regionHandler = new DynmapEventRelay (this, markerAPI); getServer().getPluginManager().registerEvents(regionHandler, this); } else { - log.info("[Regions] Dynmap marker API not found. Disabling map support."); + log.info("Dynmap marker API not found. Disabling map support."); } } else { - log.info("[Regions] Dynmap not found. Disabling map support."); + log.info("Dynmap not found. Disabling map support."); } - ServicesManager sm = getServer().getServicesManager(); - sm.register(RegionAPI.class, this, this, ServicePriority.Normal); - - loadRegions(); - } - - private void loadTestRegions() { - log.info("[Regions] Loading test regions for development"); - String[] regionNames = {"Redstone", "Lapis", "Dwarf City"}; - Random rand = new Random(); - for(World w : getServer().getWorlds()) { - for(String name : regionNames) { - Location loc = new Location(w, rand.nextInt(30), 64, rand.nextInt(30)); - Region r = new Region(name, loc); - m_regions.addRegion(r); - } - } + // Install the event handler after things are loaded so players aren't spammed with text + getServer().getPluginManager().registerEvents(m_playerWatcher, this); + getServer().getPluginManager().registerEvents(new PlayerNotifier(this, m_regions), this); + getServer().getPluginManager().registerEvents(new PlayerInventoryTeleporter(this, m_regions), this); + getServer().getPluginManager().registerEvents(new RegionPostItemWatcher(this, m_regions), this); + getServer().getPluginManager().registerEvents(new RegionPostInteractionWatcher(this, m_regions), this); } public void loadRegions() { reloadConfig(); - ConfigurationSection section = getConfig().getConfigurationSection("worlds"); m_regions.clear(); - if (section != null) - m_regions.loadRegions(section, getServer()); - m_regions.recalculatePlayerRegions(); + this.getDataFolder().mkdir(); + File regionConfigFile = new File(this.getDataFolder(), "regions.yml"); + Configuration regionConf = YamlConfiguration.loadConfiguration(regionConfigFile); + m_regions.loadRegions(regionConf); } public void saveRegions() { - m_regions.saveRegions(getConfig().createSection("worlds")); + this.getDataFolder().mkdir(); + File regionConfigFile = new File(this.getDataFolder(), "regions.yml"); + YamlConfiguration regionConf = YamlConfiguration.loadConfiguration(regionConfigFile); + m_regions.saveRegions(regionConf); + try { + regionConf.save(regionConfigFile); + } catch (IOException e) { + log.log(Level.SEVERE, "Failed to write out regions.yml!!! Your data has not been saved!", e); + } saveConfig(); } public void onDisable() { + m_regionPosts.release(); saveRegions(); - log.info("[Regions] Plugin disabled"); - } - - public void regenRegionPost(Region r) { - World world = r.location().getWorld(); - Location center = world.getHighestBlockAt(r.location()).getLocation(); - for(int x = center.getBlockX()-1;x <= center.getBlockX()+1;x++) { - for(int z = center.getBlockZ()-1;z <= center.getBlockZ()+1;z++) { - Block b = world.getBlockAt(x, center.getBlockY()-1, z); - b.setType(Material.COBBLESTONE); - } - } - - for(int y = center.getBlockY()-2;y < center.getBlockY()+2;y++) { - Block b = world.getBlockAt(center.getBlockX(), y, center.getBlockZ()); - b.setType(Material.GLOWSTONE); - } + log.info("Plugin disabled"); } } diff --git a/src/main/java/us/camin/regions/Region.java b/src/main/java/us/camin/regions/Region.java index 9f06ebc..db61b1d 100644 --- a/src/main/java/us/camin/regions/Region.java +++ b/src/main/java/us/camin/regions/Region.java @@ -19,14 +19,89 @@ package us.camin.regions; */ import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.block.banner.Pattern; +import org.bukkit.DyeColor; +import org.bukkit.Particle.DustOptions; +import org.bukkit.OfflinePlayer; +import org.bukkit.inventory.ItemStack; +import org.bukkit.Material; +import org.bukkit.inventory.meta.BannerMeta; +import org.bukkit.material.MaterialData; + +import us.camin.regions.config.RegionConfiguration; +import us.camin.regions.ui.Colors; + +import java.util.List; +import java.util.ArrayList; +import java.util.UUID; + +import org.bukkit.ChatColor; public class Region { private Location m_location; private String m_name; + private List m_bannerPatterns = new ArrayList(); + private DyeColor m_color = null; + private List m_seenPlayers = new ArrayList(); + private boolean m_isHub = false; public Region(String name, Location location) { - m_location = location; + m_location = location.toBlockLocation(); m_name = name; + // Pick a random color + m_color = DyeColor.values()[(int)(System.currentTimeMillis() % DyeColor.values().length)]; + } + + public Region(String name, Location location, int visits, int charges, DyeColor color) { + m_location = location.toBlockLocation(); + m_name = name; + m_visits = visits; + m_charges = charges; + m_color = color; + } + + public Region(String name, World world, RegionConfiguration conf) { + if (conf.y == -1) { + Location defaultLoc = new Location(world, conf.x, 64, conf.z); + conf.y = world.getHighestBlockAt(defaultLoc).getY(); + } + m_name = name; + m_visits = conf.visits; + m_charges = conf.charges; + m_location = new Location(world, conf.x, conf.y, conf.z); + m_bannerPatterns = conf.patterns; + m_color = conf.color; + m_seenPlayers = conf.seenBy; + m_isHub = conf.isHub; + } + + public boolean isHub() { + return m_isHub; + } + + public boolean seenByPlayer(OfflinePlayer p) { + return m_seenPlayers.contains(p.getUniqueId()); + } + + public boolean markSeenByPlayer(OfflinePlayer p) { + if (!m_seenPlayers.contains(p.getUniqueId())) { + m_seenPlayers.add(p.getUniqueId()); + return true; + } + return false; + } + + public List seenPlayers() { + return m_seenPlayers; + } + + public List bannerPatterns() { + return m_bannerPatterns; + } + + public void setBannerPatterns(List p) { + m_bannerPatterns = p; } public Location location() { @@ -34,17 +109,188 @@ public class Region { } public Location teleportLocation() { - return m_location.getWorld().getHighestBlockAt(m_location).getLocation(); + return m_location.clone().add(0.5, 3, 0.5); + } + + public Location interactLocation() { + return m_location.clone().add(0, 1, 0); + } + + public void addCharges(int charges) { + m_charges += charges; + } + + int m_visits = 0; + int m_charges = 0; + + public boolean shouldKeepLoaded() { + return false; + } + + public int visits() { + return m_visits; + } + + public int charges() { + return m_charges; + } + + public void addVisit() { + m_visits++; } public String name() { return m_name; } + public DyeColor color() { + return m_color; + } + + public void setColor(DyeColor color) { + m_color = color; + } + /** * An alternative to Location.distance() which doesn't use floating point math. */ public int distanceTo(Location loc) { - return Math.abs((m_location.getBlockX()-loc.getBlockX())+Math.abs(m_location.getBlockZ()-loc.getBlockZ())); + return (int)m_location.distance(loc); + } + + public String coloredName() { + return Colors.chatColorForColor(m_color) + name() + ChatColor.RESET; + } + + public DustOptions dustOptions() { + return new DustOptions(m_color.getColor(), 1); + } + + public Material bannerIconMaterial() { + switch(m_color) { + case BLACK: + return Material.BLACK_BANNER; + case BLUE: + return Material.BLUE_BANNER; + case BROWN: + return Material.BROWN_BANNER; + case CYAN: + return Material.CYAN_BANNER; + case GRAY: + return Material.GRAY_BANNER; + case GREEN: + return Material.GREEN_BANNER; + case LIGHT_BLUE: + return Material.LIGHT_BLUE_BANNER; + case LIGHT_GRAY: + return Material.LIGHT_GRAY_BANNER; + case LIME: + return Material.LIME_BANNER; + case MAGENTA: + return Material.MAGENTA_BANNER; + case ORANGE: + return Material.ORANGE_BANNER; + case PINK: + return Material.PINK_BANNER; + case PURPLE: + return Material.PURPLE_BANNER; + case RED: + return Material.RED_BANNER; + case WHITE: + return Material.WHITE_BANNER; + case YELLOW: + return Material.YELLOW_BANNER; + default: + break; + } + return Material.YELLOW_BANNER; + } + + public Material bannerBlockMaterial() { + switch(m_color) { + case BLACK: + return Material.BLACK_WALL_BANNER; + case BLUE: + return Material.BLUE_WALL_BANNER; + case BROWN: + return Material.BROWN_WALL_BANNER; + case CYAN: + return Material.CYAN_WALL_BANNER; + case GRAY: + return Material.GRAY_WALL_BANNER; + case GREEN: + return Material.GREEN_WALL_BANNER; + case LIGHT_BLUE: + return Material.LIGHT_BLUE_WALL_BANNER; + case LIGHT_GRAY: + return Material.LIGHT_GRAY_WALL_BANNER; + case LIME: + return Material.LIME_WALL_BANNER; + case MAGENTA: + return Material.MAGENTA_WALL_BANNER; + case ORANGE: + return Material.ORANGE_WALL_BANNER; + case PINK: + return Material.PINK_WALL_BANNER; + case PURPLE: + return Material.PURPLE_WALL_BANNER; + case RED: + return Material.RED_WALL_BANNER; + case WHITE: + return Material.WHITE_WALL_BANNER; + case YELLOW: + return Material.YELLOW_WALL_BANNER; + default: + break; + } + return Material.YELLOW_WALL_BANNER; + } + + public Material blockMaterial() { + switch(m_color) { + case BLACK: + return Material.BLACK_WOOL; + case BLUE: + return Material.BLUE_WOOL; + case BROWN: + return Material.BROWN_WOOL; + case CYAN: + return Material.CYAN_WOOL; + case GRAY: + return Material.GRAY_WOOL; + case GREEN: + return Material.GREEN_WOOL; + case LIGHT_BLUE: + return Material.LIGHT_BLUE_WOOL; + case LIGHT_GRAY: + return Material.LIGHT_GRAY_WOOL; + case LIME: + return Material.LIME_WOOL; + case MAGENTA: + return Material.MAGENTA_WOOL; + case ORANGE: + return Material.ORANGE_WOOL; + case PINK: + return Material.PINK_WOOL; + case PURPLE: + return Material.PURPLE_WOOL; + case RED: + return Material.RED_WOOL; + case WHITE: + return Material.WHITE_WOOL; + case YELLOW: + return Material.YELLOW_WOOL; + default: + break; + } + return Material.YELLOW_WOOL; + } + + public ItemStack icon() { + ItemStack item = new ItemStack(bannerIconMaterial()); + BannerMeta bannerMeta = (BannerMeta)item.getItemMeta(); + bannerMeta.setPatterns(bannerPatterns()); + item.setItemMeta(bannerMeta); + return item; } } diff --git a/src/main/java/us/camin/regions/RegionManager.java b/src/main/java/us/camin/regions/RegionManager.java index ed8fac3..fbf2c05 100644 --- a/src/main/java/us/camin/regions/RegionManager.java +++ b/src/main/java/us/camin/regions/RegionManager.java @@ -21,96 +21,49 @@ package us.camin.regions; import org.bukkit.Location; import org.bukkit.World; import org.bukkit.configuration.ConfigurationSection; -import org.bukkit.Bukkit; import org.bukkit.Server; import org.bukkit.entity.Player; -import org.bukkit.event.Event; -import org.bukkit.plugin.PluginManager; - import java.util.logging.Logger; import java.util.Map; -import java.util.List; import java.util.HashMap; import java.util.Collection; -import java.util.Collections; import java.util.ArrayList; import java.util.Set; -public class RegionManager { - Logger log = Logger.getLogger("Regions.RegionManager"); - private Map> m_regions; - private Map m_cityRegions; - private Map m_homeRegions; - private Server m_server; - private Map m_lastKnownRegions; - private Map> m_regionPlayerLists; +import us.camin.regions.config.RegionConfiguration; +import us.camin.regions.config.WorldConfiguration; +import us.camin.regions.events.RegionCreateEvent; +import us.camin.regions.events.RegionRemoveEvent; +import us.camin.regions.geometry.RegionSet; +import java.util.logging.Level; - public RegionManager(Server server) { +public class RegionManager { + + Logger log = Logger.getLogger("Regions.RegionManager"); + private Map m_regions; + private Server m_server; + public RegionManager(Plugin plugin, Server server) { m_server = server; + log.setLevel(Level.ALL); clear(); } - public synchronized Collection playersInRegion(Region r) { - if (m_regionPlayerLists.get(r) == null) - return Collections.unmodifiableCollection(new ArrayList()); - return Collections.unmodifiableCollection(m_regionPlayerLists.get(r)); - } - - public synchronized void recalculatePlayerRegions() { - ArrayList updateEvents = new ArrayList(); - Player[] allPlayers = m_server.getOnlinePlayers(); - for (Player p : allPlayers) { - Location loc = p.getLocation(); - Region nearest = nearestRegion(loc); - if (nearest != null) { - log.finest("Current region for "+p.getName()+": "+nearest.name()); - Region last = m_lastKnownRegions.get(p); - if (nearest != last) { - updateEvents.add(new PlayerRegionChangeEvent(p, last, nearest)); - log.fine("Player "+p.getName()+" entered region "+nearest.name()); - m_regionPlayerLists.get(nearest).add(p); - if (m_regionPlayerLists.get(last) != null) - m_regionPlayerLists.get(last).remove(p); - m_lastKnownRegions.put(p, nearest); - } - } - } - for (Event e : updateEvents) { - m_server.getPluginManager().callEvent(e); - } - } - public synchronized void clear() { - m_regions = new HashMap>(); - m_cityRegions = new HashMap(); - m_homeRegions = new HashMap(); - m_lastKnownRegions = new HashMap(); - m_regionPlayerLists = new HashMap>(); + m_regions = new HashMap<>(); } public synchronized void renameWorld(String oldName, String newName) { log.fine("Renaming "+oldName+" to "+newName); m_regions.put(newName, m_regions.remove(oldName)); - m_cityRegions.put(newName, m_cityRegions.remove(oldName)); - } - - public synchronized Region cityRegion(String worldName) { - return m_cityRegions.get(worldName); - } - - public synchronized void setCityRegion(String worldName, Region region) { - m_cityRegions.put(worldName, region); } public synchronized boolean addRegion(Region r) { String worldName = r.location().getWorld().getName(); log.fine("Adding new region "+r.name()+" at "+r.location()); if (!m_regions.containsKey(worldName)) - m_regions.put(worldName, new ArrayList()); + m_regions.put(worldName, new RegionSet()); if (m_regions.get(worldName).add(r)) { - m_regionPlayerLists.put(r, new ArrayList()); m_server.getPluginManager().callEvent(new RegionCreateEvent(r)); - recalculatePlayerRegions(); } return false; } @@ -120,99 +73,79 @@ public class RegionManager { log.fine("Removing region "+r.name()+" from "+r.location()); if (m_regions.containsKey(worldName)) { if (m_regions.get(worldName).remove(r)) { - m_regionPlayerLists.remove(r); m_server.getPluginManager().callEvent(new RegionRemoveEvent(r)); - recalculatePlayerRegions(); } return true; } return false; } - public Collection regionsForWorld(World world) { + public RegionSet regionsForWorld(World world) { return regionsForWorld(world.getName()); } - public synchronized Collection regionsForWorld(String worldName) { - if (m_regions.containsKey(worldName)) - return Collections.unmodifiableCollection(m_regions.get(worldName)); - else - return Collections.unmodifiableCollection(new ArrayList()); + public Collection neighborsForRegion(Region region) { + return regionsForWorld(region.location().getWorld()).borders().neighbors(region); + } + + public Collection worldHubs(World world) { + ArrayList regions = new ArrayList(); + for(Region r : regionsForWorld(world.getName())) { + if (r.isHub()) { + regions.add(r); + } + } + return regions; + } + + public synchronized RegionSet regionsForWorld(String worldName) { + if (m_regions.containsKey(worldName)) { + return m_regions.get(worldName); + } else { + return new RegionSet(); + } } public Region nearestRegion(Location loc) { - Collection regions = regionsForWorld(loc.getWorld()); - Region nearest = null; - int minDistance = -1; - for(Region r : regions) { - int check = r.distanceTo(loc); - if (minDistance == -1 || check < minDistance) { - nearest = r; - minDistance = check; - } - } - return nearest; + return regionsForWorld(loc.getWorld()).nearestRegion(loc); } public synchronized void saveRegions(ConfigurationSection section) { for(String worldName : m_regions.keySet()) { - ConfigurationSection worldSection = section.createSection(worldName); - Region cityRegion = cityRegion(worldName); - if (cityRegion != null) - worldSection.set("city", cityRegion.name()); - ConfigurationSection worldRegionSection = worldSection.createSection("regions"); + ConfigurationSection worldRegionSection = section.createSection(worldName); for(Region r : regionsForWorld(worldName)) { - ConfigurationSection regionSection = worldRegionSection.createSection(r.name()); - regionSection.set("x", r.location().getBlockX()); - regionSection.set("z", r.location().getBlockZ()); - ArrayList homePlayers = new ArrayList(); - for(String player : m_homeRegions.keySet()) { - if (m_homeRegions.get(player) == r) { - homePlayers.add(player); - } - } - regionSection.set("players", homePlayers); + RegionConfiguration conf = new RegionConfiguration(r); + worldRegionSection.createSection(r.name(), conf.serialize()); } } } public synchronized Region homeRegion(String playerName) { - return m_homeRegions.get(playerName); + return null; + //return m_homeRegions.get(playerName); } - public synchronized void setHomeRegion(String player, Region r) { - m_homeRegions.put(player, r); + public synchronized void setHomeRegion(Player player, Region r) { + /*Region old = m_homeRegions.get(player.getName()); + m_homeRegions.put(player.getName(), r); + log.info("Player "+player.getName()+" moved in to "+r.name()); + PlayerMoveInEvent evt = new PlayerMoveInEvent(player, r, old); + m_plugin.getServer().getPluginManager().callEvent(evt);*/ } - public synchronized void loadRegions(ConfigurationSection section, Server server) { - Set worldNames = section.getKeys(false); - for(String worldName : worldNames) { - ConfigurationSection worldSection = section.getConfigurationSection(worldName); - String cityName = worldSection.getString("city"); - ConfigurationSection worldRegionSection = worldSection.getConfigurationSection("regions"); - Set regionNames = worldRegionSection.getKeys(false); - World world = server.getWorld(worldName); - if (world == null) { - log.warning("Could not find world: "+worldName); - continue; - } - for(String regionName : regionNames) { - ConfigurationSection regionSection = worldRegionSection.getConfigurationSection(regionName); - int x = regionSection.getInt("x"); - int z = regionSection.getInt("z"); - Location loc = new Location(world, x, 64, z); - Region r = new Region(regionName, loc); - addRegion(r); + public synchronized void loadRegions(ConfigurationSection section) { + for(World world : m_server.getWorlds()) { + ConfigurationSection worldConfig = section.getConfigurationSection(world.getName()); - if (regionName.equals(cityName)) { - m_cityRegions.put(worldName, r); - } + if (worldConfig == null) { + log.info("No regions configured for world " + world.getName()); + continue; + } - List regionPlayers = regionSection.getStringList("players"); - for(String player : regionPlayers) { - m_homeRegions.put(player, r); - } + for(String regionName : worldConfig.getKeys(false)) { + RegionConfiguration conf = new RegionConfiguration(worldConfig.getConfigurationSection(regionName).getValues(false)); + addRegion(new Region(regionName, world, conf)); } - } + } } } diff --git a/src/main/java/us/camin/regions/RegionPostInteractionWatcher.java b/src/main/java/us/camin/regions/RegionPostInteractionWatcher.java new file mode 100644 index 0000000..f78f6b6 --- /dev/null +++ b/src/main/java/us/camin/regions/RegionPostInteractionWatcher.java @@ -0,0 +1,134 @@ +package us.camin.regions; + +/** + * This file is part of Regions + * + * Regions is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Regions is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Regions. If not, see . + * + */ + +import org.bukkit.event.Listener; +import org.bukkit.event.Event; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.inventory.meta.BannerMeta; +import org.bukkit.material.Banner; +import org.bukkit.block.Block; +import org.bukkit.Material; +import org.bukkit.DyeColor; +import java.util.logging.Logger; + +import us.camin.regions.ui.RegionPostBuilder; +import us.camin.regions.events.PlayerPostInteractEvent; +import us.camin.regions.events.PlayerAddRegionChargeEvent; + +public class RegionPostInteractionWatcher implements Listener { + private RegionManager m_manager; + private Plugin m_plugin; + Logger log = Logger.getLogger("Regions.RegionPostBuilder"); + + public RegionPostInteractionWatcher(Plugin plugin, RegionManager manager) { + m_manager = manager; + m_plugin = plugin; + } + + private final Material[] bannerTypes = { + Material.WHITE_BANNER, + Material.YELLOW_BANNER, + Material.BLUE_BANNER, + Material.BLACK_BANNER, + Material.BROWN_BANNER, + Material.CYAN_BANNER, + Material.GREEN_BANNER, + Material.LIGHT_BLUE_BANNER, + Material.GRAY_BANNER, + Material.LIGHT_GRAY_BANNER, + Material.LIME_BANNER, + Material.MAGENTA_BANNER, + Material.ORANGE_BANNER, + Material.PINK_BANNER, + Material.PURPLE_BANNER, + Material.RED_BANNER, + }; + + private boolean isBannerItem(ItemStack item) { + for(Material mat : bannerTypes) { + if (item.getType() == mat) { + return true; + } + } + return false; + } + + @EventHandler + public void onInteract(PlayerInteractEvent event) { + Player player = event.getPlayer(); + ItemStack handStack = player.getItemInHand(); + ItemMeta meta = handStack.getItemMeta(); + Region nearest = m_manager.nearestRegion(player.getLocation()); + if (nearest == null) { + return; + } + Block clickedBlock = event.getClickedBlock(); + Location interactRegion = nearest.interactLocation(); + Location lanternRegion = nearest.interactLocation().add(0, 1, 0); + boolean isInteracted = false; + if (clickedBlock != null) { + isInteracted |= clickedBlock.getBlockKey() == interactRegion.toBlockKey(); + isInteracted |= clickedBlock.getBlockKey() == lanternRegion.toBlockKey(); + isInteracted |= nearest.interactLocation().add(1, 0, 0).toBlockKey() == clickedBlock.getBlockKey(); + isInteracted |= nearest.interactLocation().add(-1, 0, 0).toBlockKey() == clickedBlock.getBlockKey(); + isInteracted |= nearest.interactLocation().add(0, 0, 1).toBlockKey() == clickedBlock.getBlockKey(); + isInteracted |= nearest.interactLocation().add(0, 0, -1).toBlockKey() == clickedBlock.getBlockKey(); + } + if (isInteracted) { + event.setCancelled(true); + event.setUseItemInHand(Event.Result.DENY); + if (!player.hasPermission("regions.use")) { + player.sendMessage("You cannot use region posts at this time."); + return; + } + if (RegionPostItemWatcher.isChargeItem(handStack)) { + nearest.addCharges(1); + m_plugin.getServer().getScheduler().runTask(m_plugin, () -> { + RegionPostBuilder builder = new RegionPostBuilder(nearest, m_plugin); + builder.updateLantern(); + }); + m_plugin.saveRegions(); + player.setItemInHand(handStack.subtract()); + m_plugin.getServer().getPluginManager().callEvent(new PlayerAddRegionChargeEvent(player, nearest)); + } else if (isBannerItem(handStack)) { + DyeColor bannerColor = DyeColor.getByDyeData(handStack.getData().getData()); + BannerMeta bannerMeta = (BannerMeta)meta; + log.info("Setting banner color to " + bannerColor); + nearest.setBannerPatterns(bannerMeta.getPatterns()); + nearest.setColor(bannerColor); + m_plugin.saveRegions(); + player.sendMessage("You've updated the region post banner"); + m_plugin.getServer().getScheduler().runTask(m_plugin, () -> { + RegionPostBuilder builder = new RegionPostBuilder(nearest, m_plugin); + builder.build(); + }); + } else if (nearest.charges() > 0 || player.hasPermission("regions.bypass.charges")) { + m_plugin.getServer().getPluginManager().callEvent(new PlayerPostInteractEvent(player, nearest)); + } else { + player.sendMessage("This region post is not charged. Right click on it while holding cobblestone."); + } + } + } +} diff --git a/src/main/java/us/camin/regions/RegionPostItemWatcher.java b/src/main/java/us/camin/regions/RegionPostItemWatcher.java new file mode 100644 index 0000000..3c659c7 --- /dev/null +++ b/src/main/java/us/camin/regions/RegionPostItemWatcher.java @@ -0,0 +1,179 @@ +package us.camin.regions; + +/** + * This file is part of Regions + * + * Regions is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Regions is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Regions. If not, see . + * + */ + +import org.bukkit.event.Listener; +import org.bukkit.event.Event; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.ItemStack; +import org.bukkit.Material; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.inventory.meta.CompassMeta; +import org.bukkit.inventory.ItemFlag; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.event.block.Action; + +import us.camin.regions.ui.RegionPostBuilder; + +import java.util.ArrayList; +import java.util.List; + +public class RegionPostItemWatcher implements Listener { + private RegionManager m_manager; + private Plugin m_plugin; + + public RegionPostItemWatcher(Plugin plugin, RegionManager manager) { + m_manager = manager; + m_plugin = plugin; + } + + static public ItemStack createCompass() { + ItemStack stack = new ItemStack(Material.COMPASS); + ItemMeta meta = stack.getItemMeta(); + List lore = new ArrayList(); + lore.add("Right click to locate the nearest Region Post"); + meta.setLore(lore); + meta.addEnchant(Enchantment.SOUL_SPEED, 1, true); + meta.addItemFlags(ItemFlag.HIDE_ENCHANTS); + stack.setItemMeta(meta); + return stack; + } + + static public ItemStack createChargeItem() { + ItemStack stack = new ItemStack(Material.GLOWSTONE_DUST); + ItemMeta meta = stack.getItemMeta(); + List lore = new ArrayList(); + lore.add("Charges or repairs a region post"); + meta.setLore(lore); + meta.addItemFlags(ItemFlag.HIDE_ENCHANTS); + meta.addEnchant(Enchantment.SOUL_SPEED, 1, true); + meta.setDisplayName("Region Post Charge"); + stack.setItemMeta(meta); + return stack; + } + + static public ItemStack createCreateItem() { + ItemStack stack = new ItemStack(Material.LANTERN); + ItemMeta meta = stack.getItemMeta(); + List lore = new ArrayList(); + lore.add("Place to create a new region post"); + meta.addItemFlags(ItemFlag.HIDE_ENCHANTS); + meta.addEnchant(Enchantment.SOUL_SPEED, 1, true); + meta.setLore(lore); + stack.setItemMeta(meta); + return stack; + } + + private ItemStack m_theCompass = createCompass(); + private ItemStack m_theItem = createCreateItem(); + private static ItemStack m_theChargeItem = createChargeItem(); + + public static boolean isChargeItem(ItemStack stack) { + return stack.isSimilar(m_theChargeItem); + } + + public boolean isRegionCompass(ItemStack stack) { + if (stack.isSimilar(m_theCompass)) { + return true; + } + + if (stack.getType() == m_theItem.getType()) { + ItemMeta meta = stack.getItemMeta(); + ItemMeta theItemMeta = m_theItem.getItemMeta(); + if (meta.getItemFlags() == theItemMeta.getItemFlags()) { + return true; + } + } + + return false; + } + + public boolean isRegionCreateItem(ItemStack stack, Player p) { + if (stack.isSimilar(m_theItem)) { + return true; + } + + if (stack.getType() == m_theItem.getType()) { + ItemMeta meta = stack.getItemMeta(); + ItemMeta theItemMeta = m_theItem.getItemMeta(); + if (meta.getLore().equals(theItemMeta.getLore())) { + return true; + } + } + + return false; + } + + @EventHandler + public void onInteract(PlayerInteractEvent event) { + Player player = event.getPlayer(); + ItemStack handStack = event.getItem(); + if (handStack == null) { + return; + } + ItemMeta meta = handStack.getItemMeta(); + + if (isRegionCompass(handStack) && event.getAction() == Action.RIGHT_CLICK_AIR) { + event.setCancelled(true); + ItemStack compassItem = new ItemStack(Material.COMPASS); + Region nearest = m_manager.nearestRegion(player.getLocation()); + if (nearest == null) { + player.sendMessage("There are no regions in this world!"); + return; + } + CompassMeta compassMeta = (CompassMeta)compassItem.getItemMeta(); + compassMeta.setDisplayName(nearest.name()); + compassMeta.setLodestone(nearest.location()); + compassMeta.setLodestoneTracked(false); + ArrayList newLore = new ArrayList(); + newLore.add("Right click to locate the nearest Region Post"); + newLore.add("Tracking: " + nearest.name()); + compassMeta.setLore(newLore); + compassItem.setItemMeta(compassMeta); + player.setItemInHand(compassItem); + player.sendMessage("Now tracking " + nearest.name()); + } else if (!event.isCancelled() && isRegionCreateItem(handStack, player) && event.getAction() == Action.RIGHT_CLICK_BLOCK && !event.getClickedBlock().getType().isInteractable()) { + event.setUseItemInHand(Event.Result.DENY); + event.setCancelled(true); + if (meta.getDisplayName().equals("")) { + player.sendMessage("You must first give this item a name!"); + } else { + Region nearest = m_manager.nearestRegion(player.getLocation()); + if (nearest != null && player.getLocation().distance(nearest.interactLocation()) <= 500) { + player.sendMessage("You are too close to the region post for " + nearest.name()); + } else { + Region r = new Region(meta.getDisplayName(), event.getClickedBlock().getRelative(event.getBlockFace()).getLocation()); + //player.teleport(r.teleportLocation()); + m_plugin.getServer().getScheduler().runTask(m_plugin, () -> { + RegionPostBuilder builder = new RegionPostBuilder(r, m_plugin); + builder.build(); + }); + player.setItemInHand(handStack.subtract()); + m_plugin.regionManager().addRegion(r); + m_plugin.saveRegions(); + player.sendMessage("You established the region "+r.coloredName()); + } + } + } + } +} + diff --git a/src/main/java/us/camin/regions/RegionPostManager.java b/src/main/java/us/camin/regions/RegionPostManager.java new file mode 100644 index 0000000..841f3b5 --- /dev/null +++ b/src/main/java/us/camin/regions/RegionPostManager.java @@ -0,0 +1,141 @@ +package us.camin.regions; + +/** + * This file is part of Regions + * + * Regions is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Regions is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Regions. If not, see . + * + */ + +import org.bukkit.event.Listener; +import org.bukkit.event.EventHandler; +import org.bukkit.Location; +import java.util.HashMap; +import java.util.ArrayList; +import java.util.logging.Logger; + +import com.gmail.filoghost.holographicdisplays.api.HologramsAPI; + +import us.camin.regions.events.PlayerNearRegionPostEvent; +import us.camin.regions.events.RegionCreateEvent; +import us.camin.regions.events.RegionRemoveEvent; +import us.camin.regions.ui.RegionPostBuilder; + +import com.gmail.filoghost.holographicdisplays.api.Hologram; + +public class RegionPostManager implements Listener { + Logger log = Logger.getLogger("Regions.RegionPostManager"); + + Plugin m_plugin; + RegionManager m_regions; + boolean m_useHolograms; + HashMap m_regionHolograms; + //HashMap m_armorStands; + + public RegionPostManager(RegionManager regions, Plugin plugin, boolean useHolograms) { + //m_armorStands = new HashMap(); + m_regions = regions; + m_plugin = plugin; + m_useHolograms = useHolograms; + m_regionHolograms = new HashMap<>(); + } + + @EventHandler + public void onRegionCreate(RegionCreateEvent event) { + Location loc = event.region.location(); + if (loc.getWorld().isChunkLoaded((int)loc.getX(), (int)loc.getY())) { + createHologram(event.region); + queueRebuild(event.region); + } + } + + @EventHandler + public void onPlayerNearRegionPost(PlayerNearRegionPostEvent event) { + createHologram(event.region); + } + + private void queueRebuild(Region region) { + if (region != null) { + log.info("Rebuilding region post for " + region.name()); + m_plugin.getServer().getScheduler().runTask(m_plugin, () -> { + RegionPostBuilder builder = new RegionPostBuilder(region, m_plugin); + builder.build(); + }); + } + } + + @EventHandler + public void onRegionDestroy(RegionRemoveEvent event) { + destroyHologram(event.region); + } + + public void release() { + for(Region r : new ArrayList<>(m_regionHolograms.keySet())) { + destroyHologram(r); + } + /*for(Region r : new ArrayList<>(m_armorStands.keySet())) { + destroyHologram(r); + }*/ + } + + private void createHologram(Region r) { + if (!m_useHolograms) { + return; + } + Hologram hologram = m_regionHolograms.get(r); + if (hologram == null) { + hologram = HologramsAPI.createHologram(m_plugin, r.teleportLocation().clone().add(0.5, 0, 0.5)); + hologram.appendTextLine(r.coloredName()); + m_regionHolograms.put(r, hologram); + } + /*boolean needsRegen = false; + if (m_armorStands.containsKey(r)) { + // If we have an armor stand, but it isn't valid, regen + needsRegen = !m_armorStands.get(r).isValid(); + } else { + // If we don't have an armor stand at all, regen + needsRegen = true; + } + if (needsRegen) { + log.info("Creating hologram for " + r.name()); + Location markerLocation = r.teleportLocation().clone().add(0.5, 0, 0.5); + ArmorStand stand = (ArmorStand) r.location().getWorld().spawnEntity(markerLocation, EntityType.ARMOR_STAND); + stand.setVisible(false); + stand.setCustomName(r.coloredName()); + stand.setMarker(true); + stand.setSmall(true); + stand.setRemoveWhenFarAway(true); + stand.setCustomNameVisible(true); + stand.setInvulnerable(true); + stand.setSilent(true); + m_armorStands.put(r, stand); + }*/ + } + + private void destroyHologram(Region r) { + if (!m_useHolograms) { + return; + } + Hologram hologram = m_regionHolograms.get(r); + if (hologram != null) { + hologram.delete(); + } + /*log.info("Destroying hologram for " + r.name()); + ArmorStand stand = m_armorStands.get(r); + if (stand != null && stand.isValid()) { + stand.remove(); + } + m_armorStands.remove(r);*/ + } +} diff --git a/src/main/java/us/camin/regions/TestRegionGenerator.java b/src/main/java/us/camin/regions/TestRegionGenerator.java new file mode 100644 index 0000000..0c6c22f --- /dev/null +++ b/src/main/java/us/camin/regions/TestRegionGenerator.java @@ -0,0 +1,29 @@ +package us.camin.regions; + +import java.util.Random; +import org.bukkit.World; +import org.bukkit.Location; +import us.camin.regions.ui.RegionPostBuilder; + +public class TestRegionGenerator { + Plugin m_plugin; + public TestRegionGenerator(Plugin plugin) { + m_plugin = plugin; + } + + public void generate() { + String[] regionNames = {"Redstone", "Lapis", "Dwarf City", "Birchshire", "Channelside", "Coldwood", "Vincente", "East Redstone City", "Westernly", "Capital City"}; + Random rand = new Random(); + for(World w : m_plugin.getServer().getWorlds()) { + for(String name : regionNames) { + Location loc = new Location(w, rand.nextInt(800), 64, rand.nextInt(800)); + Region r = new Region(name, loc); + m_plugin.regionManager().addRegion(r); + m_plugin.getServer().getScheduler().runTask(m_plugin, () -> { + RegionPostBuilder builder = new RegionPostBuilder(r, m_plugin); + //builder.build(); + }); + } + } + } +} diff --git a/src/main/java/us/camin/regions/MoveinCommand.java b/src/main/java/us/camin/regions/commands/MoveinCommand.java similarity index 90% rename from src/main/java/us/camin/regions/MoveinCommand.java rename to src/main/java/us/camin/regions/commands/MoveinCommand.java index 201c107..ed6f3fc 100644 --- a/src/main/java/us/camin/regions/MoveinCommand.java +++ b/src/main/java/us/camin/regions/commands/MoveinCommand.java @@ -1,4 +1,4 @@ -package us.camin.regions; +package us.camin.regions.commands; /** * This file is part of Regions @@ -22,7 +22,8 @@ import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.command.Command; import org.bukkit.entity.Player; -import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import us.camin.regions.Plugin; +import us.camin.regions.Region; public class MoveinCommand implements CommandExecutor { @@ -40,7 +41,7 @@ public class MoveinCommand implements CommandExecutor { Player p = (Player)sender; Region nearest = m_plugin.regionManager().nearestRegion(p.getLocation()); if (nearest != null) { - m_plugin.regionManager().setHomeRegion(p.getName(), nearest); + m_plugin.regionManager().setHomeRegion(p, nearest); sender.sendMessage("Your home region has been set to "+nearest.name()); } else { sender.sendMessage("There are no regions in this world."); diff --git a/src/main/java/us/camin/regions/RegionCommand.java b/src/main/java/us/camin/regions/commands/RegionCommand.java similarity index 57% rename from src/main/java/us/camin/regions/RegionCommand.java rename to src/main/java/us/camin/regions/commands/RegionCommand.java index 23bdb9e..c7acf00 100644 --- a/src/main/java/us/camin/regions/RegionCommand.java +++ b/src/main/java/us/camin/regions/commands/RegionCommand.java @@ -1,4 +1,4 @@ -package us.camin.regions; +package us.camin.regions.commands; /** * This file is part of Regions @@ -21,9 +21,17 @@ package us.camin.regions; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.command.Command; +import org.bukkit.command.TabCompleter; import org.bukkit.entity.Player; -public class RegionCommand implements CommandExecutor { +import java.util.List; +import java.util.ArrayList; + +import us.camin.regions.Plugin; +import us.camin.regions.Region; +import us.camin.regions.ui.RegionPostBuilder; + +public class RegionCommand implements CommandExecutor, TabCompleter { Plugin m_plugin; @@ -31,6 +39,17 @@ public class RegionCommand implements CommandExecutor { m_plugin = p; } + public List onTabComplete(CommandSender sender, Command command, String alias, String[] args) { + List ret = new ArrayList(); + if (args.length <= 1) { + ret.add("create"); + ret.add("remove"); + ret.add("regen"); + ret.add("regenall"); + } + return ret; + } + public boolean onCommand(CommandSender sender, Command command, String label, String[] split) { if (!(sender instanceof Player)) { sender.sendMessage("Region command is only available to players."); @@ -43,11 +62,19 @@ public class RegionCommand implements CommandExecutor { p.sendMessage("There are no regions in this world."); return true; } - p.sendMessage("Current region: "+r.name()); + p.sendMessage("Current region: "+r.coloredName()); + p.sendMessage("Players in region:"); + for(Player neighbor : m_plugin.playerWatcher().playersInRegion(r)) { + p.sendMessage(neighbor.getName()); + } return true; } String subCommand = split[0]; if (subCommand.equals("create") && p.hasPermission("regions.create")) { + if (split.length <= 1) { + p.sendMessage("Must specify a region name"); + return true; + } StringBuilder regionName = new StringBuilder(); for(int i = 1;i { + RegionPostBuilder builder = new RegionPostBuilder(r, m_plugin); + builder.build(); + p.sendMessage("Region post regenerated."); + }); } - } else if (subCommand.equals("save") && p.hasPermission("regions.create")) { - m_plugin.saveRegions(); - p.sendMessage("Regions saved."); - } else if (subCommand.equals("load") && p.hasPermission("regions.create")) { - m_plugin.loadRegions(); - p.sendMessage("Regions loaded."); + } else if (subCommand.equals("regenall") && p.hasPermission("regions.regen.all")) { + for(Region r : m_plugin.regionManager().regionsForWorld(p.getLocation().getWorld())) { + m_plugin.getServer().getScheduler().runTask(m_plugin, () -> { + RegionPostBuilder builder = new RegionPostBuilder(r, m_plugin); + builder.build(); + }); + } + p.sendMessage("Region posts will be regenerated"); } else { - p.sendMessage("Unknown operation. Options are create, remove, city."); + p.sendMessage("Unknown operation. Options are: create, remove, regen."); } return true; } diff --git a/src/main/java/us/camin/regions/commands/RegionOpCommand.java b/src/main/java/us/camin/regions/commands/RegionOpCommand.java new file mode 100644 index 0000000..d5418b9 --- /dev/null +++ b/src/main/java/us/camin/regions/commands/RegionOpCommand.java @@ -0,0 +1,99 @@ +package us.camin.regions.commands; + +/** + * This file is part of Regions + * + * Regions is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Regions is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Regions. If not, see . + * + */ + +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.Command; +import org.bukkit.command.TabCompleter; +import java.util.List; +import java.util.ArrayList; +import java.util.HashMap; + +import org.bukkit.inventory.ItemStack; +import org.bukkit.entity.Player; + +import us.camin.regions.Plugin; +import us.camin.regions.RegionPostItemWatcher; + +public class RegionOpCommand implements CommandExecutor, TabCompleter { + Plugin m_plugin; + + public RegionOpCommand(Plugin p) { + m_plugin = p; + } + + public List onTabComplete(CommandSender sender, Command command, String alias, String[] args) { + List ret = new ArrayList(); + ret.add("save"); + ret.add("load"); + ret.add("item"); + ret.add("compass"); + ret.add("chargeitem"); + return ret; + } + + public boolean onCommand(CommandSender sender, Command command, String label, String[] split) { + String subCommand = split[0]; + if (subCommand.equals("save") && sender.hasPermission("regions.create")) { + m_plugin.saveRegions(); + sender.sendMessage("Regions saved."); + } else if (subCommand.equals("load") && sender.hasPermission("regions.create")) { + sender.sendMessage("Reloading regions..."); + m_plugin.getServer().getScheduler().runTask(m_plugin, () -> { + m_plugin.loadRegions(); + sender.sendMessage("Regions loaded."); + }); + } else if (subCommand.equals("compass") && sender.hasPermission("regions.give-items.compass")) { + Player player = (Player)sender; + ItemStack compassItem = RegionPostItemWatcher.createCreateItem(); + if (split.length > 1) { + compassItem.setAmount(Integer.parseInt(split[1])); + } + HashMap rejected = player.getInventory().addItem(compassItem); + for(ItemStack item : rejected.values()) { + player.getLocation().getWorld().dropItem(player.getLocation(), item); + } + } else if (subCommand.equals("item") && sender.hasPermission("regions.give-items.creator")) { + Player player = (Player)sender; + ItemStack createItem = RegionPostItemWatcher.createCreateItem(); + if (split.length > 1) { + createItem.setAmount(Integer.parseInt(split[1])); + } + HashMap rejected = player.getInventory().addItem(createItem); + for(ItemStack item : rejected.values()) { + player.getLocation().getWorld().dropItem(player.getLocation(), item); + } + } else if (subCommand.equals("chargeitem") && sender.hasPermission("regions.give-items.charge")) { + Player player = (Player)sender; + ItemStack chargeItem = RegionPostItemWatcher.createChargeItem(); + if (split.length > 1) { + chargeItem.setAmount(Integer.parseInt(split[1])); + } + HashMap rejected = player.getInventory().addItem(chargeItem); + for(ItemStack item : rejected.values()) { + player.getLocation().getWorld().dropItem(player.getLocation(), item); + } + } else { + sender.sendMessage("Unknown operation. Options are save, load, item, compass."); + } + return true; + } +} + diff --git a/src/main/java/us/camin/regions/commands/RegionTabComplete.java b/src/main/java/us/camin/regions/commands/RegionTabComplete.java new file mode 100644 index 0000000..45c99a2 --- /dev/null +++ b/src/main/java/us/camin/regions/commands/RegionTabComplete.java @@ -0,0 +1,37 @@ +package us.camin.regions.commands; + +import java.util.ArrayList; +import java.util.List; +import java.util.Collection; +import org.bukkit.command.TabCompleter; +import org.bukkit.command.CommandSender; +import org.bukkit.command.Command; +import org.bukkit.entity.Player; + +import us.camin.regions.Plugin; +import us.camin.regions.Region; + +public class RegionTabComplete implements TabCompleter { + private Plugin m_plugin; + + public RegionTabComplete(Plugin plugin) { + m_plugin = plugin; + } + + public List onTabComplete(CommandSender sender, Command command, String alias, String[] args) { + if (!(sender instanceof Player)) { + return new ArrayList(); + } + Player p = (Player)sender; + String fullName = String.join(" ", args); + + ArrayList ret = new ArrayList(); + Collection regions = m_plugin.regionManager().regionsForWorld(p.getLocation().getWorld()); + for (Region r : regions) { + if (r.name().startsWith(fullName)) { + ret.add(r.name()); + } + } + return ret; + } +} diff --git a/src/main/java/us/camin/regions/commands/RegionsCommand.java b/src/main/java/us/camin/regions/commands/RegionsCommand.java new file mode 100644 index 0000000..ea7b38f --- /dev/null +++ b/src/main/java/us/camin/regions/commands/RegionsCommand.java @@ -0,0 +1,70 @@ +package us.camin.regions.commands; + +/** + * This file is part of Regions + * + * Regions is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Regions is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Regions. If not, see . + * + */ + +import org.bukkit.command.CommandExecutor; +import org.bukkit.command.CommandSender; +import org.bukkit.command.Command; +import org.bukkit.entity.Player; +import org.bukkit.command.TabCompleter; +import org.bukkit.World; + +import java.util.List; +import java.util.ArrayList; + +import us.camin.regions.Plugin; +import us.camin.regions.Region; + +public class RegionsCommand implements CommandExecutor, TabCompleter { + Plugin m_plugin; + + public RegionsCommand(Plugin p) { + m_plugin = p; + } + + public List onTabComplete(CommandSender sender, Command command, String alias, String[] args) { + List ret = new ArrayList(); + for(World w : m_plugin.getServer().getWorlds()) { + ret.add(w.getName()); + } + return ret; + } + + public boolean onCommand(CommandSender sender, Command command, String label, String[] split) { + World world = null; + if (sender instanceof Player) { + Player p = (Player)sender; + world = p.getLocation().getWorld(); + } + if (split.length >= 0) { + world = m_plugin.getServer().getWorld(String.join(" ", split)); + } + + if (world == null) { + sender.sendMessage("Please specify a world."); + return true; + } + + sender.sendMessage("Regions in this world:"); + for (Region r : m_plugin.regionManager().regionsForWorld(world)) { + sender.sendMessage(r.coloredName()); + } + return true; + } +} diff --git a/src/main/java/us/camin/regions/config/RegionConfiguration.java b/src/main/java/us/camin/regions/config/RegionConfiguration.java new file mode 100644 index 0000000..c7f6a22 --- /dev/null +++ b/src/main/java/us/camin/regions/config/RegionConfiguration.java @@ -0,0 +1,75 @@ +package us.camin.regions.config; + +import java.util.HashMap; +import java.util.Map; +import java.util.List; +import java.util.ArrayList; + +import org.bukkit.Location; +import org.bukkit.block.banner.Pattern; +import org.bukkit.configuration.serialization.ConfigurationSerializable; +import org.bukkit.DyeColor; +import org.bukkit.Color; +import org.bukkit.OfflinePlayer; + +import us.camin.regions.Region; +import java.util.UUID; + +public class RegionConfiguration implements ConfigurationSerializable { + public int x; + public int y; + public int z; + public int visits; + public int charges; + public boolean isHub; + public List patterns; + public List seenBy; + public DyeColor color = DyeColor.YELLOW; + + public RegionConfiguration(Region region) { + Location loc = region.location(); + x = loc.getBlockX(); + y = loc.getBlockY(); + z = loc.getBlockZ(); + charges = region.charges(); + isHub = region.isHub(); + visits = region.visits(); + patterns = region.bannerPatterns(); + color = region.color(); + seenBy = region.seenPlayers(); + } + + public RegionConfiguration(Map confSection) { + x = (Integer)confSection.get("x"); + y = (Integer)confSection.getOrDefault("y", -1); + z = (Integer)confSection.get("z"); + isHub = (Boolean)confSection.getOrDefault("isHub", false); + visits = (Integer)confSection.getOrDefault("visits", 0); + charges = (Integer)confSection.getOrDefault("charges", 0); + patterns = (List)confSection.getOrDefault("banner", new ArrayList()); + color = DyeColor.valueOf((String)confSection.getOrDefault("color", "YELLOW")); + seenBy = new ArrayList(); + List strList = (List)confSection.getOrDefault("seenBy", new ArrayList()); + for(String s : strList) { + seenBy.add(UUID.fromString(s)); + } + } + + public Map serialize() { + Map ret = new HashMap<>(); + ret.put("x", x); + ret.put("y", y); + ret.put("z", z); + ret.put("visits", visits); + ret.put("charges", charges); + ret.put("banner", patterns); + List strList = new ArrayList(); + for(UUID uuid : seenBy) { + strList.add(uuid.toString()); + } + ret.put("seenBy", strList); + ret.put("color", color.toString()); + ret.put("isHub", isHub); + return ret; + } +} diff --git a/src/main/java/us/camin/regions/config/WorldConfiguration.java b/src/main/java/us/camin/regions/config/WorldConfiguration.java new file mode 100644 index 0000000..8f9d9e8 --- /dev/null +++ b/src/main/java/us/camin/regions/config/WorldConfiguration.java @@ -0,0 +1,26 @@ +package us.camin.regions.config; + +import java.util.HashMap; +import java.util.Map; + +import org.bukkit.configuration.serialization.ConfigurationSerializable; + +public class WorldConfiguration implements ConfigurationSerializable { + public Map regions = new HashMap<>(); + + public WorldConfiguration(Map confSection) { + Map regionConfigs = (Map)confSection.get("regions"); + for(String regionName : regionConfigs.keySet()) { + regions.put(regionName, new RegionConfiguration((Map)regionConfigs.get(regionName))); + } + } + + public Map serialize() { + Map ret = new HashMap<>(); + Map regionConfigs = new HashMap<>(); + for(String regionName : regions.keySet()) { + regionConfigs.put(regionName, regions.get(regionName).serialize()); + } + return ret; + } +} \ No newline at end of file diff --git a/src/main/java/us/camin/regions/events/PlayerAddRegionChargeEvent.java b/src/main/java/us/camin/regions/events/PlayerAddRegionChargeEvent.java new file mode 100644 index 0000000..9277f7a --- /dev/null +++ b/src/main/java/us/camin/regions/events/PlayerAddRegionChargeEvent.java @@ -0,0 +1,45 @@ +package us.camin.regions.events; + +/** + * This file is part of Regions + * + * Regions is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Regions is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Regions. If not, see . + * + */ + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +import us.camin.regions.Region; +import org.bukkit.entity.Player; + +public class PlayerAddRegionChargeEvent extends Event { + private static final HandlerList s_handlers = new HandlerList(); + public final Region region; + public final Player player; + + public PlayerAddRegionChargeEvent(Player p, Region region ) { + this.region = region; + this.player = p; + } + + @Override + public HandlerList getHandlers() { + return s_handlers; + } + + public static HandlerList getHandlerList() { + return s_handlers; + } +} diff --git a/src/main/java/us/camin/regions/events/PlayerMoveInEvent.java b/src/main/java/us/camin/regions/events/PlayerMoveInEvent.java new file mode 100644 index 0000000..6e90aa9 --- /dev/null +++ b/src/main/java/us/camin/regions/events/PlayerMoveInEvent.java @@ -0,0 +1,49 @@ +package us.camin.regions.events; + +/** + * This file is part of Regions + * + * Regions is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Regions is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Regions. If not, see . + * + */ + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +import us.camin.regions.Region; + +import org.bukkit.entity.Player; + +public class PlayerMoveInEvent extends Event { + + private static final HandlerList s_handlers = new HandlerList(); + public final Region region; + public final Region oldRegion; + public final Player player; + + public PlayerMoveInEvent(Player p, Region region, Region oldRegion) { + this.region = region; + this.oldRegion = oldRegion; + this.player = p; + } + + @Override + public HandlerList getHandlers() { + return s_handlers; + } + + public static HandlerList getHandlerList() { + return s_handlers; + } +} diff --git a/src/main/java/us/camin/regions/events/PlayerNearRegionPostEvent.java b/src/main/java/us/camin/regions/events/PlayerNearRegionPostEvent.java new file mode 100644 index 0000000..62e799d --- /dev/null +++ b/src/main/java/us/camin/regions/events/PlayerNearRegionPostEvent.java @@ -0,0 +1,47 @@ +package us.camin.regions.events; + +/** + * This file is part of Regions + * + * Regions is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Regions is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Regions. If not, see . + * + */ + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +import us.camin.regions.Region; + +import org.bukkit.entity.Player; + +public class PlayerNearRegionPostEvent extends Event { + private static final HandlerList s_handlers = new HandlerList(); + public final Region region; + public final Player player; + + public PlayerNearRegionPostEvent(Player p, Region region) { + this.region = region; + this.player = p; + } + + @Override + public HandlerList getHandlers() { + return s_handlers; + } + + public static HandlerList getHandlerList() { + return s_handlers; + } +} + diff --git a/src/main/java/us/camin/regions/RegionAPI.java b/src/main/java/us/camin/regions/events/PlayerPostInteractEvent.java similarity index 52% rename from src/main/java/us/camin/regions/RegionAPI.java rename to src/main/java/us/camin/regions/events/PlayerPostInteractEvent.java index 7144493..1f38116 100644 --- a/src/main/java/us/camin/regions/RegionAPI.java +++ b/src/main/java/us/camin/regions/events/PlayerPostInteractEvent.java @@ -1,4 +1,4 @@ -package us.camin.regions; +package us.camin.regions.events; /** * This file is part of Regions @@ -18,6 +18,31 @@ package us.camin.regions; * */ -public interface RegionAPI { - public RegionManager regionManager(); +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +import us.camin.regions.Region; + +import org.bukkit.entity.Player; + +public class PlayerPostInteractEvent extends Event { + + private static final HandlerList s_handlers = new HandlerList(); + public final Region region; + public final Player player; + + public PlayerPostInteractEvent(Player p, Region region) { + this.region = region; + this.player = p; + } + + @Override + public HandlerList getHandlers() { + return s_handlers; + } + + public static HandlerList getHandlerList() { + return s_handlers; + } } + diff --git a/src/main/java/us/camin/regions/PlayerRegionChangeEvent.java b/src/main/java/us/camin/regions/events/PlayerRegionChangeEvent.java similarity index 95% rename from src/main/java/us/camin/regions/PlayerRegionChangeEvent.java rename to src/main/java/us/camin/regions/events/PlayerRegionChangeEvent.java index 078f7e8..916338b 100644 --- a/src/main/java/us/camin/regions/PlayerRegionChangeEvent.java +++ b/src/main/java/us/camin/regions/events/PlayerRegionChangeEvent.java @@ -1,4 +1,4 @@ -package us.camin.regions; +package us.camin.regions.events; /** * This file is part of Regions @@ -20,6 +20,9 @@ package us.camin.regions; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; + +import us.camin.regions.Region; + import org.bukkit.entity.Player; public class PlayerRegionChangeEvent extends Event { diff --git a/src/main/java/us/camin/regions/RegionCreateEvent.java b/src/main/java/us/camin/regions/events/RegionCreateEvent.java similarity index 94% rename from src/main/java/us/camin/regions/RegionCreateEvent.java rename to src/main/java/us/camin/regions/events/RegionCreateEvent.java index 838e1ce..530e467 100644 --- a/src/main/java/us/camin/regions/RegionCreateEvent.java +++ b/src/main/java/us/camin/regions/events/RegionCreateEvent.java @@ -1,4 +1,4 @@ -package us.camin.regions; +package us.camin.regions.events; /** * This file is part of Regions @@ -18,9 +18,10 @@ package us.camin.regions; * */ -import org.bukkit.event.Event; import org.bukkit.event.HandlerList; +import us.camin.regions.Region; + public class RegionCreateEvent extends RegionEvent { public static final HandlerList s_handlers = new HandlerList(); diff --git a/src/main/java/us/camin/regions/RegionEvent.java b/src/main/java/us/camin/regions/events/RegionEvent.java similarity index 94% rename from src/main/java/us/camin/regions/RegionEvent.java rename to src/main/java/us/camin/regions/events/RegionEvent.java index 2f94309..34af11e 100644 --- a/src/main/java/us/camin/regions/RegionEvent.java +++ b/src/main/java/us/camin/regions/events/RegionEvent.java @@ -1,4 +1,4 @@ -package us.camin.regions; +package us.camin.regions.events; /** * This file is part of Regions @@ -21,6 +21,8 @@ package us.camin.regions; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; +import us.camin.regions.Region; + public abstract class RegionEvent extends Event { private static final HandlerList s_handlers = new HandlerList(); public final Region region; diff --git a/src/main/java/us/camin/regions/RegionRemoveEvent.java b/src/main/java/us/camin/regions/events/RegionRemoveEvent.java similarity index 94% rename from src/main/java/us/camin/regions/RegionRemoveEvent.java rename to src/main/java/us/camin/regions/events/RegionRemoveEvent.java index 8584c08..2414531 100644 --- a/src/main/java/us/camin/regions/RegionRemoveEvent.java +++ b/src/main/java/us/camin/regions/events/RegionRemoveEvent.java @@ -1,4 +1,4 @@ -package us.camin.regions; +package us.camin.regions.events; /** * This file is part of Regions @@ -18,9 +18,10 @@ package us.camin.regions; * */ -import org.bukkit.event.Event; import org.bukkit.event.HandlerList; +import us.camin.regions.Region; + public class RegionRemoveEvent extends RegionEvent { public static final HandlerList s_handlers = new HandlerList(); diff --git a/src/main/java/us/camin/regions/geometry/BorderMesh.java b/src/main/java/us/camin/regions/geometry/BorderMesh.java new file mode 100644 index 0000000..b79ecf8 --- /dev/null +++ b/src/main/java/us/camin/regions/geometry/BorderMesh.java @@ -0,0 +1,248 @@ +package us.camin.regions.geometry; + +import java.util.ArrayList; +import java.util.List; +import java.util.Collection; +import java.util.Map; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Set; +import java.util.logging.Logger; + +import org.bukkit.Location; + +import io.github.jdiemke.triangulation.DelaunayTriangulator; +import io.github.jdiemke.triangulation.NotEnoughPointsException; +import io.github.jdiemke.triangulation.Vector2D; +import io.github.jdiemke.triangulation.Triangle2D; + +import us.camin.regions.Region; + +public class BorderMesh { + Logger log = Logger.getLogger("Regions.Geometry.BorderMesh"); + Collection m_regions; + Map m_polygons; + Map> m_neighbors; + + public Collection neighbors(Region region) { + Collection ret = new ArrayList(); + if (m_neighbors.containsKey(region)) { + Collection allNeighbors = m_neighbors.get(region); + if (false /*isFrontier(region)*/) { + //log.trace("Region " + region.name() + " is a frontier"); + for(Region neighbor : allNeighbors) { + if (!isFrontier(neighbor)) { + ret.add(neighbor); + } else { + //log.trace("Not linking " + region.name() + " to " + neighbor.name() + " as it is also a frontier"); + } + } + } else { + //log.trace("Region " + region.name() + " is not a frontier"); + for(Region neighbor : allNeighbors) { + ret.add(neighbor); + } + } + } + return ret; + } + + public BorderMesh(Collection regions) { + m_regions = regions; + m_polygons = new HashMap(); + m_neighbors = new HashMap>(); + } + + public class Polygon { + public double x[]; + public double y[]; + public double z[]; + + public Polygon(List points) { + x = new double[points.size()]; + y = new double[points.size()]; + z = new double[points.size()]; + + for(int i = 0; i < points.size(); i++) { + Vector2D borderPoint = points.get(i); + if (Double.isNaN(borderPoint.x) || Double.isNaN(borderPoint.y)) { + continue; + } + //log.info("Coords " + borderPoint.x + ", " + borderPoint.y); + x[i] = borderPoint.x; + y[i] = 64; + z[i] = borderPoint.y; + } + } + + public boolean contains(double testx, double testy) { + return crossings(testx, testy) % 2 == 0; + } + + public int crossings(double testx, double testy) { + // winding rule algorithm + int nvert = x.length; + int i, j; + int crossings = 0; + for (i = 0, j = nvert-1; i < nvert; j = i++) { + if ( ((z[i]>testy) != (z[j]>testy)) && + (testx < (x[j]-x[i]) * (testy-z[i]) / (z[j]-z[i]) + x[i]) ) + crossings++; + } + return crossings; + } + } + + public Polygon polygonForRegion(Region r) { + return m_polygons.get(r); + } + + public boolean triangulate() { + // Create a list of points that we'll then use Delaunay on to form a mesh + List regionPoints = new ArrayList(); + for (Region r : m_regions) { + Location loc = r.location(); + Vector2D vec = new Vector2D(loc.getBlockX(), loc.getBlockZ()); + regionPoints.add(vec); + } + DelaunayTriangulator mesh = new DelaunayTriangulator(regionPoints); + try { + mesh.triangulate(); + log.info("Mesh triangulated!"); + } catch (NullPointerException e) { + log.warning("Got a null pointer when triangulating, skipping world."); + return false; + } catch (NotEnoughPointsException e) { + log.info("Not enough points to triangulate mesh. Add more regions!"); + return false; + } + + ArrayList triangleSoup = new ArrayList(); + + for(Region region : m_regions) { + log.info("Creating region mesh for " + region.name()); + Location loc = region.location(); + Vector2D regionCenter = new Vector2D(loc.getBlockX(), loc.getBlockZ()); + + for(Triangle2D tri : mesh.getTriangles()) { + if (vecEquals(tri.a, regionCenter)) { + triangleSoup.add(new Triangle(tri, region)); + } else if (vecEquals(tri.b, regionCenter)) { + triangleSoup.add(new Triangle(tri, region)); + } else if (vecEquals(tri.c, regionCenter)) { + triangleSoup.add(new Triangle(tri, region)); + } + } + } + + // Now we go back through our region mesh to generate vornoi points + for(Region region : m_regions) { + ArrayList points = new ArrayList(); + HashSet neighbors = new HashSet(); + log.info("Executing voronoi transform..."); + for(Triangle tri : triangleSoup) { + if (tri.region == region) { + for (Region neighbor : m_regions) { + if (neighbor == region) { + continue; + } + Location neighborLoc = neighbor.location(); + Vector2D neighborCenter = new Vector2D(neighborLoc.getBlockX(), neighborLoc.getBlockZ()); + if (vecEquals(tri.a, neighborCenter) || vecEquals(tri.b, neighborCenter) || vecEquals(tri.c, neighborCenter)) { + neighbors.add(neighbor); + } + } + points.add(tri.circumcenter()); + } + } + + // Sort points into a renderable polygon based on direction to center + Location loc = region.location(); + Vector2D regionCenter = new Vector2D(loc.getBlockX(), loc.getBlockZ()); + points.sort((Vector2D a, Vector2D b) -> Double.compare(direction(a, regionCenter), direction(b, regionCenter))); + log.info("Border for " + region.name() + " is defined by " + points.size() + " points"); + Polygon polygon = new Polygon(points); + m_polygons.put(region, polygon); + m_neighbors.put(region, neighbors); + } + return true; + } + + /*public boolean validNeighbors(Region regionA, Region regionB) { + Polygon polyA = polygonForRegion(regionA); + Polygon polyB = polygonForRegion(regionA); + Location center = region.location(); + if (poly.contains(center.getBlockX(), center.getBlockZ())) { + return false; + } else { + return true; + } + }*/ + + public boolean isFrontier(Region region) { + Polygon poly = polygonForRegion(region); + Location center = region.location(); + if (poly.contains(center.getBlockX(), center.getBlockZ())) { + return false; + } else { + return true; + } + } + + private boolean vecEquals(Vector2D a, Vector2D b) { + return a.x == b.x && a.y == b.y; + } + + class Triangle { + Vector2D a; + Vector2D b; + Vector2D c; + Region region; + + public Triangle(Vector2D a, Vector2D b, Vector2D c, Region region) { + this.a = a; + this.b = b; + this.c = c; + this.region = region; + } + + public Triangle(Triangle2D tri, Region region) { + this.a = tri.a; + this.b = tri.b; + this.c = tri.c; + this.region = region; + } + + public boolean equals(Triangle o) { + return vecEquals(o.a, a) && vecEquals(o.b, b) && vecEquals(o.c, c); + } + + private Vector2D midpoint(Vector2D a, Vector2D b) { + return new Vector2D((a.x + b.x) / 2, (a.y + b.y) / 2); + } + + private double slope(Vector2D from, Vector2D to) { + return (to.y - from.y) / (to.x - from.x); + } + + public Vector2D circumcenter() { + Vector2D midAB = midpoint(a, b); + Vector2D midBC = midpoint(b, c); + + double slopeAB = -1 / slope(a, b); + double slopeBC = -1 / slope(b, c); + + double bAB = midAB.y - slopeAB * midAB.x; + double bBC = midBC.y - slopeBC * midBC.x; + + double x = (bAB - bBC) / (slopeBC - slopeAB); + + return new Vector2D(x, (slopeAB * x) + bAB); + } + } + + private double direction(Vector2D a, Vector2D b) { + double dir = Math.atan2(a.y - b.y, a.x - b.x); + return dir; + } +} diff --git a/src/main/java/us/camin/regions/geometry/RegionSet.java b/src/main/java/us/camin/regions/geometry/RegionSet.java new file mode 100644 index 0000000..fc5685b --- /dev/null +++ b/src/main/java/us/camin/regions/geometry/RegionSet.java @@ -0,0 +1,59 @@ +package us.camin.regions.geometry; + +import us.camin.regions.Region; + +import org.bukkit.Location; + +import java.util.HashSet; +import java.util.AbstractSet; +import java.util.Set; +import java.util.Iterator; + +public class RegionSet extends AbstractSet { + private Set m_regions = new HashSet(); + + public Iterator iterator() { + return m_regions.iterator(); + } + + public int size() { + return m_regions.size(); + } + + public Region nearestRegion(Location loc) { + Region nearest = null; + int minDistance = -1; + for(Region r : m_regions) { + int check = r.distanceTo(loc); + if (minDistance == -1 || check < minDistance) { + nearest = r; + minDistance = check; + } + } + return nearest; + } + + public boolean add(Region r) { + boolean ret = m_regions.add(r); + if (ret) { + m_mesh = new BorderMesh(m_regions); + m_mesh.triangulate(); + } + return ret; + } + + public boolean remove(Region r) { + boolean ret = m_regions.remove(r); + if (ret) { + m_mesh = new BorderMesh(m_regions); + m_mesh.triangulate(); + } + return ret; + } + + public BorderMesh borders() { + return m_mesh; + } + + BorderMesh m_mesh = null; +} diff --git a/src/main/java/us/camin/regions/ui/Colors.java b/src/main/java/us/camin/regions/ui/Colors.java new file mode 100644 index 0000000..9c3a538 --- /dev/null +++ b/src/main/java/us/camin/regions/ui/Colors.java @@ -0,0 +1,72 @@ +package us.camin.regions.ui; + +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.Color; +import org.bukkit.DyeColor; + +public class Colors { + public static ChatColor[] regionColors = { + ChatColor.AQUA, + ChatColor.BLACK, + ChatColor.BLUE, + ChatColor.BOLD, + ChatColor.DARK_AQUA, + ChatColor.DARK_BLUE, + ChatColor.DARK_GRAY, + ChatColor.DARK_GREEN, + ChatColor.DARK_PURPLE, + ChatColor.DARK_RED, + ChatColor.GOLD, + ChatColor.GRAY, + ChatColor.GREEN, + ChatColor.LIGHT_PURPLE, + ChatColor.RED, + ChatColor.WHITE, + ChatColor.YELLOW, + }; + + public static ChatColor chatColorForName(String name) { + int colorCount = regionColors.length; + int hashed = Math.abs(name.hashCode()); + return regionColors[hashed % (colorCount - 1)]; + } + + public static ChatColor chatColorForColor(DyeColor color) { + switch (color) { + case BLACK: + return ChatColor.BLACK; + case BLUE: + return ChatColor.BLUE; + case BROWN: + return ChatColor.DARK_RED; + case CYAN: + return ChatColor.AQUA; + case GRAY: + return ChatColor.GRAY; + case GREEN: + return ChatColor.GREEN; + case LIGHT_BLUE: + return ChatColor.BLUE; + case LIGHT_GRAY: + return ChatColor.GRAY; + case LIME: + return ChatColor.GREEN; + case MAGENTA: + return ChatColor.LIGHT_PURPLE; + case ORANGE: + return ChatColor.GOLD; + case PINK: + return ChatColor.RED; + case PURPLE: + return ChatColor.DARK_PURPLE; + case RED: + return ChatColor.RED; + case WHITE: + return ChatColor.WHITE; + case YELLOW: + return ChatColor.YELLOW; + } + return ChatColor.WHITE; + } +} diff --git a/src/main/java/us/camin/regions/ui/PlayerInventoryTeleporter.java b/src/main/java/us/camin/regions/ui/PlayerInventoryTeleporter.java new file mode 100644 index 0000000..6f7357d --- /dev/null +++ b/src/main/java/us/camin/regions/ui/PlayerInventoryTeleporter.java @@ -0,0 +1,210 @@ +package us.camin.regions.ui; + +/** + * This file is part of Regions + * + * Regions is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Regions is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Regions. If not, see . + * + */ + +import java.util.Map; +import java.util.HashMap; +import java.util.ArrayList; +import java.util.Optional; +import org.bukkit.event.Listener; +import org.bukkit.event.EventHandler; +import org.bukkit.ChatColor; +import org.bukkit.event.Event.Result; +import org.bukkit.DyeColor; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.Sound; +import org.bukkit.entity.Player; +import org.bukkit.Location; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.inventory.meta.CompassMeta; +import org.bukkit.inventory.meta.BannerMeta; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemFlag; +import org.bukkit.Material; +import org.bukkit.material.MaterialData; + +import us.camin.regions.Plugin; +import us.camin.regions.Region; +import us.camin.regions.RegionManager; +import us.camin.regions.events.PlayerPostInteractEvent; + +import org.bukkit.event.inventory.InventoryType; +import org.bukkit.entity.HumanEntity; +import org.bukkit.Bukkit; + +import java.util.Collection; +import java.lang.Math; + +public class PlayerInventoryTeleporter implements Listener { + Map m_playerInventories; + Plugin m_plugin; + RegionManager m_manager; + + public PlayerInventoryTeleporter(Plugin plugin, RegionManager manager) { + m_plugin = plugin; + m_manager = manager; + m_playerInventories = new HashMap<>(); + } + + @EventHandler + public void onInventory(InventoryClickEvent event) { + if (event.getCurrentItem() == null) { + return; + } + + HumanEntity clicker = event.getWhoClicked(); + + if (clicker instanceof Player) { + Player player = (Player)clicker; + Inventory neighborInv = m_playerInventories.get(player); + if (neighborInv == null) { + return; + } + + if (event.getView().getTopInventory() == neighborInv) { + event.setResult(Result.DENY); + event.setCancelled(true); + } + + if (event.getClickedInventory() == neighborInv) { + ItemMeta meta = event.getCurrentItem().getItemMeta(); + Material mat = event.getCurrentItem().getType(); + if (mat == Material.BEDROCK || mat == Material.COMPASS || mat == Material.LANTERN) { + return; + } + m_plugin.getServer().getScheduler().runTask(m_plugin, () -> event.getView().close()); + Region nearest = m_manager.nearestRegion(player.getLocation()); + + final String selectedName = meta.getDisplayName(); + final Optional destination = m_plugin.regionManager().regionsForWorld(player.getLocation().getWorld()) + .stream() + .filter((r) -> r.name().equals(selectedName)) + .findFirst(); + if (destination.isPresent()) { + player.sendMessage("Teleporting you there now..."); + nearest.addCharges(-1); + destination.get().addCharges(1); + m_plugin.getServer().getScheduler().runTask(m_plugin, () -> { + RegionPostBuilder builder = new RegionPostBuilder(nearest, m_plugin); + builder.updateLantern(); + }); + m_plugin.saveRegions(); + new PlayerTeleporter(player, nearest.teleportLocation(), destination.get().teleportLocation(), m_plugin).teleport().thenRun(() -> { + RegionPostBuilder builder = new RegionPostBuilder(destination.get(), m_plugin); + builder.updateLantern(); + }); + if (nearest.charges() == 0) { + nearest.location().getWorld().playSound(player.getLocation(), Sound.ENTITY_ITEM_BREAK, (float)1.0, (float)1.0); + //player.sendMessage("You've consumed this region post's last charge! You won't be able to return without recharging it."); + } + } else { + player.sendMessage("There is no region with that name. This is a bug."); + } + } + } + } + + @EventHandler + public void onPlayerInteract(PlayerPostInteractEvent event) { + Collection nearby = m_manager.neighborsForRegion(event.region); + Collection hubs = m_manager.worldHubs(event.region.location().getWorld()); + if (!m_playerInventories.containsKey(event.player)) { + m_playerInventories.put(event.player, Bukkit.createInventory(null, InventoryType.CHEST, "Nearby Regions")); + } + Inventory neighborInventory = m_playerInventories.get(event.player); + neighborInventory.clear(); + + ArrayList sorted = new ArrayList(); + ArrayList foundNames = new ArrayList(); + Region nearest = m_manager.nearestRegion(event.player.getLocation()); + for(Region r : nearby) { + if (!r.name().equals(nearest.name())) { + foundNames.add(r.name()); + sorted.add(r); + } + } + for(Region r : hubs) { + if (!foundNames.contains(r.name()) && !r.name().equals(nearest.name())) { + foundNames.add(r.name()); + sorted.add(r); + } + } + sorted.sort((Region a, Region b) -> Integer.compare(b.visits(), a.visits())); + + for(Region region : sorted) { + boolean isHub = hubs.contains(region); + boolean visible = isHub || region.seenByPlayer(event.player) || event.player.hasPermission("regions.bypass.discovery"); + ItemStack item; + if (visible) { + item = region.icon(); + } else { + item = new ItemStack(Material.BEDROCK); + } + ItemMeta meta = item.getItemMeta(); + meta.setDisplayName(region.name()); + ArrayList lore = new ArrayList(); + Location center = region.location(); + int altitude = center.getBlockY(); + if (visible) { + if (isHub) { + lore.add("" + ChatColor.GOLD + ChatColor.BOLD + "World hub" + ChatColor.GOLD + " - Accessable from anywhere"); + meta.addItemFlags(ItemFlag.HIDE_ENCHANTS); + meta.addEnchant(Enchantment.SOUL_SPEED, 1, true); + } + lore.add(ChatColor.WHITE + "Distance: " + ChatColor.YELLOW + Math.round(region.location().distance(event.region.location()))); + lore.add(ChatColor.WHITE + "Population: " + ChatColor.YELLOW + m_plugin.playerWatcher().playersInRegion(region).size()); + lore.add(ChatColor.WHITE + "Altitude: " + ChatColor.YELLOW + altitude); + Collection neighborNeighbors = m_manager.neighborsForRegion(region); + ArrayList neighborNames = new ArrayList(); + for(Region r : neighborNeighbors) { + if (!foundNames.contains(r.name())) { + neighborNames.add(r.coloredName()); + } + } + lore.add("Nearby connections: " + String.join(", ", neighborNames)); + } else { + lore.add("" + ChatColor.BOLD + ChatColor.GOLD + "You haven't discovered this location yet!"); + lore.add(ChatColor.WHITE + "Distance: " + ChatColor.YELLOW + ChatColor.MAGIC + Math.round(region.location().distance(event.region.location()))); + lore.add(ChatColor.WHITE + "Population: " + ChatColor.YELLOW + ChatColor.MAGIC + m_plugin.playerWatcher().playersInRegion(region).size()); + lore.add(ChatColor.WHITE + "Altitude: " + ChatColor.YELLOW + ChatColor.MAGIC + altitude); + lore.add(ChatColor.GOLD + "Coordinates: " + ChatColor.YELLOW + region.location().getX() + ", "+ region.location().getZ()); + } + meta.setLore(lore); + item.setItemMeta(meta); + neighborInventory.addItem(item); + } + ItemStack chargesItem = new ItemStack(event.region.charges() == 0 ? Material.SOUL_LANTERN : Material.LANTERN); + ItemMeta meta = chargesItem.getItemMeta(); + meta.setDisplayName(ChatColor.BOLD + "Region Post Configuration"); + ArrayList lore = new ArrayList(); + lore.add(ChatColor.WHITE + "Name: "+event.region.coloredName()); + lore.add(ChatColor.WHITE + "Visits: " + ChatColor.YELLOW + event.region.visits()); + lore.add(ChatColor.WHITE + "Charges remaining: " + (event.region.charges() == 0 ? ChatColor.RED : ChatColor.GREEN) + event.region.charges()); + meta.setLore(lore); + meta.addItemFlags(ItemFlag.HIDE_ENCHANTS); + meta.addEnchant(Enchantment.SOUL_SPEED, 1, true); + chargesItem.setItemMeta(meta); + // 22 Is the middle of the bottom row + neighborInventory.setItem(22, chargesItem); + + event.player.openInventory(neighborInventory); + } +} diff --git a/src/main/java/us/camin/regions/ui/PlayerTeleporter.java b/src/main/java/us/camin/regions/ui/PlayerTeleporter.java new file mode 100644 index 0000000..0c5f720 --- /dev/null +++ b/src/main/java/us/camin/regions/ui/PlayerTeleporter.java @@ -0,0 +1,88 @@ +package us.camin.regions.ui; + +import org.bukkit.entity.Player; +import org.bukkit.Location; +import org.bukkit.scheduler.BukkitTask; +import org.bukkit.Particle; +import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; +import org.bukkit.scheduler.BukkitScheduler; +import org.bukkit.World; +import org.bukkit.Sound; +import io.papermc.lib.PaperLib; +import us.camin.regions.Plugin; + +import java.util.logging.Logger; +import org.bukkit.potion.PotionEffectType; +import org.bukkit.potion.PotionEffect; +import java.util.concurrent.CompletableFuture; + +public class PlayerTeleporter { + Logger log = Logger.getLogger("Regions.PlayerTeleporter"); + + private Player m_player; + private Location m_src; + private Location m_dest; + private Plugin m_plugin; + + public PlayerTeleporter(Player p, Location src, Location dest, Plugin plugin) { + this.m_player = p; + this.m_src = src; + this.m_dest = dest; + this.m_plugin = plugin; + } + + public CompletableFuture teleport() { + CompletableFuture ret = new CompletableFuture(); + BukkitScheduler scheduler = m_plugin.getServer().getScheduler(); + + BukkitTask hoverGenerator = scheduler.runTaskTimer(m_plugin, () -> applyHoverEffect(), 0, 10); + BukkitTask particleGenerator = scheduler.runTaskTimer(m_plugin, () -> spawnHoverParticles(), 0, 1); + playTeleportSound(); + + m_plugin.getServer().getScheduler().runTaskLater(m_plugin, () -> { + m_player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 20 * 3 + 120, 1, false, false, false)); + }, 20 * 3); + + m_plugin.getServer().getScheduler().runTaskLater(m_plugin, () -> { + spawnTeleportStartParticles(); + PaperLib.teleportAsync(m_player, m_dest, TeleportCause.COMMAND).thenAccept(res -> { + m_player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 60, 1, false, false, false)); + m_player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 40, 1, false, false, false)); + spawnEndParticles(); + particleGenerator.cancel(); + hoverGenerator.cancel(); + ret.complete(null); + }); + }, 20 * 6); + return ret; + } + + void spawnTeleportStartParticles() { + World world = m_dest.getWorld(); + world.spawnParticle(Particle.CLOUD, m_dest, 70, 1, 1, 1); + } + + void spawnEndParticles() { + World world = m_player.getLocation().getWorld(); + world.playSound(m_dest, Sound.BLOCK_PORTAL_TRAVEL, (float)0.5, (float)1.0); + world.spawnParticle(Particle.CLOUD, m_dest, 70, 1, 1, 1); + world.spawnParticle(Particle.SPELL_MOB, m_dest, 5, 2, 2, 2); + world.spawnParticle(Particle.PORTAL, m_player.getLocation(), 70, 1, 1, 1); + } + + void applyHoverEffect() { + m_player.addPotionEffect(new PotionEffect(PotionEffectType.LEVITATION, 10, 1, false, false, false)); + } + + void spawnHoverParticles() { + World w = m_player.getLocation().getWorld(); + w.spawnParticle(Particle.SPELL_MOB, m_dest, 5, 1, 1, 1); + w.spawnParticle(Particle.SPELL_MOB, m_src, 5, 2, 2, 2); + } + + void playTeleportSound() { + m_src.getWorld().playSound(m_src, Sound.BLOCK_PORTAL_TRIGGER, (float)0.5, (float)0.6); + m_src.getWorld().playSound(m_src, Sound.BLOCK_RESPAWN_ANCHOR_DEPLETE, (float)0.5, (float)1.0); + m_src.getWorld().playSound(m_dest, Sound.BLOCK_RESPAWN_ANCHOR_CHARGE, (float)0.5, (float)1.0); + } +} diff --git a/src/main/java/us/camin/regions/ui/RegionPostBuilder.java b/src/main/java/us/camin/regions/ui/RegionPostBuilder.java new file mode 100644 index 0000000..4330fed --- /dev/null +++ b/src/main/java/us/camin/regions/ui/RegionPostBuilder.java @@ -0,0 +1,231 @@ +package us.camin.regions.ui; + +/** + * This file is part of Regions + * + * Regions is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Regions is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Regions. If not, see . + * + */ + +import org.bukkit.World; +import org.bukkit.Material; +import org.bukkit.block.Block; +import org.bukkit.Tag; +import org.bukkit.block.data.Directional; +import org.bukkit.block.Banner; +import org.bukkit.block.data.type.Lantern; +import org.bukkit.block.BlockFace; +import org.bukkit.entity.EntityType; +import org.bukkit.Particle; +import org.bukkit.Sound; +import org.bukkit.scheduler.BukkitScheduler; +import java.util.logging.Logger; +import java.lang.Runnable; + +import us.camin.regions.Region; +import us.camin.regions.Plugin; + +import org.bukkit.Location; +import org.bukkit.Color; + +public class RegionPostBuilder { + Logger log = Logger.getLogger("Regions.RegionPostBuilder"); + Plugin m_plugin; + Region m_region; + public RegionPostBuilder(Region r, Plugin p) { + m_region = r; + m_plugin = p; + } + + private final Material[] bannerTypes = { + Material.WHITE_WALL_BANNER, + Material.YELLOW_WALL_BANNER, + Material.BLUE_WALL_BANNER, + Material.BLACK_WALL_BANNER, + Material.BROWN_WALL_BANNER, + Material.CYAN_WALL_BANNER, + Material.GREEN_WALL_BANNER, + Material.LIGHT_BLUE_WALL_BANNER, + Material.GRAY_WALL_BANNER, + Material.LIGHT_GRAY_WALL_BANNER, + Material.LIME_WALL_BANNER, + Material.MAGENTA_WALL_BANNER, + Material.ORANGE_WALL_BANNER, + Material.PINK_WALL_BANNER, + Material.PURPLE_WALL_BANNER, + Material.RED_WALL_BANNER, + }; + + boolean isBannerBlock(Block block) { + return Tag.BANNERS.isTagged(block.getType()); + } + + /*void buildPad(Location center) { + World world = m_region.location().getWorld(); + // Fill in the cobblestone pad + for(int x = -1;x <= 1;x++) { + for(int z = -1;z <= 1;z++) { + Block b = world.getBlockAt(center.getBlockX() + x, center.getBlockY(), center.getBlockZ() + z); + if (b.getType() != Material.COBBLESTONE) { + b.breakNaturally(); + b.setType(Material.COBBLESTONE); + } + } + } + world.playSound(center, Sound.BLOCK_STONE_PLACE, (float)1.0, (float)1.0); + world.spawnParticle(Particle.REDSTONE, center, 1000, 1, 1, 1, m_region.dustOptions()); + } + + void buildPost(Location center) { + World world = m_region.location().getWorld(); + BukkitScheduler scheduler = m_plugin.getServer().getScheduler(); + + // Draw the glowstone base of the tower + Block b = world.getBlockAt(center.getBlockX(), center.getBlockY() + 1, center.getBlockZ()); + if (b.getType() != Material.GLOWSTONE) { + b.breakNaturally(); + b.setType(Material.GLOWSTONE); + world.playSound(b.getLocation(), Sound.BLOCK_GLASS_PLACE, (float)1.0, (float)1.0); + world.spawnParticle(Particle.REDSTONE, b.getLocation().add(0.5, 0.5, 0.5), 1000, 1, 1, 1, m_region.dustOptions()); + } + Block markerBlock = world.getBlockAt(center.getBlockX(), center.getBlockY() + 2, center.getBlockZ()); + + scheduler.runTaskLater(m_plugin, () -> { + if (markerBlock.getType() != m_region.blockMaterial()) { + markerBlock.breakNaturally(); + markerBlock.setType(m_region.blockMaterial()); + world.playSound(b.getLocation(), Sound.BLOCK_WOOL_PLACE, (float)1.0, (float)1.0); + world.spawnParticle(Particle.REDSTONE, markerBlock.getLocation().add(0.5, 0.5, 0.5), 1000, 1, 1, 1, m_region.dustOptions()); + } + }, 15); + + scheduler.runTaskLater(m_plugin, () -> { + Block lanternBlock = markerBlock.getRelative(BlockFace.UP); + Material lanternType = (m_region.charges() > 0) ? Material.LANTERN : Material.SOUL_LANTERN; + if (lanternBlock.getType() != Material.LANTERN && lanternBlock.getType() != Material.SOUL_LANTERN) { + lanternBlock.breakNaturally(); + world.playSound(center, Sound.BLOCK_LANTERN_PLACE, (float)1.0, (float)1.0); + world.spawnParticle(Particle.REDSTONE, lanternBlock.getLocation().add(0.5, 0.5, 0.5), 1000, 1, 1, 1, m_region.dustOptions()); + } + lanternBlock.setType(lanternType); + Lantern lanternData = (Lantern)lanternBlock.getBlockData(); + lanternData.setHanging(false); + lanternBlock.setBlockData(lanternData); + }, 30); + }*/ + + public void build() { + BukkitScheduler scheduler = m_plugin.getServer().getScheduler(); + scheduler.runTask(m_plugin, new PadBuilder()); + scheduler.runTaskLater(m_plugin, new PostBuilder(), 20); + scheduler.runTaskLater(m_plugin, new BannerBuilder(), 20 * 2); + scheduler.runTaskLater(m_plugin, new LanternBuilder(), 20 * 3); + } + + public void updateLantern() { + BukkitScheduler scheduler = m_plugin.getServer().getScheduler(); + scheduler.runTask(m_plugin, new LanternBuilder()); + } + + public class PadBuilder implements Runnable { + public void run() { + World world = m_region.location().getWorld(); + Location center = m_region.location().clone().add(0, -1, 0); + // Fill in the cobblestone pad + for(int x = -1;x <= 1;x++) { + for(int z = -1;z <= 1;z++) { + Block b = world.getBlockAt(center.getBlockX() + x, center.getBlockY(), center.getBlockZ() + z); + if (b.getType() != Material.COBBLESTONE) { + b.breakNaturally(); + b.setType(Material.COBBLESTONE); + } + } + } + world.playSound(center, Sound.BLOCK_STONE_PLACE, (float)1.0, (float)1.0); + world.spawnParticle(Particle.REDSTONE, center, 1000, 1, 1, 1, m_region.dustOptions()); + } + } + + public class PostBuilder implements Runnable { + public void run() { + World world = m_region.location().getWorld(); + BukkitScheduler scheduler = m_plugin.getServer().getScheduler(); + Location center = m_region.location().clone().add(0, -1, 0); + + // Draw the glowstone base of the tower + Block b = world.getBlockAt(center.getBlockX(), center.getBlockY() + 1, center.getBlockZ()); + if (b.getType() != Material.GLOWSTONE) { + b.breakNaturally(); + b.setType(Material.GLOWSTONE); + world.playSound(b.getLocation(), Sound.BLOCK_GLASS_PLACE, (float)1.0, (float)1.0); + world.spawnParticle(Particle.REDSTONE, b.getLocation().add(0.5, 0.5, 0.5), 1000, 1, 1, 1, m_region.dustOptions()); + } + Block markerBlock = world.getBlockAt(center.getBlockX(), center.getBlockY() + 2, center.getBlockZ()); + + scheduler.runTaskLater(m_plugin, () -> { + if (markerBlock.getType() != m_region.blockMaterial()) { + markerBlock.breakNaturally(); + markerBlock.setType(m_region.blockMaterial()); + world.playSound(b.getLocation(), Sound.BLOCK_WOOL_PLACE, (float)1.0, (float)1.0); + world.spawnParticle(Particle.REDSTONE, markerBlock.getLocation().add(0.5, 0.5, 0.5), 1000, 1, 1, 1, m_region.dustOptions()); + } + }, 15); + } + } + + public class BannerBuilder implements Runnable { + public void run() { + World world = m_region.location().getWorld(); + Location center = m_region.location().clone().add(0, -1, 0); + // Place the banners + Block markerBlock = world.getBlockAt(center.getBlockX(), center.getBlockY() + 2, center.getBlockZ()); + BlockFace[] directions = {BlockFace.NORTH, BlockFace.SOUTH, BlockFace.WEST, BlockFace.EAST}; + log.info("Rebuilding banners..."); + for(BlockFace face : directions) { + Block b = markerBlock.getRelative(face); + if (!isBannerBlock(b)) { + b.breakNaturally(); + } + b.setType(m_region.bannerBlockMaterial()); + Directional bannerDir = (Directional)b.getBlockData(); + bannerDir.setFacing(face); + b.setBlockData(bannerDir); + Banner bannerState = (Banner)b.getState(); + bannerState.setPatterns(m_region.bannerPatterns()); + bannerState.update(); + world.spawnParticle(Particle.CLOUD, markerBlock.getLocation().add(0.5, 0.5, 0.5), 10, 1, 1, 1); + } + world.playSound(center, Sound.BLOCK_WOOL_PLACE, (float)1.0, (float)1.0); + } + } + + public class LanternBuilder implements Runnable { + public void run() { + World world = m_region.location().getWorld(); + Location center = m_region.location().clone().add(0, -1, 0); + Block markerBlock = world.getBlockAt(center.getBlockX(), center.getBlockY() + 2, center.getBlockZ()); + Block lanternBlock = markerBlock.getRelative(BlockFace.UP); + Material lanternType = (m_region.charges() > 0) ? Material.LANTERN : Material.SOUL_LANTERN; + if (lanternBlock.getType() != Material.LANTERN && lanternBlock.getType() != Material.SOUL_LANTERN) { + lanternBlock.breakNaturally(); + world.playSound(center, Sound.BLOCK_LANTERN_PLACE, (float)1.0, (float)1.0); + world.spawnParticle(Particle.REDSTONE, lanternBlock.getLocation().add(0.5, 0.5, 0.5), 1000, 1, 1, 1, m_region.dustOptions()); + } + lanternBlock.setType(lanternType); + Lantern lanternData = (Lantern)lanternBlock.getBlockData(); + lanternData.setHanging(false); + lanternBlock.setBlockData(lanternData); + } + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 0ed16d0..d5dbf2f 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,22 +1,20 @@ name: Regions main: us.camin.regions.Plugin -author: Trever Fischer -website: http://camin.us/ +author: Torrie Fischer +website: http://hackerbots.net/ version: ${version} -softdepend: [dynmap] +api-version: 1.16 +softdepend: [dynmap, HolographicDisplays, ProtocolLib] commands: + regions: + description: "List available regions." + usage: / + regionop: + description: "Region admin tools" + usage: / [args...] region: description: Interface to the region system usage: / [args...] - movein: - description: "Sets your home region." - usage: / - cityregion: - description: "Teleports you to the world's city region." - usage: / - homeregion: - description: "Teleports you to your home region." - usage: / permissions: regions.*: default: op @@ -24,13 +22,51 @@ permissions: children: regions.create: true regions.remove: true - regions.city: true + regions.regen.*: true + regions.bypass.*: true + regions.give-items.*: true + regions.regen.*: + default: op + description: Allows regeneration of all regions + children: + regions.regen: true + regions.regen.all: true + regions.use: + default: true + description: Use region posts regions.create: default: op description: Create a region regions.remove: default: op description: Remove a region - regions.city: + regions.regen: + default: op + description: Regenerates a region post + regions.regen.all: + default: op + description: Regenerates all region posts, including in unloaded chunks + regions.bypass.*: + default: op + description: Bypasses all region post travel requirements + children: + regions.bypass.charges: true + regions.bypass.discovery: true + regions.bypass.charges: + default: op + description: Allows you to bypass post charge requirements + regions.bypass.discovery: + default: op + description: Allows you to bypass discovery requirements + regions.give-items.*: + default: op + children: + regions.give-items.compass: true + regions.give-items.charge: true + regions.give-items.creator: true + regions.give-items.compass: + default: op + regions.give-items.charge: + default: op + regions.give-items.creator: default: op - description: Defines the city region