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

Commit 649a40a

Browse files
committed
first version
0 parents  commit 649a40a

File tree

6 files changed

+433
-0
lines changed

6 files changed

+433
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.gradle/
2+
.nb-gradle/
3+
build/
4+
gradle/
5+
gradlew

build.gradle

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
buildscript {
2+
repositories {
3+
jcenter()
4+
maven { url = "http://files.minecraftforge.net/maven" }
5+
}
6+
dependencies {
7+
classpath 'net.minecraftforge.gradle:ForgeGradle:2.2-SNAPSHOT'
8+
}
9+
}
10+
apply plugin: 'net.minecraftforge.gradle.forge'
11+
//Only edit below this line, the above code adds and enables the necessary things for Forge to be setup.
12+
13+
14+
version = "1.0"
15+
group = "de.guntram.mcmod.easiervillagertrading" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
16+
archivesBaseName = "easiervillagertrading"
17+
18+
sourceCompatibility = targetCompatibility = "1.6" // Need this here so eclipse task generates correctly.
19+
compileJava {
20+
sourceCompatibility = targetCompatibility = "1.6"
21+
}
22+
23+
minecraft {
24+
version = "1.11.2-13.20.0.2231"
25+
runDir = "run"
26+
27+
// the mappings can be changed at any time, and must be in the following format.
28+
// snapshot_YYYYMMDD snapshot are built nightly.
29+
// stable_# stables are built at the discretion of the MCP team.
30+
// Use non-default mappings at your own risk. they may not always work.
31+
// simply re-run your setup task after changing the mappings to update your workspace.
32+
mappings = "snapshot_20170222"
33+
// makeObfSourceJar = false // an Srg named sources jar is made by default. uncomment this to disable.
34+
}
35+
36+
dependencies {
37+
// you may put jars on which you depend on in ./libs
38+
// or you may define them like so..
39+
//compile "some.group:artifact:version:classifier"
40+
//compile "some.group:artifact:version"
41+
42+
// real examples
43+
//compile 'com.mod-buildcraft:buildcraft:6.0.8:dev' // adds buildcraft to the dev env
44+
//compile 'com.googlecode.efficient-java-matrix-library:ejml:0.24' // adds ejml to the dev env
45+
46+
// the 'provided' configuration is for optional dependencies that exist at compile-time but might not at runtime.
47+
//provided 'com.mod-buildcraft:buildcraft:6.0.8:dev'
48+
49+
// the deobf configurations: 'deobfCompile' and 'deobfProvided' are the same as the normal compile and provided,
50+
// except that these dependencies get remapped to your current MCP mappings
51+
//deobfCompile 'com.mod-buildcraft:buildcraft:6.0.8:dev'
52+
//deobfProvided 'com.mod-buildcraft:buildcraft:6.0.8:dev'
53+
54+
// for more info...
55+
// http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html
56+
// http://www.gradle.org/docs/current/userguide/dependency_management.html
57+
58+
}
59+
60+
processResources {
61+
// this will ensure that this task is redone when the versions change.
62+
inputs.property "version", project.version
63+
inputs.property "mcversion", project.minecraft.version
64+
65+
// replace stuff in mcmod.info, nothing else
66+
from(sourceSets.main.resources.srcDirs) {
67+
include 'mcmod.info'
68+
69+
// replace version and mcversion
70+
expand 'version':project.version, 'mcversion':project.minecraft.version
71+
}
72+
73+
// copy everything else except the mcmod.info
74+
from(sourceSets.main.resources.srcDirs) {
75+
exclude 'mcmod.info'
76+
}
77+
}
Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,289 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package de.guntram.mcmod.easiervillagertrading;
7+
8+
import java.io.IOException;
9+
import java.util.List;
10+
import net.minecraft.client.gui.GuiButton;
11+
import net.minecraft.client.gui.GuiMerchant;
12+
import net.minecraft.enchantment.Enchantment;
13+
import net.minecraft.entity.player.InventoryPlayer;
14+
import net.minecraft.inventory.ClickType;
15+
import net.minecraft.inventory.Slot;
16+
import net.minecraft.item.Item;
17+
import net.minecraft.item.ItemEnchantedBook;
18+
import net.minecraft.item.ItemStack;
19+
import net.minecraft.nbt.NBTTagList;
20+
import net.minecraft.village.MerchantRecipe;
21+
import net.minecraft.village.MerchantRecipeList;
22+
import net.minecraft.world.World;
23+
24+
/**
25+
*
26+
* @author gbl
27+
*/
28+
public class BetterGuiMerchant extends GuiMerchant {
29+
30+
private final int addXSize=0;
31+
private final ItemStack tradeOK, tradeNOK;
32+
33+
BetterGuiMerchant (InventoryPlayer inv, GuiMerchant template, World world) {
34+
super(inv, template.getMerchant(), world);
35+
this.xSize+=addXSize;
36+
tradeOK=new ItemStack(Item.getItemById(351), 1, 2);
37+
tradeNOK=new ItemStack(Item.getItemById(351), 1, 1);
38+
}
39+
40+
@Override
41+
protected void drawGuiContainerBackgroundLayer(float partialTicks, int mouseX, int mouseY) {
42+
super.drawGuiContainerBackgroundLayer(partialTicks, mouseX, mouseY);
43+
}
44+
45+
@Override
46+
public void drawScreen(int mouseX, int mouseY, float partialTicks) {
47+
super.drawScreen(mouseX, mouseY, partialTicks);
48+
49+
}
50+
51+
@Override
52+
protected void drawGuiContainerForegroundLayer(int mouseX, int mouseY)
53+
{
54+
super.drawGuiContainerForegroundLayer(mouseX, mouseY);
55+
MerchantRecipeList trades=getMerchant().getRecipes(null);
56+
if (trades==null)
57+
return;
58+
String s = trades.size()+" trades";
59+
this.fontRenderer.drawString(s, this.xSize-addXSize+5, 0, 0xff00ff);
60+
// First draw all items, then all tooltips. This is extra effort,
61+
// but we don't want any items in front of any tooltips.
62+
for (int i=0; i<trades.size(); i++) {
63+
MerchantRecipe trade=trades.get(i);
64+
ItemStack i1=trade.getItemToBuy();
65+
ItemStack i2=trade.hasSecondItemToBuy() ? trade.getSecondItemToBuy() : null;
66+
ItemStack o1=trade.getItemToSell();
67+
drawItem(i1, this.xSize-addXSize+5, i*18+20);
68+
drawItem(i2, this.xSize-addXSize+5+18, i*18+20);
69+
drawItem(o1, this.xSize-addXSize+5+60, i*18+20);
70+
71+
NBTTagList enchantments;
72+
73+
if (o1.getItem() instanceof ItemEnchantedBook) {
74+
enchantments=((ItemEnchantedBook)(o1.getItem())).getEnchantments(o1);
75+
} else {
76+
enchantments=o1.getEnchantmentTagList();
77+
}
78+
if (enchantments != null)
79+
{
80+
StringBuilder enchants=new StringBuilder();
81+
for (int t = 0; t < enchantments.tagCount(); ++t)
82+
{
83+
int j = enchantments.getCompoundTagAt(t).getShort("id");
84+
int k = enchantments.getCompoundTagAt(t).getShort("lvl");
85+
86+
if (Enchantment.getEnchantmentByID(j) != null)
87+
{
88+
if (t>0)
89+
enchants.append(", ");
90+
enchants.append(Enchantment.getEnchantmentByID(j).getTranslatedName(k));
91+
}
92+
}
93+
fontRenderer.drawString(enchants.toString(), this.xSize-addXSize+85, i*18+24, 0xffff00);
94+
}
95+
drawItem(trade.isRecipeDisabled() ? tradeNOK : tradeOK, xSize-addXSize+5+40, i*18+20);
96+
}
97+
for (int i=0; i<trades.size(); i++) {
98+
MerchantRecipe trade=trades.get(i);
99+
ItemStack i1=trade.getItemToBuy();
100+
ItemStack i2=trade.hasSecondItemToBuy() ? trade.getSecondItemToBuy() : null;
101+
ItemStack o1=trade.getItemToSell();
102+
drawTooltip(i1, this.xSize-addXSize+5, i*18+20, mouseX, mouseY);
103+
drawTooltip(i2, this.xSize-addXSize+5+18, i*18+20, mouseX, mouseY);
104+
drawTooltip(o1, this.xSize-addXSize+5+54, i*18+20, mouseX, mouseY);
105+
}
106+
}
107+
108+
private void drawItem(ItemStack stack, int x, int y) {
109+
if (stack==null)
110+
return;
111+
itemRender.renderItemAndEffectIntoGUI(stack, x, y);
112+
itemRender.renderItemOverlays(fontRenderer, stack, x, y);
113+
}
114+
115+
private void drawTooltip(ItemStack stack, int x, int y, int mousex, int mousey) {
116+
if (stack==null)
117+
return;
118+
mousex-=guiLeft;
119+
mousey-=guiTop;
120+
if (mousex>=x && mousex<=x+16 && mousey>=y && mousey<=y+16)
121+
renderToolTip(stack, mousex, mousey);
122+
}
123+
124+
@Override
125+
protected void mouseClicked(final int mouseX, final int mouseY, final int mouseButton) throws IOException {
126+
System.out.println("click at "+mouseX+"/"+mouseY);
127+
if ((mouseX - this.guiLeft) > this.xSize-addXSize && (mouseX - this.guiLeft) < this.xSize-addXSize+80) {
128+
int tradeIndex=(mouseY-this.guiTop-20)/18;
129+
MerchantRecipeList trades=getMerchant().getRecipes(null);
130+
int numTrades=trades.size();
131+
if (tradeIndex>=0 && tradeIndex<numTrades) {
132+
System.out.println("tradeIndex="+tradeIndex+", numTrades="+numTrades);
133+
GuiButton myNextButton = this.buttonList.get(0);
134+
GuiButton myPrevButton = this.buttonList.get(1);
135+
for (int i=0; i<numTrades; i++)
136+
this.actionPerformed(myPrevButton);
137+
for (int i=0; i<tradeIndex; i++)
138+
this.actionPerformed(myNextButton);
139+
MerchantRecipe recipe=trades.get(tradeIndex);
140+
if (!recipe.isRecipeDisabled()
141+
&& inputSlotsAreEmpty()
142+
&& hasEnoughItemsInInventory(recipe)
143+
&& canReceiveOutput(recipe.getItemToSell())) {
144+
transact(recipe);
145+
}
146+
147+
}
148+
} else {
149+
super.mouseClicked(mouseX, mouseY, mouseButton);
150+
}
151+
}
152+
153+
private boolean inputSlotsAreEmpty() {
154+
return
155+
inventorySlots.getSlot(0).getHasStack() == false
156+
&& inventorySlots.getSlot(1).getHasStack() == false
157+
&& inventorySlots.getSlot(2).getHasStack() == false;
158+
159+
}
160+
161+
private boolean hasEnoughItemsInInventory(MerchantRecipe recipe) {
162+
if (!hasEnoughItemsInInventory(recipe.getItemToBuy()))
163+
return false;
164+
if (recipe.hasSecondItemToBuy() && !hasEnoughItemsInInventory(recipe.getSecondItemToBuy()))
165+
return false;
166+
return true;
167+
}
168+
169+
private boolean hasEnoughItemsInInventory(ItemStack stack) {
170+
int remaining=stack.getCount();
171+
for (int i=inventorySlots.inventorySlots.size()-36; i<inventorySlots.inventorySlots.size(); i++) {
172+
ItemStack invstack=inventorySlots.getSlot(i).getStack();
173+
if (invstack==null)
174+
continue;
175+
if (stack.getItem().equals(invstack.getItem())) {
176+
System.out.println("taking "+invstack.getCount()+" items from slot # "+i);
177+
remaining-=invstack.getCount();
178+
}
179+
if (remaining<=0)
180+
return true;
181+
}
182+
return false;
183+
}
184+
185+
private boolean canReceiveOutput(ItemStack stack) {
186+
int remaining=stack.getCount();
187+
for (int i=inventorySlots.inventorySlots.size()-36; i<inventorySlots.inventorySlots.size(); i++) {
188+
ItemStack invstack=inventorySlots.getSlot(i).getStack();
189+
if (invstack==null || invstack.isEmpty()) {
190+
System.out.println("can put result into empty slot "+i);
191+
return true;
192+
}
193+
if (stack.getItem().equals(invstack.getItem())) {
194+
System.out.println("Can merge "+(invstack.getMaxStackSize()-invstack.getCount())+" items with slot "+i);
195+
remaining-=(invstack.getMaxStackSize()-invstack.getCount());
196+
}
197+
if (remaining<=0)
198+
return true;
199+
}
200+
return false;
201+
}
202+
203+
private void transact(MerchantRecipe recipe) {
204+
System.out.println("fill input slots called");
205+
int putback0=-1, putback1=-1;
206+
putback0=fillSlot(0, recipe.getItemToBuy());
207+
if (recipe.hasSecondItemToBuy()) {
208+
putback1=fillSlot(1, recipe.getSecondItemToBuy());
209+
}
210+
getslot(2, recipe.getItemToSell(), putback0, putback1);
211+
System.out.println("putting back to slot "+putback0+" from 0, and to "+putback1+"from 1");
212+
if (putback0!=-1) {
213+
mc.playerController.windowClick(mc.player.openContainer.windowId, 0, 0, ClickType.PICKUP, mc.player);
214+
mc.playerController.windowClick(mc.player.openContainer.windowId, putback0, 0, ClickType.PICKUP, mc.player);
215+
}
216+
if (putback1!=-1) {
217+
mc.playerController.windowClick(mc.player.openContainer.windowId, 1, 0, ClickType.PICKUP, mc.player);
218+
mc.playerController.windowClick(mc.player.openContainer.windowId, putback1, 0, ClickType.PICKUP, mc.player);
219+
}
220+
}
221+
222+
/**
223+
*
224+
* @param slot - the number of the (trading) slot that should receive items
225+
* @param stack - what the trading slot should receive
226+
* @return the number of the inventory slot into which these items should be put back
227+
* after the transaction. May be -1 if nothing needs to be put back.
228+
*/
229+
private int fillSlot(int slot, ItemStack stack) {
230+
int remaining=stack.getCount();
231+
for (int i=inventorySlots.inventorySlots.size()-36; i<inventorySlots.inventorySlots.size(); i++) {
232+
ItemStack invstack=inventorySlots.getSlot(i).getStack();
233+
if (invstack==null)
234+
continue;
235+
boolean needPutBack=false;
236+
if (stack.getItem().equals(invstack.getItem())) {
237+
if (stack.getCount()+invstack.getCount() > stack.getMaxStackSize())
238+
needPutBack=true;
239+
remaining-=invstack.getCount();
240+
System.out.println("taking "+invstack.getCount()+" items from slot # "+i+", remaining is now "+remaining);
241+
mc.playerController.windowClick(mc.player.openContainer.windowId, i, 0, ClickType.PICKUP, mc.player);
242+
mc.playerController.windowClick(mc.player.openContainer.windowId, slot, 0, ClickType.PICKUP, mc.player);
243+
}
244+
if (needPutBack) {
245+
mc.playerController.windowClick(mc.player.openContainer.windowId, i, 0, ClickType.PICKUP, mc.player);
246+
}
247+
if (remaining<=0)
248+
return remaining<0 ? i : -1;
249+
}
250+
// We should not be able to arrive here, since hasEnoughItemsInInventory should have been
251+
// called before fillSlot. But if we do, something went wrong; in this case better do a bit less.
252+
return -1;
253+
}
254+
255+
private void getslot(int slot, ItemStack stack, int... forbidden) {
256+
int remaining=stack.getCount();
257+
mc.playerController.windowClick(mc.player.openContainer.windowId, slot, 0, ClickType.PICKUP, mc.player);
258+
for (int i=inventorySlots.inventorySlots.size()-36; i<inventorySlots.inventorySlots.size(); i++) {
259+
ItemStack invstack=inventorySlots.getSlot(i).getStack();
260+
if (invstack==null || invstack.isEmpty()) {
261+
continue;
262+
}
263+
if (stack.getItem().equals(invstack.getItem())) {
264+
System.out.println("Can merge "+(invstack.getMaxStackSize()-invstack.getCount())+" items with slot "+i);
265+
remaining-=(invstack.getMaxStackSize()-invstack.getCount());
266+
mc.playerController.windowClick(mc.player.openContainer.windowId, i, 0, ClickType.PICKUP, mc.player);
267+
}
268+
if (remaining<=0)
269+
return;
270+
}
271+
272+
// When looking for an empty slot, don't take one that we want to put some input back to.
273+
for (int i=inventorySlots.inventorySlots.size()-36; i<inventorySlots.inventorySlots.size(); i++) {
274+
boolean isForbidden=false;
275+
for (int f:forbidden) {
276+
if (i==f)
277+
isForbidden=true;
278+
}
279+
if (isForbidden)
280+
continue;
281+
ItemStack invstack=inventorySlots.getSlot(i).getStack();
282+
if (invstack==null || invstack.isEmpty()) {
283+
mc.playerController.windowClick(mc.player.openContainer.windowId, i, 0, ClickType.PICKUP, mc.player);
284+
System.out.println("putting result into empty slot "+i);
285+
return;
286+
}
287+
}
288+
}
289+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package de.guntram.mcmod.easiervillagertrading;
2+
3+
import net.minecraftforge.common.MinecraftForge;
4+
import net.minecraftforge.fml.common.Mod;
5+
import net.minecraftforge.fml.common.Mod.EventHandler;
6+
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
7+
8+
@Mod(modid = EasierVillagerTrading.MODID, version = EasierVillagerTrading.VERSION)
9+
public class EasierVillagerTrading {
10+
11+
public static final String MODID = "easiervillagertrading";
12+
public static final String VERSION = "0.1";
13+
14+
@EventHandler
15+
public void init(FMLInitializationEvent event)
16+
{
17+
MinecraftForge.EVENT_BUS.register(OpenTradeEventHandler.getInstance());
18+
}
19+
}

0 commit comments

Comments
 (0)