Skip to content

Commit 38052ad

Browse files
committed
speed up explosions, add saving logic to explosives, fix global state bug
1 parent bf70599 commit 38052ad

File tree

3 files changed

+231
-56
lines changed

3 files changed

+231
-56
lines changed

src/main/java/gregtech/common/entities/GT_Entity_Explosive.java

+17-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import net.minecraft.entity.EntityLivingBase;
1717
import net.minecraft.entity.item.EntityTNTPrimed;
1818
import net.minecraft.nbt.NBTTagCompound;
19+
import net.minecraft.nbt.NBTTagList;
1920
import net.minecraft.world.Explosion;
2021
import net.minecraft.world.World;
2122

@@ -92,9 +93,6 @@ public void onUpdate() {
9293
this.setDead();
9394
if (!this.worldObj.isRemote) {
9495
this.doExplode();
95-
if (preCalc != null) {
96-
this.preCalc.finalizeExplosion();
97-
}
9896
}
9997
} else {
10098
final int n = rand.nextInt(2) + 1;
@@ -118,12 +116,19 @@ public void onUpdate() {
118116
protected void writeEntityToNBT(final NBTTagCompound compound) {
119117
super.writeEntityToNBT(compound);
120118
compound.setInteger("initialFuse", initialFuse);
119+
compound.setInteger("fuse", fuse);
121120
compound.setInteger("meta", metadata);
122121
compound.setDouble("rX", realX);
123122
compound.setDouble("rY", realY);
124123
compound.setDouble("rZ", realZ);
125124
compound.setInteger("tier", tier.getTier());
126125
compound.setInteger("typeIndex", tier.getTierTrackIndex());
126+
if (explosion != null) {
127+
compound.setTag("explosion", explosion.serializeNBT());
128+
}
129+
if (preCalc != null) {
130+
compound.setTag("preCalc", preCalc.serializeNBT());
131+
}
127132
}
128133

129134
/**
@@ -136,14 +141,20 @@ protected void writeEntityToNBT(final NBTTagCompound compound) {
136141
protected void readEntityFromNBT(final NBTTagCompound compound) {
137142
super.readEntityFromNBT(compound);
138143
this.initialFuse = compound.getInteger("initialFuse");
144+
this.fuse = compound.getInteger("fuse");
139145
this.metadata = compound.getInteger("meta");
140146
this.realX = compound.getDouble("rX");
141147
this.realY = compound.getDouble("rY");
142148
this.realZ = compound.getDouble("rZ");
143149
this.tier = getTier(compound);
144-
if (!worldObj.isRemote && explosion == null) {
145-
// Uncomment this to *somewhat* fix the issue of re-logging or crashing deleting explosives. The calculations appear to be messed up though in doing so
146-
// createAndInitExplosion();
150+
if (compound.hasKey("explosion")) {
151+
val exp = compound.getCompoundTag("explosion");
152+
explosion = tier.createExplosion(this);
153+
explosion.deserializeNBT(exp);
154+
if (compound.hasKey("preCalc")) {
155+
val pre = compound.getCompoundTag("preCalc");
156+
preCalc = GT_Explosion_PreCalculation.deserializeNBT(explosion, pre);
157+
}
147158
}
148159
}
149160

src/main/java/gregtech/common/misc/explosions/GT_Explosion.java

+39-8
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
import net.minecraft.entity.item.EntityItem;
1515
import net.minecraft.entity.player.EntityPlayer;
1616
import net.minecraft.item.ItemStack;
17+
import net.minecraft.nbt.NBTTagCompound;
18+
import net.minecraft.nbt.NBTTagIntArray;
19+
import net.minecraft.nbt.NBTTagList;
1720
import net.minecraft.util.AxisAlignedBB;
1821
import net.minecraft.util.DamageSource;
1922
import net.minecraft.util.MathHelper;
@@ -22,6 +25,8 @@
2225
import net.minecraft.world.Explosion;
2326
import net.minecraft.world.World;
2427

28+
import net.minecraftforge.common.util.Constants;
29+
2530
import java.util.ArrayList;
2631
import java.util.HashSet;
2732
import java.util.List;
@@ -33,9 +38,7 @@
3338
@Getter
3439
public abstract class GT_Explosion<TierType extends Enum<TierType> & IGT_ExplosiveTier<TierType>> extends Explosion {
3540

36-
protected final List<ItemStack> harvested = new ArrayList<>();
37-
38-
protected final Set<ChunkPosition> seen = new HashSet<>(), targeted = new HashSet<>();
41+
protected final Set<ChunkPosition> targeted = new HashSet<>();
3942

4043
private final GT_Entity_Explosive gtExplosive;
4144

@@ -52,6 +55,33 @@ public GT_Explosion(
5255
this.isSmoking = true;
5356
}
5457

58+
public NBTTagCompound serializeNBT() {
59+
val nbt = new NBTTagCompound();
60+
val targeted = new NBTTagList();
61+
nbt.setTag("targeted", targeted);
62+
for (val pos: this.targeted) {
63+
targeted.appendTag(new NBTTagIntArray(serializePos(pos)));
64+
}
65+
return nbt;
66+
}
67+
68+
public void deserializeNBT(NBTTagCompound nbt) {
69+
targeted.clear();
70+
val t = nbt.getTagList("targeted", Constants.NBT.TAG_INT_ARRAY);
71+
int length = t.tagCount();
72+
for (int i = 0; i < length; i++) {
73+
targeted.add(deserializePos(t.func_150306_c(i)));
74+
}
75+
}
76+
77+
public static int[] serializePos(final ChunkPosition pos) {
78+
return new int[]{pos.chunkPosX, pos.chunkPosY, pos.chunkPosZ};
79+
}
80+
81+
public static ChunkPosition deserializePos(final int[] nbt) {
82+
return new ChunkPosition(nbt[0], nbt[1], nbt[2]);
83+
}
84+
5585
public int getX() {
5686
return (int) explosionX;
5787
}
@@ -122,6 +152,7 @@ public void doExplosionB(final boolean shouldDoParticles) {
122152
} else {
123153
blocksToDestroy = targeted;
124154
}
155+
val harvested = new HashSet<ItemStack>();
125156
for (ChunkPosition position : blocksToDestroy) {
126157
int i, j, k, meta;
127158
Block block;
@@ -136,12 +167,12 @@ public void doExplosionB(final boolean shouldDoParticles) {
136167
}
137168
if (block.getMaterial() != Material.air) {
138169
if (block.canDropFromExplosion(this)) {
139-
getDrops(block, i, j, k, meta);
170+
getDrops(harvested, block, i, j, k, meta);
140171
}
141172
destroyBlock(block, i, j, k);
142173
}
143174
}
144-
processDrops();
175+
processDrops(harvested);
145176
}
146177

147178
protected void explosionPost() {
@@ -240,7 +271,7 @@ protected void doParticles(final float x, final float y, final float z) {
240271
pubWorld.spawnParticle("smoke", d0, d1, d2, d3, d4, d5);
241272
}
242273

243-
protected void getDrops(final Block block, final int x, final int y, final int z, final int metadata) {
274+
protected void getDrops(Set<ItemStack> harvested, final Block block, final int x, final int y, final int z, final int metadata) {
244275
if (!pubWorld.isRemote) {
245276
final float chance = getDropChance(block);
246277
ArrayList<ItemStack> items = block.getDrops(pubWorld, x, y, z, metadata, getFortune());
@@ -252,7 +283,7 @@ protected void destroyBlock(final Block block, final int i, final int j, final i
252283
block.onBlockExploded(pubWorld, i, j, k, this);
253284
}
254285

255-
protected void processDrops() {
286+
protected void processDrops(Set<ItemStack> harvested) {
256287
harvested.forEach(this::spawnItem);
257288
}
258289

@@ -291,7 +322,7 @@ protected boolean hasEncountered(final int x, final int y, final int z) {
291322
}
292323

293324
protected boolean hasEncountered(final ChunkPosition pos) {
294-
return seen.contains(pos) || targeted.contains(pos);
325+
return targeted.contains(pos);
295326
}
296327

297328
protected float getRayPower() {

0 commit comments

Comments
 (0)