/
TeleportationTools.java
413 lines (366 loc) · 17.3 KB
/
TeleportationTools.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package mcjty.rftools.blocks.teleporter;
import mcjty.lib.tools.DamageSourceTools;
import mcjty.lib.tools.WorldTools;
import mcjty.lib.varia.GlobalCoordinate;
import mcjty.lib.varia.Logging;
import mcjty.lib.varia.SoundTools;
import mcjty.rftools.Achievements;
import mcjty.rftools.RFTools;
import mcjty.rftools.blocks.environmental.NoTeleportAreaManager;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.potion.Potion;
import net.minecraft.potion.PotionEffect;
import net.minecraft.server.MinecraftServer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.SoundEvent;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
import net.minecraft.world.WorldServer;
import net.minecraftforge.common.DimensionManager;
public class TeleportationTools {
public static final int STATUS_OK = 0;
public static final int STATUS_WARN = 1;
public static final int STATUS_UNKNOWN = 2;
public static Potion confusion;
public static Potion harm;
public static Potion wither;
public static void getPotions() {
if (confusion == null) {
confusion = Potion.REGISTRY.getObject(new ResourceLocation("nausea"));
harm = Potion.REGISTRY.getObject(new ResourceLocation("instant_damage"));
wither = Potion.REGISTRY.getObject(new ResourceLocation("wither"));
}
}
public static void applyEffectForSeverity(EntityPlayer player, int severity, boolean boostNeeded) {
getPotions();
switch (severity) {
case 1:
if (boostNeeded) {
player.addPotionEffect(new PotionEffect(confusion, 100));
player.addPotionEffect(new PotionEffect(harm, 5));
}
break;
case 2:
player.addPotionEffect(new PotionEffect(harm, 100));
break;
case 3:
player.addPotionEffect(new PotionEffect(harm, 100));
player.attackEntityFrom(DamageSourceTools.getGenericSource(), 0.5f);
break;
case 4:
player.addPotionEffect(new PotionEffect(harm, 200));
player.attackEntityFrom(DamageSourceTools.getGenericSource(), 0.5f);
break;
case 5:
player.addPotionEffect(new PotionEffect(harm, 200));
player.attackEntityFrom(DamageSourceTools.getGenericSource(), 1.0f);
break;
case 6:
player.addPotionEffect(new PotionEffect(harm, 300));
player.attackEntityFrom(DamageSourceTools.getGenericSource(), 1.0f);
break;
case 7:
player.addPotionEffect(new PotionEffect(harm, 300));
player.addPotionEffect(new PotionEffect(wither, 200));
player.attackEntityFrom(DamageSourceTools.getGenericSource(), 2.0f);
break;
case 8:
player.addPotionEffect(new PotionEffect(harm, 400));
player.addPotionEffect(new PotionEffect(wither, 300));
player.attackEntityFrom(DamageSourceTools.getGenericSource(), 2.0f);
break;
case 9:
player.addPotionEffect(new PotionEffect(harm, 400));
player.addPotionEffect(new PotionEffect(wither, 400));
player.attackEntityFrom(DamageSourceTools.getGenericSource(), 3.0f);
break;
case 10:
player.addPotionEffect(new PotionEffect(harm, 500));
player.addPotionEffect(new PotionEffect(wither, 500));
player.attackEntityFrom(DamageSourceTools.getGenericSource(), 3.0f);
break;
}
}
/**
* Calculate the cost of doing a dial between a transmitter and a destination.
* @param world
* @param c1 the start coordinate
* @param teleportDestination
* @return
*/
public static int calculateRFCost(World world, BlockPos c1, TeleportDestination teleportDestination) {
if (world.provider.getDimension() != teleportDestination.getDimension()) {
return TeleportConfiguration.rfStartTeleportBaseDim;
} else {
BlockPos c2 = teleportDestination.getCoordinate();
double dist = new Vec3d(c1.getX(), c1.getY(), c1.getZ()).distanceTo(new Vec3d(c2.getX(), c2.getY(), c2.getZ()));
int rf = TeleportConfiguration.rfStartTeleportBaseLocal + (int)(TeleportConfiguration.rfStartTeleportDist * dist);
if (rf > TeleportConfiguration.rfStartTeleportBaseDim) {
rf = TeleportConfiguration.rfStartTeleportBaseDim;
}
return rf;
}
}
/**
* Calculate the time in ticks of doing a dial between a transmitter and a destination.
* @param world
* @param c1 the start coordinate
* @param teleportDestination
* @return
*/
public static int calculateTime(World world, BlockPos c1, TeleportDestination teleportDestination) {
if (world.provider.getDimension() != teleportDestination.getDimension()) {
return TeleportConfiguration.timeTeleportBaseDim;
} else {
BlockPos c2 = teleportDestination.getCoordinate();
double dist = new Vec3d(c1.getX(), c1.getY(), c1.getZ()).distanceTo(new Vec3d(c2.getX(), c2.getY(), c2.getZ()));
int time = TeleportConfiguration.timeTeleportBaseLocal + (int)(TeleportConfiguration.timeTeleportDist * dist / 1000);
if (time > TeleportConfiguration.timeTeleportBaseDim) {
time = TeleportConfiguration.timeTeleportBaseDim;
}
return time;
}
}
// Return true if we needed a boost.
public static boolean performTeleport(EntityPlayer player, TeleportDestination dest, int bad, int good, boolean boosted) {
BlockPos c = dest.getCoordinate();
BlockPos old = new BlockPos((int)player.posX, (int)player.posY, (int)player.posZ);
int oldId = player.getEntityWorld().provider.getDimension();
if (!TeleportationTools.allowTeleport(player, oldId, old, dest.getDimension(), dest.getCoordinate())) {
return false;
}
if (oldId != dest.getDimension()) {
TeleportationTools.teleportToDimension(player, dest.getDimension(), c.getX() + 0.5, c.getY() + 1.5, c.getZ() + 0.5);
} else {
player.setPositionAndUpdate(c.getX()+0.5, c.getY()+1, c.getZ()+0.5);
}
if (TeleportConfiguration.whooshMessage) {
Logging.message(player, "Whoosh!");
}
Achievements.trigger(player, Achievements.firstTeleport);
boolean boostNeeded = false;
int severity = consumeReceiverEnergy(player, dest.getCoordinate(), dest.getDimension());
if (severity > 0 && boosted) {
boostNeeded = true;
severity = 1;
}
severity = applyBadEffectIfNeeded(player, severity, bad, good, boostNeeded);
if (severity <= 0) {
if (TeleportConfiguration.teleportVolume >= 0.01) {
SoundEvent sound = SoundEvent.REGISTRY.getObject(new ResourceLocation(RFTools.MODID + ":teleport_whoosh"));
SoundTools.playSound(player.getEntityWorld(), sound, player.posX, player.posY, player.posZ, TeleportConfiguration.teleportVolume, 1.0f);
}
}
if (TeleportConfiguration.logTeleportUsages) {
Logging.log("Teleport: Player " + player.getName() + " from " + old + " (dim " + oldId + ") to " + dest.getCoordinate() + " (dim " + dest.getDimension() + ") with severity " + severity);
}
return boostNeeded;
}
/**
* Get a world for a dimension, possibly loading it from the configuration manager.
*/
public static World getWorldForDimension(World world, int id) {
World w = DimensionManager.getWorld(id);
if (w == null) {
w = world.getMinecraftServer().worldServerForDimension(id);
}
return w;
}
// Server side only
public static int dial(World worldObj, DialingDeviceTileEntity dialingDeviceTileEntity, String player, BlockPos transmitter, int transDim, BlockPos coordinate, int dimension, boolean once) {
World transWorld = getWorldForDimension(worldObj, transDim);
if (transWorld == null) {
return DialingDeviceTileEntity.DIAL_INVALID_SOURCE_MASK;
}
MatterTransmitterTileEntity transmitterTileEntity = (MatterTransmitterTileEntity) transWorld.getTileEntity(transmitter);
if (transmitterTileEntity == null) {
return DialingDeviceTileEntity.DIAL_INVALID_TRANSMITTER;
}
if (player != null && !transmitterTileEntity.checkAccess(player)) {
return DialingDeviceTileEntity.DIAL_TRANSMITTER_NOACCESS;
}
if (coordinate == null) {
transmitterTileEntity.setTeleportDestination(null, false);
return DialingDeviceTileEntity.DIAL_INTERRUPTED;
}
TeleportDestination teleportDestination = findDestination(worldObj, coordinate, dimension);
if (teleportDestination == null) {
return DialingDeviceTileEntity.DIAL_INVALID_DESTINATION_MASK;
}
BlockPos c = teleportDestination.getCoordinate();
World recWorld = getWorldForDimension(worldObj, teleportDestination.getDimension());
if (recWorld == null) {
recWorld = worldObj.getMinecraftServer().worldServerForDimension(teleportDestination.getDimension());
if (recWorld == null) {
return DialingDeviceTileEntity.DIAL_INVALID_DESTINATION_MASK;
}
}
// Only do this if not an rftools dimension.
TileEntity tileEntity = recWorld.getTileEntity(c);
if (!(tileEntity instanceof MatterReceiverTileEntity)) {
return DialingDeviceTileEntity.DIAL_INVALID_DESTINATION_MASK;
}
MatterReceiverTileEntity matterReceiverTileEntity = (MatterReceiverTileEntity) tileEntity;
matterReceiverTileEntity.updateDestination(); // Make sure destination is ok.
if (player != null && !matterReceiverTileEntity.checkAccess(player)) {
return DialingDeviceTileEntity.DIAL_RECEIVER_NOACCESS;
}
if (!checkBeam(transmitter, transWorld, 1, 4, 2)) {
return DialingDeviceTileEntity.DIAL_TRANSMITTER_BLOCKED_MASK;
}
if (dialingDeviceTileEntity != null) {
int cost = TeleportConfiguration.rfPerDial;
cost = (int) (cost * (2.0f - dialingDeviceTileEntity.getInfusedFactor()) / 2.0f);
if (dialingDeviceTileEntity.getEnergyStored(EnumFacing.DOWN) < cost) {
return DialingDeviceTileEntity.DIAL_DIALER_POWER_LOW_MASK;
}
dialingDeviceTileEntity.consumeEnergy(cost);
}
transmitterTileEntity.setTeleportDestination(teleportDestination, once);
return DialingDeviceTileEntity.DIAL_OK;
}
/**
* Consume energy on the receiving side and return a number indicating how good this went.
*
* @param c
* @param dimension
* @return 0 in case of success. 10 in case of severe failure
*/
private static int consumeReceiverEnergy(EntityPlayer player, BlockPos c, int dimension) {
World world = DimensionManager.getWorld(dimension);
TileEntity te = world.getTileEntity(c);
if (!(te instanceof MatterReceiverTileEntity)) {
Logging.warn(player, "Something went wrong with the destination!");
return 0;
}
MatterReceiverTileEntity matterReceiverTileEntity = (MatterReceiverTileEntity) te;
int rf = TeleportConfiguration.rfPerTeleportReceiver;
rf = (int) (rf * (2.0f - matterReceiverTileEntity.getInfusedFactor()) / 2.0f);
if (rf <= 0) {
return 0;
}
int extracted = rf;
if (rf > matterReceiverTileEntity.getEnergyStored(EnumFacing.DOWN)) {
extracted = matterReceiverTileEntity.getEnergyStored(EnumFacing.DOWN);
}
matterReceiverTileEntity.consumeEnergy(rf);
int remainingRf = matterReceiverTileEntity.getEnergyStored(EnumFacing.DOWN);
if (remainingRf <= 1) {
Logging.warn(player, "The matter receiver has run out of power!");
} else if (remainingRf < (TeleportConfiguration.RECEIVER_MAXENERGY / 10)) {
Logging.warn(player, "The matter receiver is getting very low on power!");
} else if (remainingRf < (TeleportConfiguration.RECEIVER_MAXENERGY / 5)) {
Logging.warn(player, "The matter receiver is getting low on power!");
}
return 10 - (extracted * 10 / rf);
}
/**
* Return a number between 0 and 10 indicating the severity of the teleportation.
* @return
*/
public static int calculateSeverity(int bad, int total) {
if (total == 0) {
total = 1;
}
int severity = bad * 10 / total;
if (mustInterrupt(bad, total)) {
// If an interrupt was done then severity is worse.
severity += 2;
}
if (severity > 10) {
severity = 10;
}
return severity;
}
public static int applyBadEffectIfNeeded(EntityPlayer player, int severity, int bad, int total, boolean boostNeeded) {
if (player == null) {
return 0;
}
severity += calculateSeverity(bad, total);
if (severity > 10) {
severity = 10;
}
if (severity <= 0) {
return 0;
}
if (TeleportConfiguration.teleportErrorVolume >= 0.01) {
SoundEvent sound = SoundEvent.REGISTRY.getObject(new ResourceLocation(RFTools.MODID + ":teleport_error"));
SoundTools.playSound(player.getEntityWorld(), sound, player.posX, player.posY, player.posZ, TeleportConfiguration.teleportVolume, 1.0f);
}
applyEffectForSeverity(player, severity, boostNeeded);
return severity;
}
public static boolean mustInterrupt(int bad, int total) {
return bad > (total / 2);
}
public static boolean allowTeleport(Entity entity, int sourceDim, BlockPos source, int destDim, BlockPos dest) {
if (NoTeleportAreaManager.isTeleportPrevented(entity, new GlobalCoordinate(source, sourceDim))) {
return false;
}
if (NoTeleportAreaManager.isTeleportPrevented(entity, new GlobalCoordinate(dest, destDim))) {
return false;
}
return true;
}
public static void teleportToDimension(EntityPlayer player, int dimension, double x, double y, double z) {
int oldDimension = player.getEntityWorld().provider.getDimension();
EntityPlayerMP entityPlayerMP = (EntityPlayerMP) player;
MinecraftServer server = player.getEntityWorld().getMinecraftServer();
WorldServer worldServer = server.worldServerForDimension(dimension);
player.addExperienceLevel(0);
worldServer.getMinecraftServer().getPlayerList().transferPlayerToDimension(entityPlayerMP, dimension, new RfToolsTeleporter(worldServer, x, y, z));
player.setPositionAndUpdate(x, y, z);
if (oldDimension == 1) {
// For some reason teleporting out of the end does weird things.
player.setPositionAndUpdate(x, y, z);
WorldTools.spawnEntity(worldServer, player);
worldServer.updateEntityWithOptionalForce(player, false);
}
}
public static TeleportDestination findDestination(World worldObj, BlockPos coordinate, int dimension) {
TeleportDestinations destinations = TeleportDestinations.getDestinations(worldObj);
return destinations.getDestination(coordinate, dimension);
}
// Check if there is room for a beam.
public static boolean checkBeam(BlockPos c, World world, int dy1, int dy2, int errory) {
for (int dy = dy1 ; dy <= dy2 ; dy++) {
BlockPos pos = new BlockPos(c.getX(), c.getY() + dy, c.getZ());
IBlockState state = world.getBlockState(pos);
Block b = state.getBlock();
if (!b.isAir(state, world, pos)) {
if (dy <= errory) {
// Everything below errory must be free.
return false;
} else {
// Everything higher then errory doesn't have to be free.
break;
}
}
}
return true;
}
public static boolean checkValidTeleport(EntityPlayer player, int srcId, int dstId) {
if (TeleportConfiguration.preventInterdimensionalTeleports) {
if (srcId == dstId) {
Logging.warn(player, "Teleportation in the same dimension is not allowed!");
return false;
}
}
if (TeleportConfiguration.getBlacklistedTeleportationDestinations().contains(dstId)) {
Logging.warn(player, "Teleportation to that dimension is not allowed!");
return false;
}
if (TeleportConfiguration.getBlacklistedTeleportationSources().contains(srcId)) {
Logging.warn(player, "Teleportation from this dimension is not allowed!");
return false;
}
return true;
}
}