/
WeaponSystem.java
272 lines (222 loc) · 7.4 KB
/
WeaponSystem.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
package smt.pack.weapon.registry;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import net.minecraft.world.World;
import net.minecraftforge.common.IExtendedEntityProperties;
import net.minecraftforge.common.util.Constants.NBT;
import smt.pack.weapon.SMTWeapon;
import smt.pack.weapon.item.ItemWeapon;
import smt.pack.weapon.registry.reloading.ReloadingDummy;
import smt.pack.weapon.registry.shooting.ShootingDummy;
public class WeaponSystem implements IExtendedEntityProperties {
public final static String TAG = SMTWeapon.MODID + ":WeaponSystem";
protected EntityPlayer player;
protected ItemStack weapon_current = null;
protected Weapon weapon = null;
protected Bullet bullet = null;
protected Magazine magazine = null;
protected Bullet[] course = null;
protected int number_course = 0;
protected FireMode fire_mode = FireMode.FUSE;
protected ShootingDummy shooting = null;
protected ReloadingDummy reloading = null;
private boolean isRight, isCenter, isReload, isAim;
public void onUpdate() {
//System.out.println("onUpdate Server, Side = " + (player.worldObj.isRemote ? "Client" : "Server"));
onRemake();
if (!(player.getHeldItem() != null && player.getHeldItem().getItem() instanceof ItemWeapon)) return;
shooting.onUpdate();
if (reloading != null) {
reloading.onUpdate();
if (reloading.getDelayReload() == -1) reloading = null;
}
}
public void onRemake() {
if (player.getHeldItem() != weapon_current) {
if (!ItemStack.areItemStacksEqual(player.getHeldItem(), weapon_current)) {
System.out.println("---------------------");
System.out.println("onRemake");
refresh(weapon_current);
weaponNull();
make(player.getHeldItem());
System.out.println("---------------------");
}
weapon_current = player.getHeldItem();
}
}
public void refresh(ItemStack itemStack) {
if (itemStack == null || !(itemStack.getItem() instanceof ItemWeapon)) return;
System.out.println("refresh - " + itemStack.toString());
if (!itemStack.hasTagCompound()) itemStack.setTagCompound(new NBTTagCompound());
NBTTagCompound nbt = itemStack.getTagCompound();
/*
if (weapon != null) {
String id = Reg.getWeaponID(weapon);
if (id != null) nbt.setString("WeaponID", id);
else if (nbt.hasKey("WeaponID")) nbt.removeTag("WeaponID");
} else if (nbt.hasKey("WeaponID")) nbt.removeTag("WeaponID");
*/
if (bullet != null) {
String id = Reg.getBulletID(bullet);
if (id != null) nbt.setString("BulletID", id);
else if (nbt.hasKey("BulletID")) nbt.removeTag("BulletID");
} else if (nbt.hasKey("BulletID")) nbt.removeTag("BulletID");
if (magazine != null) {
String id = Reg.getMagazineID(magazine);
if (id != null) nbt.setString("MagazineID", id);
else if (nbt.hasKey("MagazineID")) nbt.removeTag("MagazineID");
} else if (nbt.hasKey("MagazineID")) nbt.removeTag("MagazineID");
if (course != null && course.length > 0 && number_course >= 0) {
NBTTagList tag_list = new NBTTagList();
for (int i = 0; i <= number_course; ++i) {
String id = Reg.getBulletID(course[i]);
if (id != null) tag_list.appendTag(new NBTTagString(id));
}
nbt.setTag("Course", tag_list);
} else if (nbt.hasKey("Course")) nbt.removeTag("Course");
if (weapon.mode_fire != null && weapon.mode_fire.length > 0 && fire_mode != weapon.mode_fire[0]) {
for (int i = 0; i < weapon.mode_fire.length; ++i) {
if (weapon.mode_fire[i] == fire_mode) {
nbt.setByte("ModeFire", (byte)i);
break;
}
}
} else if (nbt.hasKey("ModeFire")) nbt.removeTag("ModeFire");
}
protected void make(ItemStack itemStack) {
if (itemStack == null || !(itemStack.getItem() instanceof ItemWeapon)) return;
System.out.println("make - " + itemStack.toString());
NBTTagCompound nbt = itemStack.getTagCompound();
if (nbt == null) return;
if (!nbt.hasKey("WeaponID", NBT.TAG_STRING)) return;
weapon = Reg.weapons.get(nbt.getString("WeaponID"));
if (nbt.hasKey("BulletID", NBT.TAG_STRING))
bullet = Reg.bullets.get(nbt.getString("BulletID"));
if (nbt.hasKey("MagazineID", NBT.TAG_STRING))
magazine = Reg.magazines.get(nbt.getString("MagazineID"));
if (nbt.hasKey("Course", NBT.TAG_LIST)) {
NBTTagList tag_list = nbt.getTagList("Course", NBT.TAG_STRING);
number_course = tag_list.tagCount() - 1;
course = new Bullet[tag_list.tagCount()];
for (int i = 0; i < tag_list.tagCount(); ++i)
course[i] = Reg.bullets.get(tag_list.getStringTagAt(i));
}
if (weapon.mode_fire != null && weapon.mode_fire.length > 0) {
if (nbt.hasKey("ModeFire", NBT.TAG_BYTE))
fire_mode = weapon.mode_fire[nbt.getByte("ModeFire")];
else
fire_mode = weapon.mode_fire[0];
}
shooting = Reg.getShooting(weapon.shooting, this, player);
}
protected void weaponNull() {
System.out.println("weaponNull");
weapon = null;
bullet = null;
magazine = null;
course = null;
number_course = -1;
fire_mode = FireMode.FUSE;
if (shooting != null) {
shooting.breaking();
shooting = null;
}
if (reloading != null) {
reloading.breaking();
reloading = null;
}
}
public void leftClick(boolean press) {
if (shooting != null)
shooting.leftClick(press);
}
public void rightClick(boolean press) {
isRight = press;
}
public void centerClick(int fireModeID) {
if (weapon == null) {
if (fire_mode != FireMode.FUSE) fire_mode = FireMode.FUSE;
return;
}
if (weapon.getModeFire() == null || weapon.getModeFire().length <= 1) {
fire_mode = weapon.getModeFire()[0];
return;
}
if (fireModeID == 0) {
for (int i = 0; i < weapon.getModeFire().length; ++i) {
if (weapon.getModeFire()[i] == fire_mode) {
if (i + 1 >= weapon.getModeFire().length) {
fire_mode = weapon.getModeFire()[0];
break;
} else {
fire_mode = weapon.getModeFire()[i + 1];
break;
}
}
}
} else {
fire_mode = weapon.getModeFire()[0];
}
}
public void reloadClick(int slotID) {
System.out.println("reloadClick");
if (weapon == null) return;
reloading = Reg.getReloading(weapon.reloading, this, player, slotID, weapon.delay_reload);
}
public ItemStack getWeaponCurrent() {
return weapon_current;
}
public Weapon getWeapon() {
return weapon;
}
public void setBullet(Bullet bullet) {
this.bullet = bullet;
}
public Bullet getBullet() {
return bullet;
}
public void setMagazine(Magazine magazine) {
this.magazine = magazine;
}
public Magazine getMagazine() {
return magazine;
}
public void setCourse(Bullet[] course) {
this.course = course;
}
public Bullet[] getCourse() {
return course;
}
public void setNumerCourse(int number_course) {
this.number_course = number_course;
}
public int getNumerCourse() {
return number_course;
}
public FireMode getFireMode() {
return fire_mode;
}
public void reg(EntityPlayer player) {
player.registerExtendedProperties(TAG, new WeaponSystem());
}
public WeaponSystem get(EntityPlayer player) {
return player != null ? (WeaponSystem)player.getExtendedProperties(TAG) : null;
}
@Override
public void init(Entity entity, World world) {
if (entity instanceof EntityPlayer) player = (EntityPlayer)entity;
}
@Override
public void saveNBTData(NBTTagCompound compound) {
//onRemake();
refresh(weapon_current);
}
@Override
public void loadNBTData(NBTTagCompound compound) {
//onRemake();
}
}