-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathBlock.java
More file actions
350 lines (297 loc) · 13.3 KB
/
Block.java
File metadata and controls
350 lines (297 loc) · 13.3 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package com.github.skriptdev.skript.api.hytale;
import com.github.skriptdev.skript.api.hytale.utils.StoreUtils;
import com.hypixel.hytale.component.CommandBuffer;
import com.hypixel.hytale.component.Ref;
import com.hypixel.hytale.component.Store;
import com.hypixel.hytale.math.util.ChunkUtil;
import com.hypixel.hytale.math.vector.Location;
import com.hypixel.hytale.math.vector.Vector3i;
import com.hypixel.hytale.server.core.asset.type.blocktype.config.BlockType;
import com.hypixel.hytale.server.core.asset.type.blocktype.config.Rotation;
import com.hypixel.hytale.server.core.asset.type.blocktype.config.RotationTuple;
import com.hypixel.hytale.server.core.asset.type.fluid.Fluid;
import com.hypixel.hytale.server.core.entity.LivingEntity;
import com.hypixel.hytale.server.core.inventory.ItemStack;
import com.hypixel.hytale.server.core.modules.blockhealth.BlockHealth;
import com.hypixel.hytale.server.core.modules.blockhealth.BlockHealthChunk;
import com.hypixel.hytale.server.core.modules.blockhealth.BlockHealthModule;
import com.hypixel.hytale.server.core.modules.interaction.BlockHarvestUtils;
import com.hypixel.hytale.server.core.universe.PlayerRef;
import com.hypixel.hytale.server.core.universe.Universe;
import com.hypixel.hytale.server.core.universe.world.World;
import com.hypixel.hytale.server.core.universe.world.chunk.BlockChunk;
import com.hypixel.hytale.server.core.universe.world.chunk.ChunkColumn;
import com.hypixel.hytale.server.core.universe.world.chunk.WorldChunk;
import com.hypixel.hytale.server.core.universe.world.chunk.section.FluidSection;
import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
import com.hypixel.hytale.server.core.universe.world.storage.EntityStore;
import io.github.syst3ms.skriptparser.util.color.Color;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Map;
import java.util.function.Predicate;
/**
* Represents a block in a world.
* Hytale doesn't appear to have a representation of a block in the world.
* This class provides a wrapper around Hytale's block system, allowing for easy interaction with blocks in a world.
* This may be changed/removed in the future.
*/
@SuppressWarnings({"unused", "deprecation"})
public class Block {
private final @NotNull World world;
private final @NotNull Vector3i pos;
public Block(@NotNull World world, @NotNull Vector3i pos) {
this.world = world;
this.pos = pos;
}
public Block(@NotNull Location location) {
World world = Universe.get().getWorld(location.getWorld());
if (world == null) {
throw new IllegalArgumentException("World '" + location.getWorld() + "' not found.");
}
this(world, location.getPosition().toVector3i());
}
public long getChunkIndex() {
return ChunkUtil.indexChunkFromBlock(this.pos.getX(), this.pos.getZ());
}
public WorldChunk getChunk() {
return this.world.getChunk(getChunkIndex());
}
public @NotNull BlockType getType() {
BlockType blockType = this.world.getBlockType(this.pos);
return blockType != null ? blockType : BlockType.EMPTY;
}
public void setType(@NotNull BlockType type, int settings) {
Runnable r = () -> Block.this.world.setBlock(Block.this.pos.getX(), Block.this.pos.getY(), Block.this.pos.getZ(), type.getId(), settings);
if (this.world.isInThread()) {
r.run();
} else {
this.world.execute(r);
}
}
public void updateChunk() {
getWorld().getNotificationHandler().updateChunk(getChunkIndex());
}
/**
* Set the rotation of this block.
*
* @param rotation Rotation of block represented by a Vector3i(yaw, pitch, roll).
*/
public void setRotation(Vector3i rotation) {
int blockId = BlockType.getAssetMap().getIndex(getType().getId());
WorldChunk chunk = getChunk();
BlockChunk blockChunk = chunk.getBlockChunk();
if (blockChunk == null) return;
Rotation pitch = getRotationFromInt(rotation.getX());
Rotation yaw = getRotationFromInt(rotation.getY());
Rotation roll = getRotationFromInt(rotation.getZ());
int rotationIndex = RotationTuple.of(yaw, pitch, roll).index();
blockChunk.setBlock(this.pos.getX(), this.pos.getY(), this.pos.getZ(),
blockId, rotationIndex, 0);
}
/**
* Get the rotation of this block.
*
* @return Rotation of block represented as a Vector3i(yaw, pitch, roll).
*/
public Vector3i getRotation() {
int blockRotationIndex = getWorld().getBlockRotationIndex(this.pos.getX(), this.pos.getY(), this.pos.getZ());
RotationTuple rotationTuple = RotationTuple.get(blockRotationIndex);
int yaw = getIntFromRotation(rotationTuple.yaw());
int pitch = getIntFromRotation(rotationTuple.pitch());
int roll = getIntFromRotation(rotationTuple.roll());
return new Vector3i(pitch, yaw, roll);
}
private Rotation getRotationFromInt(int v) {
if (v < 90) return Rotation.None;
else if (v < 180) return Rotation.Ninety;
else if (v < 270) return Rotation.OneEighty;
else return Rotation.TwoSeventy;
}
private int getIntFromRotation(Rotation rotation) {
return switch (rotation) {
case None -> 0;
case Ninety -> 90;
case OneEighty -> 180;
case TwoSeventy -> 270;
};
}
public Color getTint() {
BlockChunk blockChunk = getChunk().getBlockChunk();
if (blockChunk == null) return null;
int x = this.pos.getX() % 32;
int z = this.pos.getZ() % 32;
int tintInt = blockChunk.getTint(x, z);
return Color.of(tintInt);
}
public void setTint(Color color) {
setTint(color, true);
}
public void setTint(Color color, boolean updateChunk) {
BlockChunk blockChunk = getChunk().getBlockChunk();
if (blockChunk == null) return;
int x = this.pos.getX() % 32;
int z = this.pos.getZ() % 32;
blockChunk.setTint(x, z, color.toJavaColor().getRGB());
if (updateChunk) {
updateChunk();
}
}
public byte getFluidLevel() {
WorldChunk chunk = this.world.getChunk(getChunkIndex());
if (chunk == null) return 0;
Ref<ChunkStore> columnRef = chunk.getReference();
Store<ChunkStore> store = columnRef.getStore();
ChunkColumn column = store.getComponent(columnRef, ChunkColumn.getComponentType());
if (column == null) return 0;
Ref<ChunkStore> section = column.getSection(ChunkUtil.chunkCoordinate(this.pos.getY()));
if (section == null) {
return 0;
} else {
FluidSection fluidSection = store.getComponent(section, FluidSection.getComponentType());
if (fluidSection == null) return 0;
return fluidSection.getFluidLevel(this.pos.getX(), this.pos.getY(), this.pos.getZ());
}
}
public void setFluidLevel(byte level) {
this.world.getChunkAsync(getChunkIndex()).thenApply((chunk) -> {
Ref<ChunkStore> columnRef = chunk.getReference();
Store<ChunkStore> store = columnRef.getStore();
ChunkColumn column = store.getComponent(columnRef, ChunkColumn.getComponentType());
if (column == null) return null;
Ref<ChunkStore> section = column.getSection(ChunkUtil.chunkCoordinate(this.pos.getY()));
if (section == null) {
return null;
} else {
FluidSection fluidSection = store.getComponent(section, FluidSection.getComponentType());
if (fluidSection == null) {
return null;
}
Fluid fluid = fluidSection.getFluid(this.pos.getX(), this.pos.getY(), this.pos.getZ());
if (fluid == null) return null;
byte fluidLevel = (byte) Math.clamp((int) level, 0, fluid.getMaxFluidLevel());
fluidSection.setFluid(this.pos.getX(), this.pos.getY(), this.pos.getZ(), fluid, fluidLevel);
}
return chunk;
});
}
public Fluid getFluid() {
int fluidId = this.world.getFluidId(this.pos.getX(), this.pos.getY(), this.pos.getZ());
if (fluidId == -1) {
return null;
}
return Fluid.getAssetMap().getAsset(fluidId);
}
public void setFluid(@NotNull Fluid fluid, @Nullable Integer level) {
this.world.getChunkAsync(getChunkIndex()).thenApply((chunk) -> {
Ref<ChunkStore> columnRef = chunk.getReference();
Store<ChunkStore> store = columnRef.getStore();
ChunkColumn column = store.getComponent(columnRef, ChunkColumn.getComponentType());
if (column == null) return null;
Ref<ChunkStore> section = column.getSection(ChunkUtil.chunkCoordinate(this.pos.getY()));
if (section == null) {
return null;
} else {
FluidSection fluidSection = store.getComponent(section, FluidSection.getComponentType());
if (fluidSection == null) {
return null;
}
byte fluidLevel;
if (level != null) {
fluidLevel = level.byteValue();
} else {
fluidLevel = fluidSection.getFluidLevel(this.pos.getX(), this.pos.getY(), this.pos.getZ());
if (fluidLevel <= 0) fluidLevel = (byte) fluid.getMaxFluidLevel();
}
fluidLevel = (byte) Math.clamp((int) fluidLevel, 0, fluid.getMaxFluidLevel());
fluidSection.setFluid(this.pos.getX(), this.pos.getY(), this.pos.getZ(), fluid, fluidLevel);
}
return chunk;
});
}
public void breakBlock(int settings) {
this.world.breakBlock(this.pos.getX(), this.pos.getY(), this.pos.getZ(), settings);
}
public void damage(@Nullable LivingEntity performer, @Nullable ItemStack itemStack, float damage) {
WorldChunk chunk = this.world.getChunk(getChunkIndex());
if (chunk == null) return;
Ref<ChunkStore> ref = chunk.getReference();
Store<ChunkStore> chunkStore = this.world.getChunkStore().getStore();
CommandBuffer<EntityStore> commandBuffer = StoreUtils.getCommandBuffer(this.world.getEntityStore().getStore());
if (performer == null) {
BlockHarvestUtils.performBlockDamage(
this.pos,
null,
null,
damage,
0,
ref,
commandBuffer,
chunkStore);
} else {
BlockHarvestUtils.performBlockDamage(
performer,
performer.getReference(),
this.pos,
itemStack,
null,
null, // TODO figure out how to get this
false,
damage,
0,
ref,
commandBuffer,
chunkStore);
}
}
public float getBlockHealth() {
WorldChunk chunk = getChunk();
if (chunk == null) return 0;
Ref<ChunkStore> ref = chunk.getReference();
Store<ChunkStore> chunkStore = this.world.getChunkStore().getStore();
BlockHealthChunk component = chunkStore.getComponent(ref, BlockHealthModule.get().getBlockHealthChunkComponentType());
if (component == null) return 0;
return component.getBlockHealth(this.pos);
}
public void setBlockHealth(float health) {
WorldChunk chunk = getChunk();
if (chunk == null) return;
Ref<ChunkStore> ref = chunk.getReference();
Store<ChunkStore> chunkStore = this.world.getChunkStore().getStore();
BlockHealthChunk component = chunkStore.getComponent(ref, BlockHealthModule.get().getBlockHealthChunkComponentType());
if (component == null) return;
Map<Vector3i, BlockHealth> blockHealthMap = component.getBlockHealthMap();
BlockHealth blockHealth = blockHealthMap.getOrDefault(this.pos, new BlockHealth());
blockHealth.setHealth(health);
blockHealthMap.put(this.pos, blockHealth);
if (!blockHealth.isDestroyed()) {
Predicate<PlayerRef> filter = (player) -> true;
world.getNotificationHandler().updateBlockDamage(this.pos.getX(), this.pos.getY(),
this.pos.getZ(), blockHealth.getHealth(), health, filter);
}
}
public @NotNull World getWorld() {
return this.world;
}
public @NotNull Vector3i getPos() {
return this.pos;
}
public @NotNull Location getLocation() {
return new Location(this.world.getName(), this.pos.getX(), this.pos.getY(), this.pos.getZ());
}
public String toTypeString() {
return String.format("[%s] block at (%s,%s,%s) in '%s'",
this.getType().getId(), this.pos.getX(), this.pos.getY(), this.pos.getZ(), this.world.getName());
}
public String toVariableNameString() {
return String.format("%s_%s_%s_%s_%s", this.world.getName(), this.getType().getId(), this.pos.getX(), this.pos.getY(), this.pos.getZ());
}
@Override
public String toString() {
return "Block{" +
"world=" + this.world.getName() +
", type=" + this.getType() +
", pos=" + this.pos +
'}';
}
}