-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathRandomTPAPI.java
More file actions
247 lines (227 loc) · 13.3 KB
/
RandomTPAPI.java
File metadata and controls
247 lines (227 loc) · 13.3 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
235
236
237
238
239
240
241
242
243
244
245
246
247
package com.gmail.picono435.randomtp.api;
import com.gmail.picono435.randomtp.RandomTPMod;
import com.gmail.picono435.randomtp.config.Config;
import com.gmail.picono435.randomtp.config.Messages;
import com.mojang.datafixers.util.Pair;
import dev.architectury.injectables.annotations.ExpectPlatform;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.CactusBlock;
import net.minecraft.world.level.block.LiquidBlock;
import net.minecraft.world.level.portal.TeleportTransition;
import net.minecraft.world.phys.Vec3;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class RandomTPAPI {
private static final ExecutorService executorService = Executors.newFixedThreadPool(10);
public static Future<Boolean> randomTeleport(ServerPlayer player, ServerLevel world) {
return randomTeleport(player, world, null);
}
public static Future<Boolean> randomTeleport(ServerPlayer player, ServerLevel world, ResourceKey<Biome> biomeResourceKey) {
return executorService.submit(() -> {
try {
Random random = new Random();
BlockPos.MutableBlockPos mutableBlockPos = new BlockPos.MutableBlockPos();
if(biomeResourceKey == null) {
Pair<Integer, Integer> coordinates = generateCoordinates(world, player, random);
int x = coordinates.getFirst();
int z = coordinates.getSecond();
mutableBlockPos.setX(x);
mutableBlockPos.setY(50);
mutableBlockPos.setZ(z);
} else {
Pair<BlockPos, Holder<Biome>> pair = world.findClosestBiome3d(biomeHolder -> biomeHolder.is(biomeResourceKey), player.getOnPos(), 6400, 32, 64);
if(pair == null) {
Component msg = Component.literal(Messages.getMaxTries().replaceAll("\\{playerName\\}", player.getName().getString()).replaceAll("&", "§"));
player.sendSystemMessage(msg, false);
return false;
}
mutableBlockPos.setX(pair.getFirst().getX());
mutableBlockPos.setY(50);
mutableBlockPos.setZ(pair.getFirst().getZ());
if(!world.getWorldBorder().isWithinBounds(mutableBlockPos)) {
Component msg = Component.literal(Messages.getMaxTries().replaceAll("\\{playerName\\}", player.getName().getString()).replaceAll("&", "§"));
player.sendSystemMessage(msg, false);
return false;
}
}
int maxTries = Config.getMaxTries();
int y = mutableBlockPos.getY();
while (!isSafe(world, mutableBlockPos) && (maxTries == -1 || maxTries > 0)) {
y++;
mutableBlockPos.setY(y);
if(mutableBlockPos.getY() >= 200 || !isInBiomeWhitelist(world.getBiome(mutableBlockPos.immutable()).unwrapKey().get().location())) {
if(biomeResourceKey != null) {
Pair<BlockPos, Holder<Biome>> pair = world.findClosestBiome3d(biomeHolder -> biomeHolder.is(biomeResourceKey), player.getOnPos(), 6400, 32, 64);
if(pair == null) {
Component msg = Component.literal(Messages.getMaxTries().replaceAll("\\{playerName\\}", player.getName().getString()).replaceAll("&", "§"));
player.sendSystemMessage(msg, false);
return false;
}
mutableBlockPos.setX(pair.getFirst().getX());
mutableBlockPos.setY(50);
mutableBlockPos.setZ(pair.getFirst().getZ());
if(!world.getWorldBorder().isWithinBounds(mutableBlockPos)) {
Component msg = Component.literal(Messages.getMaxTries().replaceAll("\\{playerName\\}", player.getName().getString()).replaceAll("&", "§"));
player.sendSystemMessage(msg, false);
return false;
}
continue;
}
Pair<Integer, Integer> coordinates = generateCoordinates(world, player, random);
int x = coordinates.getFirst();
int z = coordinates.getSecond();
mutableBlockPos.setX(x);
mutableBlockPos.setY(50);
mutableBlockPos.setZ(z);
continue;
}
if(maxTries > 0){
maxTries--;
}
if(maxTries == 0) {
Component msg = Component.literal(Messages.getMaxTries().replaceAll("\\{playerName\\}", player.getName().getString()).replaceAll("&", "§"));
player.sendSystemMessage(msg, false);
return false;
}
}
player.getServer().submit(() -> {
TeleportTransition teleportTransition = new TeleportTransition(world, mutableBlockPos.getCenter(), Vec3.ZERO, player.getYRot(), player.getXRot(), false, false, Set.of(), TeleportTransition.DO_NOTHING);
player.teleport(teleportTransition);
Component successful = Component.literal(Messages.getSuccessful().replaceAll("\\{playerName\\}", player.getName().getString()).replaceAll("\\{blockX\\}", "" + (int)player.position().x).replaceAll("\\{blockY\\}", "" + (int)player.position().y).replaceAll("\\{blockZ\\}", "" + (int)player.position().z).replaceAll("&", "§"));
player.sendSystemMessage(successful, false);
});
return true;
} catch(Exception ex) {
RandomTPMod.getLogger().info("Error executing command.");
ex.printStackTrace();
return false;
}
});
}
private static Pair<Integer, Integer> generateCoordinates(ServerLevel world, Player player, Random random) {
// Calculating X coordinates
int x;
if(random.nextInt(2) == 1) {
// Calculating X coordinates from min to max
int maxDistance = Config.getMaxDistance() == 0 ? (int) world.getWorldBorder().getMinX() : (int) (player.getX() + Config.getMaxDistance());
if(maxDistance < world.getWorldBorder().getMinX()) maxDistance = (int) world.getWorldBorder().getMinX();
int minDistance = (int) (player.getX() - Config.getMinDistance());
if(minDistance < world.getWorldBorder().getMinX()) minDistance = (int) (world.getWorldBorder().getMinX() + 10);
if(maxDistance < minDistance) maxDistance = maxDistance ^ minDistance ^ (minDistance = maxDistance);
if(maxDistance == minDistance) minDistance = minDistance - 1;
x = random.nextInt(maxDistance - minDistance) + minDistance;
} else {
// Calculating X coordinates from max to min
int maxDistance = Config.getMaxDistance() == 0 ? (int) world.getWorldBorder().getMaxX() : (int) (player.getX() - Config.getMaxDistance());
if(maxDistance > world.getWorldBorder().getMaxX()) maxDistance = (int) world.getWorldBorder().getMaxX();
int minDistance = (int) (player.getX() + Config.getMinDistance());
if(minDistance > world.getWorldBorder().getMaxX()) minDistance = (int) (world.getWorldBorder().getMaxX() - 10);
if(maxDistance < minDistance) maxDistance = maxDistance ^ minDistance ^ (minDistance = maxDistance);
if(maxDistance == minDistance) minDistance = minDistance - 1;
x = random.nextInt(maxDistance - minDistance) + minDistance;
}
int z;
if(random.nextInt(2) == 1) {
// Calculating Z coordinates from min to max
int maxDistance = Config.getMaxDistance() == 0 ? (int) world.getWorldBorder().getMinZ() : (int) (player.getZ() + Config.getMaxDistance());
if(maxDistance < world.getWorldBorder().getMinZ()) maxDistance = (int) world.getWorldBorder().getMinZ();
int minDistance = (int) (player.getZ() - Config.getMinDistance());
if(minDistance < world.getWorldBorder().getMinZ()) minDistance = (int) (world.getWorldBorder().getMinZ() + 10);
if(maxDistance < minDistance) maxDistance = maxDistance ^ minDistance ^ (minDistance = maxDistance);
if(maxDistance == minDistance) minDistance = minDistance - 1;
z = random.nextInt(maxDistance - minDistance) + minDistance;
} else {
// Calculating Z coordinates from max to min
int maxDistance = Config.getMaxDistance() == 0 ? (int) world.getWorldBorder().getMaxZ() : (int) (player.getZ() - Config.getMaxDistance());
if(maxDistance > world.getWorldBorder().getMaxZ()) maxDistance = (int) world.getWorldBorder().getMaxZ();
int minDistance = (int) (player.getZ() + Config.getMinDistance());
if(minDistance > world.getWorldBorder().getMaxZ()) minDistance = (int) (world.getWorldBorder().getMaxZ() - 10);
if(maxDistance < minDistance) maxDistance = maxDistance ^ minDistance ^ (minDistance = maxDistance);
if(maxDistance == minDistance) minDistance = minDistance - 1;
z = random.nextInt(maxDistance - minDistance) + minDistance;
}
return new Pair<>(x, z);
}
public static ServerLevel getWorld(String world, MinecraftServer server) {
try {
ResourceLocation resourcelocation = ResourceLocation.tryParse(world);
ResourceKey<Level> registrykey = ResourceKey.create(Registries.DIMENSION, resourcelocation);
ServerLevel worldTo = server.getLevel(registrykey);
return worldTo;
} catch(Exception ex) {
ex.printStackTrace();
return null;
}
}
public static boolean checkCooldown(ServerPlayer player, Map<String, Long> cooldowns) {
int cooldownTime = Config.getCooldown();
if(cooldowns.containsKey(player.getName().getString())) {
long secondsLeft = ((cooldowns.get(player.getName().getString())/1000)+cooldownTime) - (System.currentTimeMillis()/1000);
return secondsLeft <= 0;
} else {
return true;
}
}
public static long getCooldownLeft(ServerPlayer player, Map<String, Long> cooldowns) {
int cooldownTime = Config.getCooldown();
long secondsLeft = ((cooldowns.get(player.getName().getString())/1000)+cooldownTime) - (System.currentTimeMillis()/1000);
return secondsLeft;
}
@ExpectPlatform
public static boolean hasPermission(ServerPlayer player, String permission) {
throw new AssertionError();
}
@ExpectPlatform
public static boolean hasPermission(CommandSourceStack source, String permission) {
throw new AssertionError();
}
public static boolean isSafe(ServerLevel world, BlockPos.MutableBlockPos mutableBlockPos) {
return isEmpty(world, mutableBlockPos) && !isDangerBlocks(world, mutableBlockPos) && world.getWorldBorder().isWithinBounds(mutableBlockPos);
}
public static boolean isEmpty(ServerLevel world, BlockPos.MutableBlockPos mutableBlockPos) {
return world.isEmptyBlock(mutableBlockPos.offset(0, 1, 0)) && world.isEmptyBlock(mutableBlockPos);
}
public static boolean isDangerBlocks(ServerLevel world, BlockPos.MutableBlockPos mutableBlockPos) {
if(isDangerBlock(world, mutableBlockPos) && isDangerBlock(world, mutableBlockPos.offset(0, 1, 0)) &&
isDangerBlock(world, mutableBlockPos.offset(0, -1, 0))) {
return true;
}
return world.getBlockState(mutableBlockPos.offset(0, -1, 0)).getBlock() == Blocks.AIR;
}
public static boolean isDangerBlock(ServerLevel world, BlockPos mutableBlockPos) {
return world.getBlockState(mutableBlockPos).getBlock() instanceof LiquidBlock
|| world.getBlockState(mutableBlockPos).getBlock() instanceof CactusBlock;
}
private static boolean isInBiomeWhitelist(ResourceLocation biome) {
//WHITELIST
if(Config.useBiomeWhitelist()) {
if(biome == null) {
return false;
}
return Config.getAllowedBiomes().contains(biome.toString());
//BLACKLIST
} else {
if(biome == null) {
return true;
}
return !Config.getAllowedBiomes().contains(biome.toString());
}
}
}