aboutsummaryrefslogtreecommitdiff
path: root/src/Java/miscutil/core/tileentities/TileEntityReverter.java
blob: 4ee8eeed4b95c2e40f261c2fa67eefabab4fe1d5 (plain)
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
package miscutil.core.tileentities;

import java.util.Random;

import miscutil.core.block.ModBlocks;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.tileentity.TileEntity;



public class TileEntityReverter extends TileEntity
{
	private static final int REVERT_CHANCE = 10;
	public int radius = 16;
	public int diameter = 8 * this.radius + 4;
	public double requiredPlayerRange = 64.0D;
	public Random rand = new Random();
	private int tickCount;
	private boolean slowScan;
	private int ticksSinceChange;
	private Block[] blockData;
	private byte[] metaData;

	public boolean canUpdate(){
		return true;
	}

	public void updateEntity()
	{
		if (anyPlayerInRange())
		{
			this.tickCount += 1;
			if (this.worldObj.isRemote)
			{
				double var1 = this.xCoord + this.worldObj.rand.nextFloat();
				double var3 = this.yCoord + this.worldObj.rand.nextFloat();
				double var5 = this.zCoord + this.worldObj.rand.nextFloat();

				this.worldObj.spawnParticle("enchantmenttable", var1, var3, var5, 0.0D, 0.0D, 0.0D);
				if (this.rand.nextInt(5) == 0)
				{
					makeRandomOutline();
					makeRandomOutline();
					makeRandomOutline();
				}
			}
			else
			{
				if ((this.blockData == null) || (this.metaData == null))
				{
					captureBlockData();
					this.slowScan = true;
				}
				if ((!this.slowScan) || (this.tickCount % 20 == 0)) {
					if (scanAndRevertChanges())
					{
						this.slowScan = false;
						this.ticksSinceChange = 0;
					}
					else
					{
						this.ticksSinceChange += 1;
						if (this.ticksSinceChange > 20) {
							this.slowScan = true;
						}
					}
				}
			}
		}
		else
		{
			this.blockData = null;
			this.metaData = null;

			this.tickCount = 0;
		}
	}

	private void makeRandomOutline()
	{
		makeOutline(this.rand.nextInt(12));
	}

	private void makeOutline(int outline)
	{
		double sx = this.xCoord;
		double sy = this.yCoord;
		double sz = this.zCoord;

		double dx = this.xCoord;
		double dy = this.yCoord;
		double dz = this.zCoord;
		switch (outline)
		{
		case 0: 
			sx -= this.radius;
			dx -= this.radius;
			sz -= this.radius;
			dz += this.radius + 1;
		case 8: 
			sx -= this.radius;
			dx += this.radius + 1;
			sz -= this.radius;
			dz -= this.radius;
			break;
		case 1: 
		case 9: 
			sx -= this.radius;
			dx -= this.radius;
			sz -= this.radius;
			dz += this.radius + 1;
			break;
		case 2: 
		case 10: 
			sx -= this.radius;
			dx += this.radius + 1;
			sz += this.radius + 1;
			dz += this.radius + 1;
			break;
		case 3: 
		case 11: 
			sx += this.radius + 1;
			dx += this.radius + 1;
			sz -= this.radius;
			dz += this.radius + 1;
			break;
		case 4: 
			sx -= this.radius;
			dx -= this.radius;
			sz -= this.radius;
			dz -= this.radius;
			break;
		case 5: 
			sx += this.radius + 1;
			dx += this.radius + 1;
			sz -= this.radius;
			dz -= this.radius;
			break;
		case 6: 
			sx += this.radius + 1;
			dx += this.radius + 1;
			sz += this.radius + 1;
			dz += this.radius + 1;
			break;
		case 7: 
			sx -= this.radius;
			dx -= this.radius;
			sz += this.radius + 1;
			dz += this.radius + 1;
		}
		switch (outline)
		{
		case 0: 
		case 1: 
		case 2: 
		case 3: 
			sy += this.radius + 1;
			dy += this.radius + 1;
			break;
		case 4: 
		case 5: 
		case 6: 
		case 7: 
			sy -= this.radius;
			dy += this.radius + 1;
			break;
		case 8: 
		case 9: 
		case 10: 
		case 11: 
			sy -= this.radius;
			dy -= this.radius;
		}
		if (this.rand.nextBoolean()) {
			drawParticleLine(this.xCoord + 0.5D, this.yCoord + 0.5D, this.zCoord + 0.5D, dx, dy, dz);
		} else {
			drawParticleLine(sx, sy, sz, this.xCoord + 0.5D, this.yCoord + 0.5D, this.zCoord + 0.5D);
		}
		drawParticleLine(sx, sy, sz, dx, dy, dz);
	}

