-
Notifications
You must be signed in to change notification settings - Fork 497
/
Identifier.java
216 lines (177 loc) · 7.96 KB
/
Identifier.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
package buildcraft.api.core;
import java.util.Map;
import java.util.UUID;
import java.util.function.Supplier;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.server.MinecraftServer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import buildcraft.api.power.bc8.IPowerConnection;
/** Identifies an in world tile entity or movable entity. Feel free to extend this class if your tile entity contains
* multiple identifiable {@link IPowerConnection}. Just make sure to register it with
* {@link #registerType(String, Supplier)} to make loading work.
* <p>
* Created on 1 Jan 2016 by AlexIIL */
public abstract class Identifier {
private static Map<String, Supplier<Identifier>> registeredIdentifiers;
static {
registeredIdentifiers.put("mincraft:tile", () -> new Tile(0, null));
registeredIdentifiers.put("mincraft:entity", () -> new MovableEntity(0, null));
}
protected final int dimId;
public static void registerType(String type, Supplier<Identifier> constructor) {
if (constructor == null) throw new NullPointerException("constructor");
if (constructor.get() == null) throw new NullPointerException("constructor.get()");
registeredIdentifiers.put(type, constructor);
}
public static Identifier getFor(Object obj) {
if (obj == null) throw new NullPointerException("obj");
if (obj instanceof TileEntity) {
return Tile.create((TileEntity) obj);
}
if (obj instanceof Entity) {
return MovableEntity.create((Entity) obj);
}
if (obj instanceof NBTTagCompound) {
NBTTagCompound nbt = (NBTTagCompound) obj;
String type = nbt.getString("type");
if (!registeredIdentifiers.containsKey(type)) return null;
return registeredIdentifiers.get(type).get().load(nbt.getCompoundTag("data"));
}
throw new IllegalArgumentException("Unknown type " + obj.getClass());
}
public static NBTTagCompound save(Identifier ident) {
NBTTagCompound tag = new NBTTagCompound();
tag.setString("type", ident.typeId());
tag.setTag("data", ident.save());
return tag;
}
private Identifier(int dimId) {
this.dimId = dimId;
}
public abstract Object getByIdentifier(MinecraftServer server);
public abstract boolean isLoaded(MinecraftServer server);
protected abstract Identifier load(NBTTagCompound tag);
protected abstract NBTTagCompound save();
public abstract String typeId();
@SideOnly(Side.CLIENT)
public abstract Object getByIdentifierClient();
@SideOnly(Side.CLIENT)
public abstract boolean isLoadedClient();
public static class Tile extends Identifier {
private final BlockPos pos;
public static Tile create(TileEntity tile) {
if (tile.getWorld() == null) throw new NullPointerException("tile.getWorld() for the tile " + tile.getClass());
if (tile.getPos() == null) throw new NullPointerException("tile.getPos() for the tile " + tile.getClass());
return new Tile(tile.getWorld().provider.getDimensionId(), tile.getPos());
}
private Tile(int dimId, BlockPos pos) {
super(dimId);
this.pos = pos;
}
@Override
public Tile load(NBTTagCompound tag) {
int dimId = tag.getInteger("dimId");
int[] arr = tag.getIntArray("pos");
if (arr.length == 0) return null;
return new Tile(dimId, new BlockPos(arr[0], arr[1], arr[2]));
}
@Override
protected NBTTagCompound save() {
NBTTagCompound tag = new NBTTagCompound();
tag.setInteger("dimId", dimId);
tag.setIntArray("pos", new int[] { pos.getX(), pos.getY(), pos.getZ() });
return tag;
}
@Override
public String typeId() {
return "minecraft:tile";
}
@Override
public TileEntity getByIdentifier(MinecraftServer server) {
World world = server.worldServerForDimension(dimId);
if (world == null) return null;
return world.getTileEntity(pos);
}
@Override
public boolean isLoaded(MinecraftServer server) {
World world = server.worldServerForDimension(dimId);
if (world == null) return false;
return world.getTileEntity(pos) != null;
}
@Override
@SideOnly(Side.CLIENT)
public TileEntity getByIdentifierClient() {
World world = Minecraft.getMinecraft().theWorld;
if (world == null || world.provider.getDimensionId() != dimId) return null;
return world.getTileEntity(pos);
}
@Override
@SideOnly(Side.CLIENT)
public boolean isLoadedClient() {
World world = Minecraft.getMinecraft().theWorld;
if (world == null || world.provider.getDimensionId() != dimId) return false;
return world.getTileEntity(pos) != null;
}
}
public static class MovableEntity extends Identifier {
private final UUID uniqueId;
public static MovableEntity create(Entity ent) {
if (ent.getEntityWorld() == null) throw new NullPointerException("ent.getEntityWorld() for the entity " + ent.getClass());
if (ent.getPersistentID() == null) throw new NullPointerException("ent.getPersistentID() for the entity " + ent.getClass());
return new MovableEntity(ent.getEntityWorld().provider.getDimensionId(), ent.getPersistentID());
}
private MovableEntity(int dimId, UUID uniqueId) {
super(dimId);
this.uniqueId = uniqueId;
}
@Override
public Entity getByIdentifier(MinecraftServer server) {
World world = server.worldServerForDimension(dimId);
if (world == null || world.provider.getDimensionId() != dimId) return null;
return world.loadedEntityList.stream().filter(e -> e.getPersistentID().equals(uniqueId)).findFirst().orElse(null);
}
@Override
public boolean isLoaded(MinecraftServer server) {
World world = server.worldServerForDimension(dimId);
if (world == null || world.provider.getDimensionId() != dimId) return false;
return world.loadedEntityList.stream().filter(e -> e.getPersistentID().equals(uniqueId)).findFirst().orElse(null) != null;
}
@Override
@SideOnly(Side.CLIENT)
public Entity getByIdentifierClient() {
World world = Minecraft.getMinecraft().theWorld;
if (world == null || world.provider.getDimensionId() != dimId) return null;
return world.loadedEntityList.stream().filter(e -> e.getPersistentID().equals(uniqueId)).findFirst().orElse(null);
}
@Override
@SideOnly(Side.CLIENT)
public boolean isLoadedClient() {
World world = Minecraft.getMinecraft().theWorld;
if (world == null || world.provider.getDimensionId() != dimId) return false;
return world.loadedEntityList.stream().filter(e -> e.getPersistentID().equals(uniqueId)).findFirst().orElse(null) != null;
}
@Override
protected MovableEntity load(NBTTagCompound tag) {
int dimId = tag.getInteger("dimId");
UUID uuid = UUID.fromString(tag.getString("uuid"));
return new MovableEntity(dimId, uuid);
}
@Override
protected NBTTagCompound save() {
NBTTagCompound tag = new NBTTagCompound();
tag.setInteger("dimId", dimId);
tag.setString("uuid", uniqueId.toString());
return tag;
}
@Override
public String typeId() {
return "minecraft:entity";
}
}
}