-
Notifications
You must be signed in to change notification settings - Fork 753
/
LandmineExplodeLogic.java
156 lines (136 loc) · 5.06 KB
/
LandmineExplodeLogic.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
package tconstruct.blocks.logic;
import java.util.ArrayList;
import java.util.Iterator;
import mantle.world.WorldHelper;
import net.minecraft.entity.Entity;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import tconstruct.util.landmine.behavior.Behavior;
/**
* This logic is designed only for blocks having TileEntityLandmine as their
* tileEntity(otherwise ClassCastException will be awarded)
*
* @author fuj1n
*
*/
public class LandmineExplodeLogic
{
private final World worldObj;
private final Entity triggerer;
private final TileEntityLandmine tileEntity;
private final int x, y, z;
public LandmineExplodeLogic(World par1World, int par2, int par3, int par4, Entity entity)
{
worldObj = par1World;
this.tileEntity = (TileEntityLandmine) par1World.getTileEntity(par2, par3, par4);
this.x = par2;
this.y = par3;
this.z = par4;
this.triggerer = entity;
}
public void explode ()
{
if (triggerer == null)
{
return;
}
boolean preventExplode = false;
boolean isOffensive = true;
boolean cancelDefault = false;
ArrayList<ItemStack> stacks = new ArrayList<ItemStack>();
boolean hasExploded = false;
if (tileEntity == null)
{
return;
}
tileEntity.isExploding = true;
for (int i = 0; i < tileEntity.getSizeTriggerInventory(); i++)
{
ItemStack currentStack = tileEntity.getStackInSlot(i);
Behavior b = Behavior.getBehaviorFromStack(currentStack);
if (b != null)
{
if (b.doesBehaviorPreventRemovalOfBlock(currentStack))
{
preventExplode = true;
}
if (!b.isBehaviorExchangableWithOffensive(currentStack))
{
isOffensive = false;
}
if (b.overridesDefault())
{
cancelDefault = true;
}
if (!stacks.isEmpty() && Behavior.arrayContainsEqualStack(stacks, currentStack) && b.effectStacks())
{
stacks.get(Behavior.arrayIndexOfStack(stacks, currentStack)).stackSize += currentStack.stackSize;
}
else
{
stacks.add(currentStack.copy());
}
}
}
LandmineSpecialStackLogic specialStacks = new LandmineSpecialStackLogic(worldObj, x, y, z, triggerer, isOffensive, stacks);
specialStacks.handleSpecialStacks();
Iterator<ItemStack> i1 = stacks.iterator();
while (i1.hasNext())
{
ItemStack currentStack = i1.next();
Behavior b = Behavior.getBehaviorFromStack(currentStack);
if (b != null)
{
if (isOffensive || !b.isOffensive(currentStack))
{
b.executeLogic(worldObj, x, y, z, currentStack, triggerer, !preventExplode);
if (b.shouldItemBeRemoved(currentStack, !preventExplode))
{
if (b.effectStacks())
{
for (int i = 0; i < tileEntity.getSizeTriggerInventory(); i++)
{
if (tileEntity.getStackInSlot(i) != null)
{
if (tileEntity.getStackInSlot(i).isItemEqual(currentStack))
{
tileEntity.setInventorySlotContents(i, null);
}
}
}
}
else
{
boolean hasRemoved = false;
for (int i = 0; i < tileEntity.getSizeTriggerInventory() && !hasRemoved; i++)
{
if (tileEntity.getStackInSlot(i) != null)
{
if (tileEntity.getStackInSlot(i).isItemEqual(currentStack))
{
tileEntity.setInventorySlotContents(i, null);
hasRemoved = true;
}
}
}
}
}
}
}
}
Behavior defBeh = Behavior.getDefaulBehavior();
if (defBeh != null && isOffensive && !cancelDefault)
{
defBeh.executeLogic(worldObj, x, y, z, null, triggerer, !preventExplode);
}
if (hasExploded || defBeh != null && !preventExplode)
{
worldObj.removeTileEntity(x, y, z);
WorldHelper.setBlockToAir(worldObj, x, y, z);
}
else
{
tileEntity.isExploding = false;
}
}
}