/
BaseBlock.java
272 lines (239 loc) · 7.26 KB
/
BaseBlock.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
/*
* WorldEdit, a Minecraft world manipulation toolkit
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldEdit team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldedit.blocks;
import com.sk89q.jnbt.CompoundTag;
import com.sk89q.jnbt.StringTag;
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.blocks.type.BlockState;
import com.sk89q.worldedit.blocks.type.BlockStateHolder;
import com.sk89q.worldedit.blocks.type.BlockType;
import com.sk89q.worldedit.blocks.type.BlockTypes;
import com.sk89q.worldedit.function.mask.Mask;
import com.sk89q.worldedit.world.registry.BundledBlockData;
import com.sk89q.worldedit.world.registry.state.State;
import com.sk89q.worldedit.world.registry.state.value.StateValue;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Nullable;
/**
* Represents a mutable "snapshot" of a block.
*
* <p>An instance of this block contains all the information needed to
* accurately reproduce the block, provided that the instance was
* made correctly. In some implementations, it may not be possible to get a
* snapshot of blocks correctly, so, for example, the NBT data for a block
* may be missing.</p>
*
* <p>A peculiar detail of this class is that it accepts {@code -1} as a
* valid data value. This is due to legacy reasons: WorldEdit uses -1
* as a "wildcard" block value, even though a {@link Mask} would be
* more appropriate.</p>
*/
public class BaseBlock implements BlockStateHolder<BaseBlock>, TileEntityBlock {
// Instances of this class should be _as small as possible_ because there will
// be millions of instances of this object.
private BlockState blockState;
@Nullable
private CompoundTag nbtData;
/**
* Construct a block with the given ID and a data value of 0.
*
* @param id ID value
*/
@Deprecated
public BaseBlock(int id) {
try {
this.blockState = BlockTypes.getBlockType(BundledBlockData.getInstance().fromLegacyId(id)).getDefaultState();
} catch (Exception e) {
System.out.println(id);
System.out.println(BundledBlockData.getInstance().fromLegacyId(id));
e.printStackTrace();
}
}
/**
* Construct a block with a state.
*
* @param blockState The blockstate
*/
public BaseBlock(BlockState blockState) {
this.blockState = blockState;
}
/**
* Construct a block with the given type and default data.
*
* @param blockType The block type
*/
public BaseBlock(BlockType blockType) {
this.blockState = blockType.getDefaultState();
}
/**
* Construct a block with the given ID, data value and NBT data structure.
*
* @param state The block state
* @param nbtData NBT data, which may be null
*/
public BaseBlock(BlockState state, @Nullable CompoundTag nbtData) {
this.blockState = state;
this.nbtData = nbtData;
}
/**
* Construct a block with the given ID and data value.
*
* @param id ID value
* @param data data value
*/
@Deprecated
public BaseBlock(int id, int data) {
this(id);
}
/**
* Construct a block with the given ID, data value and NBT data structure.
*
* @param id ID value
* @param data data value
* @param nbtData NBT data, which may be null
*/
@Deprecated
public BaseBlock(int id, int data, @Nullable CompoundTag nbtData) {
this(id);
this.nbtData = nbtData;
}
/**
* Create a clone of another block.
*
* @param other the other block
*/
public BaseBlock(BaseBlock other) {
this(other.getState(), other.getNbtData());
}
/**
* Get the block state
*
* @return The block state
*/
public BlockState getState() {
return this.blockState;
}
/**
* Get the legacy numerical ID of the block.
*
* @return legacy numerical ID
*/
@Deprecated
public int getId() {
return this.blockState.getBlockType().getLegacyId();
}
/**
* Get the block's data value.
*
* Broken - do not use
*
* @return data value (0-15)
*/
@Deprecated
public int getData() {
return 0;
}
/**
* Gets a map of state to statevalue
*
* @return The state map
*/
@Override
public Map<State, StateValue> getStates() {
return this.blockState.getStates();
}
@Override
public BlockType getBlockType() {
return this.blockState.getBlockType();
}
@Override
public BaseBlock with(State state, StateValue value) {
return new BaseBlock(this.blockState.with(state, value), getNbtData());
}
/**
* Gets the State for this Block.
*
* @param state The state to get the value for
* @return The state value
*/
@Override
public StateValue getState(State state) {
return this.blockState.getState(state);
}
@Override
public boolean hasNbtData() {
return getNbtData() != null;
}
@Override
public String getNbtId() {
CompoundTag nbtData = getNbtData();
if (nbtData == null) {
return "";
}
Tag idTag = nbtData.getValue().get("id");
if (idTag instanceof StringTag) {
return ((StringTag) idTag).getValue();
} else {
return "";
}
}
@Nullable
@Override
public CompoundTag getNbtData() {
return this.nbtData;
}
@Override
public void setNbtData(@Nullable CompoundTag nbtData) {
throw new UnsupportedOperationException("This class is immutable.");
}
/**
* Checks whether the type ID and data value are equal.
*/
@Override
public boolean equals(Object o) {
if (!(o instanceof BaseBlock)) {
return false;
}
final BaseBlock otherBlock = (BaseBlock) o;
return this.getState().equals(otherBlock.getState()) && Objects.equals(getNbtData(), otherBlock.getNbtData());
}
/**
* Checks if the type is the same, and if the matched states are the same.
*
* @param o other block
* @return true if equal
*/
@Override
public boolean equalsFuzzy(BlockStateHolder o) {
return this.getState().equalsFuzzy(o);
}
@Override
public int hashCode() {
int ret = getState().hashCode() << 3;
if (hasNbtData()) {
ret += getNbtData().hashCode();
}
return ret;
}
@Override
public String toString() {
return "Block{State: " + this.getState().toString() + ", NBT: " + String.valueOf(getNbtData()) + "}";
}
}