This repository has been archived by the owner on Jan 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 709
/
Trading_tombailey94.js
191 lines (163 loc) · 6.68 KB
/
Trading_tombailey94.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// Trading mod by tombailey94; trades are *mostly* (lower values for count are used; e.g. 2 emeralds for leather cap not 2-3 emeralds and, also, trades involving items that are not in Pocket Edition are omitted) according to http://hydra-media.cursecdn.com/minecraft.gamepedia.com/1/1e/Trading-Chart.png
// version 0.0.4 (butcher and farmer implemented, trades for blacksmith, librarian and priest still to go)
var imgview;
var ctx;
var simpleGui;
var simpleGuiButton;
var trades = [
[["Emerald", 388, 1],["Steak", 364, 6]],
[["Emerald", 388, 1],["Cooked Porkchop", 320, 6]],
[["Emerald", 388, 2],["Leather Cap", 298, 1]],
[["Emerald", 388, 4],["Leather Tunic", 299, 1]],
[["Emerald", 388, 2],["Leather Pants", 300, 1]],
[["Emerald", 388, 2],["Leather Boots", 301, 1]],
[["Emerald", 388, 6],["Saddle", 329, 1]],
[["Raw Beef", 363, 14],["Emerald", 388, 1]],
[["Raw Porkchop", 319, 14],["Emerald", 388, 1]],
[["Coal", 263, 16],["Emerald", 388, 1]],
[["Gold Ingot", 266, 8],["Emerald", 388, 1]],
[["Emerald", 388, 1],["Apple", 260, 5]],
[["Emerald", 388, 1],["Bread", 297, 3]],
[["Emerald", 388, 1],["Cooked Chicken", 366, 7]],
[["Emerald", 388, 1],["Cookies", 357, 1]],
[["Emerald", 388, 1],["Melon", 360, 5]],
[["Emerald", 388, 1],["Arrow", 262, 9]],
[["Emerald", 388, 1],["Flint and Steel", 259, 1]],
[["Emerald", 388, 3],["Shears", 359, 1]],
[["Emerald", 388, 3],["Flint", 318, 4]],
[["Raw Chicken", 365, 14],["Emerald", 388, 1]],
[["Wheat", 296, 18],["Emerald", 388, 1]],
[["Wool", 35, 14],["Emerald", 388, 1]],
[["Gold Ignot", 266, 8],["Emerald", 388, 1]]
];
function newLevel() { //load world
ctx = com.mojang.minecraftpe.MainActivity.currentMainActivity.get();
}
function leaveGame() { //exit world
if(ctx!=null) {
ctx.runOnUiThread(new java.lang.Runnable({ run: function() {
try {
if(simpleGui != null) {
simpleGui.dismiss();
simpleGui = null;
}
if(simpleGuiButton != null) {
simpleGuiButton.dismiss();
simpleGuiButton = null;
}
} catch (err){
print("Error: " + err);
}
}}));
}
}
function showTradeGui(){
if (simpleGui == null) { //don't allow multiple simpleGuis to be opened
ctx.runOnUiThread(new java.lang.Runnable({ run: function() {
try {
simpleGui = new android.widget.PopupWindow();
var layout = new android.widget.LinearLayout(ctx);
layout.setBackgroundColor(android.graphics.Color.GRAY);
layout.setOrientation(android.widget.LinearLayout.VERTICAL);
var tradeText = new android.widget.TextView(ctx);
tradeText.setGravity(android.view.Gravity.CENTER);
var rnd = Math.floor(Math.random()*(trades.length)); //(pseudo) random number between 0 and trades.length-1 (inclusive; that means 0 and trades.length-1 could be the number returned)
tradeText.setText("For " + trades[rnd][0][2] + " " + trades[rnd][0][0] + " I will give you " + trades[rnd][1][2] + " " + trades[rnd][1][0]);
layout.addView(tradeText);
var tradeButton = new android.widget.Button(ctx);
tradeButton.setText("Trade with villager");
tradeButton.setOnClickListener(new android.view.View.OnClickListener() {
onClick: function(v) {
if (Level.getGameMode() == 1 || invContains(trades[rnd][0][1], trades[rnd][0][2])) { //if the player is in creative mode or has enough of the required item to trade
invDeduct(trades[rnd][0][1], trades[rnd][0][2]);
Player.addItemInventory(trades[rnd][1][1], trades[rnd][1][2], 0);
print("Trade complete!");
} else {
print("You don't have the items required to complete this trade.");
}
simpleGui.dismiss();
simpleGui = null;
}
});
layout.addView(tradeButton);
var closeButton = new android.widget.Button(ctx);
closeButton.setText("Close");
closeButton.setOnClickListener(new android.view.View.OnClickListener() {
onClick: function(v) {
simpleGui.dismiss();
simpleGui = null;
}
});
layout.addView(closeButton);
simpleGui.setHeight(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
simpleGui.setWidth(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
simpleGui.setContentView(layout);
simpleGui.showAtLocation(ctx.getWindow().getDecorView(), android.view.Gravity.CENTER, 0, 0);
} catch (err){
print("Error: "+err);
}
} }));
} else {
print("Cancel the previous trade first.");
}
}
function invContains(itemId, count) {
var hasItem = false; //presume the player doesn't have the item
for (var i = 0; i < 36; i++) {
if (Player.getInventorySlot(i) == itemId && Player.getInventorySlotCount(i) >= count) {
hasItem = true;
break; //break the loop early if the item is found and the count is good
}
}
return hasItem;
}
function invDeduct(itemId, count) {
var didDeduct = false; //presume we didn't deduct
for (var i = 0; i < 36; i++) {
if (Player.getInventorySlot(i) == itemId && Player.getInventorySlotCount(i) >= count) {
var remainder = Player.getInventorySlotCount(i) - count;
Player.clearInventorySlot(i);
Player.addItemInventory(itemId, remainder, 0);
//net.zhuoweizhang.mcpelauncher.ScriptManager.nativeSetInventorySlot(i, itemId, Player.getInventorySlotCount(i) - count, 0);
didDeduct = true;
break; //break the loop early if the item has been deducted
}
}
return didDeduct;
}
function modTick() {
if (Entity.getEntityTypeId(Player.getPointedEntity()) == 15) { //player is looking at a villager
if (simpleGui == null && simpleGuiButton == null) { //if gui isn't open
ctx.runOnUiThread(new java.lang.Runnable({ run: function() {
try {
simpleGuiButton = new android.widget.PopupWindow();
var layout = new android.widget.LinearLayout(ctx);
var tradeButton = new android.widget.Button(ctx);
tradeButton.setText("Trade");
tradeButton.setBackgroundColor(android.graphics.Color.GRAY);
tradeButton.setOnClickListener(new android.view.View.OnClickListener() {
onClick: function(v) {
showTradeGui();
simpleGuiButton.dismiss();
simpleGuiButton = null;
}
});
layout.addView(tradeButton);
simpleGuiButton.setHeight(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
simpleGuiButton.setWidth(android.widget.LinearLayout.LayoutParams.WRAP_CONTENT);
simpleGuiButton.setContentView(layout);
simpleGuiButton.showAtLocation(ctx.getWindow().getDecorView(), android.view.Gravity.CENTER||android.view.Gravity.BOTTOM, 0, 300);
} catch (err){
//print("Error: "+err); //modTick causes a spam of error messages
}
} }));
}
} else {
if (simpleGuiButton != null) {
ctx.runOnUiThread(new java.lang.Runnable({ run: function() {
simpleGuiButton.dismiss();
simpleGuiButton = null;
}}));
}
}
}