	protected void drawParticleLine(double srcX, double srcY, double srcZ, double destX, double destY, double destZ)
	{
		int particles = 16;
		for (int i = 0; i < particles; i++)
		{
			double trailFactor = i / (particles - 1.0D);

			double tx = srcX + (destX - srcX) * trailFactor + this.rand.nextFloat() * 0.005D;
			double ty = srcY + (destY - srcY) * trailFactor + this.rand.nextFloat() * 0.005D;
			double tz = srcZ + (destZ - srcZ) * trailFactor + this.rand.nextFloat() * 0.005D;
			this.worldObj.spawnParticle("portal", tx, ty, tz, 0.0D, 0.0D, 0.0D);
		}
	}

	private boolean scanAndRevertChanges()
	{
		int index = 0;
		boolean reverted = false;
		for (int x = -this.radius; x <= this.radius; x++) {
			for (int y = -this.radius; y <= this.radius; y++) {
				for (int z = -this.radius; z <= this.radius; z++)
				{
					Block blockID = this.worldObj.getBlock(this.xCoord + x, this.yCoord + y, this.zCoord + z);
					byte meta = (byte)this.worldObj.getBlockMetadata(this.xCoord + x, this.yCoord + y, this.zCoord + z);
					if (this.blockData[index] != blockID) {
						if (revertBlock(this.xCoord + x, this.yCoord + y, this.zCoord + z, blockID, meta, this.blockData[index], this.metaData[index]))
						{
							reverted = true;
						}
						else
						{
							this.blockData[index] = blockID;
							this.metaData[index] = meta;
						}
					}
					index++;
				}
			}
		}
		return reverted;
	}

	private boolean revertBlock(int x, int y, int z, Block thereBlockID, byte thereMeta, Block replaceBlockID, byte replaceMeta)
	{
		/*if ((thereBlockID == Blocks.air) && (!replaceBlockID.getMaterial().blocksMovement()))
		{
			System.out.println("Not replacing block " + replaceBlockID + " because it doesn't block movement");

			return false;
		}*/
		if (isUnrevertable(thereBlockID, thereMeta, replaceBlockID, replaceMeta)) {
			return false;
		}
		if (this.rand.nextInt(5) == 0)
		{
			if (replaceBlockID != Blocks.air)
			{
				//replaceBlockID = null;
				replaceMeta = 4;
			}
			this.worldObj.setBlock(x, y, z, replaceBlockID, replaceMeta, 2);
			if (thereBlockID == Blocks.air)
			{
				this.worldObj.playAuxSFX(2001, x, y, z, Block.getIdFromBlock(replaceBlockID) + (replaceMeta << 12));
			}
			else if (replaceBlockID == Blocks.air)
			{
				this.worldObj.playAuxSFX(2001, x, y, z, Block.getIdFromBlock(thereBlockID) + (thereMeta << 12));
				thereBlockID.dropBlockAsItem(this.worldObj, x, y, z, thereMeta, 0);
			}
		}
		return true;
	}

	private boolean isUnrevertable(Block thereBlockID, byte thereMeta, Block replaceBlockID, byte replaceMeta)
	{
		if ((thereBlockID == ModBlocks.blockGriefSaver) || (replaceBlockID == ModBlocks.blockGriefSaver)) {
			return true;
		}
		/*if (((thereBlockID == towerTranslucent) && (thereMeta != 4)) || ((replaceBlockID == towerTranslucent) && (replaceMeta != 4))) {
			return true;
		}*/
		if ((thereBlockID == Blocks.redstone_lamp) && (replaceBlockID == Blocks.lit_redstone_lamp)) {
			return true;
		}
		if ((thereBlockID == Blocks.lit_redstone_lamp) && (replaceBlockID == Blocks.redstone_lamp)) {
			return true;
		}
		/*if ((thereBlockID == Blocks.water) || (replaceBlockID == Blocks.flowing_water)) {
			return true;
		}
		if ((thereBlockID == Blocks.flowing_water) || (replaceBlockID == Blocks.water)) {
			return true;
		}*/
		if (replaceBlockID == Blocks.tnt) {
			return true;
		}
		return false;
	}

	private void captureBlockData()
	{
		this.blockData = new Block[this.diameter * this.diameter * this.diameter];
		this.metaData = new byte[this.diameter * this.diameter * this.diameter];

		int index = 0;
		for (int x = -this.radius; x <= this.radius; x++) {
			for (int y = -this.radius; y <= this.radius; y++) {
				for (int z = -this.radius; z <= this.radius; z++)
				{
					Block blockID = this.worldObj.getBlock(this.xCoord + x, this.yCoord + y, this.zCoord + z);
					int meta = this.worldObj.getBlockMetadata(this.xCoord + x, this.yCoord + y, this.zCoord + z);

					this.blockData[index] = blockID;
					this.metaData[index] = ((byte)meta);

					index++;
				}
			}
		}
	}

	public boolean anyPlayerInRange()
	{
		return this.worldObj.getClosestPlayer(this.xCoord + 0.5D, this.yCoord + 0.5D, this.zCoord + 0.5D, this.requiredPlayerRange) != null;
	}
}