From 9d85db13b15f374422bf2c7e5962b20e9dcaa95d Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Wed, 15 Jun 2016 17:20:38 +0200 Subject: [PATCH] GUI. getNearbyPlayers. Stuff --- .../massivecore/chestgui/ChestGui.java | 24 ++++++++--- .../engine/EngineMassiveCoreChestGui.java | 35 ++++++++++++---- .../event/EventMassiveCoreAknowledge.java | 41 +++++++++++++++++++ .../massivecore/store/SenderEntity.java | 12 ++++++ .../massivecraft/massivecore/util/MUtil.java | 33 +++++++++++++++ 5 files changed, 132 insertions(+), 13 deletions(-) create mode 100644 src/com/massivecraft/massivecore/event/EventMassiveCoreAknowledge.java diff --git a/src/com/massivecraft/massivecore/chestgui/ChestGui.java b/src/com/massivecraft/massivecore/chestgui/ChestGui.java index 82c44e09..3644c6df 100644 --- a/src/com/massivecraft/massivecore/chestgui/ChestGui.java +++ b/src/com/massivecraft/massivecore/chestgui/ChestGui.java @@ -80,14 +80,26 @@ public class ChestGui public List getRunnablesClose() { return this.runnablesClose; } // -------------------------------------------- // - // SOUND + // SOUNDS // -------------------------------------------- // - // The click sound you should hear when clicking an action slot. - // You can disable it by setting null. + // This section contains all kinds of sounds. + // You can disable a sound by setting it to null. - private SoundEffect soundEffect = MassiveCoreMConf.get().clickSound; - public SoundEffect getSoundEffect() { return this.soundEffect; } - public ChestGui setSoundEffect(SoundEffect soundEffect) { this.soundEffect = soundEffect; return this; } + // The sound you should hear when clicking an action slot. + private SoundEffect soundClick = MassiveCoreMConf.get().clickSound; + public SoundEffect getSoundClick() { return this.soundClick; } + public ChestGui setSoundClick(SoundEffect soundClick) { this.soundClick = soundClick; return this; } + + // The sound you should hear when opening the GUI. + private SoundEffect soundOpen = SoundEffect.valueOf("CHEST_OPEN", 0.75f, 1.0f); + public SoundEffect getSoundOpen() { return this.soundOpen; } + public ChestGui setSoundOpen(SoundEffect soundOpen) { this.soundOpen = soundOpen; return this; } + + // The sound you should hear when closing the GUI. + // This sound will be skipped if another inventory was opened by the GUI action. + private SoundEffect soundClose = SoundEffect.valueOf("CHEST_CLOSE", 0.75f, 1.0f); + public SoundEffect getSoundClose() { return this.soundClose; } + public ChestGui setSoundClose(SoundEffect soundClose) { this.soundClose= soundClose; return this; } // -------------------------------------------- // // AUTOCLOSING diff --git a/src/com/massivecraft/massivecore/engine/EngineMassiveCoreChestGui.java b/src/com/massivecraft/massivecore/engine/EngineMassiveCoreChestGui.java index 1538eac3..2933e86c 100644 --- a/src/com/massivecraft/massivecore/engine/EngineMassiveCoreChestGui.java +++ b/src/com/massivecraft/massivecore/engine/EngineMassiveCoreChestGui.java @@ -1,12 +1,15 @@ package com.massivecraft.massivecore.engine; import org.bukkit.event.Event.Result; + import org.bukkit.Bukkit; +import org.bukkit.entity.HumanEntity; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.inventory.InventoryOpenEvent; +import org.bukkit.event.inventory.InventoryType; import org.bukkit.inventory.Inventory; import com.massivecraft.massivecore.Engine; import com.massivecraft.massivecore.SoundEffect; @@ -60,9 +63,9 @@ public class EngineMassiveCoreChestGui extends Engine // ... set last action ... gui.setLastAction(action); - // ... then play the sound ... - SoundEffect soundEffect = gui.getSoundEffect(); - if (soundEffect != null) soundEffect.run(event.getWhoClicked()); + // ... then play click sound ... + SoundEffect sound = gui.getSoundClick(); + if (sound != null) sound.run(event.getWhoClicked()); // ... close the GUI ... if (gui.isAutoclosing()) event.getView().close(); @@ -80,12 +83,21 @@ public class EngineMassiveCoreChestGui extends Engine final ChestGui gui = ChestGui.get(inventory); if (gui == null) return; - // Runnables + // Sound + SoundEffect sound = gui.getSoundOpen(); + if (sound != null) + { + HumanEntity human = event.getPlayer(); + sound.run(human); + } + + // Later Bukkit.getScheduler().runTask(getPlugin(), new Runnable() { @Override public void run() { + // Runnables for (Runnable runnable : gui.getRunnablesOpen()) { runnable.run(); @@ -94,8 +106,6 @@ public class EngineMassiveCoreChestGui extends Engine }); } - // NOTE: - @EventHandler(priority = EventPriority.MONITOR) public void onClose(InventoryCloseEvent event) { @@ -105,16 +115,27 @@ public class EngineMassiveCoreChestGui extends Engine final ChestGui gui = ChestGui.get(inventory); if (gui == null) return; - // Runnables + // Human + final HumanEntity human = event.getPlayer(); + + // Later Bukkit.getScheduler().runTask(getPlugin(), new Runnable() { @Override public void run() { + // Runnables for (Runnable runnable : gui.getRunnablesClose()) { runnable.run(); } + + // Sound + SoundEffect sound = gui.getSoundClose(); + if (sound != null && human.getOpenInventory().getTopInventory().getType() == InventoryType.CRAFTING) + { + sound.run(human); + } } }); diff --git a/src/com/massivecraft/massivecore/event/EventMassiveCoreAknowledge.java b/src/com/massivecraft/massivecore/event/EventMassiveCoreAknowledge.java new file mode 100644 index 00000000..3d8f919d --- /dev/null +++ b/src/com/massivecraft/massivecore/event/EventMassiveCoreAknowledge.java @@ -0,0 +1,41 @@ +package com.massivecraft.massivecore.event; + +import org.bukkit.event.HandlerList; + +// "Aknowledge" is in our mind the opposite of "Ignore". +// The purpose of this event is to decide if a unit of communication should be received or ignored. +// A unit of communication can for example be a chat message or a sound effect. +public class EventMassiveCoreAknowledge extends EventMassiveCore +{ + // -------------------------------------------- // + // REQUIRED EVENT CODE + // -------------------------------------------- // + + private static final HandlerList handlers = new HandlerList(); + @Override public HandlerList getHandlers() { return handlers; } + public static HandlerList getHandlerList() { return handlers; } + + // -------------------------------------------- // + // FIELDS + // -------------------------------------------- // + + private final Object sender; + public Object getSender() { return this.sender; } + + private final Object sendee; + public Object getSendee() { return this.sendee; } + + // -------------------------------------------- // + // CONSTRUCT + // -------------------------------------------- // + + public EventMassiveCoreAknowledge(Object sender, Object sendee) + { + if (sender == null) throw new NullPointerException("sender"); + if (sendee == null) throw new NullPointerException("sendee"); + + this.sender = sender; + this.sendee = sendee; + } + +} diff --git a/src/com/massivecraft/massivecore/store/SenderEntity.java b/src/com/massivecraft/massivecore/store/SenderEntity.java index 956f8e41..ed36a656 100644 --- a/src/com/massivecraft/massivecore/store/SenderEntity.java +++ b/src/com/massivecraft/massivecore/store/SenderEntity.java @@ -10,6 +10,7 @@ import org.bukkit.entity.Player; import com.google.common.base.Objects; import com.massivecraft.massivecore.Named; +import com.massivecraft.massivecore.event.EventMassiveCoreAknowledge; import com.massivecraft.massivecore.mixin.MixinDisplayName; import com.massivecraft.massivecore.mixin.MixinMessage; import com.massivecraft.massivecore.mixin.MixinPlayed; @@ -184,6 +185,17 @@ public abstract class SenderEntity> extends Entity return MixinDisplayName.get().getDisplayNameMson(this.getId(), watcherObject); } + // -------------------------------------------- // + // AKNOWLEDGE + // -------------------------------------------- // + + public boolean isAcknowledging(Object sender) + { + EventMassiveCoreAknowledge event = new EventMassiveCoreAknowledge(sender, this); + event.run(); + return ! event.isCancelled(); + } + // -------------------------------------------- // // MSG / MESSAGE // -------------------------------------------- // diff --git a/src/com/massivecraft/massivecore/util/MUtil.java b/src/com/massivecraft/massivecore/util/MUtil.java index 841cc4e0..f188199b 100644 --- a/src/com/massivecraft/massivecore/util/MUtil.java +++ b/src/com/massivecraft/massivecore/util/MUtil.java @@ -177,6 +177,39 @@ public class MUtil } } + // -------------------------------------------- // + // GET NEARBY PLAYERS + // -------------------------------------------- // + + public static Set getNearbyPlayers(Entity entity, double raidus, boolean includeSelf) + { + Set ret = getNearbyPlayers(entity.getLocation(), raidus); + if (isPlayer(entity) && !includeSelf) ret.remove(entity); + return ret; + } + + public static Set getNearbyPlayers(Location location, double radius) + { + // Create + Set ret = new MassiveSet<>(); + + // Fill + final World world = location.getWorld(); + final double radiusSquared = radius * radius; + for (Player player : MUtil.getOnlinePlayers()) + { + Location playerLocation = player.getLocation(); + World playerWorld = playerLocation.getWorld(); + if ( ! world.equals(playerWorld)) continue; + double distanceSquared = location.distanceSquared(playerLocation); + if (distanceSquared > radiusSquared) continue; + ret.add(player); + } + + // Return + return ret; + } + // -------------------------------------------- // // IS SYNCHRONOUS // -------------------------------------------- //