Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch internal logic for handling prizes, or status of crates to be more event based. #785

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -106,6 +107,9 @@ public void onEnable() {
new CrateMainMenu(),
new CrateTierMenu(),

// Crate Monitoring
new CrateStatusListener(),

// Other listeners.
new BrokeLocationsListener(),
new CrateControlListener(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading