From 2b9fe5afdade9198e0e42a6045141b6025c76e14 Mon Sep 17 00:00:00 2001 From: Sauilitired Date: Wed, 26 Dec 2018 19:16:34 +0100 Subject: [PATCH 1/2] Fix plot music (and plot inventories) --- .../bukkit/listeners/PlayerEvents.java | 9 ++-- .../bukkit/object/BukkitPlayer.java | 22 +++++++--- .../bukkit/util/BukkitInventoryUtil.java | 2 - .../plotsquared/plot/commands/Music.java | 41 ++++++++++++------- .../plotsquared/plot/flag/Flags.java | 2 +- .../plot/listener/PlotListener.java | 22 +++++----- .../plot/object/ConsolePlayer.java | 2 +- .../plot/object/PlotInventory.java | 32 ++++++++++++++- .../plot/object/PlotItemStack.java | 22 +++++++--- .../plotsquared/plot/object/PlotPlayer.java | 4 +- 10 files changed, 109 insertions(+), 49 deletions(-) diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/listeners/PlayerEvents.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/listeners/PlayerEvents.java index bb001769c..d45803d81 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/listeners/PlayerEvents.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/listeners/PlayerEvents.java @@ -32,7 +32,6 @@ import org.bukkit.event.block.*; import org.bukkit.event.entity.*; import org.bukkit.event.hanging.HangingBreakByEntityEvent; import org.bukkit.event.hanging.HangingPlaceEvent; -import org.bukkit.event.inventory.InventoryAction; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.player.*; @@ -1498,10 +1497,10 @@ import java.util.regex.Pattern; @SuppressWarnings("deprecation") @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void onInventoryClick(InventoryClickEvent event) { - if (!event.isLeftClick() || (event.getAction() != InventoryAction.PLACE_ALL) || event + /*if (!event.isLeftClick() || (event.getAction() != InventoryAction.PLACE_ALL) || event .isShiftClick()) { return; - } + }*/ HumanEntity entity = event.getWhoClicked(); if (!(entity instanceof Player) || !PlotSquared.get() .hasPlotArea(entity.getWorld().getName())) { @@ -1514,7 +1513,7 @@ import java.util.regex.Pattern; } Player player = (Player) clicker; PlotPlayer pp = BukkitUtil.getPlayer(player); - PlotInventory inventory = pp.getMeta("inventory"); + final PlotInventory inventory = PlotInventory.getOpenPlotInventory(pp); if (inventory != null && event.getRawSlot() == event.getSlot()) { if (!inventory.onClick(event.getSlot())) { event.setResult(Event.Result.DENY); @@ -2212,7 +2211,7 @@ import java.util.regex.Pattern; return; } Player player = (Player) closer; - BukkitUtil.getPlayer(player).deleteMeta("inventory"); + PlotInventory.removePlotInventoryOpen(BukkitUtil.getPlayer(player)); } @EventHandler(priority = EventPriority.MONITOR) public void onLeave(PlayerQuitEvent event) { diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/object/BukkitPlayer.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/object/BukkitPlayer.java index a97993cfc..7aecb854c 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/object/BukkitPlayer.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/object/BukkitPlayer.java @@ -4,11 +4,10 @@ import com.github.intellectualsites.plotsquared.bukkit.util.BukkitUtil; import com.github.intellectualsites.plotsquared.plot.PlotSquared; import com.github.intellectualsites.plotsquared.plot.config.C; import com.github.intellectualsites.plotsquared.plot.object.Location; +import com.github.intellectualsites.plotsquared.plot.object.PlotBlock; import com.github.intellectualsites.plotsquared.plot.object.PlotPlayer; import com.github.intellectualsites.plotsquared.plot.util.*; -import org.bukkit.Effect; -import org.bukkit.GameMode; -import org.bukkit.WeatherType; +import org.bukkit.*; import org.bukkit.entity.Player; import org.bukkit.event.Event; import org.bukkit.event.EventException; @@ -17,8 +16,10 @@ import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; import org.bukkit.permissions.PermissionAttachmentInfo; import org.bukkit.plugin.RegisteredListener; +import java.util.Arrays; import java.util.Set; import java.util.UUID; +import java.util.stream.Collectors; public class BukkitPlayer extends PlotPlayer { @@ -251,9 +252,18 @@ public class BukkitPlayer extends PlotPlayer { this.player.setAllowFlight(fly); } - @Override public void playMusic(Location location, int id) { - //noinspection deprecation - this.player.playEffect(BukkitUtil.getLocation(location), Effect.RECORD_PLAY, id); + @Override public void playMusic(Location location, PlotBlock id) { + if (PlotBlock.isEverything(id) || id.isAir()) { + // Let's just stop all the discs because why not? + for (final Sound sound : Arrays.stream(Sound.values()).filter(sound -> sound.name().contains("DISC")).collect( + Collectors.toList())) { + player.stopSound(sound); + } + // this.player.playEffect(BukkitUtil.getLocation(location), Effect.RECORD_PLAY, Material.AIR); + } else { + // this.player.playEffect(BukkitUtil.getLocation(location), Effect.RECORD_PLAY, id.to(Material.class)); + this.player.playSound(BukkitUtil.getLocation(location), Sound.valueOf(id.to(Material.class).name()), Float.MAX_VALUE, 1f); + } } @Override public void kick(String message) { diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitInventoryUtil.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitInventoryUtil.java index 6beebcd95..c40e13d09 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitInventoryUtil.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/util/BukkitInventoryUtil.java @@ -56,7 +56,6 @@ public class BukkitInventoryUtil extends InventoryUtil { inventory.setItem(i, getItem(item)); } } - inv.player.setMeta("inventory", inv); bp.player.openInventory(inventory); } @@ -64,7 +63,6 @@ public class BukkitInventoryUtil extends InventoryUtil { if (!inv.isOpen()) { return; } - inv.player.deleteMeta("inventory"); BukkitPlayer bp = (BukkitPlayer) inv.player; bp.player.closeInventory(); } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Music.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Music.java index c889ba334..457d92e30 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Music.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/commands/Music.java @@ -4,11 +4,18 @@ import com.github.intellectualsites.plotsquared.commands.CommandDeclaration; import com.github.intellectualsites.plotsquared.plot.config.C; import com.github.intellectualsites.plotsquared.plot.flag.Flags; import com.github.intellectualsites.plotsquared.plot.object.*; -import com.github.intellectualsites.plotsquared.plot.util.WorldUtil; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Locale; @CommandDeclaration(command = "music", permission = "plots.music", description = "Play music in your plot", usage = "/plot music", category = CommandCategory.APPEARANCE, requiredType = RequiredType.PLAYER) public class Music extends SubCommand { + private static final Collection DISCS = Arrays.asList("music_disc_13", "music_disc_cat", + "music_disc_blocks", "music_disc_chirp", "music_disc_far", "music_disc_mall", "music_disc_mellohi", + "music_disc_stal", "music_disc_strad", "music_disc_ward", "music_disc_11", "music_disc_wait"); + @Override public boolean onCommand(PlotPlayer player, String[] args) { Location loc = player.getLocation(); final Plot plot = loc.getPlotAbs(); @@ -27,26 +34,32 @@ public class Music extends SubCommand { } if (item.getPlotBlock().equalsAny(7, "bedrock")) { plot.removeFlag(Flags.MUSIC); - } else { + C.FLAG_REMOVED.send(player); + } else if (item.name.toLowerCase(Locale.ENGLISH).contains("disc")) { plot.setFlag(Flags.MUSIC, item.getPlotBlock().getRawId()); + C.FLAG_ADDED.send(player); + } else { + C.FLAG_NOT_ADDED.send(player); } return false; } }; int index = 0; - for (int i = 2256; i < 2268; i++) { - String name = - "&r&6" + WorldUtil.IMP.getClosestMatchingName(PlotBlock.get((short) i, (byte) 0)); - String[] lore = {"&r&aClick to play!"}; - PlotItemStack item = new PlotItemStack(i, (byte) 0, 1, name, lore); - inv.setItem(index, item); - index++; - } - if (player.getMeta("music") != null) { - String name = "&r&6Cancel music"; - String[] lore = {"&r&cClick to cancel!"}; - inv.setItem(index, new PlotItemStack(7, (short) 0, 1, name, lore)); + + for (final String disc : DISCS) { + final String name = String.format("&r&6%s", disc); + final String[] lore = {"&r&aClick to play!"}; + final PlotItemStack item = new PlotItemStack(disc, 1, name, lore); + inv.setItem(index++, item); } + + // Always add the cancel button + // if (player.getMeta("music") != null) { + String name = "&r&6Cancel music"; + String[] lore = {"&r&cClick to cancel!"}; + inv.setItem(index, new PlotItemStack("bedrock", 1, name, lore)); + // } + inv.openInventory(); return true; } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flag/Flags.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flag/Flags.java index 6603b8387..6b86a890a 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flag/Flags.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/flag/Flags.java @@ -15,7 +15,7 @@ import java.util.HashMap; public final class Flags { - public static final IntegerFlag MUSIC = new IntegerFlag("music"); + public static final StringFlag MUSIC = new StringFlag("music"); public static final StringFlag DESCRIPTION = new StringFlag("description"); public static final IntegerListFlag ANALYSIS = (IntegerListFlag) new IntegerListFlag("analysis").reserve(); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/listener/PlotListener.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/listener/PlotListener.java index d47d7a94f..5f056a0bd 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/listener/PlotListener.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/listener/PlotListener.java @@ -124,24 +124,24 @@ public class PlotListener { Optional weatherFlag = plot.getFlag(Flags.WEATHER); if (weatherFlag.isPresent()) { player.setWeather(weatherFlag.get()); - } - - Optional musicFlag = plot.getFlag(Flags.MUSIC); + } Optional musicFlag = plot.getFlag(Flags.MUSIC); if (musicFlag.isPresent()) { - Number id = musicFlag.get(); - if ((id.intValue() >= 2256 && id.intValue() <= 2267) || (id.intValue() == 0)) { + final String id = musicFlag.get(); + final PlotBlock block = PlotBlock.get(id); + final String rawId = block.getRawId().toString(); + if (rawId.contains("disc") || PlotBlock.isEverything(block) || block.isAir()) { Location loc = player.getLocation(); Location lastLoc = player.getMeta("music"); if (lastLoc != null) { - player.playMusic(lastLoc, 0); - if (id.intValue() == 0) { + player.playMusic(lastLoc, PlotBlock.get("air")); + if (PlotBlock.isEverything(block) || block.isAir()) { player.deleteMeta("music"); } } - if (id.intValue() != 0) { + if (!(PlotBlock.isEverything(block) || block.isAir())) { try { player.setMeta("music", loc); - player.playMusic(loc, id.intValue()); + player.playMusic(loc, block); } catch (Exception ignored) { } } @@ -150,7 +150,7 @@ public class PlotListener { Location lastLoc = player.getMeta("music"); if (lastLoc != null) { player.deleteMeta("music"); - player.playMusic(lastLoc, 0); + player.playMusic(lastLoc, PlotBlock.get("air")); } } CommentManager.sendTitle(player, plot); @@ -257,7 +257,7 @@ public class PlotListener { Location lastLoc = player.getMeta("music"); if (lastLoc != null) { player.deleteMeta("music"); - player.playMusic(lastLoc, 0); + player.playMusic(lastLoc, PlotBlock.get("air")); } } return true; diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/ConsolePlayer.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/ConsolePlayer.java index 340c0952b..18a0ce7bc 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/ConsolePlayer.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/ConsolePlayer.java @@ -115,7 +115,7 @@ public class ConsolePlayer extends PlotPlayer { @Override public void setFlight(boolean fly) { } - @Override public void playMusic(Location location, int id) { + @Override public void playMusic(Location location, PlotBlock id) { } @Override public void kick(String message) { diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotInventory.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotInventory.java index 630e36ce7..51539dbdb 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotInventory.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotInventory.java @@ -1,9 +1,30 @@ package com.github.intellectualsites.plotsquared.plot.object; +import com.github.intellectualsites.plotsquared.plot.PlotSquared; import com.github.intellectualsites.plotsquared.plot.util.InventoryUtil; +import lombok.NonNull; public class PlotInventory { + private static final String META_KEY = "inventory"; + + public static boolean hasPlotInventoryOpen(@NonNull final PlotPlayer plotPlayer) { + return getOpenPlotInventory(plotPlayer) != null; + } + + public static PlotInventory getOpenPlotInventory(@NonNull final PlotPlayer plotPlayer) { + return plotPlayer.getMeta(META_KEY, null); + } + + public static void setPlotInventoryOpen(@NonNull final PlotPlayer plotPlayer, + @NonNull final PlotInventory plotInventory) { + plotPlayer.setMeta(META_KEY, plotInventory); + } + + public static void removePlotInventoryOpen(@NonNull final PlotPlayer plotPlayer) { + plotPlayer.deleteMeta(META_KEY); + } + public final PlotPlayer player; public final int size; private final PlotItemStack[] items; @@ -32,14 +53,21 @@ public class PlotInventory { if (this.title == null) { return; } - this.open = true; - InventoryUtil.manager.open(this); + if (hasPlotInventoryOpen(player)) { + PlotSquared.debug(String.format("Failed to open plot inventory for %s " + + "because the player already has an open plot inventory", player.getName())); + } else { + this.open = true; + setPlotInventoryOpen(player, this); + InventoryUtil.manager.open(this); + } } public void close() { if (this.title == null) { return; } + removePlotInventoryOpen(player); InventoryUtil.manager.close(this); this.open = false; } diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotItemStack.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotItemStack.java index 05f427ff7..c0a9d93d0 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotItemStack.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotItemStack.java @@ -5,22 +5,34 @@ import com.github.intellectualsites.plotsquared.plot.util.WorldUtil; import lombok.Getter; public class PlotItemStack { + public final int amount; public final String name; public final String[] lore; - // public final int id; - // public final short data; @Getter private final PlotBlock plotBlock; - @Deprecated - public PlotItemStack(final int id, final short data, final int amount, final String name, - final String... lore) { + /** + * @param id Legacy numerical item ID + * @param data Legacy numerical item data + * @param amount Amount of items in the stack + * @param name The display name of the item stack + * @param lore The item stack lore + * @deprecated Use {@link PlotItemStack(String, int, String, String...)} + */ + @Deprecated public PlotItemStack(final int id, final short data, final int amount, + final String name, final String... lore) { this.amount = amount; this.name = name; this.lore = lore; this.plotBlock = PlotBlock.get(id, data); } + /** + * @param id String ID + * @param amount Amount of items in the stack + * @param name The display name of the item stack + * @param lore The item stack lore + */ public PlotItemStack(final String id, final int amount, final String name, final String... lore) { StringComparison.ComparisonResult match = WorldUtil.IMP.getClosestBlock(id); diff --git a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotPlayer.java b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotPlayer.java index 23224c74c..dccc4f3a7 100644 --- a/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotPlayer.java +++ b/Core/src/main/java/com/github/intellectualsites/plotsquared/plot/object/PlotPlayer.java @@ -434,9 +434,9 @@ public abstract class PlotPlayer implements CommandCaller, OfflinePlotPlayer { * Play music at a location for this player. * * @param location where to play the music - * @param id the numerical record item id + * @param id the record item id */ - public abstract void playMusic(Location location, int id); + public abstract void playMusic(Location location, PlotBlock id); /** * Check if this player is banned. From d12bdcfda4e4f6235b99bd7000c345e6791fea74 Mon Sep 17 00:00:00 2001 From: Sauilitired Date: Wed, 26 Dec 2018 19:25:20 +0100 Subject: [PATCH 2/2] Fix NPE on VehicleMoveEvent. Fixes #2195. --- .../bukkit/listeners/PlayerEvents.java | 111 +++++++++--------- 1 file changed, 56 insertions(+), 55 deletions(-) diff --git a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/listeners/PlayerEvents.java b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/listeners/PlayerEvents.java index d45803d81..ac1ec03a7 100644 --- a/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/listeners/PlayerEvents.java +++ b/Bukkit/src/main/java/com/github/intellectualsites/plotsquared/bukkit/listeners/PlayerEvents.java @@ -642,72 +642,73 @@ import java.util.regex.Pattern; Vehicle vehicle = event.getVehicle(); // Check allowed + if (!vehicle.getPassengers().isEmpty()) { + Entity passenger = vehicle.getPassengers().get(0); + if (passenger instanceof Player) { + final Player player = (Player) passenger; + // reset + if (moveTmp == null) + moveTmp = new PlayerMoveEvent(null, from, to); + moveTmp.setFrom(from); + moveTmp.setTo(to); + moveTmp.setCancelled(false); + fieldPlayer.set(moveTmp, player); - Entity passenger = vehicle.getPassengers().get(1); + List passengers = vehicle.getPassengers(); - if (passenger instanceof Player) { - final Player player = (Player) passenger; - // reset - if (moveTmp == null) - moveTmp = new PlayerMoveEvent(null, from, to); - moveTmp.setFrom(from); - moveTmp.setTo(to); - moveTmp.setCancelled(false); - fieldPlayer.set(moveTmp, player); - - List passengers = vehicle.getPassengers(); - - this.playerMove(moveTmp); - org.bukkit.Location dest; - if (moveTmp.isCancelled()) { - dest = from; - } else if (MathMan.roundInt(moveTmp.getTo().getX()) != toX - || MathMan.roundInt(moveTmp.getTo().getZ()) != toZ) { - dest = to; - } else { - dest = null; - } - if (dest != null) { - if (passengers != null) { - vehicle.eject(); - vehicle.setVelocity(new Vector(0d, 0d, 0d)); - vehicle.teleport(dest); - passengers.forEach(vehicle::addPassenger); + this.playerMove(moveTmp); + org.bukkit.Location dest; + if (moveTmp.isCancelled()) { + dest = from; + } else if (MathMan.roundInt(moveTmp.getTo().getX()) != toX + || MathMan.roundInt(moveTmp.getTo().getZ()) != toZ) { + dest = to; } else { - vehicle.eject(); - vehicle.setVelocity(new Vector(0d, 0d, 0d)); - vehicle.teleport(dest); - vehicle.setPassenger(player); + dest = null; + } + if (dest != null) { + if (passengers != null) { + vehicle.eject(); + vehicle.setVelocity(new Vector(0d, 0d, 0d)); + vehicle.teleport(dest); + passengers.forEach(vehicle::addPassenger); + } else { + vehicle.eject(); + vehicle.setVelocity(new Vector(0d, 0d, 0d)); + vehicle.teleport(dest); + vehicle.setPassenger(player); + } + return; } - return; } - } - if (Settings.Enabled_Components.KILL_ROAD_VEHICLES) { - switch (vehicle.getType()) { - case MINECART: - case MINECART_CHEST: - case MINECART_COMMAND: - case MINECART_FURNACE: - case MINECART_HOPPER: - case MINECART_MOB_SPAWNER: - case ENDER_CRYSTAL: - case MINECART_TNT: - case BOAT: { - List meta = vehicle.getMetadata("plot"); - Plot toPlot = BukkitUtil.getLocation(to).getPlot(); - if (!meta.isEmpty()) { - Plot origin = (Plot) meta.get(0).value(); - if (!origin.getBasePlot(false).equals(toPlot)) { - vehicle.remove(); + if (Settings.Enabled_Components.KILL_ROAD_VEHICLES) { + switch (vehicle.getType()) { + case MINECART: + case MINECART_CHEST: + case MINECART_COMMAND: + case MINECART_FURNACE: + case MINECART_HOPPER: + case MINECART_MOB_SPAWNER: + case ENDER_CRYSTAL: + case MINECART_TNT: + case BOAT: { + List meta = vehicle.getMetadata("plot"); + Plot toPlot = BukkitUtil.getLocation(to).getPlot(); + if (!meta.isEmpty()) { + Plot origin = (Plot) meta.get(0).value(); + if (!origin.getBasePlot(false).equals(toPlot)) { + vehicle.remove(); + } + } else if (toPlot != null) { + vehicle.setMetadata("plot", + new FixedMetadataValue((Plugin) PlotSquared.get().IMP, toPlot)); } - } else if (toPlot != null) { - vehicle.setMetadata("plot", - new FixedMetadataValue((Plugin) PlotSquared.get().IMP, toPlot)); } } } } + } }