|
| 1 | +package com.ghostchu.quickshop.addon.bluemap; |
| 2 | + |
| 3 | +import com.ghostchu.quickshop.QuickShop; |
| 4 | +import com.ghostchu.quickshop.api.event.*; |
| 5 | +import com.ghostchu.quickshop.api.localization.text.TextManager; |
| 6 | +import com.ghostchu.quickshop.api.shop.Shop; |
| 7 | +import com.ghostchu.quickshop.api.shop.ShopType; |
| 8 | +import com.ghostchu.quickshop.util.Util; |
| 9 | +import de.bluecolored.bluemap.api.BlueMapAPI; |
| 10 | +import de.bluecolored.bluemap.api.BlueMapMap; |
| 11 | +import de.bluecolored.bluemap.api.BlueMapWorld; |
| 12 | +import de.bluecolored.bluemap.api.markers.MarkerSet; |
| 13 | +import de.bluecolored.bluemap.api.markers.POIMarker; |
| 14 | +import net.kyori.adventure.text.Component; |
| 15 | +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; |
| 16 | +import org.bukkit.Bukkit; |
| 17 | +import org.bukkit.event.EventHandler; |
| 18 | +import org.bukkit.event.HandlerList; |
| 19 | +import org.bukkit.event.Listener; |
| 20 | +import org.bukkit.event.world.WorldLoadEvent; |
| 21 | +import org.bukkit.event.world.WorldUnloadEvent; |
| 22 | +import org.bukkit.plugin.Plugin; |
| 23 | +import org.bukkit.plugin.java.JavaPlugin; |
| 24 | +import org.jetbrains.annotations.NotNull; |
| 25 | + |
| 26 | +import java.util.Optional; |
| 27 | + |
| 28 | +public final class Main extends JavaPlugin implements Listener { |
| 29 | + static Main instance; |
| 30 | + private QuickShop plugin; |
| 31 | + private BlueMapAPI blueMapAPI; |
| 32 | + @Override |
| 33 | + public void onLoad() { |
| 34 | + instance = this; |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void onDisable() { |
| 39 | + HandlerList.unregisterAll((Plugin) this); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void onEnable() { |
| 44 | + saveDefaultConfig(); |
| 45 | + plugin = QuickShop.getInstance(); |
| 46 | + getLogger().info("Registering the per shop permissions..."); |
| 47 | + Bukkit.getScheduler().runTaskAsynchronously(this,()->{ |
| 48 | + while(BlueMapAPI.getInstance().isEmpty()){ |
| 49 | + try { |
| 50 | + Thread.sleep(1000); |
| 51 | + } catch (InterruptedException e) { |
| 52 | + throw new RuntimeException(e); |
| 53 | + } |
| 54 | + } |
| 55 | + getLogger().info("Found Pl3xMap loaded! Hooking!"); |
| 56 | + blueMapAPI = BlueMapAPI.getInstance().orElseThrow(); |
| 57 | + Bukkit.getPluginManager().registerEvents(this, this); |
| 58 | + Bukkit.getScheduler().runTaskLater(this, this::updateAllMarkers, 80); |
| 59 | + }); |
| 60 | + } |
| 61 | + |
| 62 | + @EventHandler(ignoreCancelled = true) |
| 63 | + public void onEvent(WorldLoadEvent event){ |
| 64 | + Util.mainThreadRun(this::updateAllMarkers); |
| 65 | + } |
| 66 | + @EventHandler(ignoreCancelled = true) |
| 67 | + public void onEvent(WorldUnloadEvent event){ |
| 68 | + Util.mainThreadRun(this::updateAllMarkers); |
| 69 | + } |
| 70 | + @EventHandler(ignoreCancelled = true) |
| 71 | + public void onEvent(QSConfigurationReloadEvent event){ |
| 72 | + Util.mainThreadRun(this::updateAllMarkers); |
| 73 | + } |
| 74 | + @EventHandler(ignoreCancelled = true) |
| 75 | + public void onEvent(ShopCreateSuccessEvent event){ |
| 76 | + updateShopMarker(event.getShop()); |
| 77 | + } |
| 78 | + @EventHandler(ignoreCancelled = true) |
| 79 | + public void onEvent(ShopDeleteEvent event){ |
| 80 | + Util.mainThreadRun(()->updateShopMarker(event.getShop())); |
| 81 | + } |
| 82 | + @EventHandler(ignoreCancelled = true) |
| 83 | + public void onEvent(ShopPriceChangeEvent event){ |
| 84 | + Util.mainThreadRun(()->updateShopMarker(event.getShop())); |
| 85 | + } |
| 86 | + @EventHandler(ignoreCancelled = true) |
| 87 | + public void onEvent(ShopItemChangeEvent event){ |
| 88 | + Util.mainThreadRun(()->updateShopMarker(event.getShop())); |
| 89 | + } |
| 90 | + @EventHandler(ignoreCancelled = true) |
| 91 | + public void onEvent(ShopOwnershipTransferEvent event){ |
| 92 | + Util.mainThreadRun(()->updateShopMarker(event.getShop())); |
| 93 | + } |
| 94 | + @EventHandler(ignoreCancelled = true) |
| 95 | + public void onEvent(ShopSuccessPurchaseEvent event){ |
| 96 | + Util.mainThreadRun(()->updateShopMarker(event.getShop())); |
| 97 | + } |
| 98 | + @EventHandler(ignoreCancelled = true) |
| 99 | + public void onEvent(ShopTypeChangeEvent event){ |
| 100 | + Util.mainThreadRun(()->updateShopMarker(event.getShop())); |
| 101 | + } |
| 102 | + @EventHandler(ignoreCancelled = true) |
| 103 | + public void onEvent(ShopNamingEvent event){ |
| 104 | + Util.mainThreadRun(()->updateShopMarker(event.getShop())); |
| 105 | + } |
| 106 | + @NotNull |
| 107 | + public String plain(@NotNull Component component) { |
| 108 | + return PlainTextComponentSerializer.plainText().serialize(component); |
| 109 | + } |
| 110 | + |
| 111 | + @NotNull |
| 112 | + public TextManager text() { |
| 113 | + return plugin.getTextManager(); |
| 114 | + } |
| 115 | + |
| 116 | + public MarkerSet createMarkerSet(){ |
| 117 | + return MarkerSet.builder() |
| 118 | + .defaultHidden(true) |
| 119 | + .label(plain(text().of("addon.bluemap.markerset-title").forLocale())) |
| 120 | + .defaultHidden(getConfig().getBoolean("display-by-default")) |
| 121 | + .toggleable(true) |
| 122 | + .build(); |
| 123 | + } |
| 124 | + private void updateAllMarkers() { |
| 125 | + blueMapAPI.getWorlds().forEach(bWorld-> bWorld.getMaps().forEach(bMap-> bMap.getMarkerSets().remove("quickshop-hikari-shops"))); |
| 126 | + plugin.getShopManager().getAllShops().forEach(this::updateShopMarker); |
| 127 | + } |
| 128 | + |
| 129 | + public void updateShopMarker(Shop shop) { |
| 130 | + Optional<BlueMapWorld> bWorld = blueMapAPI.getWorld(shop.getLocation().getWorld()); |
| 131 | + if(bWorld.isEmpty()) return; |
| 132 | + String shopName = shop.getShopName(); |
| 133 | + String posStr = String.format("%s %s, %s, %s", shop.getLocation().getWorld().getName(), shop.getLocation().getBlockX(), shop.getLocation().getBlockY(), shop.getLocation().getBlockZ()); |
| 134 | + if (shopName == null) { |
| 135 | + shopName = posStr; |
| 136 | + } |
| 137 | + for (BlueMapMap map : bWorld.get().getMaps()) { |
| 138 | + MarkerSet markerSet = map.getMarkerSets().computeIfAbsent("quickshop-hikari-shops",(key)-> createMarkerSet()); |
| 139 | + String markerName = plain(text().of("addon.bluemap.marker-name", |
| 140 | + shopName, |
| 141 | + plain(shop.ownerName()), |
| 142 | + plain(Util.getItemStackName(shop.getItem())), |
| 143 | + plugin.getShopManager().format(shop.getPrice(), shop), |
| 144 | + shop.getShopStackingAmount(), |
| 145 | + shop.getShopType() == ShopType.SELLING ? plain(text().of("shop-type.selling").forLocale()) : plain(text().of("shop-type.buying").forLocale()), |
| 146 | + shop.isUnlimited(), |
| 147 | + posStr |
| 148 | + ).forLocale()); |
| 149 | + String desc = plain(text().of("addon.bluemap.marker-description", |
| 150 | + shopName, |
| 151 | + plain(shop.ownerName()), |
| 152 | + plain(Util.getItemStackName(shop.getItem())), |
| 153 | + plugin.getShopManager().format(shop.getPrice(), shop), |
| 154 | + shop.getShopStackingAmount(), |
| 155 | + shop.getShopType() == ShopType.SELLING ? plain(text().of("shop-type.selling").forLocale()) : plain(text().of("shop-type.buying").forLocale()), |
| 156 | + shop.isUnlimited(), |
| 157 | + posStr |
| 158 | + ).forLocale()); |
| 159 | + POIMarker marker = POIMarker.builder() |
| 160 | + .label(markerName) |
| 161 | + .position(shop.getLocation().getX(), |
| 162 | + shop.getLocation().getY(), |
| 163 | + shop.getLocation().getZ()) |
| 164 | + .maxDistance(1000) |
| 165 | + .detail(desc) |
| 166 | + .styleClasses() |
| 167 | + .build(); |
| 168 | + markerSet.getMarkers().put("quickshop-hikari-shop"+shop.getShopId(), marker); |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | +} |
0 commit comments