From 893a12d06a8cbafb0891ff532ef202b81bff1601 Mon Sep 17 00:00:00 2001 From: Hassan Syyid Date: Fri, 29 Apr 2016 23:40:47 -0400 Subject: [PATCH] Fix AdminShops not being immune to natural causes. --- build.gradle | 2 +- .../io/github/hsyyid/adminshop/AdminShop.java | 10 +-- .../listeners/BreakBlockListener.java | 62 +++++++++++++++++++ ...stener.java => InteractBlockListener.java} | 2 +- .../listeners/PlayerBreakBlockListener.java | 45 -------------- 5 files changed, 69 insertions(+), 52 deletions(-) create mode 100644 src/main/java/io/github/hsyyid/adminshop/listeners/BreakBlockListener.java rename src/main/java/io/github/hsyyid/adminshop/listeners/{PlayerInteractBlockListener.java => InteractBlockListener.java} (99%) delete mode 100644 src/main/java/io/github/hsyyid/adminshop/listeners/PlayerBreakBlockListener.java diff --git a/build.gradle b/build.gradle index 7c81750..12a2dd6 100644 --- a/build.gradle +++ b/build.gradle @@ -19,7 +19,7 @@ apply plugin: 'eclipse' sourceCompatibility = 1.8 targetCompatibility = 1.8 -version = '1.7c' +version = '1.8' group = "io.github.hsyyid" archivesBaseName = "AdminShop" diff --git a/src/main/java/io/github/hsyyid/adminshop/AdminShop.java b/src/main/java/io/github/hsyyid/adminshop/AdminShop.java index 526bd76..9278801 100644 --- a/src/main/java/io/github/hsyyid/adminshop/AdminShop.java +++ b/src/main/java/io/github/hsyyid/adminshop/AdminShop.java @@ -6,8 +6,8 @@ import io.github.hsyyid.adminshop.cmdexecutors.SetShopExecutor; import io.github.hsyyid.adminshop.config.Config; import io.github.hsyyid.adminshop.config.ShopConfig; -import io.github.hsyyid.adminshop.listeners.PlayerBreakBlockListener; -import io.github.hsyyid.adminshop.listeners.PlayerInteractBlockListener; +import io.github.hsyyid.adminshop.listeners.BreakBlockListener; +import io.github.hsyyid.adminshop.listeners.InteractBlockListener; import io.github.hsyyid.adminshop.utils.ConfigManager; import io.github.hsyyid.adminshop.utils.Shop; import io.github.hsyyid.adminshop.utils.ShopModifier; @@ -34,7 +34,7 @@ import java.util.Set; import java.util.UUID; -@Plugin(id = "io.github.hsyyid.adminshop", name = "AdminShop", description = "This plugin adds sign shops for users to buy items.", version = "1.7c") +@Plugin(id = "io.github.hsyyid.adminshop", name = "AdminShop", description = "This plugin adds sign shops for users to buy items.", version = "1.8") public class AdminShop { protected AdminShop() @@ -118,8 +118,8 @@ public void init(GameInitializationEvent event) Sponge.getCommandManager().register(this, setItemShopCommandSpec, "setshop"); - Sponge.getEventManager().registerListeners(this, new PlayerBreakBlockListener()); - Sponge.getEventManager().registerListeners(this, new PlayerInteractBlockListener()); + Sponge.getEventManager().registerListeners(this, new BreakBlockListener()); + Sponge.getEventManager().registerListeners(this, new InteractBlockListener()); getLogger().info("-----------------------------"); getLogger().info("AdminShop was made by HassanS6000!"); diff --git a/src/main/java/io/github/hsyyid/adminshop/listeners/BreakBlockListener.java b/src/main/java/io/github/hsyyid/adminshop/listeners/BreakBlockListener.java new file mode 100644 index 0000000..4b813ad --- /dev/null +++ b/src/main/java/io/github/hsyyid/adminshop/listeners/BreakBlockListener.java @@ -0,0 +1,62 @@ +package io.github.hsyyid.adminshop.listeners; + +import io.github.hsyyid.adminshop.AdminShop; +import io.github.hsyyid.adminshop.utils.ConfigManager; +import io.github.hsyyid.adminshop.utils.Shop; +import org.spongepowered.api.block.BlockSnapshot; +import org.spongepowered.api.data.Transaction; +import org.spongepowered.api.entity.living.player.Player; +import org.spongepowered.api.event.Listener; +import org.spongepowered.api.event.block.ChangeBlockEvent; +import org.spongepowered.api.text.Text; +import org.spongepowered.api.text.format.TextColors; +import org.spongepowered.api.world.Location; +import org.spongepowered.api.world.World; + +import java.util.Optional; + +public class BreakBlockListener +{ + @Listener + public void onBreakBlock(ChangeBlockEvent.Break event) + { + if (event.getCause().first(Player.class).isPresent()) + { + Player player = event.getCause().first(Player.class).get(); + + for (Transaction transaction : event.getTransactions()) + { + Location location = transaction.getOriginal().getLocation().get(); + Optional shop = AdminShop.shops.values().stream().filter(s -> s.getSignLocation().equals(location)).findAny(); + + if (shop.isPresent()) + { + if (player.hasPermission("adminshop.remove")) + { + AdminShop.shops.values().remove(shop.get()); + ConfigManager.writeShops(); + player.sendMessage(Text.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.GREEN, "Shop successfully removed!")); + } + else + { + player.sendMessage(Text.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.RED, "You do not have permission to remove shops.")); + event.setCancelled(true); + } + } + } + } + else + { + for (Transaction transaction : event.getTransactions()) + { + Location location = transaction.getOriginal().getLocation().get(); + Optional shop = AdminShop.shops.values().stream().filter(s -> s.getSignLocation().equals(location)).findAny(); + + if (shop.isPresent()) + { + event.setCancelled(true); + } + } + } + } +} diff --git a/src/main/java/io/github/hsyyid/adminshop/listeners/PlayerInteractBlockListener.java b/src/main/java/io/github/hsyyid/adminshop/listeners/InteractBlockListener.java similarity index 99% rename from src/main/java/io/github/hsyyid/adminshop/listeners/PlayerInteractBlockListener.java rename to src/main/java/io/github/hsyyid/adminshop/listeners/InteractBlockListener.java index f96fc00..7875a68 100644 --- a/src/main/java/io/github/hsyyid/adminshop/listeners/PlayerInteractBlockListener.java +++ b/src/main/java/io/github/hsyyid/adminshop/listeners/InteractBlockListener.java @@ -30,7 +30,7 @@ import java.util.Optional; import java.util.UUID; -public class PlayerInteractBlockListener +public class InteractBlockListener { @Listener public void onPlayerRightClickBlock(InteractBlockEvent.Secondary event, @First Player player) diff --git a/src/main/java/io/github/hsyyid/adminshop/listeners/PlayerBreakBlockListener.java b/src/main/java/io/github/hsyyid/adminshop/listeners/PlayerBreakBlockListener.java deleted file mode 100644 index fc57f62..0000000 --- a/src/main/java/io/github/hsyyid/adminshop/listeners/PlayerBreakBlockListener.java +++ /dev/null @@ -1,45 +0,0 @@ -package io.github.hsyyid.adminshop.listeners; - -import io.github.hsyyid.adminshop.AdminShop; -import io.github.hsyyid.adminshop.utils.ConfigManager; -import io.github.hsyyid.adminshop.utils.Shop; -import org.spongepowered.api.block.BlockSnapshot; -import org.spongepowered.api.data.Transaction; -import org.spongepowered.api.entity.living.player.Player; -import org.spongepowered.api.event.Listener; -import org.spongepowered.api.event.block.ChangeBlockEvent; -import org.spongepowered.api.event.filter.cause.First; -import org.spongepowered.api.text.Text; -import org.spongepowered.api.text.format.TextColors; -import org.spongepowered.api.world.Location; -import org.spongepowered.api.world.World; - -import java.util.Optional; - -public class PlayerBreakBlockListener -{ - @Listener - public void onPlayerBreakBlock(ChangeBlockEvent.Break event, @First Player player) - { - for (Transaction transaction : event.getTransactions()) - { - Location location = transaction.getOriginal().getLocation().get(); - Optional shop = AdminShop.shops.values().stream().filter(s -> s.getSignLocation().equals(location)).findAny(); - - if (shop.isPresent()) - { - if (player.hasPermission("adminshop.remove")) - { - AdminShop.shops.values().remove(shop.get()); - ConfigManager.writeShops(); - player.sendMessage(Text.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.GREEN, "Shop successfully removed!")); - } - else - { - player.sendMessage(Text.of(TextColors.DARK_RED, "[AdminShop]: ", TextColors.RED, "You do not have permission to remove shops.")); - event.setCancelled(true); - } - } - } - } -}