Clearer error message for the Trade Station

#170
This commit is contained in:
mezz committed 2014-09-08 17:58:29 -07:00
1 parent ea1f3f0ee4
commit 7ee9f5dd95
7 files changed
+107 -30

No files matched your search

+17 -3
View File
@@ -66,7 +66,7 @@ public enum EnumErrorCode {
NOHONEY("noHoney", 22),
NOTPOSTPAID("notPostpaid", 23),
NOTPOSTPAID("notPostpaid", "noStamps", 23),
NORECIPIENT("noRecipient", 24),
@@ -86,14 +86,28 @@ public enum EnumErrorCode {
CIRCUITMISMATCH("circuitMismatch", 26),
NOLIQUID("noLiquid", 29);
NOLIQUID("noLiquid", 29),
NOPAPER("noPaper", 30),
NOSTAMPSNOPAPER("noStampsNoPaper", "noStamps", 31),
NOSUPPLIES("noSupplies", "noResource", 32),
NOTRADE("noTrade", "noResource", 33);
private String name;
private String iconName;
@SideOnly(Side.CLIENT)
private IIcon icon;
private EnumErrorCode(String name, int iconIndex) {
this(name, name, iconIndex);
}
private EnumErrorCode(String name, String iconName, int iconIndex) {
this.name = name;
this.iconName = iconName;
}
public String getDescription() {
@@ -106,7 +120,7 @@ public enum EnumErrorCode {
@SideOnly(Side.CLIENT)
public void registerIcons(IIconRegister register) {
icon = TextureManager.getInstance().registerTex(register, "errors/" + name);
icon = TextureManager.getInstance().registerTex(register, "errors/" + iconName);
}
@SideOnly(Side.CLIENT)
@@ -89,18 +89,51 @@ public class MachineTrader extends TileBase implements ISpecialInventory, ISided
}
/* UPDATING */
/**
* The trade station should show errors for missing stamps and paper first.
* Once it is able to send letters, it should display other error states.
*/
@Override
public void updateServerSide() {
if (worldObj.getTotalWorldTime() % 40 * 10 != 0)
if (!isLinked() || worldObj.getTotalWorldTime() % 4 != 0)
return;
if (!hasPaperMin(0.0f) || !hasInputBufMin(0.0f)) {
setErrorState(EnumErrorCode.NORESOURCE);
EnumErrorCode errorCode = EnumErrorCode.OK;
if (!hasPostageMin(3))
errorCode = EnumErrorCode.NOSTAMPS;
if (!hasPaperMin(2)) {
if (errorCode == EnumErrorCode.NOSTAMPS)
errorCode = EnumErrorCode.NOSTAMPSNOPAPER;
else
errorCode = EnumErrorCode.NOPAPER;
}
if (errorCode != EnumErrorCode.OK) {
setErrorState(errorCode);
return;
}
if (!hasPostageMin(3)) { // Assumes that the trade station owner should have a notice.
setErrorState(EnumErrorCode.NOSTAMPS);
IInventory inventory = getOrCreateTradeInventory();
ItemStack tradeGood = inventory.getStackInSlot(TradeStation.SLOT_TRADEGOOD);
if (tradeGood == null) {
setErrorState(EnumErrorCode.NOTRADE);
return;
}
boolean hasRequest = hasItemCount(TradeStation.SLOT_EXCHANGE_1, TradeStation.SLOT_EXCHANGE_COUNT, null, 1);
if (!hasRequest) {
setErrorState(EnumErrorCode.NOTRADE);
return;
}
boolean hasSupplies = hasItemCount(TradeStation.SLOT_BUFFER, TradeStation.SLOT_BUFFER_COUNT, tradeGood, tradeGood.stackSize);
if(!hasSupplies) {
setErrorState(EnumErrorCode.NOSUPPLIES);
return;
}
@@ -113,31 +146,60 @@ public class MachineTrader extends TileBase implements ISpecialInventory, ISided
return address.isValid();
}
private float percentOccupied(int startSlot, int countSlots, ItemStack item) {
int max = countSlots * 64;
int avail = 0;
/**
* Returns true if there are 'itemCount' of 'item' in the inventory
* wildcard when item == null, counts all types of items
*/
private boolean hasItemCount(int startSlot, int countSlots, ItemStack item, int itemCount) {
int count = 0;
IInventory tradeInventory = this.getOrCreateTradeInventory();
for (int i = startSlot; i < startSlot + countSlots; i++) {
ItemStack itemInSlot = tradeInventory.getStackInSlot(i);
if (itemInSlot == null || (item != null && !StackUtils.isIdenticalItem(itemInSlot, item)))
if (itemInSlot == null)
continue;
avail += itemInSlot.stackSize;
if (item == null || StackUtils.isIdenticalItem(itemInSlot, item))
count += itemInSlot.stackSize;
if (count >= itemCount)
return true;
}
return ((float) avail / (float) max);
return false;
}
public boolean hasPaperMin(float percentage) {
return percentOccupied(TradeStation.SLOT_LETTERS_1, TradeStation.SLOT_LETTERS_COUNT, new ItemStack(Items.paper)) > percentage;
/**
* Returns the percentage of the inventory that is occupied by 'item'
* if item == null, returns the percentage occupied by all kinds of items
*/
private float percentOccupied(int startSlot, int countSlots, ItemStack item) {
int count = 0;
int total = 0;
IInventory tradeInventory = this.getOrCreateTradeInventory();
for (int i = startSlot; i < startSlot + countSlots; i++) {
ItemStack itemInSlot = tradeInventory.getStackInSlot(i);
if (itemInSlot == null) {
total += 64;
} else {
total += itemInSlot.getMaxStackSize();
if (item == null || StackUtils.isIdenticalItem(itemInSlot, item))
count += itemInSlot.stackSize;
}
}
return ((float)count / (float)total);
}
public boolean hasPaperMin(int count) {
return hasItemCount(TradeStation.SLOT_LETTERS_1, TradeStation.SLOT_LETTERS_COUNT, new ItemStack(Items.paper), count);
}
public boolean hasInputBufMin(float percentage) {
IInventory inventory = getOrCreateTradeInventory();
ItemStack tradegood = inventory.getStackInSlot(TradeStation.SLOT_TRADEGOOD);
if (tradegood == null)
ItemStack tradeGood = inventory.getStackInSlot(TradeStation.SLOT_TRADEGOOD);
if (tradeGood == null)
return true;
return percentOccupied(TradeStation.SLOT_BUFFER, TradeStation.SLOT_BUFFER_COUNT, tradegood) > percentage;
return percentOccupied(TradeStation.SLOT_BUFFER, TradeStation.SLOT_BUFFER_COUNT, tradeGood) > percentage;
}
public boolean hasOutputBufMin(float percentage) {
@@ -157,9 +219,11 @@ public class MachineTrader extends TileBase implements ISpecialInventory, ISided
continue;
posted += ((IStamps) stamp.getItem()).getPostage(stamp).getValue() * stamp.stackSize;
if (posted >= postage)
return true;
}
return posted >= postage;
return false;
}
/* ADDRESS */
@@ -188,7 +252,6 @@ public class MachineTrader extends TileBase implements ISpecialInventory, ISided
this.address = address;
PostManager.postRegistry.getOrCreateTradeStation(worldObj, getOwnerProfile(), address);
setErrorState(EnumErrorCode.OK);
sendNetworkUpdate();
} else
this.address = address;
}
@@ -409,8 +472,8 @@ public class MachineTrader extends TileBase implements ISpecialInventory, ISided
@Override
public LinkedList<ITrigger> getCustomTriggers() {
LinkedList<ITrigger> res = new LinkedList<ITrigger>();
res.add(PluginMail.lowPaper25);
res.add(PluginMail.lowPaper10);
res.add(PluginMail.lowPaper64);
res.add(PluginMail.lowPaper32);
res.add(PluginMail.lowInput25);
res.add(PluginMail.lowInput10);
res.add(PluginMail.lowPostage40);
@@ -21,9 +21,9 @@ import forestry.mail.gadgets.MachineTrader;
public class TriggerLowPaper extends Trigger {
float threshold;
int threshold;
public TriggerLowPaper(String tag, float threshold) {
public TriggerLowPaper(String tag, int threshold) {
super(tag, "lowPaper");
this.threshold = threshold;
}
@@ -65,8 +65,8 @@ public class PluginMail extends NativePlugin {
@SidedProxy(clientSide = "forestry.mail.proxy.ClientProxyMail", serverSide = "forestry.mail.proxy.ProxyMail")
public static ProxyMail proxy;
public static Trigger triggerHasMail;
public static Trigger lowPaper25;
public static Trigger lowPaper10;
public static Trigger lowPaper64;
public static Trigger lowPaper32;
public static Trigger lowPostage40;
public static Trigger lowPostage20;
public static Trigger lowInput25;
@@ -90,8 +90,8 @@ public class PluginMail extends NativePlugin {
new TickHandlerMailClient();
triggerHasMail = new TriggerHasMail();
lowPaper25 = new TriggerLowPaper("mail.lowPaper.25", 0.25f);
lowPaper10 = new TriggerLowPaper("mail.lowPaper.10", 0.1f);
lowPaper64 = new TriggerLowPaper("mail.lowPaper.64", 64);
lowPaper32 = new TriggerLowPaper("mail.lowPaper.32", 32);
lowPostage40 = new TriggerLowStamps("mail.lowStamps.40", 40);
lowPostage20 = new TriggerLowStamps("mail.lowStamps.20", 20);
lowInput25 = new TriggerLowInput("mail.lowInput.25", 0.25f);