From 7ee9f5dd955f4fbe3d5a801266d74173d275ea35 Mon Sep 17 00:00:00 2001 From: mezz Date: Mon, 8 Sep 2014 17:40:58 -0700 Subject: [PATCH] Clearer error message for the Trade Station #170 --- lang | 2 +- .../java/forestry/core/EnumErrorCode.java | 20 +++- .../forestry/mail/gadgets/MachineTrader.java | 103 ++++++++++++++---- .../mail/triggers/TriggerLowPaper.java | 4 +- .../java/forestry/plugins/PluginMail.java | 8 +- .../textures/items/errors/noPaper.png | Bin 0 -> 1040 bytes .../textures/items/errors/notPostpaid.png | Bin 458 -> 0 bytes 7 files changed, 107 insertions(+), 30 deletions(-) create mode 100644 src/main/resources/assets/forestry/textures/items/errors/noPaper.png delete mode 100755 src/main/resources/assets/forestry/textures/items/errors/notPostpaid.png diff --git a/lang b/lang index 7e3f048c0..92402cb0d 160000 --- a/lang +++ b/lang @@ -1 +1 @@ -Subproject commit 7e3f048c0e226df569c1b9db159543a189be64b5 +Subproject commit 92402cb0d104dd5cc25d7e720cb76487d1dc5953 diff --git a/src/main/java/forestry/core/EnumErrorCode.java b/src/main/java/forestry/core/EnumErrorCode.java index 0efcfb27e..64d96143b 100755 --- a/src/main/java/forestry/core/EnumErrorCode.java +++ b/src/main/java/forestry/core/EnumErrorCode.java @@ -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) diff --git a/src/main/java/forestry/mail/gadgets/MachineTrader.java b/src/main/java/forestry/mail/gadgets/MachineTrader.java index ac02b47ea..c0bb66cd8 100755 --- a/src/main/java/forestry/mail/gadgets/MachineTrader.java +++ b/src/main/java/forestry/mail/gadgets/MachineTrader.java @@ -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 getCustomTriggers() { LinkedList res = new LinkedList(); - 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); diff --git a/src/main/java/forestry/mail/triggers/TriggerLowPaper.java b/src/main/java/forestry/mail/triggers/TriggerLowPaper.java index 3d9402c1c..f2e374310 100755 --- a/src/main/java/forestry/mail/triggers/TriggerLowPaper.java +++ b/src/main/java/forestry/mail/triggers/TriggerLowPaper.java @@ -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; } diff --git a/src/main/java/forestry/plugins/PluginMail.java b/src/main/java/forestry/plugins/PluginMail.java index 22b471e6e..048371773 100755 --- a/src/main/java/forestry/plugins/PluginMail.java +++ b/src/main/java/forestry/plugins/PluginMail.java @@ -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); diff --git a/src/main/resources/assets/forestry/textures/items/errors/noPaper.png b/src/main/resources/assets/forestry/textures/items/errors/noPaper.png new file mode 100644 index 0000000000000000000000000000000000000000..da8fb928b3e30926cda32581bd6a799521574066 GIT binary patch literal 1040 zcmaJ=O-$2J9B%{-Ap%}BiivragP5>(-ABhN1?IZJ;yTAR!!CYw?SnO=eYJhKvI|Ox zQ7&@uX5zt{#u(y(i!mnLj90UR2M;PSAtoM7Jm_J*j%|2wHf`V6@AvzEzWdqC%s~He zKSfaksaYvU#$(>s*F*lJzM(uBPT@om=g|VLDmJ8I8Y)4MGL%J_gNn9t^DUg9s2*A` z6md~b3o0@g#ltYJVG%Y(O-#9#qE;XVCAg@YBK`f<8ye`ENY965zih=}S)Z-hFkj0Q z)LKQ2YV_13m~aImU_h(@*H|(g!4>JIu0ZDAHcNvh1Xo0Q->IUU1#x6U5N3it)z5K& z=NT>(=6P-m1pHip^^=$PaUmhh3xNn|Jv6aqYYRe7O15l~l}MK{wgi@~R;x@k$RK-> z<)YE3rx6JF2*T&Am{@UrrZdu3kf5X5x`lOQ0#8vXp=B)6#MAu{467q+I;}L3gt4w- zu^i*~LTUqL`TtPE=%5{(g9rZpQ`jl2Sdh&@2QAwwDcr({XUY=dHdHXO3kWT>vzRR- zjGQvEKs?Wb(Rtm}P}Mo##LKdfG99d#DojZtO%xbi*Mv}VJQfT|iC`oaw@CMR(@xuxMW^whNxI!8ZM~Wf?~p4oluZ{Bjns6#Ufo12?! zG#bx+rN`vxFwSN2WZ9s8Zx4Fcub7fz1^2`HgFF4y*}nMKdSmrR^!O8CW>+7qN ckzc(hse83UpDSBi3b8?@5*g{m#T#pX06Qa2r2qf` literal 0 HcmV?d00001 diff --git a/src/main/resources/assets/forestry/textures/items/errors/notPostpaid.png b/src/main/resources/assets/forestry/textures/items/errors/notPostpaid.png deleted file mode 100755 index 2f373319d52d9bf2c8724bc7b29ae7c7a9ea319e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 458 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!73?$#)eFPFur)B-u21s;*b3=G^tAk28_ZrvZCV3MbcV+hCfF9wqT4wd~ljqgkS7p`M;7TY%eg#*kB|B^K>qT{Gk8(j>8n z-5u4>UnwgvxAr(PoJcS;FiSIYuwY_%A|Wgh<=u4?Xq#$@YeY#(Vo9o1a#1RfVlXl= zG}1LN*EKQ-F*LL?HLx-@);2J*GBD71aulc@NkeXaN@iLmNQ1GifuXLEd5Dn_NP`Jb zgCS6ZAp=i6P=hkahP2F_)V##pRI3sogP|ZXJyow#-_R^J9?Yx&F|%(Z6@V=8boFyt I=akR{0R4rHK>z>%