forked from Minecraft-Java-Edition-Speedrunning/set-spawn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerPlayerEntityMixin.java
More file actions
76 lines (65 loc) · 3.75 KB
/
ServerPlayerEntityMixin.java
File metadata and controls
76 lines (65 loc) · 3.75 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
package net.set.spawn.mod.mixin;
import com.llamalad7.mixinextras.injector.ModifyExpressionValue;
import com.llamalad7.mixinextras.injector.wrapoperation.*;
import com.llamalad7.mixinextras.sugar.*;
import com.llamalad7.mixinextras.sugar.ref.*;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.text.ChatMessage;
import net.minecraft.util.Formatting;
import net.minecraft.util.math.*;
import net.set.spawn.mod.*;
import net.set.spawn.mod.interfaces.MinecraftServerExtended;
import org.spongepowered.asm.mixin.*;
import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import java.util.Random;
@Mixin(ServerPlayerEntity.class)
public abstract class ServerPlayerEntityMixin {
@Unique
private String setSpawnError;
@Shadow
public abstract void method_5505(ChatMessage message);
@WrapOperation(method = "<init>", at = @At(value = "INVOKE", target = "Ljava/util/Random;nextInt(I)I", ordinal = 0))
private int setSpawnX(Random random, int bounds, Operation<Integer> original, @Local(argsOnly = true) MinecraftServer server, @Local BlockPos worldSpawn, @Local(ordinal = 3) int spawnRadius, @Share("seed") LocalRef<Seed> seed, @Share("zCoord") LocalRef<Integer> zCoord, @Share("isRandomSpawn") LocalBooleanRef isRandomSpawn) {
isRandomSpawn.set(true);
int originalResult = original.call(random, bounds);
if (((MinecraftServerExtended) server).setspawnmod$shouldModifySpawn()) {
((MinecraftServerExtended) server).setspawnmod$setShouldModifySpawn(false);
seed.set(SetSpawn.findSeedObjectFromLong(server.getWorld().getSeed()));
}
Seed seedObject = seed.get();
if (seedObject == null) {
return originalResult;
}
// Transform x and z coordinates into correct Random#nextInt result.
int resultX = MathHelper.floor(seedObject.getX()) - worldSpawn.x + spawnRadius;
int resultZ = MathHelper.floor(seedObject.getZ()) - worldSpawn.z + spawnRadius;
if (resultX >= 0 && resultX < bounds && resultZ >= 0 && resultZ < bounds) {
zCoord.set(resultZ);
System.out.println("Setting spawn");
return resultX;
} else {
this.setSpawnError = "The X or Z coordinates given (" + seed.get().getX() + ", " + seed.get().getZ() + ") are more than the worlds spawn radius (" + spawnRadius + " blocks) away from the world spawn. Not overriding player spawnpoint.";
}
return originalResult;
}
@ModifyExpressionValue(method = "<init>", at = @At(value = "INVOKE", target = "Ljava/util/Random;nextInt(I)I", ordinal = 1))
private int setSpawnZ(int original, @Share("zCoord") LocalRef<Integer> zCoord) {
// if zCoord is not null, it has been validated in the method
return zCoord.get() != null ? zCoord.get() : original;
}
@Inject(method = "<init>", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/ServerPlayerEntity;refreshPositionAndAngles(DDDFF)V"))
private void failOnNonRandomSpawns(CallbackInfo ci, @Share("seed") LocalRef<Seed> seed, @Share("isRandomSpawn") LocalBooleanRef isRandomSpawn) {
if (!isRandomSpawn.get() && seed.get() != null) {
this.setSpawnError = "Failed to apply SetSpawn configuration because the spawn was not random. Not overriding player spawnpoint.";
}
}
@Inject(method = "listenToScreenHandler", at = @At("TAIL"))
private void sendErrorMessage(CallbackInfo ci) {
if (this.setSpawnError != null) {
this.method_5505(ChatMessage.createTextMessage(this.setSpawnError + " This run is not verifiable.").setColor(Formatting.RED));
this.setSpawnError = null;
}
}
}