diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 0ed27090d..ab5b00f18 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -31,7 +31,7 @@ vault = "1.7.1" triumph-cmd = "2.0.0-ALPHA-10" jetbrains = "24.1.0" adventure = "4.17.0" -vital-paper = "0.0.2" +vital-paper = "0.0.3" # Modrinth diff --git a/paper/src/main/java/com/badbones69/crazycrates/CrazyCrates.java b/paper/src/main/java/com/badbones69/crazycrates/CrazyCrates.java index 6246f73a7..e69891d47 100644 --- a/paper/src/main/java/com/badbones69/crazycrates/CrazyCrates.java +++ b/paper/src/main/java/com/badbones69/crazycrates/CrazyCrates.java @@ -11,6 +11,7 @@ import com.badbones69.crazycrates.listeners.BrokeLocationsListener; import com.badbones69.crazycrates.listeners.CrateControlListener; import com.badbones69.crazycrates.listeners.MiscListener; +import com.badbones69.crazycrates.listeners.crates.CrateStatusListener; import com.badbones69.crazycrates.listeners.crates.types.CosmicCrateListener; import com.badbones69.crazycrates.listeners.crates.CrateOpenListener; import com.badbones69.crazycrates.listeners.crates.types.MobileCrateListener; @@ -106,6 +107,9 @@ public void onEnable() { new CrateMainMenu(), new CrateTierMenu(), + // Crate Monitoring + new CrateStatusListener(), + // Other listeners. new BrokeLocationsListener(), new CrateControlListener(), diff --git a/paper/src/main/java/com/badbones69/crazycrates/api/builders/v2/CrateBuilder.java b/paper/src/main/java/com/badbones69/crazycrates/api/builders/v2/CrateBuilder.java new file mode 100644 index 000000000..ece71ae71 --- /dev/null +++ b/paper/src/main/java/com/badbones69/crazycrates/api/builders/v2/CrateBuilder.java @@ -0,0 +1,93 @@ +package com.badbones69.crazycrates.api.builders.v2; + +import com.badbones69.crazycrates.CrazyCrates; +import com.badbones69.crazycrates.api.events.crates.CrateStatusEvent; +import com.badbones69.crazycrates.api.objects.Crate; +import com.badbones69.crazycrates.api.utils.MiscUtils; +import com.ryderbelserion.vital.paper.api.builders.gui.interfaces.Gui; +import com.ryderbelserion.vital.paper.api.builders.gui.interfaces.GuiItem; +import com.ryderbelserion.vital.paper.api.builders.gui.interfaces.GuiType; +import com.ryderbelserion.vital.paper.util.scheduler.FoliaRunnable; +import net.kyori.adventure.text.logger.slf4j.ComponentLogger; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import us.crazycrew.crazycrates.api.enums.types.KeyType; + +@SuppressWarnings("UnusedReturnValue") +public abstract class CrateBuilder extends FoliaRunnable { + + protected CrazyCrates plugin = CrazyCrates.getPlugin(); + protected ComponentLogger logger = this.plugin.getComponentLogger(); + + private final CrateStatusEvent event; + + private final Player player; + private final Crate crate; + + private final Gui gui; + + private String title; + private int seconds; + + public CrateBuilder(final Player player, final Crate crate, final int rows) { + super(player.getScheduler(), null); + + this.gui = Gui.gui().setType(GuiType.CHEST).disableInteractions().setTitle(crate.getCrateName()).setRows(rows).create(); + this.event = new CrateStatusEvent(player, crate); + + this.player = player; + this.crate = crate; + } + + public abstract void open(final KeyType keyType, final boolean inspectInventory); + + public final CrateBuilder setSeconds(final int seconds) { + this.seconds = seconds; + + return this; + } + + public final CrateBuilder setTitle(final String title) { + this.gui.setTitle(this.title = title); + + return this; + } + + // Items + public void setGuiItem(final ItemStack item, final int slot) { + this.gui.setItem(slot, new GuiItem(item)); + } + + public void setRandomGlass(final int slot) { + setGuiItem(getRandomGlass(), slot); + } + + // Getters + public final ItemStack getRandomGlass() { + return MiscUtils.getRandomPaneColor().setDisplayName(" ").getStack(); + } + + public final CrateStatusEvent getEvent() { + return this.event; + } + + public final Player getPlayer() { + return this.player; + } + + public final String getTitle() { + return this.title; + } + + public final Crate getCrate() { + return this.crate; + } + + public final int getSeconds() { + return this.seconds; + } + + public final Gui getGui() { + return this.gui; + } +} \ No newline at end of file diff --git a/paper/src/main/java/com/badbones69/crazycrates/api/enums/crates/CrateStatus.java b/paper/src/main/java/com/badbones69/crazycrates/api/enums/crates/CrateStatus.java new file mode 100644 index 000000000..e0556b779 --- /dev/null +++ b/paper/src/main/java/com/badbones69/crazycrates/api/enums/crates/CrateStatus.java @@ -0,0 +1,20 @@ +package com.badbones69.crazycrates.api.enums.crates; + +public enum CrateStatus { + + failed("Failed"), + ended("Ended"), + cycling("Cycling"), + opened("Opened"), + silent("Silent"); + + private final String status; + + CrateStatus(final String status) { + this.status = status; + } + + public final String getStatus() { + return this.status; + } +} \ No newline at end of file diff --git a/paper/src/main/java/com/badbones69/crazycrates/api/events/PlayerPrizeEvent.java b/paper/src/main/java/com/badbones69/crazycrates/api/events/PlayerPrizeEvent.java index da929a717..6e51a351d 100644 --- a/paper/src/main/java/com/badbones69/crazycrates/api/events/PlayerPrizeEvent.java +++ b/paper/src/main/java/com/badbones69/crazycrates/api/events/PlayerPrizeEvent.java @@ -1,30 +1,47 @@ package com.badbones69.crazycrates.api.events; +import com.badbones69.crazycrates.api.events.crates.CrateStatusEvent; import com.badbones69.crazycrates.api.objects.Prize; import com.badbones69.crazycrates.api.objects.Crate; import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; -public class PlayerPrizeEvent extends Event { +public class PlayerPrizeEvent extends Event implements Cancellable { private static final HandlerList handlers = new HandlerList(); + + private final CrateStatusEvent event; + private final String crateName; private final Player player; private final Crate crate; private final Prize prize; - private final String crateName; + + private boolean isCancelled; @Deprecated(since = "3.7.4", forRemoval = true) - public PlayerPrizeEvent(@NotNull final Player player, @NotNull final Crate crate, @NotNull final String crateName, @NotNull final Prize prize) { + public PlayerPrizeEvent(@NotNull final Player player, @NotNull final Crate crate, @NotNull final String crateName, @Nullable final Prize prize) { + this(player, crate, prize); + } + + public PlayerPrizeEvent(@NotNull final Player player, @NotNull final Crate crate, @Nullable final Prize prize) { + this(player, null, crate, prize); + } + + @ApiStatus.Internal + public PlayerPrizeEvent(@NotNull final Player player, @Nullable final CrateStatusEvent event, @NotNull final Crate crate, @Nullable final Prize prize) { this.player = player; this.crate = crate; + this.crateName = this.crate.getFileName(); this.prize = prize; - this.crateName = crateName; - } - public PlayerPrizeEvent(@NotNull final Player player, @NotNull final Crate crate, @NotNull final Prize prize) { - this(player, crate, crate.getFileName(), prize); + this.isCancelled = false; + + this.event = event; } public static HandlerList getHandlerList() { @@ -47,7 +64,21 @@ public static HandlerList getHandlerList() { return this.crateName; } - public @NotNull final Prize getPrize() { + public @Nullable final Prize getPrize() { return this.prize; } + + public @Nullable final CrateStatusEvent getEvent() { + return this.event; + } + + @Override + public final boolean isCancelled() { + return this.isCancelled; + } + + @Override + public void setCancelled(final boolean cancel) { + this.isCancelled = cancel; + } } \ No newline at end of file diff --git a/paper/src/main/java/com/badbones69/crazycrates/api/events/crates/CrateStatusEvent.java b/paper/src/main/java/com/badbones69/crazycrates/api/events/crates/CrateStatusEvent.java new file mode 100644 index 000000000..75c1add34 --- /dev/null +++ b/paper/src/main/java/com/badbones69/crazycrates/api/events/crates/CrateStatusEvent.java @@ -0,0 +1,72 @@ +package com.badbones69.crazycrates.api.events.crates; + +import com.badbones69.crazycrates.api.enums.crates.CrateStatus; +import com.badbones69.crazycrates.api.objects.Crate; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.bukkit.inventory.Inventory; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class CrateStatusEvent extends Event { + + private final Player player; + private final Crate crate; + + public CrateStatusEvent(final Player player, final Crate crate) { + this.status = CrateStatus.opened; + this.player = player; + this.crate = crate; + } + + private Inventory inventory; + private CrateStatus status; + private Location location; + + public void setInventory(@Nullable final Inventory inventory) { + this.inventory = inventory; + } + + public @Nullable final Inventory getInventory() { + return this.inventory; + } + + public void setLocation(@Nullable final Location location) { + this.location = location != null ? location : this.player.getLocation(); + } + + public final Location getLocation() { + return this.location; + } + + public final CrateStatusEvent setStatus(@NotNull final CrateStatus status) { + this.status = status; + + return this; + } + + public final CrateStatus getStatus() { + return this.status; + } + + public final Player getPlayer() { + return this.player; + } + + public final Crate getCrate() { + return this.crate; + } + + private static final HandlerList handlers = new HandlerList(); + + public static HandlerList getHandlerList() { + return handlers; + } + + @Override + public @NotNull HandlerList getHandlers() { + return handlers; + } +} \ No newline at end of file diff --git a/paper/src/main/java/com/badbones69/crazycrates/listeners/crates/CrateStatusListener.java b/paper/src/main/java/com/badbones69/crazycrates/listeners/crates/CrateStatusListener.java new file mode 100644 index 000000000..4124ca608 --- /dev/null +++ b/paper/src/main/java/com/badbones69/crazycrates/listeners/crates/CrateStatusListener.java @@ -0,0 +1,132 @@ +package com.badbones69.crazycrates.listeners.crates; + +import com.badbones69.crazycrates.CrazyCrates; +import com.badbones69.crazycrates.api.PrizeManager; +import com.badbones69.crazycrates.api.enums.Messages; +import com.badbones69.crazycrates.api.enums.crates.CrateStatus; +import com.badbones69.crazycrates.api.events.PlayerPrizeEvent; +import com.badbones69.crazycrates.api.events.crates.CrateStatusEvent; +import com.badbones69.crazycrates.api.objects.Crate; +import com.badbones69.crazycrates.api.objects.Prize; +import com.badbones69.crazycrates.api.utils.MiscUtils; +import com.badbones69.crazycrates.tasks.crates.CrateManager; +import com.ryderbelserion.vital.paper.util.scheduler.FoliaRunnable; +import net.kyori.adventure.sound.Sound; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.plugin.java.JavaPlugin; +import us.crazycrew.crazycrates.api.enums.types.KeyType; +import us.crazycrew.crazycrates.api.users.UserManager; + +public class CrateStatusListener implements Listener { + + private final CrazyCrates plugin = JavaPlugin.getPlugin(CrazyCrates.class); + + private final CrateManager crateManager = this.plugin.getCrateManager(); + + private final UserManager userManager = this.plugin.getUserManager(); + + @EventHandler + public void onPlayerPrize(PlayerPrizeEvent event) { + final Player player = event.getPlayer(); + final Prize prize = event.getPrize(); + final Crate crate = event.getCrate(); + + final CrateStatusEvent status = event.getEvent(); + + if (prize == null) { + if (status != null) { + status.setStatus(CrateStatus.failed).callEvent(); + } + + if (MiscUtils.isLogging()) { + this.plugin.getComponentLogger().warn("Prize was not found, thus the crate failed! A refund has been given to {} for the crate {}", player.getName(), crate.getFileName()); + } + + event.setCancelled(true); + + return; + } + + PrizeManager.givePrize(player, prize, crate); + + if (prize.useFireworks()) { + MiscUtils.spawnFirework(player.getLocation().add(0, 1, 0), null); + } + + if (status != null) { + status.setStatus(CrateStatus.ended).callEvent(); + } + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onCrateStatus(CrateStatusEvent event) { + final CrateStatus status = event.getStatus(); + final Player player = event.getPlayer(); + final Crate crate = event.getCrate(); + + final Location location = player.getLocation(); + + switch (status) { + case ended -> janitor(crate, player, location, event); + + case cycling -> crate.playSound(player, location, "cycle-sound", "block.note_block.xylophone", Sound.Source.PLAYER); + + case opened -> { + + } + + case failed -> { + janitor(crate, player, location, event); + + // add the key back, since the crate failed. + this.userManager.addKeys(player.getUniqueId(), crate.getFileName(), KeyType.virtual_key, 1); + + // inform the player, they have been given a refund. + Messages.key_refund.sendMessage(player, "{crate}", crate.getCrateName()); + } + + case silent -> { + + } + + default -> {} + } + } + + /** + * Sends common tasks, for ending a crate in the cycle. + * + * @param crate {@link Crate} + * @param player {@link Player} + * @param location {@link Location} + * @param event {@link CrateStatusEvent} + */ + private void janitor(final Crate crate, final Player player, final Location location, final CrateStatusEvent event) { + crate.playSound(player, location, "stop-sound", "entity.player.levelup", Sound.Source.PLAYER); + + switch (crate.getCrateType()) { + case quad_crate -> this.crateManager.endQuadCrate(player); + + case quick_crate -> this.crateManager.endQuickCrate(player, location, crate, false); + + default -> this.crateManager.endCrate(player); + } + + // Always remove thy player from thy opening list! + this.crateManager.removePlayerFromOpeningList(player); + + // Always close the inventory, after 40 ticks. + new FoliaRunnable(player.getScheduler(), null) { + @Override + public void run() { + player.closeInventory(InventoryCloseEvent.Reason.UNLOADED); + } + }.runDelayed(plugin, 100); // 5 seconds, to take screenshots. + } +} \ No newline at end of file diff --git a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/CrateManager.java b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/CrateManager.java index 6810787f3..e85e2efb6 100644 --- a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/CrateManager.java +++ b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/CrateManager.java @@ -2,7 +2,7 @@ import ch.jalu.configme.SettingsManager; import com.Zrips.CMI.Modules.ModuleHandling.CMIModule; -import com.badbones69.crazycrates.api.builders.CrateBuilder; +import com.badbones69.crazycrates.api.builders.v2.CrateBuilder; import com.badbones69.crazycrates.api.crates.CrateHologram; import com.badbones69.crazycrates.api.crates.quadcrates.CrateSchematic; import com.badbones69.crazycrates.api.enums.misc.Files; @@ -23,7 +23,7 @@ import com.badbones69.crazycrates.tasks.crates.types.RouletteCrate; import com.badbones69.crazycrates.tasks.crates.types.WarCrate; import com.badbones69.crazycrates.tasks.crates.types.WheelCrate; -import com.badbones69.crazycrates.tasks.crates.types.WonderCrate; +import com.badbones69.crazycrates.tasks.crates.types.v2.WonderCrate; import com.ryderbelserion.vital.paper.api.files.CustomFile; import com.ryderbelserion.vital.paper.api.files.FileManager; import com.ryderbelserion.vital.common.utils.FileUtil; @@ -588,8 +588,8 @@ public void openCrate(@NotNull final Player player, @NotNull final Crate crate, CrateBuilder crateBuilder; switch (crate.getCrateType()) { - case csgo -> crateBuilder = new CsgoCrate(crate, player, 27); - case casino -> crateBuilder = new CasinoCrate(crate, player, 27); + case csgo -> crateBuilder = new CsgoCrate(player, crate); + /*case casino -> crateBuilder = new CasinoCrate(crate, player, 27); case wonder -> crateBuilder = new WonderCrate(crate, player, 45); case wheel -> crateBuilder = new WheelCrate(crate, player, 54); case roulette -> crateBuilder = new RouletteCrate(crate, player, 27); @@ -677,10 +677,10 @@ public void openCrate(@NotNull final Player player, @NotNull final Crate crate, } crateBuilder = new QuickCrate(crate, player, location); - } + }*/ default -> { - crateBuilder = new CsgoCrate(crate, player, 27); + crateBuilder = new CsgoCrate(player, crate); if (MiscUtils.isLogging()) { List.of( @@ -1495,4 +1495,4 @@ public final boolean containsSlot(final Player player) { public void removeSlot(final Player player) { this.slots.remove(player.getUniqueId()); } -} \ No newline at end of file +} diff --git a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/CasinoCrate.java b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/CasinoCrate.java index 708793019..b32da5ec3 100644 --- a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/CasinoCrate.java +++ b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/CasinoCrate.java @@ -7,8 +7,6 @@ import com.badbones69.crazycrates.api.utils.MiscUtils; import com.badbones69.crazycrates.api.PrizeManager; import com.ryderbelserion.vital.paper.util.scheduler.FoliaRunnable; -import com.badbones69.crazycrates.tasks.BukkitUserManager; -import com.badbones69.crazycrates.tasks.crates.CrateManager; import io.papermc.paper.persistence.PersistentDataContainerView; import net.kyori.adventure.sound.Sound; import net.kyori.adventure.text.logger.slf4j.ComponentLogger; @@ -23,10 +21,6 @@ public class CasinoCrate extends CrateBuilder { - private @NotNull final CrateManager crateManager = this.plugin.getCrateManager(); - - private @NotNull final BukkitUserManager userManager = this.plugin.getUserManager(); - public CasinoCrate(@NotNull final Crate crate, @NotNull final Player player, final int size) { super(crate, player, size); } diff --git a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/CosmicCrate.java b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/CosmicCrate.java index 196615fb2..f07b8b67e 100644 --- a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/CosmicCrate.java +++ b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/CosmicCrate.java @@ -2,7 +2,6 @@ import com.badbones69.crazycrates.api.PrizeManager; import com.badbones69.crazycrates.api.objects.Tier; -import com.badbones69.crazycrates.tasks.crates.CrateManager; import com.badbones69.crazycrates.tasks.crates.other.CosmicCrateManager; import com.badbones69.crazycrates.api.objects.Crate; import com.badbones69.crazycrates.api.builders.ItemBuilder; @@ -13,8 +12,6 @@ public class CosmicCrate extends CrateBuilder { - private @NotNull final CrateManager crateManager = this.plugin.getCrateManager(); - public CosmicCrate(@NotNull final Crate crate, @NotNull final Player player, final int size) { super(crate, player, size, crate.getCrateName() + " - Choose"); } diff --git a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/CrateOnTheGo.java b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/CrateOnTheGo.java index c7a210f39..8fc9bfe55 100644 --- a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/CrateOnTheGo.java +++ b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/CrateOnTheGo.java @@ -3,25 +3,15 @@ import com.badbones69.crazycrates.api.objects.Crate; import com.badbones69.crazycrates.api.objects.Prize; import com.badbones69.crazycrates.api.PrizeManager; -import com.badbones69.crazycrates.tasks.BukkitUserManager; import org.bukkit.entity.Player; -import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; import us.crazycrew.crazycrates.api.enums.types.KeyType; -import com.badbones69.crazycrates.CrazyCrates; import com.badbones69.crazycrates.api.builders.CrateBuilder; -import com.badbones69.crazycrates.tasks.crates.CrateManager; import com.badbones69.crazycrates.api.utils.MiscUtils; import java.util.UUID; public class CrateOnTheGo extends CrateBuilder { - private @NotNull final CrazyCrates plugin = CrazyCrates.getPlugin(); - - private @NotNull final CrateManager crateManager = this.plugin.getCrateManager(); - - private @NotNull final BukkitUserManager userManager = this.plugin.getUserManager(); - public CrateOnTheGo(@NotNull final Crate crate, @NotNull final Player player) { super(crate, player); } diff --git a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/CsgoCrate.java b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/CsgoCrate.java index e0861bd4a..2ea2e1115 100644 --- a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/CsgoCrate.java +++ b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/CsgoCrate.java @@ -1,162 +1,152 @@ package com.badbones69.crazycrates.tasks.crates.types; +import com.badbones69.crazycrates.api.builders.v2.CrateBuilder; +import com.badbones69.crazycrates.api.enums.crates.CrateStatus; +import com.badbones69.crazycrates.api.events.crates.CrateStatusEvent; import com.badbones69.crazycrates.api.objects.Crate; -import com.badbones69.crazycrates.api.objects.Prize; -import com.badbones69.crazycrates.api.PrizeManager; +import com.ryderbelserion.vital.paper.api.builders.gui.interfaces.Gui; +import com.ryderbelserion.vital.paper.api.builders.gui.interfaces.GuiItem; import com.ryderbelserion.vital.paper.util.scheduler.FoliaRunnable; -import com.badbones69.crazycrates.tasks.BukkitUserManager; -import com.badbones69.crazycrates.tasks.crates.CrateManager; -import net.kyori.adventure.sound.Sound; -import org.bukkit.Material; import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; import us.crazycrew.crazycrates.api.enums.types.KeyType; -import com.badbones69.crazycrates.api.builders.CrateBuilder; -import com.badbones69.crazycrates.api.utils.MiscUtils; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.UUID; public class CsgoCrate extends CrateBuilder { - private @NotNull final CrateManager crateManager = this.plugin.getCrateManager(); + public CsgoCrate(final Player player, final Crate crate) { + super(player, crate, 3); - private @NotNull final BukkitUserManager userManager = this.plugin.getUserManager(); - - public CsgoCrate(@NotNull final Crate crate, @NotNull final Player player, final int size) { - super(crate, player, size); + setSeconds(6000); } - @Override - public void open(@NotNull final KeyType type, final boolean checkHand) { - // Crate event failed so we return. - if (isCrateEventValid(type, checkHand)) { - return; - } + private final CrateStatusEvent event = getEvent(); + private final Player player = getPlayer(); + private final Crate crate = getCrate(); + private final Gui gui = getGui(); - final Player player = getPlayer(); - final UUID uuid = player.getUniqueId(); - final Crate crate = getCrate(); - final String fileName = crate.getFileName(); + private final long startTime = System.currentTimeMillis(); // 5.4 seconds - final boolean keyCheck = this.userManager.takeKeys(uuid, fileName, type, crate.useRequiredKeys() ? crate.getRequiredKeys() : 1, checkHand); + private int inventoryStatus = 0; + private int delayCounter = 0; + + @Override + public void run() { + final long elapsedTime = System.currentTimeMillis() - this.startTime; - if (!keyCheck) { - // Remove from opening list. - this.crateManager.removePlayerFromOpeningList(player); + if (elapsedTime >= getSeconds()) { // elapsed the allowed time. + this.event.setStatus(CrateStatus.ended).callEvent(); + + cancel(); return; } - // Set the glass/display items to the inventory. - populate(); + if (elapsedTime <= 5000) { + this.event.setStatus(CrateStatus.cycling).callEvent(); - // Open the inventory. - player.openInventory(getInventory()); + moveItemsAndSetGlass(); + } - addCrateTask(new FoliaRunnable(player.getScheduler(), null) { - int time = 1; + this.inventoryStatus++; - int full = 0; + if (this.inventoryStatus >= 5) { + this.player.openInventory(this.gui.getInventory()); - int open = 0; + this.inventoryStatus = 0; + } - @Override - public void run() { - if (this.full <= 50) { // When Spinning - moveItemsAndSetGlass(); + if (elapsedTime > 5100) { // slowing down + this.delayCounter = this.delayCounter + 10; - playSound("cycle-sound", Sound.Source.PLAYER, "block.note_block.xylophone"); - } + this.event.setStatus(CrateStatus.silent).callEvent(); - this.open++; + new FoliaRunnable(this.player.getScheduler(), null) { + @Override + public void run() { + moveItemsAndSetGlass(); - if (this.open >= 5) { - player.openInventory(getInventory()); + event.setStatus(CrateStatus.cycling).callEvent(); - this.open = 0; + cancel(); } + }.runAtFixedRate(this.plugin, this.delayCounter, 1); + } - this.full++; - - if (this.full > 51) { - if (MiscUtils.slowSpin(120, 15).contains(this.time)) { // When Slowing Down - moveItemsAndSetGlass(); + /* + this.full++; - playSound("cycle-sound", Sound.Source.PLAYER, "block.note_block.xylophone"); - } + if (this.full > 51) { // slowing down + if (MiscUtils.slowSpin(120, 15).contains(this.time)) { // cycling + moveItemsAndSetGlass(); - this.time++; + this.event.setStatus(CrateStatus.cycling).callEvent(); + } - if (this.time == 60) { // When done - playSound("stop-sound", Sound.Source.PLAYER, "entity.player.levelup"); + this.time++; - crateManager.endCrate(player); + if (this.time >= 60) { // finished + final ItemStack itemStack = new ItemStack(Material.GRAY_STAINED_GLASS); - final ItemStack itemStack = new ItemStack(Material.GRAY_STAINED_GLASS); + setItem(4, itemStack); + setItem(22, itemStack); - setItem(4, itemStack); - setItem(22, itemStack); + final ItemStack item = getInventory().getItem(13); - final ItemStack item = getInventory().getItem(13); + Prize prize = null; - if (item != null) { - final Prize prize = crate.getPrize(item); + if (item != null) { + prize = crate.getPrize(item); + } - PrizeManager.givePrize(player, crate, prize); - } + final PlayerPrizeEvent playerPrizeEvent = new PlayerPrizeEvent(player, this.event, crate, prize); - crateManager.removePlayerFromOpeningList(player); + playerPrizeEvent.callEvent(); - cancel(); + if (playerPrizeEvent.isCancelled()) { + cancel(); - new FoliaRunnable(player.getScheduler(), null) { - @Override - public void run() { - if (player.getOpenInventory().getTopInventory().equals(getInventory())) player.closeInventory(); - } - }.runDelayed(plugin, 40); - } else if (this.time > 60) { // Added this due reports of the prizes spamming when low tps. - cancel(); - } + return; } + + cancel(); } - }.runAtFixedRate(this.plugin, 0, 1)); + }*/ } - private void populate() { - getBorder().forEach(this::setCustomGlassPane); + @Override + public void open(@NotNull final KeyType type, final boolean inspectInventory) { + getBorder().forEach(this::setRandomGlass); - // Set display items. for (int index = 9; index > 8 && index < 18; index++) { - setItem(index, getCrate().pickPrize(getPlayer()).getDisplayItem(getPlayer(), getCrate())); + this.gui.setItem(index, new GuiItem(this.crate.pickPrize(this.player).getDisplayItem(this.player, this.crate))); } + + this.gui.open(this.player); + + runAtFixedRate(this.plugin, 0, 1); } private void moveItemsAndSetGlass() { final List items = new ArrayList<>(); - final Player player = getPlayer(); - final Crate crate = getCrate(); + final Inventory inventory = this.gui.getInventory(); - for (int i = 9; i > 8 && i < 17; i++) { - items.add(getInventory().getItem(i)); + for (int count = 9; count > 8 && count < 17; count++) { + items.add(inventory.getItem(count)); } - setItem(9, crate.pickPrize(player).getDisplayItem(player, crate)); + this.gui.setItem(9, new GuiItem(this.crate.pickPrize(this.player).getDisplayItem(this.player, this.crate))); - for (int i = 0; i < 8; i++) { - setItem(i + 10, items.get(i)); + for (int count = 0; count < 8; count++) { + this.gui.setItem(count, new GuiItem(items.get(count))); } - getBorder().forEach(this::setCustomGlassPane); - } - - @Override - public void run() { - + getBorder().forEach(this::setRandomGlass); } private List getBorder() { diff --git a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/FireCrackerCrate.java b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/FireCrackerCrate.java index dbc9be8d7..dbe3039ed 100644 --- a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/FireCrackerCrate.java +++ b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/FireCrackerCrate.java @@ -4,8 +4,6 @@ import com.badbones69.crazycrates.api.objects.other.CrateLocation; import com.badbones69.crazycrates.support.holograms.HologramManager; import com.ryderbelserion.vital.paper.util.scheduler.FoliaRunnable; -import com.badbones69.crazycrates.tasks.BukkitUserManager; -import com.badbones69.crazycrates.tasks.crates.CrateManager; import org.bukkit.Color; import org.bukkit.Location; import org.bukkit.entity.Player; @@ -20,10 +18,6 @@ public class FireCrackerCrate extends CrateBuilder { - private @NotNull final CrateManager crateManager = this.plugin.getCrateManager(); - - private @NotNull final BukkitUserManager userManager = this.plugin.getUserManager(); - public FireCrackerCrate(@NotNull final Crate crate, @NotNull final Player player, final int size, @NotNull final Location location) { super(crate, player, size, location); } diff --git a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/QuadCrate.java b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/QuadCrate.java index 9865227c0..5ddae8beb 100644 --- a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/QuadCrate.java +++ b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/QuadCrate.java @@ -9,19 +9,13 @@ import org.jetbrains.annotations.NotNull; import org.bukkit.configuration.file.YamlConfiguration; import us.crazycrew.crazycrates.api.enums.types.KeyType; -import com.badbones69.crazycrates.CrazyCrates; import com.badbones69.crazycrates.api.builders.CrateBuilder; -import com.badbones69.crazycrates.tasks.crates.CrateManager; import com.badbones69.crazycrates.tasks.crates.other.quadcrates.QuadCrateManager; import java.util.List; import java.util.concurrent.ThreadLocalRandom; public class QuadCrate extends CrateBuilder { - private @NotNull final CrazyCrates plugin = CrazyCrates.getPlugin(); - - private @NotNull final CrateManager crateManager = this.plugin.getCrateManager(); - private final Location location; public QuadCrate(@NotNull final Crate crate, @NotNull final Player player, @NotNull final Location location) { diff --git a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/QuickCrate.java b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/QuickCrate.java index 53965f3f3..819a178fb 100644 --- a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/QuickCrate.java +++ b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/QuickCrate.java @@ -10,8 +10,6 @@ import com.badbones69.crazycrates.support.holograms.HologramManager; import com.ryderbelserion.vital.paper.util.AdvUtil; import com.ryderbelserion.vital.paper.util.scheduler.FoliaRunnable; -import com.badbones69.crazycrates.tasks.BukkitUserManager; -import com.badbones69.crazycrates.tasks.crates.CrateManager; import org.bukkit.Location; import org.bukkit.entity.Item; import org.bukkit.entity.Player; @@ -31,10 +29,6 @@ public class QuickCrate extends CrateBuilder { - private @NotNull final CrateManager crateManager = this.plugin.getCrateManager(); - - private @NotNull final BukkitUserManager userManager = this.plugin.getUserManager(); - public QuickCrate(@NotNull final Crate crate, @NotNull final Player player, @NotNull final Location location) { super(crate, player, location); } diff --git a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/RouletteCrate.java b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/RouletteCrate.java index 44b7a0783..7e7f12d81 100644 --- a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/RouletteCrate.java +++ b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/RouletteCrate.java @@ -4,8 +4,6 @@ import com.badbones69.crazycrates.api.objects.Prize; import com.badbones69.crazycrates.api.PrizeManager; import com.ryderbelserion.vital.paper.util.scheduler.FoliaRunnable; -import com.badbones69.crazycrates.tasks.BukkitUserManager; -import com.badbones69.crazycrates.tasks.crates.CrateManager; import net.kyori.adventure.sound.Sound; import org.bukkit.entity.Player; import org.bukkit.event.inventory.InventoryCloseEvent; @@ -18,10 +16,6 @@ public class RouletteCrate extends CrateBuilder { - private @NotNull final CrateManager crateManager = this.plugin.getCrateManager(); - - private @NotNull final BukkitUserManager userManager = this.plugin.getUserManager(); - public RouletteCrate(@NotNull final Crate crate, @NotNull final Player player, final int size) { super(crate, player, size); } diff --git a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/WarCrate.java b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/WarCrate.java index 5a37b7132..d6601dcac 100644 --- a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/WarCrate.java +++ b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/WarCrate.java @@ -3,8 +3,6 @@ import com.badbones69.crazycrates.api.objects.Crate; import com.badbones69.crazycrates.api.builders.ItemBuilder; import com.ryderbelserion.vital.paper.util.scheduler.FoliaRunnable; -import com.badbones69.crazycrates.tasks.BukkitUserManager; -import com.badbones69.crazycrates.tasks.crates.CrateManager; import net.kyori.adventure.sound.Sound; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -20,10 +18,6 @@ public class WarCrate extends CrateBuilder { - private @NotNull final CrateManager crateManager = this.plugin.getCrateManager(); - - private @NotNull final BukkitUserManager userManager = this.plugin.getUserManager(); - private final Map colorCodes = new HashMap<>(); public WarCrate(@NotNull final Crate crate, @NotNull final Player player, final int size) { diff --git a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/WheelCrate.java b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/WheelCrate.java index 4c38b3b71..21b84d220 100644 --- a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/WheelCrate.java +++ b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/WheelCrate.java @@ -4,8 +4,6 @@ import com.badbones69.crazycrates.api.objects.Prize; import com.badbones69.crazycrates.api.PrizeManager; import com.ryderbelserion.vital.paper.util.scheduler.FoliaRunnable; -import com.badbones69.crazycrates.tasks.BukkitUserManager; -import com.badbones69.crazycrates.tasks.crates.CrateManager; import net.kyori.adventure.sound.Sound; import org.bukkit.Material; import org.bukkit.entity.Player; @@ -23,10 +21,6 @@ public class WheelCrate extends CrateBuilder { - private @NotNull final CrateManager crateManager = this.plugin.getCrateManager(); - - private @NotNull final BukkitUserManager userManager = this.plugin.getUserManager(); - public WheelCrate(@NotNull final Crate crate, @NotNull final Player player, final int size) { super(crate, player, size); } diff --git a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/WonderCrate.java b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/WonderCrate.java deleted file mode 100644 index 219592f70..000000000 --- a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/WonderCrate.java +++ /dev/null @@ -1,144 +0,0 @@ -package com.badbones69.crazycrates.tasks.crates.types; - -import com.badbones69.crazycrates.api.events.PlayerPrizeEvent; -import com.badbones69.crazycrates.api.objects.Crate; -import com.badbones69.crazycrates.api.objects.Prize; -import com.badbones69.crazycrates.api.PrizeManager; -import com.badbones69.crazycrates.api.builders.ItemBuilder; -import com.ryderbelserion.vital.paper.util.scheduler.FoliaRunnable; -import com.badbones69.crazycrates.tasks.BukkitUserManager; -import com.badbones69.crazycrates.tasks.crates.CrateManager; -import net.kyori.adventure.sound.Sound; -import org.bukkit.Material; -import org.bukkit.entity.Player; -import org.bukkit.event.inventory.InventoryCloseEvent; -import org.bukkit.inventory.ItemStack; -import org.jetbrains.annotations.NotNull; -import us.crazycrew.crazycrates.api.enums.types.KeyType; -import com.badbones69.crazycrates.api.builders.CrateBuilder; -import com.badbones69.crazycrates.api.utils.MiscUtils; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; - -public class WonderCrate extends CrateBuilder { - - private @NotNull final CrateManager crateManager = this.plugin.getCrateManager(); - - private @NotNull final BukkitUserManager userManager = this.plugin.getUserManager(); - - public WonderCrate(@NotNull final Crate crate, @NotNull final Player player, final int size) { - super(crate, player, size); - } - - @Override - public void open(@NotNull final KeyType type, final boolean checkHand) { - // Crate event failed so we return. - if (isCrateEventValid(type, checkHand)) { - return; - } - - final Player player = getPlayer(); - final UUID uuid = player.getUniqueId(); - final Crate crate = getCrate(); - final String fileName = crate.getFileName(); - - final boolean keyCheck = this.userManager.takeKeys(uuid, fileName, type, crate.useRequiredKeys() ? crate.getRequiredKeys() : 1, checkHand); - - if (!keyCheck) { - // Remove from opening list. - this.crateManager.removePlayerFromOpeningList(getPlayer()); - - return; - } - - final List slots = new ArrayList<>(); - - for (int index = 0; index < getSize(); index++) { - final Prize prize = crate.pickPrize(player); - - slots.add(String.valueOf(index)); - - setItem(index, prize.getDisplayItem(player, crate)); - } - - player.openInventory(getInventory()); - - addCrateTask(new FoliaRunnable(player.getScheduler(), null) { - int time = 0; - int full = 0; - - int slot1 = 0; - int slot2 = 44; - - final List other = new ArrayList<>(); - - Prize prize = null; - - @Override - public void run() { - if (this.time >= 2 && this.full <= 65) { - slots.remove(this.slot1 + ""); - slots.remove(this.slot2 + ""); - - other.add(this.slot1); - other.add(this.slot2); - - final ItemStack material = new ItemBuilder(Material.BLACK_STAINED_GLASS_PANE).setDisplayName(" ").getStack(); - - setItem(this.slot1, material); - setItem(this.slot2, material); - - for (String slot : slots) { - this.prize = crate.pickPrize(player); - - setItem(Integer.parseInt(slot), this.prize.getDisplayItem(player, crate)); - } - - playSound("cycle-sound", Sound.Source.PLAYER, "block.note_block.xylophone"); - - this.slot1++; - this.slot2--; - } - - if (this.full > 67) { - for (int slot : this.other) { - setCustomGlassPane(slot); - } - - playSound("cycle-sound", Sound.Source.PLAYER, "block.note_block.xylophone"); - } - - player.openInventory(getInventory()); - - if (this.full > 100) { - crateManager.endCrate(player); - - getPlayer().closeInventory(InventoryCloseEvent.Reason.UNLOADED); - - PrizeManager.givePrize(player, this.prize, crate); - - playSound("stop-sound", Sound.Source.PLAYER, "entity.player.levelup"); - - if (this.prize.useFireworks()) MiscUtils.spawnFirework(getPlayer().getLocation().add(0, 1, 0), null); - - plugin.getServer().getPluginManager().callEvent(new PlayerPrizeEvent(player, crate, this.prize)); - - crateManager.removePlayerFromOpeningList(player); - - return; - } - - this.full++; - this.time++; - - if (this.time > 2) this.time = 0; - } - }.runAtFixedRate(this.plugin, 0, 2)); - } - - @Override - public void run() { - - } -} \ No newline at end of file diff --git a/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/v2/WonderCrate.java b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/v2/WonderCrate.java new file mode 100644 index 000000000..e991033d3 --- /dev/null +++ b/paper/src/main/java/com/badbones69/crazycrates/tasks/crates/types/v2/WonderCrate.java @@ -0,0 +1,133 @@ +package com.badbones69.crazycrates.tasks.crates.types.v2; + +import com.badbones69.crazycrates.api.enums.crates.CrateStatus; +import com.badbones69.crazycrates.api.events.PlayerPrizeEvent; +import com.badbones69.crazycrates.api.events.crates.CrateStatusEvent; +import com.badbones69.crazycrates.api.objects.Crate; +import com.badbones69.crazycrates.api.objects.Prize; +import com.badbones69.crazycrates.api.builders.ItemBuilder; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; +import us.crazycrew.crazycrates.api.enums.types.KeyType; +import com.badbones69.crazycrates.api.builders.CrateBuilder; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class WonderCrate extends CrateBuilder { + + private final CrateStatusEvent event; + + public WonderCrate(@NotNull final Crate crate, @NotNull final Player player, final int size) { + super(crate, player, size); + + this.event = new CrateStatusEvent(player, crate); + } + + private final List slots = new ArrayList<>(); + + private int time = 0; + private int full = 0; + + private int slot1 = 0; + private int slot2 = 44; + + @Override + public void run() { + final Player player = getPlayer(); + final Crate crate = getCrate(); + + final List other = new ArrayList<>(); + + Prize prize = null; + + if (this.time >= 2 && this.full <= 65) { + this.slots.remove(this.slot1); + this.slots.remove(this.slot2); + + other.add(this.slot1); + other.add(this.slot2); + + final ItemStack material = new ItemBuilder(Material.BLACK_STAINED_GLASS_PANE).setDisplayName(" ").getStack(); + + setItem(this.slot1, material); + setItem(this.slot2, material); + + for (int slot : this.slots) { + prize = crate.pickPrize(player); + + setItem(slot, prize.getDisplayItem(player, crate)); + } + + this.event.setStatus(CrateStatus.cycling).callEvent(); + + this.slot1++; + this.slot2--; + } + + if (this.full > 67) { + for (int slot : other) { + setCustomGlassPane(slot); + } + + this.event.setStatus(CrateStatus.cycling).callEvent(); + } + + player.openInventory(getInventory()); + + if (this.full > 100) { + final PlayerPrizeEvent playerPrizeEvent = new PlayerPrizeEvent(player, this.event, crate, prize); + + playerPrizeEvent.callEvent(); + + if (playerPrizeEvent.isCancelled()) { + cancel(); + + return; + } + + cancel(); + + return; + } + + this.full++; + this.time++; + + if (this.time > 2) this.time = 0; + } + + @Override + public void open(@NotNull final KeyType type, final boolean checkHand) { + // Crate event failed so we return. + if (isCrateEventValid(type, checkHand)) { + return; + } + + final Player player = getPlayer(); + final UUID uuid = player.getUniqueId(); + final Crate crate = getCrate(); + final String fileName = crate.getFileName(); + + final boolean keyCheck = this.userManager.takeKeys(uuid, fileName, type, crate.useRequiredKeys() ? crate.getRequiredKeys() : 1, checkHand); + + if (!keyCheck) { + this.crateManager.removePlayerFromOpeningList(getPlayer()); + + return; + } + + for (int index = 0; index < getSize(); index++) { + this.slots.add(index); + + setItem(index, crate.pickPrize(player).getDisplayItem(player, crate)); + } + + player.openInventory(getInventory()); + + // run the task, which uses the run method above. + runAtFixedRate(this.plugin, 0, 2); + } +} \ No newline at end of file