-
-
Notifications
You must be signed in to change notification settings - Fork 204
/
HistoryExtent.java
184 lines (154 loc) · 5.63 KB
/
HistoryExtent.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
package com.fastasyncworldedit.core.extent;
import com.fastasyncworldedit.core.history.changeset.AbstractChangeSet;
import com.fastasyncworldedit.core.math.MutableBlockVector3;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.entity.BaseEntity;
import com.sk89q.worldedit.entity.Entity;
import com.sk89q.worldedit.extent.AbstractDelegateExtent;
import com.sk89q.worldedit.extent.Extent;
import com.sk89q.worldedit.history.changeset.ChangeSet;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.util.Location;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockStateHolder;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Stores changes to a {@link ChangeSet}.
*/
public final class HistoryExtent extends AbstractDelegateExtent {
private final MutableBlockVector3 mutable = new MutableBlockVector3();
private AbstractChangeSet changeSet;
/**
* Create a new instance.
*
* @param extent the extent
* @param changeSet the change set
*/
public HistoryExtent(Extent extent, AbstractChangeSet changeSet) {
super(extent);
checkNotNull(changeSet);
this.changeSet = changeSet;
}
public AbstractChangeSet getChangeSet() {
return changeSet;
}
@Override
public void setChangeSet(AbstractChangeSet fcs) {
this.changeSet = fcs;
}
@Override
public <B extends BlockStateHolder<B>> boolean setBlock(int x, int y, int z, B block) throws WorldEditException {
BaseBlock previous = getFullBlock(x, y, z);
if (previous.getInternalId() == block.getInternalId()) {
if (!previous.hasNbtData() && block instanceof BaseBlock && !block.hasNbtData()) {
return false;
}
}
this.changeSet.add(x, y, z, previous, block.toBaseBlock());
return getExtent().setBlock(x, y, z, block);
}
@Override
public <B extends BlockStateHolder<B>> boolean setBlock(BlockVector3 location, B block) throws WorldEditException {
return setBlock(location.getBlockX(), location.getBlockY(), location.getBlockZ(), block);
}
@Nullable
@Override
public Entity createEntity(Location location, BaseEntity state) {
final Entity entity = super.createEntity(location, state);
if (state != null) {
this.changeSet.addEntityCreate(state.getNbtData());
}
return entity;
}
@Nullable
@Override
public Entity createEntity(Location location, BaseEntity state, UUID uuid) {
final Entity entity = super.createEntity(location, state, uuid);
if (state != null) {
this.changeSet.addEntityCreate(state.getNbtData());
}
return entity;
}
@Override
public List<? extends Entity> getEntities() {
return this.wrapEntities(super.getEntities());
}
@Override
public List<? extends Entity> getEntities(Region region) {
return this.wrapEntities(super.getEntities(region));
}
private List<? extends Entity> wrapEntities(List<? extends Entity> entities) {
final List<Entity> newList = new ArrayList<>(entities.size());
for (Entity entity : entities) {
newList.add(new TrackedEntity(entity));
}
return newList;
}
@Override
public boolean setBiome(BlockVector3 position, BiomeType newBiome) {
BiomeType oldBiome = this.getBiome(position);
if (!oldBiome.getId().equals(newBiome.getId())) {
this.changeSet.addBiomeChange(position.getBlockX(), position.getBlockY(), position.getBlockZ(), oldBiome, newBiome);
return getExtent().setBiome(position, newBiome);
} else {
return false;
}
}
@Override
public boolean setBiome(int x, int y, int z, BiomeType newBiome) {
BiomeType oldBiome = this.getBiome(mutable.setComponents(x, y, z));
if (!oldBiome.getId().equals(newBiome.getId())) {
this.changeSet.addBiomeChange(x, y, z, oldBiome, newBiome);
return getExtent().setBiome(x, y, z, newBiome);
} else {
return false;
}
}
public class TrackedEntity implements Entity {
private final Entity entity;
private TrackedEntity(Entity entity) {
this.entity = entity;
}
@Override
public BaseEntity getState() {
return this.entity.getState();
}
@Override
public Location getLocation() {
return this.entity.getLocation();
}
@Override
public Extent getExtent() {
return this.entity.getExtent();
}
@Override
public boolean remove() {
final Location location = this.entity.getLocation();
final BaseEntity state = this.entity.getState();
final boolean success = this.entity.remove();
if (state != null && success) {
HistoryExtent.this.changeSet.addEntityRemove(state.getNbtData());
}
return success;
}
@Nullable
@Override
public <T> T getFacet(Class<? extends T> cls) {
return this.entity.getFacet(cls);
}
@Override
public boolean setLocation(Location location) {
return this.entity.setLocation(location);
}
}
@Override
public Extent disableHistory() {
return getExtent();
}
}