mirror of
https://github.com/EssentialsX/Essentials.git
synced 2024-12-28 20:17:49 +01:00
Cleanup of repair command and allow different charges for each item.
This commit is contained in:
parent
0543a8f8ba
commit
14030deb16
@ -1,21 +1,17 @@
|
|||||||
/*
|
|
||||||
* To change this template, choose Tools | Templates
|
|
||||||
* and open the template in the editor.
|
|
||||||
*/
|
|
||||||
package com.earth2me.essentials.commands;
|
package com.earth2me.essentials.commands;
|
||||||
|
|
||||||
|
import com.earth2me.essentials.ChargeException;
|
||||||
|
import com.earth2me.essentials.IUser;
|
||||||
|
import com.earth2me.essentials.Trade;
|
||||||
import com.earth2me.essentials.User;
|
import com.earth2me.essentials.User;
|
||||||
import com.earth2me.essentials.Util;
|
import com.earth2me.essentials.Util;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import org.bukkit.Material;
|
import org.bukkit.Material;
|
||||||
import org.bukkit.Server;
|
import org.bukkit.Server;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
import org.bukkit.material.MaterialData;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @author Seiji
|
|
||||||
*/
|
|
||||||
public class Commandrepair extends EssentialsCommand
|
public class Commandrepair extends EssentialsCommand
|
||||||
{
|
{
|
||||||
public Commandrepair()
|
public Commandrepair()
|
||||||
@ -24,7 +20,7 @@ public class Commandrepair extends EssentialsCommand
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run(Server server, User user, String commandLabel, String[] args) throws Exception
|
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception
|
||||||
{
|
{
|
||||||
if (args.length < 1)
|
if (args.length < 1)
|
||||||
{
|
{
|
||||||
@ -33,7 +29,20 @@ public class Commandrepair extends EssentialsCommand
|
|||||||
|
|
||||||
if (args[0].equalsIgnoreCase("hand"))
|
if (args[0].equalsIgnoreCase("hand"))
|
||||||
{
|
{
|
||||||
ItemStack item = user.getItemInHand();
|
final ItemStack item = user.getItemInHand();
|
||||||
|
final String itemName = item.getType().toString().toLowerCase();
|
||||||
|
final Trade charge = new Trade("repair-" + itemName.replace('_', '-'), ess);
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
charge.isAffordableFor(user);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
user.sendMessage(ex.getMessage());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
repairItem(item);
|
repairItem(item);
|
||||||
@ -43,26 +52,26 @@ public class Commandrepair extends EssentialsCommand
|
|||||||
user.sendMessage(e.getMessage());
|
user.sendMessage(e.getMessage());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
charge.charge(user);
|
||||||
|
|
||||||
String itemName = item.getType().toString().toLowerCase().replace('_', ' ');
|
|
||||||
charge(user);
|
charge(user);
|
||||||
user.sendMessage(Util.format("repair", itemName));
|
user.sendMessage(Util.format("repair", itemName.replace('_', ' ')));
|
||||||
}
|
}
|
||||||
else if (args[0].equalsIgnoreCase("all"))
|
else if (args[0].equalsIgnoreCase("all"))
|
||||||
{
|
{
|
||||||
StringBuilder itemList = new StringBuilder();
|
final List<String> repaired = new ArrayList<String>();
|
||||||
itemList.append(repairItems(user.getInventory().getContents()));
|
repairItems(user.getInventory().getContents(), user, repaired);
|
||||||
|
|
||||||
String armor = repairItems(user.getInventory().getArmorContents());
|
repairItems(user.getInventory().getArmorContents(), user, repaired);
|
||||||
|
|
||||||
if (itemList.length() == 0)
|
if (repaired.isEmpty())
|
||||||
{
|
{
|
||||||
user.sendMessage(Util.format("repairNone"));
|
user.sendMessage(Util.format("repairNone"));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
charge(user);
|
charge(user);
|
||||||
user.sendMessage(Util.format("repair", Util.joinList(itemList)));
|
user.sendMessage(Util.format("repair", Util.joinList(repaired)));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -72,10 +81,9 @@ public class Commandrepair extends EssentialsCommand
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void repairItem(ItemStack item) throws Exception
|
private void repairItem(final ItemStack item) throws Exception
|
||||||
{
|
{
|
||||||
Material material = Material.getMaterial(item.getTypeId());
|
final Material material = Material.getMaterial(item.getTypeId());
|
||||||
String error = null;
|
|
||||||
if (material.isBlock() || material.getMaxDurability() < 0)
|
if (material.isBlock() || material.getMaxDurability() < 0)
|
||||||
{
|
{
|
||||||
throw new Exception(Util.i18n("repairInvalidType"));
|
throw new Exception(Util.i18n("repairInvalidType"));
|
||||||
@ -89,28 +97,43 @@ public class Commandrepair extends EssentialsCommand
|
|||||||
item.setDurability((short)0);
|
item.setDurability((short)0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String repairItems(ItemStack[] items)
|
private void repairItems(final ItemStack[] items, final IUser user, final List<String> repaired)
|
||||||
{
|
{
|
||||||
StringBuilder itemList = new StringBuilder();
|
|
||||||
for (ItemStack item : items)
|
for (ItemStack item : items)
|
||||||
{
|
{
|
||||||
|
final String itemName = item.getType().toString().toLowerCase();
|
||||||
|
final Trade charge = new Trade("repair-" + itemName.replace('_', '-'), ess);
|
||||||
|
boolean canBeRepaired = true;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
repairItem(item);
|
charge.isAffordableFor(user);
|
||||||
if (itemList.length() > 0)
|
|
||||||
{
|
|
||||||
itemList.append(", ");
|
|
||||||
}
|
|
||||||
|
|
||||||
String itemName = item.getType().toString().toLowerCase().replace('_', ' ');
|
|
||||||
itemList.append(itemName);
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (ChargeException ex)
|
||||||
{
|
{
|
||||||
|
canBeRepaired = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (canBeRepaired)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
repairItem(item);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
try
|
||||||
|
{
|
||||||
|
charge.charge(user);
|
||||||
|
}
|
||||||
|
catch (ChargeException ex)
|
||||||
|
{
|
||||||
|
user.sendMessage(ex.getMessage());
|
||||||
|
}
|
||||||
|
repaired.add(itemName.replace('_', ' '));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return itemList.toString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user