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
103 lines (89 loc) · 5.1 KB
/
ServerPlayerEntityMixin.java
File metadata and controls
103 lines (89 loc) · 5.1 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
package net.set.spawn.mod.mixin;
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.server.world.ServerWorld;
import net.minecraft.text.*;
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.*;
import java.util.Random;
@Mixin(ServerPlayerEntity.class)
public abstract class ServerPlayerEntityMixin {
@Shadow
@Final
public MinecraftServer server;
@Unique
private String setSpawnError;
@Shadow
public abstract void sendMessage(Text message, boolean actionBar);
@Shadow
public abstract ServerWorld getServerWorld();
@WrapOperation(method = "method_21281", at = @At(value = "INVOKE", target = "Ljava/util/Random;nextInt(I)I"))
private int setSpawn(Random random, int bounds, Operation<Integer> original, @Local(ordinal = 0) BlockPos worldSpawn, @Local(ordinal = 0) int spawnRadius, @Share("seed") LocalRef<Seed> seed, @Share("originalRandomResult") LocalRef<Integer> originalRandomResult, @Share("newRandomValue") LocalRef<Integer> newRandomValue) {
int originalResult = original.call(random, bounds);
if (((MinecraftServerExtended) this.server).setspawnmod$shouldModifySpawn()) {
((MinecraftServerExtended) this.server).setspawnmod$setShouldModifySpawn(false);
seed.set(SetSpawn.findSeedObjectFromLong(this.getServerWorld().method_3588().getSeed()));
}
Seed seedObject = seed.get();
if (seedObject == null) {
return originalResult;
}
// Transform x and z coordinates into corresponding Random#nextInt result.
int spawnDiameter = spawnRadius * 2 + 1;
int x = MathHelper.floor(seedObject.getX());
int z = MathHelper.floor(seedObject.getZ());
int xLocal = x - worldSpawn.getX() + spawnRadius;
int result = xLocal + (z - worldSpawn.getZ() + spawnRadius) * spawnDiameter;
if (xLocal >=0 && xLocal < spawnDiameter && result >= 0 && result < bounds) {
// we save the original result in case the set spawn is invalid, see fallbackOnInvalidSpawn
originalRandomResult.set(originalResult);
newRandomValue.set(result);
System.out.println("Setting spawn");
return result;
} 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;
}
@ModifyVariable(method = "method_21281", at = @At(value = "LOAD", ordinal = 0), ordinal = 5)
private int fallbackOnInvalidSpawn(int p, @Local(ordinal = 2) int k, @Local(ordinal = 4) LocalIntRef o, @Share("seed") LocalRef<Seed> seed, @Share("originalRandomResult") LocalRef<Integer> originalRandomResult, @Share("newRandomValue") LocalRef<Integer> newRandomValue) {
// checks if the for loop is on its second iteration (p == 1), meaning the setspawn given spawn was invalid
// and restores the original result of Random#nextInt
if (p == 1 && originalRandomResult.get() != null) {
o.set(originalRandomResult.get());
originalRandomResult.set(null);
p = 0;
this.setSpawnError = "There is no valid spawning location at the specified coordinates (" + seed.get().getX() + ", " + seed.get().getZ() + "). Not overriding player spawnpoint.";
}
// if we made it to the end of the loop after an obstructed spawn and didn't find another non-obstructed spawn
// redo the last iteration of the loop with the choice obstructed spawn
if (p == k && originalRandomResult.get() == null && newRandomValue.get() != null) {
o.set(newRandomValue.get());
newRandomValue.set(null);
p = k - 1;
this.setSpawnError = null;
}
return p;
}
@Inject(method = "method_21281", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/ServerPlayerEntity;refreshPositionAndAngles(Lnet/minecraft/util/math/BlockPos;FF)V", ordinal = 1))
private void failOnNonRandomSpawns(CallbackInfo ci, @Share("seed") LocalRef<Seed> seed) {
if (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.sendMessage(new LiteralText(this.setSpawnError + " This run is not verifiable.").formatted(Formatting.RED), false);
this.setSpawnError = null;
}
}
}