-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBlock.java
More file actions
167 lines (141 loc) · 6.37 KB
/
Copy pathBlock.java
File metadata and controls
167 lines (141 loc) · 6.37 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
package com.github.skriptdev.skript.api.hytale;
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.fluid.Fluid;
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.ChunkColumn;
import com.hypixel.hytale.server.core.universe.world.chunk.section.FluidSection;
import com.hypixel.hytale.server.core.universe.world.storage.ChunkStore;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
/**
* Represents a block in a world.
* Hytale doesn't appear to have a representation of a block in a 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")
public class Block {
private final @NotNull World world;
private @NotNull BlockType type;
private final @NotNull Vector3i pos;
public Block(@NotNull World world, @NotNull Vector3i pos) {
this.world = world;
this.pos = pos;
this.type = Objects.requireNonNull(world.getBlockType(pos));
}
public Block(@NotNull World world, @NotNull Vector3i pos, @NotNull BlockType type) {
this.world = world;
this.pos = pos;
this.type = type;
}
public Block(@NotNull Location location) {
World world = Universe.get().getWorld(location.getWorld());
if (world == null) {
throw new IllegalArgumentException("World '" + location.getWorld() + "' not found.");
}
BlockType blockType = world.getBlockType(location.getPosition().toVector3i());
assert blockType != null;
this(world, location.getPosition().toVector3i(), blockType);
}
public @NotNull BlockType getType() {
return this.type;
}
public void setType(@NotNull BlockType type, int settings) {
this.type = type;
this.world.setBlock(this.pos.getX(), this.pos.getY(), this.pos.getZ(), type.getId(), settings);
}
public byte getFluidLevel() {
long index = ChunkUtil.indexChunkFromBlock(this.pos.getX(), this.pos.getZ());
Ref<ChunkStore> columnRef = this.world.getChunk(index).getReference();
Store<ChunkStore> store = columnRef.getStore();
ChunkColumn column = store.getComponent(columnRef, ChunkColumn.getComponentType());
Ref<ChunkStore> section = column.getSection(ChunkUtil.chunkCoordinate(this.pos.getY()));
if (section == null) {
return 0;
} else {
FluidSection fluidSection = store.getComponent(section, FluidSection.getComponentType());
return fluidSection.getFluidLevel(this.pos.getX(), this.pos.getY(), this.pos.getZ());
}
}
public void setFluidLevel(byte level) {
long index = ChunkUtil.indexChunkFromBlock(this.pos.getX(), this.pos.getZ());
this.world.getChunkAsync(index).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;
fluidSection.setFluid(this.pos.getX(), this.pos.getY(), this.pos.getZ(), fluid, level);
}
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) {
long index = ChunkUtil.indexChunkFromBlock(this.pos.getX(), this.pos.getZ());
this.world.getChunkAsync(index).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 level = fluidSection.getFluidLevel(this.pos.getX(), this.pos.getY(), this.pos.getZ());
if (level <= 0) level = 8;
fluidSection.setFluid(this.pos.getX(), this.pos.getY(), this.pos.getZ(), fluid, level);
}
return chunk;
});
}
public void breakBlock(int settings) {
this.world.breakBlock(this.pos.getX(), this.pos.getY(), this.pos.getZ(), settings);
}
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.type.getId(), this.pos.getX(), this.pos.getY(), this.pos.getZ(), this.world.getName());
}
@Override
public String toString() {
return "Block{" +
"world=" + world.getName() +
", type=" + type +
", pos=" + pos +
'}';
}
}