From 183dbed0036f2e272e3bdffa944e9b5855a188f1 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Thu, 4 Jan 2024 20:03:11 +0100 Subject: [PATCH] Fix AdventCalender only in December, Reduce code duplication --- src/de/steamwar/lobby/listener/InventoryInteraction.java | 8 ++------ src/de/steamwar/lobby/listener/PlayerSpawn.java | 8 ++------ src/de/steamwar/lobby/special/advent/AdventCommand.java | 8 ++------ src/de/steamwar/lobby/special/advent/AdventListener.java | 7 +------ src/de/steamwar/lobby/special/advent/AdventsCalendar.java | 8 ++++++++ .../lobby/special/advent/PresentClickListener.java | 7 ++----- 6 files changed, 17 insertions(+), 29 deletions(-) diff --git a/src/de/steamwar/lobby/listener/InventoryInteraction.java b/src/de/steamwar/lobby/listener/InventoryInteraction.java index e40ea73..14b28d9 100644 --- a/src/de/steamwar/lobby/listener/InventoryInteraction.java +++ b/src/de/steamwar/lobby/listener/InventoryInteraction.java @@ -21,6 +21,7 @@ package de.steamwar.lobby.listener; import de.steamwar.lobby.LobbySystem; import de.steamwar.lobby.command.ModifyCommand; +import de.steamwar.lobby.special.advent.AdventsCalendar; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.event.EventHandler; @@ -33,9 +34,6 @@ import org.bukkit.event.player.PlayerPickupItemEvent; import org.bukkit.event.player.PlayerSwapHandItemsEvent; import org.bukkit.inventory.ItemStack; -import java.time.LocalDate; -import java.time.Month; - public class InventoryInteraction extends BasicListener { @EventHandler @@ -46,9 +44,7 @@ public class InventoryInteraction extends BasicListener { if(item.getType() == Material.FIREWORK_ROCKET && (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)) { int offset; - LocalDate localDate = LocalDate.now(); - Month month = localDate.getMonth(); - if (month == Month.NOVEMBER || month == Month.DECEMBER || month == Month.JANUARY) { + if (AdventsCalendar.active()) { offset = -1; } else { offset = 0; diff --git a/src/de/steamwar/lobby/listener/PlayerSpawn.java b/src/de/steamwar/lobby/listener/PlayerSpawn.java index 3c18b31..3ca809c 100644 --- a/src/de/steamwar/lobby/listener/PlayerSpawn.java +++ b/src/de/steamwar/lobby/listener/PlayerSpawn.java @@ -21,6 +21,7 @@ package de.steamwar.lobby.listener; import de.steamwar.lobby.LobbySystem; import de.steamwar.lobby.boatrace.BoatRace; +import de.steamwar.lobby.special.advent.AdventsCalendar; import de.steamwar.lobby.util.ItemBuilder; import de.steamwar.network.NetworkSender; import de.steamwar.network.packets.client.ImALobbyPacket; @@ -36,9 +37,6 @@ import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; -import java.time.LocalDate; -import java.time.Month; - public class PlayerSpawn extends BasicListener { public static final int FIREWORK_SLOT = 2; @@ -75,9 +73,7 @@ public class PlayerSpawn extends BasicListener { inventory.setItem(EquipmentSlot.CHEST, ELYTRA); int offset = 0; - LocalDate localDate = LocalDate.now(); - Month month = localDate.getMonth(); - if (month == Month.NOVEMBER || month == Month.DECEMBER || month == Month.JANUARY) { + if (AdventsCalendar.active()) { offset = -1; } diff --git a/src/de/steamwar/lobby/special/advent/AdventCommand.java b/src/de/steamwar/lobby/special/advent/AdventCommand.java index 6697eeb..efbac11 100644 --- a/src/de/steamwar/lobby/special/advent/AdventCommand.java +++ b/src/de/steamwar/lobby/special/advent/AdventCommand.java @@ -29,8 +29,6 @@ import de.steamwar.sql.SteamwarUser; import org.bukkit.Material; import org.bukkit.entity.Player; -import java.time.LocalDate; -import java.time.Month; import java.util.ArrayList; import java.util.List; import java.util.Random; @@ -59,13 +57,11 @@ public class AdventCommand extends SWCommand { @Register public void genericCommand(Player player, String... args) { - LocalDate localDate = LocalDate.now(); - Month month = localDate.getMonth(); - if (month != Month.NOVEMBER && month != Month.DECEMBER && month != Month.JANUARY) { + if (!AdventsCalendar.active()) { return; } - Random random = new Random(localDate.getYear()); + Random random = new Random(); List> itemList = new ArrayList<>(); SteamwarUser steamwarUser = SteamwarUser.get(player.getUniqueId()); diff --git a/src/de/steamwar/lobby/special/advent/AdventListener.java b/src/de/steamwar/lobby/special/advent/AdventListener.java index d614d55..061d4ce 100644 --- a/src/de/steamwar/lobby/special/advent/AdventListener.java +++ b/src/de/steamwar/lobby/special/advent/AdventListener.java @@ -28,9 +28,6 @@ import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.inventory.ItemStack; -import java.time.LocalDate; -import java.time.Month; - public class AdventListener extends BasicListener { public static final int ADVENT_SLOT = 7; @@ -52,9 +49,7 @@ public class AdventListener extends BasicListener { @EventHandler(priority = EventPriority.HIGH) public void onJoin(PlayerJoinEvent e) { - LocalDate localDate = LocalDate.now(); - Month month = localDate.getMonth(); - if (month != Month.NOVEMBER && month != Month.DECEMBER && month != Month.JANUARY) { + if (!AdventsCalendar.active()) { return; } e.getPlayer().getInventory().setItem(ADVENT_SLOT, ADVENT.getItemStack()); diff --git a/src/de/steamwar/lobby/special/advent/AdventsCalendar.java b/src/de/steamwar/lobby/special/advent/AdventsCalendar.java index 06d7af8..43ae6af 100644 --- a/src/de/steamwar/lobby/special/advent/AdventsCalendar.java +++ b/src/de/steamwar/lobby/special/advent/AdventsCalendar.java @@ -26,6 +26,8 @@ import org.bukkit.configuration.file.YamlConfiguration; import java.io.File; import java.io.IOException; +import java.time.LocalDate; +import java.time.Month; import java.util.ArrayList; import java.util.List; @@ -70,4 +72,10 @@ public class AdventsCalendar { } System.out.println("Save time: " + (System.currentTimeMillis() - time) + "ms"); } + + public static boolean active() { + LocalDate localDate = LocalDate.now(); + Month month = localDate.getMonth(); + return month == Month.DECEMBER; + } } diff --git a/src/de/steamwar/lobby/special/advent/PresentClickListener.java b/src/de/steamwar/lobby/special/advent/PresentClickListener.java index 9180791..5e78898 100644 --- a/src/de/steamwar/lobby/special/advent/PresentClickListener.java +++ b/src/de/steamwar/lobby/special/advent/PresentClickListener.java @@ -29,7 +29,6 @@ import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerJoinEvent; import java.time.LocalDate; -import java.time.Month; public class PresentClickListener extends BasicListener { @@ -37,9 +36,8 @@ public class PresentClickListener extends BasicListener { public void onPlayerJoin(PlayerJoinEvent event) { LocalDate localDate = LocalDate.now(); int day = localDate.getDayOfMonth(); - Month month = localDate.getMonth(); - if (month != Month.DECEMBER) { + if (!AdventsCalendar.active()) { return; } AdventsCalendar.getPresentList().forEach(present -> { @@ -56,9 +54,8 @@ public class PresentClickListener extends BasicListener { LocalDate localDate = LocalDate.now(); int day = localDate.getDayOfMonth(); - Month month = localDate.getMonth(); - if (month != Month.DECEMBER) { + if (!AdventsCalendar.active()) { return; }