1
0
mirror of https://github.com/essentials/Essentials.git synced 2025-08-13 18:14:38 +02:00

Possible sign refactor?

This commit is contained in:
KHobbits
2012-01-04 18:34:37 +00:00
parent de0a419476
commit 897571db7d
5 changed files with 26 additions and 31 deletions

View File

@@ -96,38 +96,32 @@ public class Trade
}
public void pay(final IUser user)
{
pay(user, true);
{
try
{
pay(user, true);
}
catch (ChargeException ex)
{
//This should never ever get here... true above means items get dropped.
user.sendMessage(ex.getMessage());
}
}
public boolean pay(final IUser user, final boolean dropItems)
public void pay(final IUser user, final boolean dropItems) throws ChargeException
{
boolean success = true;
if (getMoney() != null && getMoney() > 0)
{
user.giveMoney(getMoney());
}
if (getItemStack() != null)
{
if (dropItems)
{
final Map<Integer, ItemStack> leftOver = InventoryWorkaround.addItem(user.getInventory(), true, getItemStack());
for (ItemStack itemStack : leftOver.values())
{
InventoryWorkaround.dropItem(user.getLocation(), itemStack);
}
}
else
{
success = InventoryWorkaround.addAllItems(user.getInventory(), true, getItemStack());
}
user.updateInventory();
user.giveItems(itemStack, dropItems);
}
if (getExperience() != null)
{
SetExpFix.setTotalExperience(user, SetExpFix.getTotalExperience(user) + getExperience());
}
return success;
}
public void charge(final IUser user) throws ChargeException

View File

@@ -1,5 +1,6 @@
package com.earth2me.essentials.api;
import com.earth2me.essentials.ChargeException;
import com.earth2me.essentials.storage.IStorageObjectHolder;
import com.earth2me.essentials.user.CooldownException;
import com.earth2me.essentials.user.UserData;
@@ -30,9 +31,9 @@ public interface IUser extends Player, IStorageObjectHolder<UserData>, IReload,
void giveMoney(double value, CommandSender initiator);
void giveItems(ItemStack itemStack, Boolean canSpew);
void giveItems(ItemStack itemStack, Boolean canSpew) throws ChargeException;
void giveItems(List<ItemStack> itemStacks, Boolean canSpew);
void giveItems(List<ItemStack> itemStacks, Boolean canSpew) throws ChargeException;
void setMoney(double value);

View File

@@ -1,5 +1,6 @@
package com.earth2me.essentials.user;
import com.earth2me.essentials.ChargeException;
import com.earth2me.essentials.Console;
import static com.earth2me.essentials.I18n._;
import com.earth2me.essentials.Teleport;
@@ -654,7 +655,7 @@ public class User extends UserBase implements IUser
}
@Override
public void giveItems(ItemStack itemStack, Boolean canSpew)
public void giveItems(ItemStack itemStack, Boolean canSpew) throws ChargeException
{
if (giveItemStack(itemStack, canSpew))
{
@@ -663,7 +664,7 @@ public class User extends UserBase implements IUser
}
@Override
public void giveItems(List<ItemStack> itemStacks, Boolean canSpew)
public void giveItems(List<ItemStack> itemStacks, Boolean canSpew) throws ChargeException
{
boolean spew = false;
for (ItemStack itemStack : itemStacks)
@@ -679,7 +680,7 @@ public class User extends UserBase implements IUser
}
}
private boolean giveItemStack(ItemStack itemStack, Boolean canSpew)
private boolean giveItemStack(ItemStack itemStack, Boolean canSpew) throws ChargeException
{
boolean spew = false;
@@ -710,6 +711,11 @@ public class User extends UserBase implements IUser
spew = true;
}
}
else {
if (!overfilled.isEmpty()) {
throw new ChargeException("Inventory full");
}
}
return spew;
}
}

View File

@@ -27,10 +27,7 @@ public class SignBuy extends EssentialsSign
final Trade items = getTrade(sign, 1, 2, player, ess);
final Trade charge = getTrade(sign, 3, ess);
charge.isAffordableFor(player);
if (!items.pay(player, false))
{
throw new ChargeException("Inventory full");
}
items.pay(player, false);
charge.charge(player);
Trade.log("Sign", "Buy", "Interact", username, charge, username, items, sign.getBlock().getLocation(), ess);
return true;

View File

@@ -55,10 +55,7 @@ public class SignTrade extends EssentialsSign
final Trade charge = getTrade(sign, 1, false, false, ess);
final Trade trade = getTrade(sign, 2, false, true, ess);
charge.isAffordableFor(player);
if (!trade.pay(player, false))
{
throw new ChargeException("Full inventory");
}
trade.pay(player, false);
substractAmount(sign, 2, trade, ess);
addAmount(sign, 1, charge, ess);
charge.charge(player);