-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathScriptBlock.java
More file actions
234 lines (186 loc) · 6.91 KB
/
Copy pathScriptBlock.java
File metadata and controls
234 lines (186 loc) · 6.91 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
package noppes.npcs.scripted;
import net.minecraft.block.Block;
import net.minecraft.inventory.IInventory;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import noppes.npcs.api.IBlock;
import noppes.npcs.api.IContainer;
import noppes.npcs.api.INbt;
import noppes.npcs.api.IPos;
import noppes.npcs.api.ITileEntity;
import noppes.npcs.api.IWorld;
import java.util.Objects;
public class ScriptBlock implements IBlock {
protected IWorld world;
protected Block block;
protected BlockPos pos;
protected IPos bPos;
protected ITileEntity tile;
public ScriptBlock(World world, Block block, BlockPos pos) {
this.world = NpcAPI.Instance().getIWorld(world);
this.block = block;
this.pos = pos;
this.bPos = NpcAPI.Instance().getIPos(pos);
this.tile = NpcAPI.Instance().getITileEntity(world.getTileEntity(pos.getX(), pos.getY(), pos.getZ()));
}
public IPos getPosition() {
return this.bPos;
}
public IPos getPos() {
return this.bPos;
}
public boolean setPosition(IPos pos, IWorld world) {
if (pos == null || world == null || !world.setBlock(pos, this))
return false;
this.world.removeBlock(this.getPos());
this.world = world;
this.block = world.getBlock(pos).getMCBlock();
this.pos = pos.getMCPos();
this.bPos = pos;
this.tile = NpcAPI.Instance().getITileEntity(world.getMCWorld().getTileEntity(pos.getX(), pos.getY(), pos.getZ()));
return true;
}
public boolean setPosition(IPos pos) {
return this.setPosition(pos, world);
}
public boolean setPos(IPos pos, IWorld world) {
return this.setPosition(pos, world);
}
public boolean setPos(IPos pos) {
return this.setPosition(pos);
}
public boolean setPosition(int x, int y, int z, IWorld world) {
return this.setPos(NpcAPI.Instance().getIPos(x, y, z), world);
}
public boolean setPosition(int x, int y, int z) {
return this.setPos(NpcAPI.Instance().getIPos(x, y, z));
}
public boolean setPos(int x, int y, int z, IWorld world) {
return this.setPosition(x, y, z, world);
}
public boolean setPos(int x, int y, int z) {
return this.setPosition(x, y, z);
}
public int getX() {
return this.pos.getX();
}
public int getY() {
return this.pos.getY();
}
public int getZ() {
return this.pos.getZ();
}
public void remove() {
this.world.getMCWorld().setBlockToAir(getX(), getY(), getZ());
}
public boolean isAir() {
return this.block.isAir(this.world.getMCWorld(), getX(), getY(), getZ());
}
public IBlock setBlock(String blockName) {
Block block = (Block) Block.blockRegistry.getObject(new ResourceLocation(blockName));
if (block == null) {
return this;
} else {
this.world.getMCWorld().setBlock(getX(), getY(), getZ(), block);
return NpcAPI.Instance().getIBlock(world, getX(), getY(), getZ());
}
}
public IBlock setBlock(IBlock block) {
this.world.getMCWorld().setBlock(getX(), getY(), getZ(), block.getMCBlock());
return NpcAPI.Instance().getIBlock(world, getX(), getY(), getZ());
}
public boolean isContainer() {
return this.tile != null && this.tile.getMCTileEntity() != null && this.tile.getMCTileEntity() instanceof IInventory && ((IInventory) this.tile.getMCTileEntity()).getSizeInventory() > 0;
}
public IContainer getContainer() {
if (!this.isContainer()) {
throw new CustomNPCsException("This block is not a container", new Object[0]);
} else {
return NpcAPI.Instance().getIContainer((IInventory) this.tile.getMCTileEntity());
}
}
public String getName() {
return Block.blockRegistry.getNameForObject(this.block) + "";
}
public String getDisplayName() {
return this.tile == null || this.tile.getMCTileEntity() == null ? this.getName() : this.tile.getMCTileEntity().blockType.getItemIconName();
}
public IWorld getWorld() {
return this.world;
}
public Block getMCBlock() {
return this.block;
}
public boolean hasTileEntity() {
return this.tile != null;
}
public ITileEntity getTileEntity() {
return this.tile;
}
public void setTileEntity(ITileEntity tileEntity) {
world.setTileEntity(pos.getX(), pos.getY(), pos.getZ(), tileEntity);
this.tile = tileEntity;
}
public TileEntity getMCTileEntity() {
if (this.tile == null)
return null;
return this.tile.getMCTileEntity();
}
public INbt getTileEntityNBT() {
if (this.tile == null || this.tile.getMCTileEntity() == null)
return null;
NBTTagCompound compound = new NBTTagCompound();
this.tile.getMCTileEntity().writeToNBT(compound);
return NpcAPI.Instance().getINbt(compound);
}
public boolean canCollide(double maxVolume) {
AxisAlignedBB alignedBB = block.getCollisionBoundingBoxFromPool(world.getMCWorld(), getX(), getY(), getZ());
if (alignedBB == null) {
return false;
}
double xEdge = alignedBB.maxX - alignedBB.minX;
double yEdge = alignedBB.maxY - alignedBB.minY;
double zEdge = alignedBB.maxZ - alignedBB.minZ;
return Math.abs(xEdge * yEdge * zEdge) > maxVolume;
}
public boolean canCollide() {
return canCollide(0);
}
public void setBounds(float minX, float minY, float minZ, float maxX, float maxY, float maxZ) {
this.block.setBlockBounds(minX, minY, minZ, maxX, maxY, maxZ);
}
public double getBlockBoundsMinX() {
return this.block.getBlockBoundsMinX();
}
public double getBlockBoundsMinY() {
return this.block.getBlockBoundsMinY();
}
public double getBlockBoundsMinZ() {
return this.block.getBlockBoundsMinZ();
}
public double getBlockBoundsMaxX() {
return this.block.getBlockBoundsMaxX();
}
public double getBlockBoundsMaxY() {
return this.block.getBlockBoundsMaxY();
}
public double getBlockBoundsMaxZ() {
return this.block.getBlockBoundsMaxZ();
}
public String toString() {
return this.getName() + " @" + this.getPos() + (world == null ? "" : " in DIM" + world.getDimensionID());
}
public boolean equals(Object obj) {
return obj instanceof IBlock && ((ScriptBlock) obj).getName().equals(this.getName()) &&
((ScriptBlock) obj).getWorld() == this.getWorld() &&
((ScriptBlock) obj).getPos().equals(this.getPos());
}
@Override
public int hashCode() {
return Objects.hash(getWorld(), block, getPos());
}
}