From cae2f8f58d5d2a33d56220d229f5f9a7245b27e2 Mon Sep 17 00:00:00 2001 From: Gabriele C Date: Fri, 26 Jun 2020 00:26:11 +0200 Subject: [PATCH] Implement FastLoginPremiumToggleEvent --- .../bukkit/command/CrackedCommand.java | 8 ++++ .../bukkit/command/PremiumCommand.java | 7 ++++ .../BukkitFastLoginPremiumToggleEvent.java | 38 +++++++++++++++++++ .../BungeeFastLoginPremiumToggleEvent.java | 26 +++++++++++++ .../bungee/task/AsyncToggleMessage.java | 11 ++++++ .../event/FastLoginPremiumToggleEvent.java | 14 +++++++ 6 files changed, 104 insertions(+) create mode 100644 bukkit/src/main/java/com/github/games647/fastlogin/bukkit/event/BukkitFastLoginPremiumToggleEvent.java create mode 100644 bungee/src/main/java/com/github/games647/fastlogin/bungee/event/BungeeFastLoginPremiumToggleEvent.java create mode 100644 core/src/main/java/com/github/games647/fastlogin/core/shared/event/FastLoginPremiumToggleEvent.java diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/command/CrackedCommand.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/command/CrackedCommand.java index c93a553a..b4c3b1da 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/command/CrackedCommand.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/command/CrackedCommand.java @@ -1,11 +1,15 @@ package com.github.games647.fastlogin.bukkit.command; import com.github.games647.fastlogin.bukkit.FastLoginBukkit; +import com.github.games647.fastlogin.bukkit.event.BukkitFastLoginPremiumToggleEvent; import com.github.games647.fastlogin.core.StoredProfile; +import com.github.games647.fastlogin.core.shared.event.FastLoginPremiumToggleEvent; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; +import static com.github.games647.fastlogin.core.shared.event.FastLoginPremiumToggleEvent.*; + public class CrackedCommand extends ToggleCommand { public CrackedCommand(FastLoginBukkit plugin) { @@ -45,6 +49,8 @@ public class CrackedCommand extends ToggleCommand { profile.setId(null); plugin.getScheduler().runAsync(() -> { plugin.getCore().getStorage().save(profile); + plugin.getServer().getPluginManager().callEvent( + new BukkitFastLoginPremiumToggleEvent(profile, PremiumToggleReason.COMMAND_OTHER)); }); } else { plugin.getCore().sendLocaleMessage("not-premium", sender); @@ -77,6 +83,8 @@ public class CrackedCommand extends ToggleCommand { profile.setPremium(false); plugin.getScheduler().runAsync(() -> { plugin.getCore().getStorage().save(profile); + plugin.getServer().getPluginManager().callEvent( + new BukkitFastLoginPremiumToggleEvent(profile, PremiumToggleReason.COMMAND_OTHER)); }); } } diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/command/PremiumCommand.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/command/PremiumCommand.java index 761d838f..f8d900fe 100644 --- a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/command/PremiumCommand.java +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/command/PremiumCommand.java @@ -1,10 +1,13 @@ package com.github.games647.fastlogin.bukkit.command; import com.github.games647.fastlogin.bukkit.FastLoginBukkit; +import com.github.games647.fastlogin.bukkit.event.BukkitFastLoginPremiumToggleEvent; import com.github.games647.fastlogin.core.StoredProfile; import java.util.UUID; +import com.github.games647.fastlogin.core.shared.event.FastLoginPremiumToggleEvent; +import com.github.games647.fastlogin.core.shared.event.FastLoginPremiumToggleEvent.PremiumToggleReason; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -57,6 +60,8 @@ public class PremiumCommand extends ToggleCommand { profile.setPremium(true); plugin.getScheduler().runAsync(() -> { plugin.getCore().getStorage().save(profile); + plugin.getServer().getPluginManager().callEvent( + new BukkitFastLoginPremiumToggleEvent(profile, PremiumToggleReason.COMMAND_SELF)); }); plugin.getCore().sendLocaleMessage("add-premium", sender); @@ -86,6 +91,8 @@ public class PremiumCommand extends ToggleCommand { profile.setPremium(true); plugin.getScheduler().runAsync(() -> { plugin.getCore().getStorage().save(profile); + plugin.getServer().getPluginManager().callEvent( + new BukkitFastLoginPremiumToggleEvent(profile, PremiumToggleReason.COMMAND_OTHER)); }); plugin.getCore().sendLocaleMessage("add-premium-other", sender); diff --git a/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/event/BukkitFastLoginPremiumToggleEvent.java b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/event/BukkitFastLoginPremiumToggleEvent.java new file mode 100644 index 00000000..27aef5a1 --- /dev/null +++ b/bukkit/src/main/java/com/github/games647/fastlogin/bukkit/event/BukkitFastLoginPremiumToggleEvent.java @@ -0,0 +1,38 @@ +package com.github.games647.fastlogin.bukkit.event; + +import com.github.games647.fastlogin.core.StoredProfile; +import com.github.games647.fastlogin.core.shared.event.FastLoginPremiumToggleEvent; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class BukkitFastLoginPremiumToggleEvent extends Event implements FastLoginPremiumToggleEvent { + + private static final HandlerList handlers = new HandlerList(); + private final StoredProfile profile; + private final PremiumToggleReason reason; + + public BukkitFastLoginPremiumToggleEvent(StoredProfile profile, PremiumToggleReason reason) { + super(true); + this.profile = profile; + this.reason = reason; + } + + @Override + public StoredProfile getProfile() { + return profile; + } + + @Override + public PremiumToggleReason getReason() { + return reason; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/event/BungeeFastLoginPremiumToggleEvent.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/event/BungeeFastLoginPremiumToggleEvent.java new file mode 100644 index 00000000..e213ba4e --- /dev/null +++ b/bungee/src/main/java/com/github/games647/fastlogin/bungee/event/BungeeFastLoginPremiumToggleEvent.java @@ -0,0 +1,26 @@ +package com.github.games647.fastlogin.bungee.event; + +import com.github.games647.fastlogin.core.StoredProfile; +import com.github.games647.fastlogin.core.shared.event.FastLoginPremiumToggleEvent; +import net.md_5.bungee.api.plugin.Event; + +public class BungeeFastLoginPremiumToggleEvent extends Event implements FastLoginPremiumToggleEvent { + + private final StoredProfile profile; + private final PremiumToggleReason reason; + + public BungeeFastLoginPremiumToggleEvent(StoredProfile profile, PremiumToggleReason reason) { + this.profile = profile; + this.reason = reason; + } + + @Override + public StoredProfile getProfile() { + return profile; + } + + @Override + public FastLoginPremiumToggleEvent.PremiumToggleReason getReason() { + return reason; + } +} diff --git a/bungee/src/main/java/com/github/games647/fastlogin/bungee/task/AsyncToggleMessage.java b/bungee/src/main/java/com/github/games647/fastlogin/bungee/task/AsyncToggleMessage.java index 14c0f9fc..132203e2 100644 --- a/bungee/src/main/java/com/github/games647/fastlogin/bungee/task/AsyncToggleMessage.java +++ b/bungee/src/main/java/com/github/games647/fastlogin/bungee/task/AsyncToggleMessage.java @@ -1,9 +1,12 @@ package com.github.games647.fastlogin.bungee.task; import com.github.games647.fastlogin.bungee.FastLoginBungee; +import com.github.games647.fastlogin.bungee.event.BungeeFastLoginPremiumToggleEvent; import com.github.games647.fastlogin.core.StoredProfile; import com.github.games647.fastlogin.core.shared.FastLoginCore; +import com.github.games647.fastlogin.core.shared.event.FastLoginPremiumToggleEvent; +import com.github.games647.fastlogin.core.shared.event.FastLoginPremiumToggleEvent.PremiumToggleReason; import net.md_5.bungee.api.CommandSender; import net.md_5.bungee.api.ProxyServer; import net.md_5.bungee.api.chat.TextComponent; @@ -46,6 +49,10 @@ public class AsyncToggleMessage implements Runnable { playerProfile.setPremium(false); playerProfile.setId(null); core.getStorage().save(playerProfile); + PremiumToggleReason reason = (!isPlayerSender || !sender.getName().equalsIgnoreCase(playerProfile.getName())) ? + PremiumToggleReason.COMMAND_OTHER : PremiumToggleReason.COMMAND_SELF; + core.getPlugin().getProxy().getPluginManager().callEvent( + new BungeeFastLoginPremiumToggleEvent(playerProfile, reason)); sendMessage("remove-premium"); } @@ -58,6 +65,10 @@ public class AsyncToggleMessage implements Runnable { playerProfile.setPremium(true); core.getStorage().save(playerProfile); + PremiumToggleReason reason = (!isPlayerSender || !sender.getName().equalsIgnoreCase(playerProfile.getName())) ? + PremiumToggleReason.COMMAND_OTHER : PremiumToggleReason.COMMAND_SELF; + core.getPlugin().getProxy().getPluginManager().callEvent( + new BungeeFastLoginPremiumToggleEvent(playerProfile, reason)); sendMessage("add-premium"); } diff --git a/core/src/main/java/com/github/games647/fastlogin/core/shared/event/FastLoginPremiumToggleEvent.java b/core/src/main/java/com/github/games647/fastlogin/core/shared/event/FastLoginPremiumToggleEvent.java new file mode 100644 index 00000000..9e5420af --- /dev/null +++ b/core/src/main/java/com/github/games647/fastlogin/core/shared/event/FastLoginPremiumToggleEvent.java @@ -0,0 +1,14 @@ +package com.github.games647.fastlogin.core.shared.event; + +import com.github.games647.fastlogin.core.StoredProfile; + +public interface FastLoginPremiumToggleEvent { + + StoredProfile getProfile(); + PremiumToggleReason getReason(); + + enum PremiumToggleReason { + COMMAND_SELF, + COMMAND_OTHER + } +}