|
| 1 | +package io.josemmo.bukkit.plugin.interaction; |
| 2 | + |
| 3 | +import com.comphenix.protocol.events.ListenerPriority; |
| 4 | +import io.josemmo.bukkit.plugin.YamipaPlugin; |
| 5 | +import io.josemmo.bukkit.plugin.renderer.FakeImage; |
| 6 | +import io.josemmo.bukkit.plugin.utils.ActionBar; |
| 7 | +import io.josemmo.bukkit.plugin.utils.Logger; |
| 8 | +import org.bukkit.ChatColor; |
| 9 | +import org.bukkit.Location; |
| 10 | +import org.bukkit.block.Block; |
| 11 | +import org.bukkit.block.BlockFace; |
| 12 | +import org.bukkit.entity.Player; |
| 13 | +import org.jetbrains.annotations.NotNull; |
| 14 | +import org.jetbrains.annotations.Nullable; |
| 15 | +import java.util.HashMap; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.UUID; |
| 18 | +import java.util.function.BiConsumer; |
| 19 | +import java.util.function.Consumer; |
| 20 | + |
| 21 | +public class SelectBlockOrImageTask { |
| 22 | + private static final Logger LOGGER = Logger.getLogger("SelectBlockOrImageTask"); |
| 23 | + private static final Map<UUID, SelectBlockOrImageTask> INSTANCES = new HashMap<>(); |
| 24 | + private static @Nullable SelectBlockListener BLOCK_LISTENER; |
| 25 | + private static @Nullable SelectFakeItemFrameListener ITEM_FRAME_LISTENER; |
| 26 | + private final @NotNull Player player; |
| 27 | + private @Nullable BiConsumer<@NotNull Location, @NotNull BlockFace> blockCallback; |
| 28 | + private @Nullable Runnable cancelCallback; |
| 29 | + private @Nullable Consumer<@NotNull FakeImage> imageCallback; |
| 30 | + private @Nullable ActionBar actionBar; |
| 31 | + |
| 32 | + /** |
| 33 | + * Class constructor |
| 34 | + * @param player Target player instance |
| 35 | + */ |
| 36 | + public SelectBlockOrImageTask(@NotNull Player player) { |
| 37 | + this.player = player; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Set on block callback |
| 42 | + * @param callback Block callback |
| 43 | + */ |
| 44 | + public void onBlock(@Nullable BiConsumer<@NotNull Location, @NotNull BlockFace> callback) { |
| 45 | + this.blockCallback = callback; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Set on fake image callback |
| 50 | + * @param callback Fake image callback |
| 51 | + */ |
| 52 | + public void onImage(@Nullable Consumer<@NotNull FakeImage> callback) { |
| 53 | + this.imageCallback = callback; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Set on cancel callback |
| 58 | + * @param callback Cancel callback |
| 59 | + */ |
| 60 | + public void onCancel(@Nullable Runnable callback) { |
| 61 | + this.cancelCallback = callback; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Run task |
| 66 | + * @param helpMessage Help message for the player |
| 67 | + */ |
| 68 | + @SuppressWarnings("deprecation") |
| 69 | + public synchronized void run(@NotNull String helpMessage) { |
| 70 | + // Keep track of this instance |
| 71 | + UUID uuid = player.getUniqueId(); |
| 72 | + if (INSTANCES.containsKey(uuid)) { |
| 73 | + player.sendMessage(ChatColor.RED + "You already have a pending action!"); |
| 74 | + return; |
| 75 | + } |
| 76 | + INSTANCES.put(uuid, this); |
| 77 | + |
| 78 | + // Create block listener singleton (if needed) |
| 79 | + if (BLOCK_LISTENER == null) { |
| 80 | + BLOCK_LISTENER = new SelectBlockListener() { |
| 81 | + @Override |
| 82 | + protected boolean onLeftClick(@NotNull Player player, @NotNull Block block, @NotNull BlockFace face) { |
| 83 | + SelectBlockOrImageTask task = getInstance(player); |
| 84 | + if (task == null) { |
| 85 | + return true; |
| 86 | + } |
| 87 | + task.stop(); |
| 88 | + if (task.cancelCallback != null) { |
| 89 | + task.cancelCallback.run(); |
| 90 | + } |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + protected boolean onRightClick(@NotNull Player player, @NotNull Block block, @NotNull BlockFace face) { |
| 96 | + SelectBlockOrImageTask task = getInstance(player); |
| 97 | + if (task == null) { |
| 98 | + return true; |
| 99 | + } |
| 100 | + task.stop(); |
| 101 | + if (task.blockCallback != null) { |
| 102 | + task.blockCallback.accept(block.getLocation(), face); |
| 103 | + } |
| 104 | + return false; |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + protected void onPlayerQuit(@NotNull Player player) { |
| 109 | + SelectBlockOrImageTask task = getInstance(player); |
| 110 | + if (task == null) { |
| 111 | + return; |
| 112 | + } |
| 113 | + task.stop(); |
| 114 | + if (task.cancelCallback != null) { |
| 115 | + task.cancelCallback.run(); |
| 116 | + } |
| 117 | + } |
| 118 | + }; |
| 119 | + BLOCK_LISTENER.register(); |
| 120 | + LOGGER.fine("Created SelectBlockListener singleton"); |
| 121 | + } |
| 122 | + |
| 123 | + // Create fake item frame listener singleton (if needed) |
| 124 | + if (ITEM_FRAME_LISTENER == null) { |
| 125 | + ITEM_FRAME_LISTENER = new SelectFakeItemFrameListener() { |
| 126 | + @Override |
| 127 | + protected boolean onLeftClick(@NotNull Player player, int entityId) { |
| 128 | + SelectBlockOrImageTask task = getInstance(player); |
| 129 | + if (task == null) { |
| 130 | + return true; |
| 131 | + } |
| 132 | + task.stop(); |
| 133 | + if (task.cancelCallback != null) { |
| 134 | + task.cancelCallback.run(); |
| 135 | + } |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + protected boolean onRightClick(@NotNull Player player, int entityId) { |
| 141 | + SelectBlockOrImageTask task = getInstance(player); |
| 142 | + if (task == null) { |
| 143 | + return true; |
| 144 | + } |
| 145 | + task.stop(); |
| 146 | + if (task.imageCallback != null) { |
| 147 | + notifyImage(player, task.imageCallback); |
| 148 | + } |
| 149 | + return false; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + protected @NotNull ListenerPriority getPriority() { |
| 154 | + return ListenerPriority.LOW; |
| 155 | + } |
| 156 | + }; |
| 157 | + ITEM_FRAME_LISTENER.register(); |
| 158 | + LOGGER.fine("Created SelectFakeItemFrameListener singleton"); |
| 159 | + } |
| 160 | + |
| 161 | + // Start action bar |
| 162 | + actionBar = ActionBar.repeat( |
| 163 | + player, |
| 164 | + ChatColor.GREEN + helpMessage + ChatColor.RESET + " - " + ChatColor.RED + "Left click to cancel" |
| 165 | + ); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Stop task |
| 170 | + */ |
| 171 | + public synchronized void stop() { |
| 172 | + // Stop action bar |
| 173 | + if (actionBar != null) { |
| 174 | + actionBar.clear(); |
| 175 | + actionBar = null; |
| 176 | + } |
| 177 | + |
| 178 | + // Remove reference to this instance |
| 179 | + INSTANCES.remove(player.getUniqueId()); |
| 180 | + if (!INSTANCES.isEmpty()) { |
| 181 | + return; |
| 182 | + } |
| 183 | + |
| 184 | + // Destroy block listener singleton |
| 185 | + if (BLOCK_LISTENER != null) { |
| 186 | + BLOCK_LISTENER.unregister(); |
| 187 | + BLOCK_LISTENER = null; |
| 188 | + LOGGER.fine("Destroyed SelectBlockListener singleton"); |
| 189 | + } |
| 190 | + |
| 191 | + // Destroy fake item frame listener singleton |
| 192 | + if (ITEM_FRAME_LISTENER != null) { |
| 193 | + ITEM_FRAME_LISTENER.unregister(); |
| 194 | + ITEM_FRAME_LISTENER = null; |
| 195 | + LOGGER.fine("Destroyed SelectFakeItemFrameListener singleton"); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + /** |
| 200 | + * Get task instance for player |
| 201 | + * @param player Targeted player |
| 202 | + * @return Task instance |
| 203 | + */ |
| 204 | + private synchronized @Nullable SelectBlockOrImageTask getInstance(@NotNull Player player) { |
| 205 | + return INSTANCES.get(player.getUniqueId()); |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Notify image |
| 210 | + * @param player Player looking at the image |
| 211 | + * @param callback Image callback |
| 212 | + */ |
| 213 | + private void notifyImage(@NotNull Player player, @NotNull Consumer<@NotNull FakeImage> callback) { |
| 214 | + YamipaPlugin.getInstance().getScheduler().runInGame(() -> { |
| 215 | + FakeImage image = SelectFakeItemFrameListener.getFakeImage(player); |
| 216 | + if (image == null) { |
| 217 | + LOGGER.warning("Failed to get image selected by Player#" + player.getName()); |
| 218 | + return; |
| 219 | + } |
| 220 | + callback.accept(image); |
| 221 | + }, player.getLocation(), 0); |
| 222 | + } |
| 223 | +} |
0 commit comments