Overhauled kit claiming logic.

This commit is contained in:
Brianna O'Keefe 2024-03-11 17:35:40 -05:00
parent a7665044cb
commit e95baae80a
11 changed files with 391 additions and 310 deletions

View File

@ -34,12 +34,7 @@ import com.craftaro.ultimatekits.handlers.DisplayItemHandler;
import com.craftaro.ultimatekits.handlers.ParticleHandler;
import com.craftaro.ultimatekits.key.Key;
import com.craftaro.ultimatekits.key.KeyManager;
import com.craftaro.ultimatekits.kit.Kit;
import com.craftaro.ultimatekits.kit.KitAnimation;
import com.craftaro.ultimatekits.kit.KitBlockData;
import com.craftaro.ultimatekits.kit.KitItem;
import com.craftaro.ultimatekits.kit.KitManager;
import com.craftaro.ultimatekits.kit.KitType;
import com.craftaro.ultimatekits.kit.*;
import com.craftaro.ultimatekits.listeners.BlockListeners;
import com.craftaro.ultimatekits.listeners.ChatListeners;
import com.craftaro.ultimatekits.listeners.ChunkListeners;
@ -82,6 +77,8 @@ public class UltimateKits extends SongodaPlugin {
private CategoryManager categoryManager;
private DataManager dataManager;
private KitHandler kitHandler;
private boolean loaded = false;
/**
@ -133,9 +130,11 @@ public class UltimateKits extends SongodaPlugin {
this.keyFile.saveChanges();
this.crateFile.saveChanges();
kitHandler = new KitHandler(this);
// setup commands
this.commandManager = new CommandManager(this);
this.commandManager.addCommand(new CommandKit(this, this.guiManager));
this.commandManager.addCommand(new CommandKit(this, this.guiManager, this.kitHandler));
this.commandManager.addCommand(new CommandPreviewKit(this, this.guiManager));
this.commandManager.addMainCommand("KitAdmin")
.addSubCommand(new CommandReload(this))
@ -151,17 +150,17 @@ public class UltimateKits extends SongodaPlugin {
// Event registration
this.guiManager.init();
guiManager.init();
PluginManager pluginManager = getServer().getPluginManager();
pluginManager.registerEvents(new BlockListeners(this), this);
pluginManager.registerEvents(new ChunkListeners(this), this);
pluginManager.registerEvents(new ChatListeners(), this);
pluginManager.registerEvents(new EntityListeners(this), this);
pluginManager.registerEvents(new InteractListeners(this, this.guiManager), this);
pluginManager.registerEvents(new InteractListeners(this, guiManager, kitHandler), this);
pluginManager.registerEvents(new PlayerListeners(this), this);
this.displayItemHandler.start();
this.particleHandler.start();
displayItemHandler.start();
particleHandler.start();
}
@Override
@ -587,4 +586,8 @@ public class UltimateKits extends SongodaPlugin {
public CategoryManager getCategoryManager() {
return this.categoryManager;
}
public KitHandler getKitHandler() {
return kitHandler;
}
}

View File

@ -6,6 +6,7 @@ import com.craftaro.ultimatekits.UltimateKits;
import com.craftaro.ultimatekits.gui.CategorySelectorGui;
import com.craftaro.ultimatekits.gui.KitSelectorGui;
import com.craftaro.ultimatekits.kit.Kit;
import com.craftaro.ultimatekits.kit.KitHandler;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -16,11 +17,13 @@ import java.util.List;
public class CommandKit extends AbstractCommand {
private final UltimateKits plugin;
private final GuiManager guiManager;
private final KitHandler kitHandler;
public CommandKit(UltimateKits plugin, GuiManager guiManager) {
public CommandKit(UltimateKits plugin, GuiManager guiManager, KitHandler kitHandler) {
super(CommandType.CONSOLE_OK, "kit");
this.plugin = plugin;
this.guiManager = guiManager;
this.kitHandler = kitHandler;
}
@Override
@ -57,7 +60,7 @@ public class CommandKit extends AbstractCommand {
return ReturnType.FAILURE;
}
kit.processGenericUse((Player) sender, false);
kitHandler.processGenericUse(kit, (Player) sender, false);
return ReturnType.SUCCESS;
} else {
// /kit <kit> <player> - Gives kit to another player.
@ -76,14 +79,14 @@ public class CommandKit extends AbstractCommand {
String who = player != null ? player.getName() : "everyone";
if (player != null) {
kit.processGenericUse(player, true);
this.plugin.getLocale().getMessage("event.claim.givesuccess")
kitHandler.processGenericUse(kit, player, true);
plugin.getLocale().getMessage("event.claim.givesuccess")
.processPlaceholder("kit", kit.getName())
.sendPrefixedMessage(sender);
} else {
Bukkit.getOnlinePlayers().forEach(onlinePlayer -> {
kit.processGenericUse(onlinePlayer, true);
this.plugin.getLocale().getMessage("event.claim.givesuccess")
kitHandler.processGenericUse(kit, onlinePlayer, true);
plugin.getLocale().getMessage("event.claim.givesuccess")
.processPlaceholder("kit", kit.getName())
.sendPrefixedMessage(sender);
});

View File

@ -24,29 +24,28 @@ public class CommandPreviewKit extends AbstractCommand {
protected ReturnType runCommand(CommandSender sender, String... args) {
Player player = (Player) sender;
if (args.length != 1) {
this.plugin.getLocale().getMessage("command.kit.nokitsupplied").sendPrefixedMessage(player);
plugin.getLocale().getMessage("command.kit.nokitsupplied").sendPrefixedMessage(player);
return ReturnType.FAILURE;
}
Kit kit = this.plugin.getKitManager().getKit(args[0].toLowerCase().trim());
if (kit == null) {
this.plugin.getLocale().getMessage("command.kit.kitdoesntexist").sendPrefixedMessage(player);
plugin.getLocale().getMessage("command.kit.kitdoesntexist").sendPrefixedMessage(player);
return ReturnType.FAILURE;
}
kit.display(player, this.guiManager, null);
plugin.getKitHandler().display(kit, player, guiManager, null);
return ReturnType.SUCCESS;
}
@Override
protected List<String> onTab(CommandSender sender, String... args) {
if (!(sender instanceof Player)) {
if (!(sender instanceof Player))
return null;
}
if (args.length == 2) {
List<String> tab = new ArrayList<>();
for (Kit kit : this.plugin.getKitManager().getKits()) {
for (Kit kit : plugin.getKitManager().getKits())
tab.add(kit.getKey());
}
return tab;
}
return new ArrayList<>();

View File

@ -15,12 +15,7 @@ import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.*;
public class AnimatedKitGui extends Gui {
static final Random rand = new Random();
@ -46,7 +41,7 @@ public class AnimatedKitGui extends Gui {
// ideally, we'd populate the items in such a way that the end item isn't far from the center when the animation is complete
// would be something to do if people have large kit loot tables.
List<KitItem> kitItems = kit.getContents();
List<KitItem> kitItems = new ArrayList<>(kit.getContents());
if (kitItems.isEmpty()) {
throw new RuntimeException("Cannot give an empty kit!");
}

View File

@ -38,7 +38,7 @@ public class ConfirmBuyGui extends Gui {
setButton(1, 2, GuiUtils.createButtonItem(Settings.BUY_ICON.getMaterial(XMaterial.EMERALD),
plugin.getLocale().getMessage("interface.yesno.yes").getMessage()),
event -> {
kit.processPurchaseUse(event.player);
plugin.getKitHandler().processPurchaseUse(kit, event.player);
exit();
});

View File

@ -165,9 +165,9 @@ public class KitSelectorGui extends Gui {
this.plugin.saveKits(false);
showPage();
} else if (event.clickType == ClickType.LEFT) {
kit.display(this.player, this.guiManager, this);
plugin.getKitHandler().display(kit, player, guiManager, this);
} else if (event.clickType == ClickType.RIGHT) {
kit.buy(event.player, event.manager);
plugin.getKitHandler().buy(kit, event.player, event.manager);
}
});
}

View File

@ -102,7 +102,7 @@ public class PreviewKitGui extends Gui {
getBuyLore()),
event -> {
exit();
kit.buy(event.player, event.manager);
plugin.getKitHandler().buy(kit, event.player, event.manager);
});
}

View File

@ -60,40 +60,13 @@ public class Kit implements Cloneable {
this.name = TextUtils.formatText(key, true);
}
public void buy(Player player, GuiManager manager) {
if (hasPermissionToClaim(player)) {
processGenericUse(player, false);
return;
}
if (!hasPermissionToBuy(player)) {
UltimateKits.getInstance().getLocale().getMessage("command.general.noperms")
.sendPrefixedMessage(player);
return;
}
if (this.link != null) {
player.sendMessage("");
plugin.getLocale().newMessage("&a" + this.link).sendPrefixedMessage(player);
player.sendMessage("");
player.closeInventory();
} else if (this.price != 0) {
manager.showGUI(player, new ConfirmBuyGui(plugin, player, this, null));
} else {
UltimateKits.getInstance().getLocale().getMessage("command.general.noperms")
.sendPrefixedMessage(player);
}
}
private boolean hasRoom(Player player, int itemAmount) {
public boolean hasRoom(Player player, int itemAmount) {
int space = 0;
for (ItemStack content : player.getInventory().getContents()) {
if (content == null) {
for (ItemStack content : player.getInventory().getContents())
if (content == null)
space++;
}
}
// Since roulette only gives one item, we don't need to check if the user has room for the whole kit.
if (this.kitAnimation == KitAnimation.ROULETTE && space >= 1) {
@ -103,125 +76,6 @@ public class Kit implements Cloneable {
return space >= itemAmount;
}
public void processKeyUse(Player player) {
ItemStack item = player.getItemInHand();
NBTItem nbtItem = new NBTItem(item);
if (!nbtItem.hasKey("key") || !nbtItem.hasKey("kit")) {
return;
}
String keyName = nbtItem.getString("key");
String kitName = nbtItem.getString("kit");
boolean any = kitName.equals("ANY");
Key key = plugin.getKeyManager().getKey(keyName);
if (key == null && !any) {
return;
}
if (!any && !kitName.equals(this.name)) {
plugin.getLocale().getMessage("event.crate.wrongkey").sendPrefixedMessage(player);
return;
}
if (giveKit(player, key)) {
plugin.getLocale().getMessage("event.key.success")
.processPlaceholder("kit", this.name).sendPrefixedMessage(player);
if (player.getInventory().getItemInHand().getAmount() != 1) {
item.setAmount(item.getAmount() - 1);
player.setItemInHand(item);
} else {
player.setItemInHand(null);
}
}
}
public void processCrateUse(Player player, ItemStack item, CompatibleHand hand) {
Crate crate = plugin.getCrateManager().getCrate(item);
if (crate == null || !giveKit(player, crate)) {
return;
}
ItemUtils.takeActiveItem(player, hand);
plugin.getLocale().getMessage("event.crate.success")
.processPlaceholder("crate", this.name).sendPrefixedMessage(player);
}
public void processPurchaseUse(Player player) {
if (!EconomyManager.isEnabled()) {
return;
}
if (!player.hasPermission("ultimatekits.buy." + this.key)) {
UltimateKits.getInstance().getLocale().getMessage("command.general.noperms")
.sendPrefixedMessage(player);
return;
} else if (!EconomyManager.hasBalance(player, this.price)) {
plugin.getLocale().getMessage("event.claim.cannotafford")
.processPlaceholder("kit", this.name).sendPrefixedMessage(player);
return;
}
if (this.delay > 0) {
if (getNextUse(player) != 0) {
plugin.getLocale().getMessage("event.claim.delay")
.processPlaceholder("time", TimeUtils.makeReadable(this.getNextUse(player)))
.sendPrefixedMessage(player);
return;
}
} else if (getNextUse(player) == -1) {
plugin.getLocale().getMessage("event.claim.nottwice").sendPrefixedMessage(player);
return;
}
if (giveKit(player)) {
EconomyManager.withdrawBalance(player, this.price);
if (this.delay != 0) {
updateDelay(player); //updates delay on buy
}
plugin.getLocale().getMessage("event.claim.purchasesuccess")
.processPlaceholder("kit", this.name).sendPrefixedMessage(player);
}
}
public void processGenericUse(Player player, boolean forced) {
if (getNextUse(player) == -1 && !forced) {
plugin.getLocale().getMessage("event.claim.nottwice").sendPrefixedMessage(player);
} else if (getNextUse(player) <= 0 || forced) {
if (giveKit(player)) {
updateDelay(player);
if (this.kitAnimation == KitAnimation.NONE) {
plugin.getLocale().getMessage("event.claim.givesuccess")
.processPlaceholder("kit", this.name).sendPrefixedMessage(player);
}
}
} else {
plugin.getLocale().getMessage("event.claim.delay")
.processPlaceholder("time", TimeUtils.makeReadable(getNextUse(player)))
.sendPrefixedMessage(player);
}
}
@SuppressWarnings("Duplicates")
public void display(Player player, GuiManager manager, Gui back) {
if (!hasPermissionToPreview(player)) {
UltimateKits.getInstance().getLocale().getMessage("command.general.noperms")
.sendPrefixedMessage(player);
return;
}
if (this.key == null) {
plugin.getLocale().getMessage("command.kit.kitdoesntexist").sendPrefixedMessage(player);
return;
}
plugin.getLocale().getMessage("event.preview.kit")
.processPlaceholder("kit", this.name).sendPrefixedMessage(player);
manager.showGUI(player, new PreviewKitGui(plugin, player, this, back));
}
public void saveKit(List<ItemStack> items) {
List<KitItem> list = new ArrayList<>();
for (ItemStack is : items) {
@ -295,103 +149,6 @@ public class Kit implements Cloneable {
return stacks;
}
public boolean giveKit(Player player) {
return giveKit(player, this.contents.size(), -1);
}
private boolean giveKit(Player player, Key key) {
if (key == null) {
return giveKit(player);
}
int amount = key.getAmount();
if (amount == -1) {
// FIXME: I don't understand how Crates, Keys, etc. actually are supposed to work.
// I think the give-algorithms are generally wrongly implemented and confusing naming is making it hard to understand.
amount = this.contents.size();
}
return giveKit(player, amount, key.getKitAmount());
}
private boolean giveKit(Player player, Crate crate) {
int amount = crate.getAmount();
if (amount == -1) {
// FIXME: I don't understand how Crates, Keys, etc. actually are supposed to work.
// I think the give-algorithms are generally wrongly implemented and confusing naming is making it hard to understand.
amount = this.contents.size();
}
return giveKit(player, amount, crate.getKitAmount());
}
private boolean giveKit(Player player, int itemAmount, int kitAmount) {
List<KitItem> innerContents = new ArrayList<>(getContents());
// Amount of items from the kit to give to the player.
if (this.kitAnimation == KitAnimation.ROULETTE) {
itemAmount = 1; //TODO how about kitAmount > 1? generateRandomItem() will only give 1 random item instead of kitAmount
}
int itemGiveAmount = kitAmount > 0 ? itemAmount * kitAmount : itemAmount;
if (Settings.NO_REDEEM_WHEN_FULL.getBoolean() && !hasRoom(player, itemGiveAmount)) {
plugin.getLocale().getMessage("event.claim.full").sendPrefixedMessage(player);
return false;
}
if (Settings.SOUNDS_ENABLED.getBoolean() && this.kitAnimation == KitAnimation.NONE) {
XSound.ENTITY_PLAYER_LEVELUP.play(player, 0.6F, 15.0F);
}
return generateRandomItem(innerContents, itemGiveAmount, 0, player);
}
private boolean generateRandomItem(List<KitItem> innerContents, int itemGiveAmount, int itemGivenAmount, Player player) {
if (innerContents.size() != itemGiveAmount || this.kitAnimation != KitAnimation.NONE) {
Collections.shuffle(innerContents);
}
for (KitItem item : new ArrayList<>(innerContents)) {
if (itemGiveAmount <= 0 && itemGivenAmount != 0) {
break;
}
double ch = item.getChance() == 0 ? 100 : item.getChance();
double rand = Math.random() * 100;
itemGiveAmount--;
if (rand < ch || ch == 100) {
itemGivenAmount++;
if (this.kitAnimation != KitAnimation.NONE) {
// TODO: this is a very bad way to solve this problem.
// Giving the player kit rewards really should be done outside of the Kit class.
plugin.getGuiManager().showGUI(player, new AnimatedKitGui(plugin, player, this, item.getItem()));
return true;
} else {
ItemStack parseStack = item.getContent().process(player);
if (item.getContent() instanceof KitContentEconomy
|| item.getContent() instanceof KitContentCommand) {
continue;
}
innerContents.remove(item);
if (Settings.AUTO_EQUIP_ARMOR.getBoolean() && ArmorType.equip(player, parseStack)) {
continue;
}
Map<Integer, ItemStack> overfilled = player.getInventory().addItem(parseStack);
for (ItemStack item2 : overfilled.values()) {
player.getWorld().dropItemNaturally(player.getLocation(), item2);
}
}
}
}
if ((itemGiveAmount > 0 || itemGivenAmount == 0) && !innerContents.isEmpty()) {
return generateRandomItem(innerContents, itemGiveAmount, itemGivenAmount, player);
}
player.updateInventory();
return true;
}
public void updateDelay(Player player) {
plugin.getDataFile().set("Kits." + this.key + ".delays." + player.getUniqueId(), System.currentTimeMillis());
}
@ -470,7 +227,7 @@ public class Kit implements Cloneable {
}
public List<KitItem> getContents() {
return this.contents;
return Collections.unmodifiableList(this.contents);
}
public Kit setContents(List<KitItem> contents) {

View File

@ -0,0 +1,322 @@
package com.craftaro.ultimatekits.kit;
import com.craftaro.core.compatibility.CompatibleHand;
import com.craftaro.core.gui.Gui;
import com.craftaro.core.gui.GuiManager;
import com.craftaro.core.hooks.EconomyManager;
import com.craftaro.core.third_party.de.tr7zw.nbtapi.NBTItem;
import com.craftaro.core.utils.ItemUtils;
import com.craftaro.core.utils.TimeUtils;
import com.craftaro.third_party.com.cryptomorin.xseries.XSound;
import com.craftaro.ultimatekits.UltimateKits;
import com.craftaro.ultimatekits.crate.Crate;
import com.craftaro.ultimatekits.gui.AnimatedKitGui;
import com.craftaro.ultimatekits.gui.ConfirmBuyGui;
import com.craftaro.ultimatekits.gui.PreviewKitGui;
import com.craftaro.ultimatekits.key.Key;
import com.craftaro.ultimatekits.kit.type.KitContentCommand;
import com.craftaro.ultimatekits.kit.type.KitContentEconomy;
import com.craftaro.ultimatekits.settings.Settings;
import com.craftaro.ultimatekits.utils.ArmorType;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
public class KitHandler {
private final UltimateKits plugin;
public KitHandler(UltimateKits plugin) {
this.plugin = plugin;
}
/**
* Process the purchase of a kit.
*
* @param kit The kit to give to the player
* @param player The player to give the kit to
* @param manager The GUI manager
*/
public void buy(Kit kit, Player player, GuiManager manager) {
if (kit.hasPermissionToClaim(player)) {
processGenericUse(kit, player, false);
return;
}
if (!kit.hasPermissionToBuy(player)) {
UltimateKits.getInstance().getLocale().getMessage("command.general.noperms")
.sendPrefixedMessage(player);
return;
}
if (kit.getLink() != null) {
player.sendMessage("");
plugin.getLocale().newMessage("&a" + kit.getLink()).sendPrefixedMessage(player);
player.sendMessage("");
player.closeInventory();
} else if (kit.getPrice() != 0) {
manager.showGUI(player, new ConfirmBuyGui(plugin, player, kit, null));
} else {
UltimateKits.getInstance().getLocale().getMessage("command.general.noperms")
.sendPrefixedMessage(player);
}
}
/**
* Process the use of a key inventory item on a kit block.
*
* @param kit The kit to give to the player
* @param player The player to give the kit to
*/
public void processKeyUse(Kit kit, Player player) {
ItemStack item = player.getItemInHand();
NBTItem nbtItem = new NBTItem(item);
if (!nbtItem.hasKey("key") || !nbtItem.hasKey("kit")) {
return;
}
String keyName = nbtItem.getString("key");
String kitName = nbtItem.getString("kit");
boolean any = kitName.equals("ANY");
Key key = plugin.getKeyManager().getKey(keyName);
if (key == null && !any) {
return;
}
String name = kit.getName();
if (!any && !kitName.equals(name)) {
plugin.getLocale().getMessage("event.crate.wrongkey").sendPrefixedMessage(player);
return;
}
int amount = key == null ? -1 : key.getAmount();
if (amount == -1) {
amount = kit.getContents().size();
}
if (key == null ? giveKit(kit, player) : giveKit(kit, player, amount, key.getKitAmount())) {
plugin.getLocale().getMessage("event.key.success")
.processPlaceholder("kit", kit.getName()).sendPrefixedMessage(player);
if (player.getInventory().getItemInHand().getAmount() != 1) {
item.setAmount(item.getAmount() - 1);
player.setItemInHand(item);
} else {
player.setItemInHand(null);
}
}
}
/**
* Process the use of a crate inventory item.
*
* @param kit The kit to give to the player
* @param player The player to give the kit to
* @param item The item that was used
* @param hand The hand that the item was used from
*/
public void processCrateUse(Kit kit, Player player, ItemStack item, CompatibleHand hand) {
Crate crate = plugin.getCrateManager().getCrate(item);
if (crate == null || !giveKit(kit, player, crate.getKitAmount() != 0 ? crate.getKitAmount() : -1, crate.getKitAmount())) {
return;
}
ItemUtils.takeActiveItem(player, hand);
plugin.getLocale().getMessage("event.crate.success")
.processPlaceholder("crate", crate.getName()).sendPrefixedMessage(player);
}
/**
* Process the purchase of a kit.
*
* @param kit The kit to give to the player
* @param player The player to give the kit to
*/
public void processPurchaseUse(Kit kit, Player player) {
if (!EconomyManager.isEnabled()) {
return;
}
String name = kit.getName();
if (!player.hasPermission("ultimatekits.buy." + kit.getKey())) {
UltimateKits.getInstance().getLocale().getMessage("command.general.noperms")
.sendPrefixedMessage(player);
return;
}
if (!EconomyManager.hasBalance(player, kit.getPrice())) {
plugin.getLocale().getMessage("event.claim.cannotafford")
.processPlaceholder("kit", name).sendPrefixedMessage(player);
return;
}
if (kit.getDelay() > 0 && kit.getNextUse(player) != 0) {
plugin.getLocale().getMessage("event.claim.delay")
.processPlaceholder("time", TimeUtils.makeReadable(kit.getNextUse(player)))
.sendPrefixedMessage(player);
return;
}
if (kit.getNextUse(player) == -1) {
plugin.getLocale().getMessage("event.claim.nottwice").sendPrefixedMessage(player);
return;
}
if (giveKit(kit, player)) {
EconomyManager.withdrawBalance(player, kit.getPrice());
if (kit.getDelay() != 0) {
kit.updateDelay(player);
}
plugin.getLocale().getMessage("event.claim.purchasesuccess")
.processPlaceholder("kit", name).sendPrefixedMessage(player);
}
}
/**
* Process the use of a kit.
*
* @param kit The kit to give to the player
* @param player The player to give the kit to
* @param forced Whether the kit should be given regardless of the delay
*/
public void processGenericUse(Kit kit, Player player, boolean forced) {
if (kit.getNextUse(player) == -1 && !forced) {
plugin.getLocale().getMessage("event.claim.nottwice").sendPrefixedMessage(player);
} else if (kit.getNextUse(player) <= 0 || forced) {
if (giveKit(kit, player)) {
kit.updateDelay(player);
if (kit.getKitAnimation() == KitAnimation.NONE) {
plugin.getLocale().getMessage("event.claim.givesuccess")
.processPlaceholder("kit", kit.getName()).sendPrefixedMessage(player);
}
}
} else {
plugin.getLocale().getMessage("event.claim.delay")
.processPlaceholder("time", TimeUtils.makeReadable(kit.getNextUse(player)))
.sendPrefixedMessage(player);
}
}
/**
* Give a kit to a player.
*
* @param kit The kit to give to the player
* @param player The player to give the kit to
* @return True if the kit was given successfully, false otherwise
*/
public boolean giveKit(Kit kit, Player player) {
return giveKit(kit, player, kit.getContents().size(), -1);
}
/**
* Give a kit to a player with a specified item amount and kit amount.
*
* @param kit The kit to give to the player
* @param player The player to give the kit to
* @param itemAmount The amount of items to give from the kit
* @param kitAmount The amount of times to give the kit
* @return True if the kit was given successfully, false otherwise
*/
private boolean giveKit(Kit kit, Player player, int itemAmount, int kitAmount) {
List<KitItem> kitItems = new ArrayList<>(kit.getContents());
int itemGiveAmount = (kitAmount > 0) ? itemAmount * kitAmount : itemAmount;
if (kit.getKitAnimation() == KitAnimation.ROULETTE) {
itemGiveAmount = 1;
}
if (Settings.NO_REDEEM_WHEN_FULL.getBoolean() && !kit.hasRoom(player, itemGiveAmount)) {
plugin.getLocale().getMessage("event.claim.full").sendPrefixedMessage(player);
return false;
}
if (Settings.SOUNDS_ENABLED.getBoolean() && kit.getKitAnimation() == KitAnimation.NONE) {
XSound.ENTITY_PLAYER_LEVELUP.play(player, 0.6F, 15.0F);
}
return giveKitItems(kit, kitItems, itemGiveAmount, player);
}
/**
* Give kit items to a player.
*
* @param kit The kit to give items from
* @param kitItems The list of kit items to give
* @param itemGiveAmount The amount of items to give
* @param player The player to give the items to
* @return True if the items were given successfully, false otherwise
*/
private boolean giveKitItems(Kit kit, List<KitItem> kitItems, int itemGiveAmount, Player player) {
if (kitItems.size() != itemGiveAmount || kit.getKitAnimation() != KitAnimation.NONE) {
Collections.shuffle(kitItems);
}
for (KitItem item : kitItems) {
if (itemGiveAmount <= 0) {
break;
}
double chance = (item.getChance() == 0) ? 100 : item.getChance();
double random = Math.random() * 100;
itemGiveAmount--;
if (random < chance) {
if (kit.getKitAnimation() != KitAnimation.NONE) {
plugin.getGuiManager().showGUI(player, new AnimatedKitGui(plugin, player, kit, item.getItem()));
return true;
} else {
ItemStack parseStack = item.getContent().process(player);
if (!(item.getContent() instanceof KitContentEconomy || item.getContent() instanceof KitContentCommand)) {
if (Settings.AUTO_EQUIP_ARMOR.getBoolean() && ArmorType.equip(player, parseStack)) {
continue;
}
Map<Integer, ItemStack> overfilled = player.getInventory().addItem(parseStack);
for (ItemStack item2 : overfilled.values()) {
player.getWorld().dropItemNaturally(player.getLocation(), item2);
}
}
}
}
}
player.updateInventory();
return true;
}
/**
* Display a kit preview to a player.
*
* @param kit The kit to display
* @param player The player to display the preview to
* @param manager The GUI manager
* @param back The GUI to go back to
*/
public void display(Kit kit, Player player, GuiManager manager, Gui back) {
if (!kit.hasPermissionToPreview(player)) {
UltimateKits.getInstance().getLocale().getMessage("command.general.noperms")
.sendPrefixedMessage(player);
return;
}
if (kit.getKey() == null) {
plugin.getLocale().getMessage("command.kit.kitdoesntexist").sendPrefixedMessage(player);
return;
}
plugin.getLocale().getMessage("event.preview.kit")
.processPlaceholder("kit", kit.getName()).sendPrefixedMessage(player);
manager.showGUI(player, new PreviewKitGui(plugin, player, kit, back));
}
}

