Skip to content
This repository has been archived by the owner on Mar 8, 2022. It is now read-only.

Commit

Permalink
fix issue #66
Browse files Browse the repository at this point in the history
  • Loading branch information
ReinWD committed Apr 20, 2020
1 parent 905ad87 commit 6b031a7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 15 deletions.
8 changes: 7 additions & 1 deletion src/main/java/cat/nyaa/HamsterEcoHelper/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import cat.nyaa.nyaacore.cmdreceiver.Arguments;
import cat.nyaa.nyaacore.cmdreceiver.CommandReceiver;
import cat.nyaa.nyaacore.cmdreceiver.SubCommand;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
Expand Down Expand Up @@ -124,7 +125,12 @@ public void userRetrieve(CommandSender sender, Arguments args) {
return;
}
for (ItemStack s : items) {
p.getWorld().dropItem(p.getEyeLocation(), s);
try{
p.getWorld().dropItem(p.getEyeLocation(), s);
}catch (Exception e){
Bukkit.getLogger().warning("exception retrieving items.");
e.printStackTrace();
}
}
plugin.database.clearTemporaryStorage(p);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.*;

public class RequisitionCommands extends CommandReceiver {
private HamsterEcoHelper plugin;
Expand Down Expand Up @@ -135,15 +133,20 @@ public void Requisition(CommandSender sender, Arguments args) {
return;
}
if (args.remains() != 3) {
msg(sender, "manual.requisition.req.usage");
return;
if ((args.remains() != 4 || !args.getRawArgs()[3].equalsIgnoreCase("true")) && !args.getRawArgs()[3].equalsIgnoreCase("false")) {
msg(sender, "manual.requisition.req.usage");
return;
}
}

Player player = asPlayer(sender);
String itemName = args.nextString().toUpperCase();
ItemStack item = null;
double unitPrice = args.nextDouble("#.##");
int amount = args.nextInt();
boolean hasStrictArg = args.top() != null;
boolean argStrict = hasStrictArg && args.nextBoolean();

if (plugin.reqManager.cooldown.containsKey(player.getUniqueId())
&& plugin.reqManager.cooldown.get(player.getUniqueId()) > System.currentTimeMillis()) {
msg(sender, "user.info.cooldown", (plugin.reqManager.cooldown.get(player.getUniqueId()) - System.currentTimeMillis()) / 1000);
Expand All @@ -167,17 +170,48 @@ public void Requisition(CommandSender sender, Arguments args) {
return;
}
}
boolean isStrict = (!hasStrictArg && isShulkerBox(item)) || argStrict;

if (!plugin.eco.enoughMoney(player, unitPrice * amount)) {
msg(sender, "user.warn.no_enough_money");
return;
}
boolean success = plugin.reqManager.newPlayerRequisition(player, item, unitPrice, amount);
boolean success = plugin.reqManager.newPlayerRequisition(player, item, unitPrice, amount, isStrict);
if (success) {
plugin.eco.withdraw(player, amount * unitPrice);
plugin.reqManager.cooldown.put(player.getUniqueId(), System.currentTimeMillis() + (plugin.config.playerRequisitionCooldownTicks * 50));
}
}


private static final Set<Material> SHULKER_BOXES = new HashSet<>();
static {
Collections.addAll(SHULKER_BOXES,
Material.SHULKER_BOX,
Material.WHITE_SHULKER_BOX,
Material.ORANGE_SHULKER_BOX,
Material.MAGENTA_SHULKER_BOX,
Material.YELLOW_SHULKER_BOX,
Material.LIME_SHULKER_BOX,
Material.PINK_SHULKER_BOX,
Material.GRAY_SHULKER_BOX,
Material.LIGHT_BLUE_SHULKER_BOX,
Material.LIGHT_GRAY_SHULKER_BOX,
Material.CYAN_SHULKER_BOX,
Material.PURPLE_SHULKER_BOX,
Material.BLUE_SHULKER_BOX,
Material.BROWN_SHULKER_BOX,
Material.GREEN_SHULKER_BOX,
Material.RED_SHULKER_BOX,
Material.BLACK_SHULKER_BOX
);
}

private boolean isShulkerBox(ItemStack item) {
if (item == null)return false;
return SHULKER_BOXES.contains(item.getType());
}

public List<String> reqTabComplete(CommandSender sender, Arguments args) {
List<String> list = new ArrayList<>();
if (args.remains() == 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ public RequisitionInstance(
public RequisitionInstance(Player player,
ItemStack item,
double unitPrice, int reqAmount,
HamsterEcoHelper plugin, Runnable finishCallback) {
boolean isStrict, HamsterEcoHelper plugin, Runnable finishCallback) {
this.owner = player;
this.plugin = plugin;
this.finishCallback = finishCallback;
this.unitPrice = unitPrice;
this.templateItem = new RequisitionSpecification();
this.templateItem.itemTemplate = item;
this.templateItem.timeoutTicks = plugin.config.playerRequisitionTimeoutTicks;
this.templateItem.matchRule.requireExact = isStrict;
this.templateItem.matchRule.enchantMatch = RequisitionSpecification.MatchingMode.EXACT;
this.templateItem.matchRule.nameMatch = RequisitionSpecification.MatchingMode.EXACT;
this.templateItem.matchRule.loreMatch = RequisitionSpecification.MatchingMode.EXACT;
Expand Down Expand Up @@ -115,18 +116,19 @@ public double purchase(Player p, int amount) {
if (!templateItem.matchRule.matches(itemHand)) return -2;
if (amountRemains < amount && amountRemains >= 0) amount = amountRemains;
int new_amount = itemHand.getAmount() - amount;
if (new_amount == 0) {
p.getInventory().setItemInMainHand(new ItemStack(Material.AIR));
} else {
itemHand.setAmount(new_amount);
}

if (owner != null) {
ItemStack tmp = itemHand.clone();
tmp.setAmount(amount);
MiscUtils.giveItem(owner, tmp);
}

if (new_amount == 0) {
p.getInventory().setItemInMainHand(new ItemStack(Material.AIR));
} else {
itemHand.setAmount(new_amount);
}

if (amountRemains >= 0) amountRemains -= amount;
soldAmount += amount;
new Message(I18n.format("user.req.sold_amount_0", p.getName(), amount))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ public boolean newRequisition(RequisitionSpecification item) {
return true;
}

public boolean newPlayerRequisition(Player player, ItemStack item, double unitPrice, int amount) {
public boolean newPlayerRequisition(Player player, ItemStack item, double unitPrice, int amount, boolean isStrict) {
if (currentReq != null) return false;
if (item == null) return false;
currentReq = new RequisitionInstance(player, item, unitPrice, amount, plugin, () -> this.currentReq = null);
currentReq = new RequisitionInstance(player, item, unitPrice, amount, isStrict, plugin, () -> this.currentReq = null);
if (plugin.config.requisitionHintInterval > 0) {
currentReq.new RequisitionHintTimer(this, plugin.config.requisitionHintInterval, plugin);
}
Expand Down

0 comments on commit 6b031a7

Please sign in to comment.