-
Notifications
You must be signed in to change notification settings - Fork 753
/
WeaponryHandler.java
286 lines (238 loc) · 11.9 KB
/
WeaponryHandler.java
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package tconstruct.weaponry;
import cpw.mods.fml.common.eventhandler.Event;
import cpw.mods.fml.common.gameevent.PlayerEvent;
import tconstruct.armor.player.TPlayerStats;
import tconstruct.tools.TinkerTools;
import tconstruct.weaponry.ammo.ArrowAmmo;
import tconstruct.weaponry.ammo.BoltAmmo;
import tconstruct.library.tools.DualMaterialToolPart;
import tconstruct.library.weaponry.ArrowShaftMaterial;
import tconstruct.library.weaponry.BowBaseAmmo;
import tconstruct.library.weaponry.ProjectileWeapon;
import tconstruct.library.weaponry.IAmmo;
import tconstruct.weaponry.weapons.Crossbow;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import tconstruct.library.TConstructRegistry;
import tconstruct.library.crafting.ToolBuilder;
import tconstruct.library.event.ToolBuildEvent;
import tconstruct.library.event.ToolCraftEvent;
import tconstruct.library.tools.*;
import tconstruct.weaponry.weapons.LongBow;
import tconstruct.weaponry.weapons.ShortBow;
public class WeaponryHandler {
@SubscribeEvent
public void onCrafting (PlayerEvent.ItemCraftedEvent event)
{
Item item = event.crafting.getItem();
if (!event.player.worldObj.isRemote)
{
if (item == Item.getItemFromBlock(TinkerTools.toolStationWood))
{
TPlayerStats stats = TPlayerStats.get(event.player);
if (!stats.weaponryManual)
{
stats.weaponryManual = true;
AbilityHelper.spawnItemAtPlayer(event.player, new ItemStack(TinkerTools.manualBook, 1, 4));
}
}
}
}
// Provides ammo-items with the necessary NBT
@SubscribeEvent
public void onAmmoCrafted(ToolCraftEvent.NormalTool event)
{
if(!(event.tool instanceof IAmmo))
return;
NBTTagCompound tags = event.toolTag.getCompoundTag("InfiTool");
// calculate its stats
if(event.tool instanceof ArrowAmmo)
{
// arrows work like this:
// the head is responsible for the damage, but also adds weight
// the shaft defines how fragile the arrow is, and also adds to the weight a bit. But mostly the fragility.
// the fletching defines the accuracy of the arrow and adds a bit breakchance. Mostly there for the durability modifier because availability
// Shortbows work better with lighter arrows
// while Longbows require a bit heavier arrows, the lighter the arrow the more impact the accuracy has otherwise
// summa sumarum: heavier arrows fall faster (less range) but accuracy has less impact
// the materials
ToolMaterial head = TConstructRegistry.getMaterial(tags.getInteger("Head"));
ArrowMaterial arrow = TConstructRegistry.getArrowMaterial(tags.getInteger("Head"));
ArrowShaftMaterial shaft = (ArrowShaftMaterial) TConstructRegistry.getCustomMaterial(tags.getInteger("Handle"), ArrowShaftMaterial.class);
FletchingMaterial fletching = (FletchingMaterial) TConstructRegistry.getCustomMaterial(tags.getInteger("Accessory"), FletchingMaterial.class);
// todo: fix leaf fletching
if(fletching == null)
fletching = (FletchingMaterial) TConstructRegistry.getCustomMaterial(tags.getInteger("Accessory"), FletchlingLeafMaterial.class);
// invalid materials
if(arrow == null || shaft == null || fletching == null)
{
event.setResult(Event.Result.DENY);
return;
}
int durability = (int)((float)head.durability() * shaft.durabilityModifier * fletching.durabilityModifier);
float weight = arrow.mass + shaft.weight;
float accuracy = fletching.accuracy;
float breakChance = shaft.fragility * arrow.breakChance + fletching.breakChance;
setAmmoData(tags, durability, weight, breakChance, accuracy, head.shoddy(), head.reinforced());
// Blaze shafts give fiery
if(tags.getInteger("Handle") == 3)
{
tags.setInteger("Fiery", 5);
}
// arrows get only 2 modifiers (so one less) by default
int mods = Math.max(0, tags.getInteger("Modifiers")-1);
tags.setInteger("Modifiers", mods);
}
else if(event.tool instanceof BoltAmmo)
{
// bolts work like ammos, but have more weight as they have 2 main materials
// Crossbows work better with heavier bolts
// the materials
ToolMaterial headMat = TConstructRegistry.getMaterial(tags.getInteger("Head"));
ToolMaterial coreMat = TConstructRegistry.getMaterial(tags.getInteger("Handle"));
ArrowMaterial head = TConstructRegistry.getArrowMaterial(tags.getInteger("Head"));
ArrowMaterial core = TConstructRegistry.getArrowMaterial(tags.getInteger("Handle"));
FletchingMaterial fletching = (FletchingMaterial) TConstructRegistry.getCustomMaterial(tags.getInteger("Accessory"), FletchingMaterial.class);
// todo: fix leaf fletching
if(fletching == null)
fletching = (FletchingMaterial) TConstructRegistry.getCustomMaterial(tags.getInteger("Accessory"), FletchlingLeafMaterial.class);
// invalid materials
if(head == null || core == null || fletching == null)
{
event.setResult(Event.Result.DENY);
return;
}
int durability = (int)((float)headMat.durability() * coreMat.handleDurability() * fletching.durabilityModifier);
float weight = head.mass + core.mass*1.5f;
float accuracy = (100f + fletching.accuracy)/2f;
float breakChance = (fletching.breakChance*2 + 0.15f * core.breakChance) * head.breakChance/2f;
float shoddy = (headMat.shoddy() + coreMat.shoddy())/2f;
int reinforced = Math.max(headMat.reinforced(), coreMat.reinforced());
setAmmoData(tags, durability, weight, breakChance, accuracy, shoddy, reinforced);
int mods = Math.max(0, tags.getInteger("Modifiers")-1);
tags.setInteger("Modifiers", mods);
}
// now that durability has been handled...
// fill the ammo full and at the same time provide the missing NBT tag
IAmmo ammoItem = (IAmmo) event.tool;
tags.setInteger("Ammo", ammoItem.getMaxAmmo(tags));
}
@SubscribeEvent
public void onProjectileWeaponCrafted(ToolCraftEvent.NormalTool event)
{
if(!(event.tool instanceof ProjectileWeapon))
return;
NBTTagCompound tags = event.toolTag.getCompoundTag("InfiTool");
ProjectileWeapon weapon = (ProjectileWeapon)event.tool;
int drawSpeed = 0;
float flightSpeed = 0;
BowMaterial top;
BowMaterial bottom;
BowstringMaterial string;
boolean enchanted = false;
if(event.tool instanceof BowBaseAmmo) {
top = TConstructRegistry.getBowMaterial(tags.getInteger("Head"));
bottom = TConstructRegistry.getBowMaterial(tags.getInteger("Accessory"));
string = (BowstringMaterial) TConstructRegistry.getCustomMaterial(tags.getInteger("Handle"), BowstringMaterial.class);
// some materials seem to be incompatible
if(top == null || bottom == null || string == null)
{
event.setResult(Event.Result.DENY);
return;
}
drawSpeed = (int) ((top.drawspeed + bottom.drawspeed) / 2f * string.drawspeedModifier);
flightSpeed = (top.flightSpeedMax + bottom.flightSpeedMax)/2 * string.flightSpeedModifier;
// SHORT bows have a SHORTER windup because they're SHORT. hahahaha.... get it?
if(event.tool instanceof ShortBow) {
drawSpeed *= 0.9;
}
// longbows have LONGER drawspeed. LOGIC!
if(event.tool instanceof LongBow) {
drawSpeed *= 1.8f;
flightSpeed *= 1.5f;
}
// enchanted bowstring
enchanted = tags.getInteger("Handle") == 1;
}
else if(event.tool instanceof Crossbow)
{
top = TConstructRegistry.getBowMaterial(tags.getInteger("Head"));
string = (BowstringMaterial) TConstructRegistry.getCustomMaterial(tags.getInteger("Accessory"), BowstringMaterial.class);
// some materials seem to be incompatible
if(top == null || string == null)
{
event.setResult(Event.Result.DENY);
return;
}
drawSpeed = (int) ((float)top.drawspeed * string.drawspeedModifier);
flightSpeed = (top.flightSpeedMax * string.flightSpeedModifier);
// crossbows are more efficient the higher the base drawspeed is. So the higher the drawspeed, the more bonus you get
drawSpeed *= 2.5f;
drawSpeed -= drawSpeed*0.25f;
// crossbows are stronk
flightSpeed *= 1.5;
// enchanted bowstring
enchanted = tags.getInteger("Accessory") == 1;
}
else
return;
// enchanted fabric
if (enchanted) {
int modifiers = tags.getInteger("Modifiers");
modifiers += 1;
tags.setInteger("Modifiers", modifiers);
}
tags.setInteger("DrawSpeed", drawSpeed);
tags.setInteger("BaseDrawSpeed", drawSpeed); // used to calculate correct speed increase with redstone modifier
tags.setFloat("FlightSpeed", flightSpeed);
}
// arrows use custom materials. But we don't allow the creation of those items
// we therefore replace the items with their toolpart counterparts here
@SubscribeEvent
public void buildArrow(ToolBuildEvent event)
{
if(event.headStack == null || event.handleStack == null || event.accessoryStack == null)
return;
// are we building an arrow?
CustomMaterial mat = TConstructRegistry.getCustomMaterial(event.handleStack, ArrowShaftMaterial.class);
if(mat == null)
return;
Item extra = event.extraStack != null ? event.extraStack.getItem() : null;
ToolCore tool = ToolBuilder.instance.getMatchingRecipe(event.headStack.getItem(), mat.craftingItem.getItem(), event.accessoryStack.getItem(), extra);
// it's an arrow!
if(tool == TinkerWeaponry.arrowAmmo)
event.handleStack = mat.craftingItem.copy();
}
// bolts require special treatment because of their dual-material cores
@SubscribeEvent
public void buildBolt(ToolBuildEvent event)
{
if(event.headStack == null || event.handleStack == null)
return;
if(event.headStack.getItem() != TinkerWeaponry.partBolt)
return;
// split the bolt into its two parts
ItemStack bolt1 = event.headStack.copy();
ItemStack bolt2 = event.headStack;
ItemStack fletching = event.handleStack;
// set the correct material on the 2nd part
DualMaterialToolPart dualPart = (DualMaterialToolPart) bolt2.getItem();
bolt1.setItemDamage(dualPart.getMaterialID2(bolt1));
// update the part positions xX
event.headStack = bolt1;
event.handleStack = bolt2;
event.accessoryStack = fletching;
}
private void setAmmoData(NBTTagCompound tags, int durability, float weight, float breakChance, float accuracy, float shoddy, int reinforced)
{
tags.setInteger("TotalDurability", durability);
tags.setInteger("BaseDurability", durability);
tags.setFloat("Mass", weight);
tags.setFloat("BreakChance", breakChance);
tags.setFloat("Accuracy", accuracy);
tags.setFloat("Shoddy", shoddy); // we could actually always set this to 0 since it has zero impact on ammo
tags.setInteger("Unbreaking", reinforced);
}
}