Skip to content
This repository has been archived by the owner on Jun 23, 2024. It is now read-only.

Commit

Permalink
Various bugfixes and improvements:
Browse files Browse the repository at this point in the history
- long trades lists center-align with the original gui now instead of top; this helps sheperds especially with their long list of different colored wool
- fixed display of items like pumpkins and melons that were shown too dark
- several additional null pointer checks to prevent crashes
- code cleanup
- removed most debugging output
- better check if two itemstacks can be merged, this prevents trade results being put into the wrong locations resulting in items in the hand after trading
- added link to mod URL
  • Loading branch information
gbl committed Mar 5, 2017
1 parent 649a40a commit 3bdde09
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@
package de.guntram.mcmod.easiervillagertrading;

import java.io.IOException;
import java.util.List;
import net.minecraft.client.gui.GuiButton;
import net.minecraft.client.gui.GuiMerchant;
import net.minecraft.client.renderer.RenderHelper;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.ClickType;
import net.minecraft.inventory.Slot;
import net.minecraft.item.Item;
import net.minecraft.item.ItemEnchantedBook;
import net.minecraft.item.ItemStack;
Expand All @@ -29,6 +28,13 @@ public class BetterGuiMerchant extends GuiMerchant {

private final int addXSize=0;
private final ItemStack tradeOK, tradeNOK;
private final int lineHeight=18;
private final int titleDistance=20;
private final int firstBuyItemXpos=0;
private final int secondBuyItemXpos=18;
private final int okNokXpos=40;
private final int sellItemXpos=60;
private final int textXpos=85;

BetterGuiMerchant (InventoryPlayer inv, GuiMerchant template, World world) {
super(inv, template.getMerchant(), world);
Expand All @@ -55,18 +61,21 @@ protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY)
MerchantRecipeList trades=getMerchant().getRecipes(null);
if (trades==null)
return;
int topAdjust=getTopAdjust(trades.size());
String s = trades.size()+" trades";
this.fontRenderer.drawString(s, this.xSize-addXSize+5, 0, 0xff00ff);
this.fontRenderer.drawString(s, this.xSize-addXSize+5, -topAdjust, 0xff00ff);
// First draw all items, then all tooltips. This is extra effort,
// but we don't want any items in front of any tooltips.
RenderHelper.enableStandardItemLighting();
RenderHelper.enableGUIStandardItemLighting();
for (int i=0; i<trades.size(); i++) {
MerchantRecipe trade=trades.get(i);
ItemStack i1=trade.getItemToBuy();
ItemStack i2=trade.hasSecondItemToBuy() ? trade.getSecondItemToBuy() : null;
ItemStack o1=trade.getItemToSell();
drawItem(i1, this.xSize-addXSize+5, i*18+20);
drawItem(i2, this.xSize-addXSize+5+18, i*18+20);
drawItem(o1, this.xSize-addXSize+5+60, i*18+20);
drawItem(i1, this.xSize-addXSize+5+firstBuyItemXpos, i*lineHeight-topAdjust+titleDistance);
drawItem(i2, this.xSize-addXSize+5+secondBuyItemXpos, i*lineHeight-topAdjust+titleDistance);
drawItem(o1, this.xSize-addXSize+5+sellItemXpos, i*lineHeight-topAdjust+titleDistance);

NBTTagList enchantments;

Expand All @@ -83,28 +92,37 @@ protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY)
int j = enchantments.getCompoundTagAt(t).getShort("id");
int k = enchantments.getCompoundTagAt(t).getShort("lvl");

if (Enchantment.getEnchantmentByID(j) != null)
Enchantment enchant = Enchantment.getEnchantmentByID(j);
if (enchant != null)
{
if (t>0)
enchants.append(", ");
enchants.append(Enchantment.getEnchantmentByID(j).getTranslatedName(k));
enchants.append(enchant.getTranslatedName(k));
}
}
fontRenderer.drawString(enchants.toString(), this.xSize-addXSize+85, i*18+24, 0xffff00);
fontRenderer.drawString(enchants.toString(), this.xSize-addXSize+textXpos, i*lineHeight-topAdjust+24, 0xffff00);
}
drawItem(trade.isRecipeDisabled() ? tradeNOK : tradeOK, xSize-addXSize+5+40, i*18+20);
drawItem(trade.isRecipeDisabled() ? tradeNOK : tradeOK, xSize-addXSize+5+okNokXpos, i*lineHeight-topAdjust+titleDistance);
}
RenderHelper.disableStandardItemLighting();
for (int i=0; i<trades.size(); i++) {
MerchantRecipe trade=trades.get(i);
ItemStack i1=trade.getItemToBuy();
ItemStack i2=trade.hasSecondItemToBuy() ? trade.getSecondItemToBuy() : null;
ItemStack o1=trade.getItemToSell();
drawTooltip(i1, this.xSize-addXSize+5, i*18+20, mouseX, mouseY);
drawTooltip(i2, this.xSize-addXSize+5+18, i*18+20, mouseX, mouseY);
drawTooltip(o1, this.xSize-addXSize+5+54, i*18+20, mouseX, mouseY);
drawTooltip(i1, this.xSize-addXSize+5+firstBuyItemXpos, i*lineHeight-topAdjust+titleDistance, mouseX, mouseY);
drawTooltip(i2, this.xSize-addXSize+5+secondBuyItemXpos, i*lineHeight-topAdjust+titleDistance, mouseX, mouseY);
drawTooltip(o1, this.xSize-addXSize+5+sellItemXpos, i*lineHeight-topAdjust+titleDistance, mouseX, mouseY);
}
}

