-
Notifications
You must be signed in to change notification settings - Fork 148
/
CommonProxy.java
411 lines (368 loc) · 13.6 KB
/
CommonProxy.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package com.flansmod.common;
import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import com.flansmod.common.driveables.ContainerDriveableInventory;
import com.flansmod.common.driveables.ContainerDriveableMenu;
import com.flansmod.common.driveables.DriveablePart;
import com.flansmod.common.driveables.DriveableType;
import com.flansmod.common.driveables.EntityDriveable;
import com.flansmod.common.driveables.EntitySeat;
import com.flansmod.common.driveables.EnumDriveablePart;
import com.flansmod.common.driveables.mechas.ContainerMechaInventory;
import com.flansmod.common.driveables.mechas.EntityMecha;
import com.flansmod.common.guns.ContainerGunModTable;
import com.flansmod.common.guns.boxes.ContainerGunBox;
import com.flansmod.common.guns.boxes.GunBoxType;
import com.flansmod.common.network.PacketBreakSound;
import com.flansmod.common.paintjob.ContainerPaintjobTable;
import com.flansmod.common.paintjob.TileEntityPaintjobTable;
import com.flansmod.common.parts.EnumPartCategory;
import com.flansmod.common.parts.ItemPart;
import com.flansmod.common.parts.PartType;
import com.flansmod.common.teams.ArmourBoxType;
import com.flansmod.common.types.EnumType;
import com.flansmod.common.types.InfoType;
import com.google.common.io.Files;
public class CommonProxy
{
protected static Pattern zipJar = Pattern.compile("(.+).(zip|jar)$");
public void CopyContentPacksFromModsFolder()
{
for(File file : FlansMod.modDir.listFiles())
{
if(file.isDirectory() || zipJar.matcher(file.getName()).matches())
{
if(file.getName().contains("Content") && file.getName().contains("Pack"))
{
try
{
Files.copy(file, new File(FlansMod.flanDir + "/" + file.getName()));
}
catch(Exception e)
{
FlansMod.log.error("Failed to copy content pack to Flan folder : " + file.getName());
FlansMod.log.throwing(e);
}
}
}
}
}
/**
* Returns the list of content pack files, and on the client, adds the content pack resources and models to the classpath
*/
public List<File> getContentList(Method method, ClassLoader classloader)
{
List<File> contentPacks = new ArrayList<>();
for(File file : FlansMod.flanDir.listFiles())
{
//Load folders and valid zip files
if(file.isDirectory() || zipJar.matcher(file.getName()).matches())
{
//Add the directory to the content pack list
FlansMod.log.info("Loaded content pack : " + file.getName());
contentPacks.add(file);
}
}
FlansMod.log.info("Loaded content pack list server side.");
return contentPacks;
}
public void addMissingJSONs(HashMap<Integer, InfoType> types)
{
}
/**
* A ton of client only methods follow
*/
public void preInit()
{
}
public void init()
{
}
public void forceReload()
{
}
public void registerRenderers()
{
}
public void doTutorialStuff(EntityPlayer player, EntityDriveable entityType)
{
}
public void changeControlMode(EntityPlayer player)
{
}
public boolean mouseControlEnabled()
{
return false;
}
public void openDriveableMenu(EntityPlayer player, World world, EntityDriveable driveable)
{
}
public <T> T loadModel(String s, String shortName, Class<T> typeClass)
{
return null;
}
public void loadSound(String contentPack, String type, String sound)
{
}
public boolean isThePlayer(EntityPlayer player)
{
return false;
}
public void buyGun(GunBoxType type, InfoType gun)
{
}
/**
* Gets the client GUI element from ClientProxy
*/
public Object getClientGui(int ID, EntityPlayer player, World world, int x, int y, int z)
{
return null;
}
/**
* Gets the container for the specified GUI
*/
public Container getServerGui(int ID, EntityPlayer player, World world, int x, int y, int z)
{
switch(ID)
{
case 0: return null; //Driveable crafting. No server side
case 1: return null; //Driveable repair. No server side
case 2: return new ContainerGunModTable(player.inventory, world);
case 3: return new ContainerDriveableMenu(player.inventory, world);
case 4: return new ContainerDriveableMenu(player.inventory, world, true, ((EntitySeat)player.getRidingEntity()).driveable);
case 5: return new ContainerGunBox(player.inventory);
//Plane inventory screens
case 6: return new ContainerDriveableInventory(player.inventory, world, ((EntitySeat)player.getRidingEntity()).driveable, 0);
case 7: return new ContainerDriveableInventory(player.inventory, world, ((EntitySeat)player.getRidingEntity()).driveable, 1);
case 8: return new ContainerDriveableMenu(player.inventory, world, true, ((EntitySeat)player.getRidingEntity()).driveable);
case 9: return new ContainerDriveableInventory(player.inventory, world, ((EntitySeat)player.getRidingEntity()).driveable, 2);
case 10: return new ContainerMechaInventory(player.inventory, world, (EntityMecha)((EntitySeat)player.getRidingEntity()).driveable);
case 11: return null; //Armour box. No server side
case 12: return new ContainerDriveableInventory(player.inventory, world, ((EntitySeat)player.getRidingEntity()).driveable, 3);
case 13: return new ContainerPaintjobTable(player.inventory, world, (TileEntityPaintjobTable)world.getTileEntity(new BlockPos(x, y, z)));
}
return null;
}
/**
* Play a block break sound here
*/
public void playBlockBreakSound(int x, int y, int z, Block blockHit)
{
FlansMod.packetHandler.sendToAll(new PacketBreakSound(x, y, z, blockHit));
}
public void craftDriveable(EntityPlayer player, DriveableType type)
{
//Create a temporary copy of the player inventory for backup purposes
InventoryPlayer temporaryInventory = new InventoryPlayer(null);
temporaryInventory.copyInventory(player.inventory);
//This becomes false if some recipe element is not found on the player
boolean canCraft = true;
//Iterate over rows then columns
for(ItemStack recipeStack : type.driveableRecipe)
{
//The total amount of items found that match this recipe stack
int totalAmountFound = 0;
//Iterate over the player's inventory
for(int n = 0; n < player.inventory.getSizeInventory(); n++)
{
//Get the stack in each slot
ItemStack stackInSlot = player.inventory.getStackInSlot(n).copy();
//If the stack is what we want
if(stackInSlot != null && stackInSlot.getItem() == recipeStack.getItem() && stackInSlot.getItemDamage() == recipeStack.getItemDamage())
{
//Work out the amount to take from the stack
int amountFound = Math.min(stackInSlot.getCount(), recipeStack.getCount() - totalAmountFound);
//Take it
stackInSlot.setCount(stackInSlot.getCount() - amountFound);
//Check for empty stacks
if(stackInSlot.getCount() <= 0)
stackInSlot = ItemStack.EMPTY.copy();
//Put the modified stack back in the inventory
player.inventory.setInventorySlotContents(n, stackInSlot);
//Increase the amount found counter
totalAmountFound += amountFound;
//If we have enough, stop looking
if(totalAmountFound == recipeStack.getCount())
break;
}
}
//If we didn't find enough, give the stack a red outline
if(totalAmountFound < recipeStack.getCount())
{
//For some reason, the player sent a craft packet, despite being unable to
canCraft = false;
break;
}
}
//Some item was missing. Restore inventory and return
if(!canCraft)
{
player.inventory.copyInventory(temporaryInventory);
return;
}
//Now we no longer need the temporary inventory backup, so we will use it to find the best stack of engines
//Collect up all the engines into neat and tidy stacks so we can find if any of them are big enough and which of those stacks are best
HashMap<PartType, ItemStack> engines = new HashMap<>();
//Find some suitable engines
for(int n = 0; n < temporaryInventory.getSizeInventory(); n++)
{
//Get the stack in each slot
ItemStack stackInSlot = temporaryInventory.getStackInSlot(n);
//Check to see if its a part
if(stackInSlot != null && stackInSlot.getItem() instanceof ItemPart)
{
PartType partType = ((ItemPart)stackInSlot.getItem()).type;
//Check its an engine
if(partType.category == EnumPartCategory.ENGINE && partType.worksWith.contains(EnumType.getFromObject(type)))
{
//If we already have engines of this type, add these ones to the stack
if(engines.containsKey(partType))
{
engines.get(partType).setCount(engines.get(partType).getCount() + stackInSlot.getCount());
}
//Else, make this the first stack
else engines.put(partType, stackInSlot);
}
}
}
//Find the stack of engines that is fastest but which also has enough for this driveable
float bestEngineSpeed = -1F;
ItemStack bestEngineStack = ItemStack.EMPTY.copy();
for(PartType part : engines.keySet())
{
//If this engine outperforms the currently selected best one and there are enough of them, swap
if(part.engineSpeed > bestEngineSpeed && engines.get(part).getCount() >= type.numEngines())
{
bestEngineSpeed = part.engineSpeed;
bestEngineStack = engines.get(part).copy();
}
}
//If the player doesn't have any suitable engines, return
if(bestEngineStack == null || bestEngineStack.isEmpty())
{
player.inventory.copyInventory(temporaryInventory);
return;
}
//Remove the engines from the inventory
int numEnginesAcquired = 0;
for(int n = 0; n < player.inventory.getSizeInventory(); n++)
{
//Get the stack in each slot
ItemStack stackInSlot = player.inventory.getStackInSlot(n);
//Check to see if its the engine we want
if(stackInSlot != null && !stackInSlot.isEmpty() && stackInSlot.getItem() == bestEngineStack.getItem())
{
//Work out the amount to take from the stack
int amountFound = Math.min(stackInSlot.getCount(), type.numEngines() - numEnginesAcquired);
//Take it
stackInSlot.setCount(stackInSlot.getCount() - amountFound);
//Check for empty stacks
if(stackInSlot.getCount() <= 0)
stackInSlot = ItemStack.EMPTY.copy();
//Put the modified stack back in the inventory
player.inventory.setInventorySlotContents(n, stackInSlot);
//Increase the amount found counter
numEnginesAcquired += amountFound;
//If we have enough, stop looking
if(numEnginesAcquired == type.numEngines())
break;
}
}
//Give them their brand new shiny driveable item :D
ItemStack driveableStack = new ItemStack(type.item);
NBTTagCompound tags = new NBTTagCompound();
tags.setString("Engine", ((ItemPart)bestEngineStack.getItem()).type.shortName);
tags.setString("Type", type.shortName);
for(EnumDriveablePart part : EnumDriveablePart.values())
{
tags.setInteger(part.getShortName() + "_Health", type.health.get(part) == null ? 0 : type.health.get(part).health);
tags.setBoolean(part.getShortName() + "_Fire", false);
}
driveableStack.setTagCompound(tags);
if(!player.inventory.addItemStackToInventory(driveableStack))
player.dropItem(driveableStack, false);
}
public void repairDriveable(EntityPlayer driver, EntityDriveable driving, DriveablePart part)
{
//If any of this parts parent parts are broken, then it cannot be repaired
for(EnumDriveablePart parent : part.type.getParents())
{
if(!driving.isPartIntact(parent))
return;
}
//Create a temporary copy of the player inventory for backup purposes
InventoryPlayer temporaryInventory = new InventoryPlayer(null);
temporaryInventory.copyInventory(driver.inventory);
//This becomes false if some recipe element is not found on the player
boolean canRepair = true;
//Get the array of stacks needed
ArrayList<ItemStack> stacksNeeded = driving.getDriveableType().getItemsRequired(part, driving.getDriveableData().engine);
//Draw the stacks that should be in each slot
for(ItemStack stackNeeded : stacksNeeded)
{
//The total amount of items found that match this recipe stack
int totalAmountFound = 0;
//Iterate over the temporary inventory
for(int m = 0; m < temporaryInventory.getSizeInventory(); m++)
{
//Get the stack in each slot
ItemStack stackInSlot = temporaryInventory.getStackInSlot(m).copy();
//If the stack is what we want
if(stackInSlot.getItem() == stackNeeded.getItem() && stackInSlot.getItemDamage() == stackNeeded.getItemDamage())
{
//Work out the amount to take from the stack
int amountFound = Math.min(stackInSlot.getCount(), stackNeeded.getCount() - totalAmountFound);
//Take it
stackInSlot.setCount(stackInSlot.getCount() - amountFound);
//Check for empty stacks
if(stackInSlot.getCount() <= 0)
stackInSlot = ItemStack.EMPTY.copy();
//Put the modified stack back in the inventory
temporaryInventory.setInventorySlotContents(m, stackInSlot);
//Increase the amount found counter
totalAmountFound += amountFound;
//If we have enough, stop looking
if(totalAmountFound == stackNeeded.getCount())
break;
}
}
if(totalAmountFound < stackNeeded.getCount())
canRepair = false;
}
if(canRepair)
{
driver.inventory.copyInventory(temporaryInventory);
part.health = Math.max(1, part.maxHealth / 10);
part.onFire = false;
part.dead = false;
driving.checkParts();
}
}
public boolean isScreenOpen()
{
return false;
}
public boolean isKeyDown(int key)
{
return false;
}
public boolean keyDown(int keycode)
{
return false;
}
public void buyArmour(String shortName, int piece, ArmourBoxType type)
{
}
}