View File

@ -9,6 +9,7 @@ import com.craftaro.ultimatekits.UltimateKits;
import com.craftaro.ultimatekits.gui.BlockEditorGui;
import com.craftaro.ultimatekits.kit.Kit;
import com.craftaro.ultimatekits.kit.KitBlockData;
import com.craftaro.ultimatekits.kit.KitHandler;
import com.craftaro.ultimatekits.kit.KitType;
import com.craftaro.ultimatekits.settings.Settings;
import org.bukkit.ChatColor;
@ -25,29 +26,27 @@ import org.bukkit.inventory.ItemStack;
public class InteractListeners implements Listener {
private final UltimateKits plugin;
private final GuiManager guiManager;
private final KitHandler kitHandler;
public InteractListeners(UltimateKits plugin, GuiManager guiManager) {
public InteractListeners(UltimateKits plugin, GuiManager guiManager, KitHandler kitHandler) {
this.plugin = plugin;
this.guiManager = guiManager;
this.kitHandler = kitHandler;
}
@EventHandler
public void onBlockInteract(PlayerInteractEvent event) {
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_9)) {
if (event.getHand() == EquipmentSlot.OFF_HAND) {
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_9))
if (event.getHand() == EquipmentSlot.OFF_HAND)
return;
}
}
Block block = event.getClickedBlock();
if (block == null) {
if (block == null)
return;
}
KitBlockData kitBlockData = this.plugin.getKitManager().getKit(block.getLocation());
if (kitBlockData == null) {
if (kitBlockData == null)
return;
}
Kit kit = kitBlockData.getKit();
@ -57,38 +56,37 @@ public class InteractListeners implements Listener {
Material keyMaterial = Settings.KEY_MATERIAL.getMaterial().parseMaterial();
if (event.getAction() == Action.LEFT_CLICK_BLOCK) {
if (player.isSneaking()) {
if (player.isSneaking())
return;
}
event.setCancelled(true);
if (kitBlockData.getType() == KitType.PREVIEW) {
kit.display(player, this.guiManager, null);
kitHandler.display(kit, player, this.guiManager, null);
} else if (kitBlockData.getType() == KitType.CRATE) {
if (itemInHand == keyMaterial) {
kit.processKeyUse(player);
kitHandler.processKeyUse(kit, player);
} else {
this.plugin.getLocale().getMessage("event.crate.needkey").sendPrefixedMessage(player);
plugin.getLocale().getMessage("event.crate.needkey").sendPrefixedMessage(player);
return;
}
} else if (kitBlockData.getType() == KitType.CLAIM) {
if (!kit.hasPermissionToClaim(player)) {
this.plugin.getLocale().getMessage("command.general.noperms").sendPrefixedMessage(player);
plugin.getLocale().getMessage("command.general.noperms").sendPrefixedMessage(player);
return;
}
if (kit.getNextUse(player) > 0) {
long time = kit.getNextUse(player);
this.plugin.getLocale().getMessage("event.crate.notyet").processPlaceholder("time", TimeUtils.makeReadable(time)).sendPrefixedMessage(player);
plugin.getLocale().getMessage("event.crate.notyet").processPlaceholder("time", TimeUtils.makeReadable(time)).sendPrefixedMessage(player);
return;
}
if (kit.getLink() != null || kit.getPrice() != 0) {
kit.buy(player, this.guiManager);
kitHandler.buy(kit, player, guiManager);
} else {
kit.processGenericUse(player, false);
kitHandler.processGenericUse(kit, player, false);
}
kit.updateDelay(player);
@ -99,14 +97,20 @@ public class InteractListeners implements Listener {
event.setCancelled(true);
if (player.isSneaking() && player.hasPermission("ultimatekits.admin")) {
this.guiManager.showGUI(player, new BlockEditorGui(this.plugin, kitBlockData));
guiManager.showGUI(player, new BlockEditorGui(plugin, kitBlockData));
return;
}
kit.display(player, this.guiManager, null);
kitHandler.display(kit, player, guiManager, null);
}
}
/**
* When a crate item is used on any block.
* The crate item and the crate keys are not the same.
*
* @param event PlayerInteractEvent
*/
@EventHandler
public void onCrateClick(PlayerInteractEvent event) {
// Would be better to use NBT to make the item persist over aesthetic changes.
@ -138,10 +142,9 @@ public class InteractListeners implements Listener {
// Function
if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
// Open the crate
kit.processCrateUse(player, item, CompatibleHand.getHand(event));
} else // There are only left click actions left
{
kit.display(player, this.guiManager, null);
kitHandler.processCrateUse(kit, player, item, CompatibleHand.getHand(event));
} else {
kitHandler.display(kit, player, this.guiManager, null);
}
}
}

View File

@ -17,16 +17,15 @@ public class PlayerListeners implements Listener {
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
if (player.hasPlayedBefore()) {
if (player.hasPlayedBefore())
return;
}
if (this.plugin.getKitManager().getKit(Settings.STARTER_KIT.getString()) == null
if (plugin.getKitManager().getKit(Settings.STARTER_KIT.getString()) == null
|| Settings.STARTER_KIT.getString() == null
|| Settings.STARTER_KIT.getString().equalsIgnoreCase("none")) {
return;
}
this.plugin.getKitManager().getKit(Settings.STARTER_KIT.getString()).giveKit(player);
plugin.getKitHandler().giveKit(plugin.getKitManager().getKit(Settings.STARTER_KIT.getString()), player);
}
}