mirror of
https://github.com/essentials/Essentials.git
synced 2025-08-16 03:24:31 +02:00
Sell & Buy Sign
This commit is contained in:
@@ -114,7 +114,7 @@ public class EssentialsSign
|
|||||||
|
|
||||||
protected final void validateCharge(final ISign sign, final int index) throws SignException
|
protected final void validateCharge(final ISign sign, final int index) throws SignException
|
||||||
{
|
{
|
||||||
final String line = sign.getLine(index);
|
final String line = sign.getLine(index).trim();
|
||||||
if (line.isEmpty())
|
if (line.isEmpty())
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
@@ -158,6 +158,49 @@ public class EssentialsSign
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected final void validateInteger(final ISign sign, final int index) throws SignException
|
||||||
|
{
|
||||||
|
final String line = sign.getLine(index).trim();
|
||||||
|
if (line.isEmpty())
|
||||||
|
{
|
||||||
|
throw new SignException("Empty line " + index);
|
||||||
|
}
|
||||||
|
final int quantity = getInteger(line);
|
||||||
|
sign.setLine(index, Integer.toString(quantity));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final int getInteger(final String line) throws SignException
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
final int quantity = Integer.parseInt(line);
|
||||||
|
if (quantity <= 1)
|
||||||
|
{
|
||||||
|
throw new SignException(Util.i18n("moreThanZero"));
|
||||||
|
}
|
||||||
|
return quantity;
|
||||||
|
}
|
||||||
|
catch (NumberFormatException ex)
|
||||||
|
{
|
||||||
|
throw new SignException("Invalid sign", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final void validateItem(final ISign sign, final int index, final boolean noair) throws SignException
|
||||||
|
{
|
||||||
|
final String line = sign.getLine(index).trim();
|
||||||
|
if (line.isEmpty())
|
||||||
|
{
|
||||||
|
throw new SignException("Empty line " + index);
|
||||||
|
}
|
||||||
|
ItemStack item = getItemStack(line);
|
||||||
|
if (noair && item.getTypeId() == 0)
|
||||||
|
{
|
||||||
|
throw new SignException("Don't sell air.");
|
||||||
|
}
|
||||||
|
sign.setLine(index, line);
|
||||||
|
}
|
||||||
|
|
||||||
protected final ItemStack getItemStack(final String itemName) throws SignException
|
protected final ItemStack getItemStack(final String itemName) throws SignException
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -170,9 +213,45 @@ public class EssentialsSign
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected final void validateMoney(final ISign sign, final int index) throws SignException
|
||||||
|
{
|
||||||
|
final String line = sign.getLine(index).trim();
|
||||||
|
if (line.isEmpty())
|
||||||
|
{
|
||||||
|
throw new SignException("Empty line " + index);
|
||||||
|
}
|
||||||
|
final double quantity = getMoney(line);
|
||||||
|
sign.setLine(index, Util.formatCurrency(quantity));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final double getMoney(final String line) throws SignException
|
||||||
|
{
|
||||||
|
final boolean isMoney = line.matches("^[^0-9-][\\.0-9]+");
|
||||||
|
if (isMoney)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
final double quantity = Double.parseDouble(line.substring(1));
|
||||||
|
if (quantity <= 0)
|
||||||
|
{
|
||||||
|
throw new SignException(Util.i18n("moreThanZero"));
|
||||||
|
}
|
||||||
|
return quantity;
|
||||||
|
}
|
||||||
|
catch (NumberFormatException ex)
|
||||||
|
{
|
||||||
|
throw new SignException(ex.getMessage(), ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new SignException("Invalid money");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected final Charge getCharge(final ISign sign, final int index, final IEssentials ess) throws SignException
|
protected final Charge getCharge(final ISign sign, final int index, final IEssentials ess) throws SignException
|
||||||
{
|
{
|
||||||
final String line = sign.getLine(index);
|
final String line = sign.getLine(index).trim();
|
||||||
if (line.isEmpty())
|
if (line.isEmpty())
|
||||||
{
|
{
|
||||||
return new Charge(signName.toLowerCase() + "sign", ess);
|
return new Charge(signName.toLowerCase() + "sign", ess);
|
||||||
@@ -181,12 +260,19 @@ public class EssentialsSign
|
|||||||
final boolean isMoney = line.matches("^[^0-9-][\\.0-9]+");
|
final boolean isMoney = line.matches("^[^0-9-][\\.0-9]+");
|
||||||
if (isMoney)
|
if (isMoney)
|
||||||
{
|
{
|
||||||
final double quantity = Double.parseDouble(line.substring(1));
|
try
|
||||||
if (quantity <= 0)
|
|
||||||
{
|
{
|
||||||
throw new SignException(Util.i18n("moreThanZero"));
|
final double quantity = Double.parseDouble(line.substring(1));
|
||||||
|
if (quantity <= 0)
|
||||||
|
{
|
||||||
|
throw new SignException(Util.i18n("moreThanZero"));
|
||||||
|
}
|
||||||
|
return new Charge(quantity, ess);
|
||||||
|
}
|
||||||
|
catch (NumberFormatException ex)
|
||||||
|
{
|
||||||
|
throw new SignException(ex.getMessage(), ex);
|
||||||
}
|
}
|
||||||
return new Charge(quantity, ess);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
45
Essentials/src/com/earth2me/essentials/signs/SignBuy.java
Normal file
45
Essentials/src/com/earth2me/essentials/signs/SignBuy.java
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package com.earth2me.essentials.signs;
|
||||||
|
|
||||||
|
import com.earth2me.essentials.Charge;
|
||||||
|
import com.earth2me.essentials.ChargeException;
|
||||||
|
import com.earth2me.essentials.IEssentials;
|
||||||
|
import com.earth2me.essentials.InventoryWorkaround;
|
||||||
|
import com.earth2me.essentials.User;
|
||||||
|
import java.util.Map;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
|
|
||||||
|
public class SignBuy extends EssentialsSign
|
||||||
|
{
|
||||||
|
public SignBuy()
|
||||||
|
{
|
||||||
|
super("Buy");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
|
||||||
|
{
|
||||||
|
validateInteger(sign, 1);
|
||||||
|
validateItem(sign, 2, true);
|
||||||
|
validateCharge(sign, 3);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException
|
||||||
|
{
|
||||||
|
final int amount = getInteger(sign.getLine(1));
|
||||||
|
final ItemStack item = getItemStack(sign.getLine(2));
|
||||||
|
item.setAmount(amount);
|
||||||
|
final Charge charge = getCharge(sign, 3, ess);
|
||||||
|
charge.isAffordableFor(player);
|
||||||
|
final Map<Integer, ItemStack> leftOver = player.getInventory().addItem(item);
|
||||||
|
for (ItemStack itemStack : leftOver.values())
|
||||||
|
{
|
||||||
|
InventoryWorkaround.dropItem(player.getLocation(), itemStack);
|
||||||
|
}
|
||||||
|
player.updateInventory();
|
||||||
|
charge.charge(player);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
39
Essentials/src/com/earth2me/essentials/signs/SignSell.java
Normal file
39
Essentials/src/com/earth2me/essentials/signs/SignSell.java
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package com.earth2me.essentials.signs;
|
||||||
|
|
||||||
|
import com.earth2me.essentials.Charge;
|
||||||
|
import com.earth2me.essentials.ChargeException;
|
||||||
|
import com.earth2me.essentials.IEssentials;
|
||||||
|
import com.earth2me.essentials.User;
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
|
|
||||||
|
public class SignSell extends EssentialsSign
|
||||||
|
{
|
||||||
|
public SignSell()
|
||||||
|
{
|
||||||
|
super("Sell");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean onSignCreate(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException
|
||||||
|
{
|
||||||
|
validateInteger(sign, 1);
|
||||||
|
validateItem(sign, 2, true);
|
||||||
|
validateMoney(sign, 3);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException
|
||||||
|
{
|
||||||
|
final int amount = getInteger(sign.getLine(1));
|
||||||
|
final ItemStack item = getItemStack(sign.getLine(2));
|
||||||
|
item.setAmount(amount);
|
||||||
|
final double money = getMoney(sign.getLine(3));
|
||||||
|
final Charge charge = new Charge(item, ess);
|
||||||
|
charge.isAffordableFor(player);
|
||||||
|
player.giveMoney(money);
|
||||||
|
charge.charge(player);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user