private int getTopAdjust(int numTrades) {
int topAdjust = ((numTrades * lineHeight + titleDistance) - this.ySize)/2;
if (topAdjust < 0)
topAdjust = 0;
return topAdjust;
}

private void drawItem(ItemStack stack, int x, int y) {
if (stack==null)
return;
Expand All @@ -123,13 +141,19 @@ private void drawTooltip(ItemStack stack, int x, int y, int mousex, int mousey)

@Override
protected void mouseClicked(final int mouseX, final int mouseY, final int mouseButton) throws IOException {
System.out.println("click at "+mouseX+"/"+mouseY);
if ((mouseX - this.guiLeft) > this.xSize-addXSize && (mouseX - this.guiLeft) < this.xSize-addXSize+80) {
int tradeIndex=(mouseY-this.guiTop-20)/18;
// System.out.println("click at "+mouseX+"/"+mouseY);
if (mouseButton==0
&& (mouseX - this.guiLeft) > this.xSize-addXSize
&& (mouseX - this.guiLeft) < this.xSize-addXSize+textXpos
) {
MerchantRecipeList trades=getMerchant().getRecipes(null);
if (trades==null)
return;
int numTrades=trades.size();
int topAdjust=getTopAdjust(numTrades);
int tradeIndex=(mouseY+topAdjust-this.guiTop-titleDistance)/lineHeight;
if (tradeIndex>=0 && tradeIndex<numTrades) {
System.out.println("tradeIndex="+tradeIndex+", numTrades="+numTrades);
// System.out.println("tradeIndex="+tradeIndex+", numTrades="+numTrades);
GuiButton myNextButton = this.buttonList.get(0);
GuiButton myPrevButton = this.buttonList.get(1);
for (int i=0; i<numTrades; i++)
Expand Down Expand Up @@ -172,8 +196,8 @@ private boolean hasEnoughItemsInInventory(ItemStack stack) {
ItemStack invstack=inventorySlots.getSlot(i).getStack();
if (invstack==null)
continue;
if (stack.getItem().equals(invstack.getItem())) {
System.out.println("taking "+invstack.getCount()+" items from slot # "+i);
if (areItemStacksMergable(stack, invstack)) {
//System.out.println("taking "+invstack.getCount()+" items from slot # "+i);
remaining-=invstack.getCount();
}
if (remaining<=0)
Expand All @@ -187,11 +211,12 @@ private boolean canReceiveOutput(ItemStack stack) {
for (int i=inventorySlots.inventorySlots.size()-36; i<inventorySlots.inventorySlots.size(); i++) {
ItemStack invstack=inventorySlots.getSlot(i).getStack();
if (invstack==null || invstack.isEmpty()) {
System.out.println("can put result into empty slot "+i);
//System.out.println("can put result into empty slot "+i);
return true;
}
if (stack.getItem().equals(invstack.getItem())) {
System.out.println("Can merge "+(invstack.getMaxStackSize()-invstack.getCount())+" items with slot "+i);
if (areItemStacksMergable(stack, invstack)
&& stack.getMaxStackSize() >= stack.getCount() + invstack.getCount()) {
//System.out.println("Can merge "+(invstack.getMaxStackSize()-invstack.getCount())+" items with slot "+i);
remaining-=(invstack.getMaxStackSize()-invstack.getCount());
}
if (remaining<=0)
Expand All @@ -201,21 +226,21 @@ private boolean canReceiveOutput(ItemStack stack) {
}

private void transact(MerchantRecipe recipe) {
System.out.println("fill input slots called");
int putback0=-1, putback1=-1;
//System.out.println("fill input slots called");
int putback0, putback1=-1;
putback0=fillSlot(0, recipe.getItemToBuy());
if (recipe.hasSecondItemToBuy()) {
putback1=fillSlot(1, recipe.getSecondItemToBuy());
}
getslot(2, recipe.getItemToSell(), putback0, putback1);
System.out.println("putting back to slot "+putback0+" from 0, and to "+putback1+"from 1");
//System.out.println("putting back to slot "+putback0+" from 0, and to "+putback1+"from 1");
if (putback0!=-1) {
mc.playerController.windowClick(mc.player.openContainer.windowId, 0, 0, ClickType.PICKUP, mc.player);
mc.playerController.windowClick(mc.player.openContainer.windowId, putback0, 0, ClickType.PICKUP, mc.player);
slotClick(0);
slotClick(putback0);
}
if (putback1!=-1) {
mc.playerController.windowClick(mc.player.openContainer.windowId, 1, 0, ClickType.PICKUP, mc.player);
mc.playerController.windowClick(mc.player.openContainer.windowId, putback1, 0, ClickType.PICKUP, mc.player);
slotClick(1);
slotClick(putback1);
}
}

Expand All @@ -233,16 +258,16 @@ private int fillSlot(int slot, ItemStack stack) {
if (invstack==null)
continue;
boolean needPutBack=false;
if (stack.getItem().equals(invstack.getItem())) {
if (areItemStacksMergable(stack, invstack)) {
if (stack.getCount()+invstack.getCount() > stack.getMaxStackSize())
needPutBack=true;
remaining-=invstack.getCount();
System.out.println("taking "+invstack.getCount()+" items from slot # "+i+", remaining is now "+remaining);
mc.playerController.windowClick(mc.player.openContainer.windowId, i, 0, ClickType.PICKUP, mc.player);
mc.playerController.windowClick(mc.player.openContainer.windowId, slot, 0, ClickType.PICKUP, mc.player);
// System.out.println("taking "+invstack.getCount()+" items from slot # "+i+", remaining is now "+remaining);
slotClick(i);
slotClick(slot);
}
if (needPutBack) {
mc.playerController.windowClick(mc.player.openContainer.windowId, i, 0, ClickType.PICKUP, mc.player);
slotClick(i);
}
if (remaining<=0)
return remaining<0 ? i : -1;
Expand All @@ -252,18 +277,30 @@ private int fillSlot(int slot, ItemStack stack) {
return -1;
}

private boolean areItemStacksMergable(ItemStack a, ItemStack b) {
if (a==null || b==null)
return false;
if (a.getItem() == b.getItem()
&& (!a.getHasSubtypes() || a.getItemDamage()==b.getItemDamage())
&& ItemStack.areItemStackTagsEqual(a, b))
return true;
return false;
}

private void getslot(int slot, ItemStack stack, int... forbidden) {
int remaining=stack.getCount();
mc.playerController.windowClick(mc.player.openContainer.windowId, slot, 0, ClickType.PICKUP, mc.player);
slotClick(slot);
for (int i=inventorySlots.inventorySlots.size()-36; i<inventorySlots.inventorySlots.size(); i++) {
ItemStack invstack=inventorySlots.getSlot(i).getStack();
if (invstack==null || invstack.isEmpty()) {
continue;
}
if (stack.getItem().equals(invstack.getItem())) {
System.out.println("Can merge "+(invstack.getMaxStackSize()-invstack.getCount())+" items with slot "+i);
if (areItemStacksMergable(stack, invstack)
&& invstack.getCount() < invstack.getMaxStackSize()
) {
// System.out.println("Can merge "+(invstack.getMaxStackSize()-invstack.getCount())+" items with slot "+i);
remaining-=(invstack.getMaxStackSize()-invstack.getCount());
mc.playerController.windowClick(mc.player.openContainer.windowId, i, 0, ClickType.PICKUP, mc.player);
slotClick(i);
}
if (remaining<=0)
return;
Expand All @@ -280,10 +317,15 @@ private void getslot(int slot, ItemStack stack, int... forbidden) {
continue;
ItemStack invstack=inventorySlots.getSlot(i).getStack();
if (invstack==null || invstack.isEmpty()) {
mc.playerController.windowClick(mc.player.openContainer.windowId, i, 0, ClickType.PICKUP, mc.player);
System.out.println("putting result into empty slot "+i);
slotClick(i);
// System.out.println("putting result into empty slot "+i);
return;
}
}
}

private void slotClick(int slot) {
System.out.println("Clicking slot "+slot);
mc.playerController.windowClick(mc.player.openContainer.windowId, slot, 0, ClickType.PICKUP, mc.player);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public class EasierVillagerTrading {

public static final String MODID = "easiervillagertrading";
public static final String VERSION = "0.1";
public static final String VERSION = "1.01";

@EventHandler
public void init(FMLInitializationEvent event)
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/mcmod.info
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"description": "Less clicks to trade with villagers",
"version": "${version}",
"mcversion": "${mcversion}",
"url": "",
"url": "https://mods.curse.com/mc-mods/minecraft/261605-easiervillagertrading",
"updateUrl": "",
"authorList": ["Giselbaer"],
"credits": "",
Expand Down

0 comments on commit 3bdde09

Please sign in to comment.