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 000000000..da8fb928b Binary files /dev/null and b/src/main/resources/assets/forestry/textures/items/errors/noPaper.png differ 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 2f373319d..000000000 Binary files a/src/main/resources/assets/forestry/textures/items/errors/notPostpaid.png and /dev/null differ