Skip to content

Commit 492fb17

Browse files
committed
TeleportCommand.java re-written.
Signed-off-by: Pavel Erokhin (MairwunNx) <MairwunNx@gmail.com>
1 parent b99f5cb commit 492fb17

1 file changed

Lines changed: 33 additions & 124 deletions

File tree

Lines changed: 33 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,49 @@
1+
/*
2+
* ! This command implementation by Mojang Studios!
3+
*
4+
* Decompiled with idea source code was converted to kotlin code.
5+
* But with additions such as permissions checking and etc.
6+
*
7+
* 1. This can be bad code.
8+
* 2. This file can be not formatter pretty.
9+
*/
110
package com.mairwunnx.projectessentials.core.impl.vanilla.commands;
211

312
import com.mairwunnx.projectessentials.core.api.v1.commands.CommandAPI;
413
import com.mairwunnx.projectessentials.core.api.v1.commands.back.BackLocationAPI;
5-
import com.mairwunnx.projectessentials.core.api.v1.configuration.ConfigurationAPI;
6-
import com.mairwunnx.projectessentials.core.impl.configurations.GeneralConfiguration;
714
import com.mojang.brigadier.CommandDispatcher;
815
import com.mojang.brigadier.exceptions.CommandSyntaxException;
916
import com.mojang.brigadier.tree.LiteralCommandNode;
10-
import net.minecraft.command.CommandException;
1117
import net.minecraft.command.CommandSource;
1218
import net.minecraft.command.Commands;
1319
import net.minecraft.command.arguments.*;
1420
import net.minecraft.entity.Entity;
15-
import net.minecraft.entity.LivingEntity;
1621
import net.minecraft.entity.player.ServerPlayerEntity;
1722
import net.minecraft.network.play.server.SPlayerPositionLookPacket;
18-
import net.minecraft.util.math.*;
19-
import net.minecraft.util.text.Style;
23+
import net.minecraft.util.math.Vec2f;
24+
import net.minecraft.util.math.Vec3d;
2025
import net.minecraft.util.text.TranslationTextComponent;
2126
import net.minecraft.world.server.ServerWorld;
22-
import net.minecraft.world.server.TicketType;
27+
import org.jetbrains.annotations.NotNull;
2328

2429
import javax.annotation.Nullable;
2530
import java.util.Collection;
2631
import java.util.Collections;
2732
import java.util.EnumSet;
2833
import java.util.Set;
2934

30-
import static com.mairwunnx.projectessentials.core.api.v1.InternalConstantsKt.SETTING_LOC_ENABLED;
31-
import static com.mairwunnx.projectessentials.core.api.v1.extensions.ExtensionsKt.hoverEventFrom;
32-
import static com.mairwunnx.projectessentials.core.api.v1.extensions.ExtensionsKt.textComponentFrom;
33-
import static com.mairwunnx.projectessentials.core.api.v1.permissions.PermissionAPIKt.hasPermission;
34-
35-
@SuppressWarnings("CodeBlock2Expr")
36-
public class TeleportCommand {
37-
private static GeneralConfiguration generalConfiguration = null;
38-
39-
private static GeneralConfiguration getGeneralConfiguration() {
40-
if (generalConfiguration == null) {
41-
generalConfiguration = ConfigurationAPI.INSTANCE.getConfigurationByName("general");
42-
}
43-
return generalConfiguration;
35+
public class TeleportCommand extends VanillaCommandBase {
36+
public TeleportCommand() {
37+
super("teleport");
4438
}
4539

46-
public static void register(CommandDispatcher<CommandSource> dispatcher) {
47-
CommandAPI.INSTANCE.removeCommand("teleport");
40+
@Override
41+
public void register(@NotNull CommandDispatcher<CommandSource> dispatcher) {
42+
super.register(dispatcher);
4843
CommandAPI.INSTANCE.removeCommand("tp");
49-
50-
LiteralCommandNode<CommandSource> literalcommandnode = dispatcher.register(Commands.literal("teleport").then(Commands.argument("targets", EntityArgument.entities()).then(Commands.argument("location", Vec3Argument.vec3()).executes((p_198807_0_) -> {
44+
LiteralCommandNode<CommandSource> literalcommandnode = dispatcher.register(Commands.literal("teleport").requires((p_198816_0_) -> {
45+
return isAllowed(p_198816_0_, "teleport", 2);
46+
}).then(Commands.argument("targets", EntityArgument.entities()).then(Commands.argument("location", Vec3Argument.vec3()).executes((p_198807_0_) -> {
5147
return teleportToPos(p_198807_0_.getSource(), EntityArgument.getEntities(p_198807_0_, "targets"), p_198807_0_.getSource().getWorld(), Vec3Argument.getLocation(p_198807_0_, "location"), null, null);
5248
}).then(Commands.argument("rotation", RotationArgument.rotation()).executes((p_198811_0_) -> {
5349
return teleportToPos(p_198811_0_.getSource(), EntityArgument.getEntities(p_198811_0_, "targets"), p_198811_0_.getSource().getWorld(), Vec3Argument.getLocation(p_198811_0_, "location"), RotationArgument.getRotation(p_198811_0_, "rotation"), null);
@@ -67,51 +63,13 @@ public static void register(CommandDispatcher<CommandSource> dispatcher) {
6763
dispatcher.register(Commands.literal("tp").redirect(literalcommandnode));
6864
}
6965

70-
private static void checkPermissions(CommandSource source) {
71-
try {
72-
if (!hasPermission(
73-
source.asPlayer(), "native.teleport", 2
74-
)) {
75-
throw new CommandException(
76-
textComponentFrom(
77-
source.asPlayer(),
78-
getGeneralConfiguration().getBoolOrDefault(SETTING_LOC_ENABLED, false),
79-
"native.command.restricted"
80-
).setStyle(
81-
new Style().setHoverEvent(
82-
hoverEventFrom(
83-
source.asPlayer(),
84-
getGeneralConfiguration().getBoolOrDefault(SETTING_LOC_ENABLED, false),
85-
"native.command.restricted_hover",
86-
"native.teleport", "2"
87-
)
88-
)
89-
)
90-
);
91-
}
92-
} catch (CommandSyntaxException e) {
93-
// ignored, because command executed by server.
94-
}
95-
}
96-
9766
private static int teleportToEntity(CommandSource source, Collection<? extends Entity> targets, Entity destination) {
98-
checkPermissions(source);
99-
100-
for (Entity entity : targets) {
101-
teleport(source, entity, (ServerWorld) destination.world, destination.posX, destination.posY, destination.posZ, EnumSet.noneOf(SPlayerPositionLookPacket.Flags.class), destination.rotationYaw, destination.rotationPitch, null);
102-
}
103-
104-
if (targets.size() == 1) {
105-
source.sendFeedback(new TranslationTextComponent("commands.teleport.success.entity.single", targets.iterator().next().getDisplayName(), destination.getDisplayName()), true);
106-
} else {
107-
source.sendFeedback(new TranslationTextComponent("commands.teleport.success.entity.multiple", targets.size(), destination.getDisplayName()), true);
108-
}
109-
110-
return targets.size();
67+
commitBack(source);
68+
return net.minecraft.command.impl.TeleportCommand.teleportToEntity(source, targets, destination);
11169
}
11270

11371
private static int teleportToPos(CommandSource source, Collection<? extends Entity> targets, ServerWorld worldIn, ILocationArgument position, @Nullable ILocationArgument rotationIn, @Nullable Facing facing) {
114-
checkPermissions(source);
72+
commitBack(source);
11573
Vec3d vec3d = position.getPosition(source);
11674
Vec2f vec2f = rotationIn == null ? null : rotationIn.getRotation(source);
11775
Set<SPlayerPositionLookPacket.Flags> set = EnumSet.noneOf(SPlayerPositionLookPacket.Flags.class);
@@ -142,9 +100,9 @@ private static int teleportToPos(CommandSource source, Collection<? extends Enti
142100

143101
for (Entity entity : targets) {
144102
if (rotationIn == null) {
145-
teleport(source, entity, worldIn, vec3d.x, vec3d.y, vec3d.z, set, entity.rotationYaw, entity.rotationPitch, facing);
103+
net.minecraft.command.impl.TeleportCommand.teleport(source, entity, worldIn, vec3d.x, vec3d.y, vec3d.z, set, entity.rotationYaw, entity.rotationPitch, facing);
146104
} else {
147-
teleport(source, entity, worldIn, vec3d.x, vec3d.y, vec3d.z, set, vec2f.y, vec2f.x, facing);
105+
net.minecraft.command.impl.TeleportCommand.teleport(source, entity, worldIn, vec3d.x, vec3d.y, vec3d.z, set, vec2f.y, vec2f.x, facing);
148106
}
149107
}
150108

@@ -157,62 +115,6 @@ private static int teleportToPos(CommandSource source, Collection<? extends Enti
157115
return targets.size();
158116
}
159117

160-
private static void teleport(CommandSource source, Entity entityIn, ServerWorld worldIn, double x, double y, double z, Set<SPlayerPositionLookPacket.Flags> relativeList, float yaw, float pitch, @Nullable Facing facing) {
161-
try {
162-
BackLocationAPI.INSTANCE.commit(source.asPlayer());
163-
} catch (CommandSyntaxException e) {
164-
// suppressed. Sorry.
165-
}
166-
167-
if (entityIn instanceof ServerPlayerEntity) {
168-
ChunkPos chunkpos = new ChunkPos(new BlockPos(x, y, z));
169-
worldIn.getChunkProvider().func_217228_a(TicketType.POST_TELEPORT, chunkpos, 1, entityIn.getEntityId());
170-
entityIn.stopRiding();
171-
if (((ServerPlayerEntity) entityIn).isSleeping()) {
172-
((ServerPlayerEntity) entityIn).wakeUpPlayer(true, true, false);
173-
}
174-
175-
if (worldIn == entityIn.world) {
176-
((ServerPlayerEntity) entityIn).connection.setPlayerLocation(x, y, z, yaw, pitch, relativeList);
177-
} else {
178-
((ServerPlayerEntity) entityIn).teleport(worldIn, x, y, z, yaw, pitch);
179-
}
180-
181-
entityIn.setRotationYawHead(yaw);
182-
} else {
183-
float f1 = MathHelper.wrapDegrees(yaw);
184-
float f = MathHelper.wrapDegrees(pitch);
185-
f = MathHelper.clamp(f, -90.0F, 90.0F);
186-
if (worldIn == entityIn.world) {
187-
entityIn.setLocationAndAngles(x, y, z, f1, f);
188-
entityIn.setRotationYawHead(f1);
189-
} else {
190-
entityIn.detach();
191-
entityIn.dimension = worldIn.dimension.getType();
192-
Entity entity = entityIn;
193-
entityIn = entityIn.getType().create(worldIn);
194-
if (entityIn == null) {
195-
return;
196-
}
197-
198-
entityIn.copyDataFromOld(entity);
199-
entityIn.setLocationAndAngles(x, y, z, f1, f);
200-
entityIn.setRotationYawHead(f1);
201-
worldIn.func_217460_e(entityIn);
202-
}
203-
}
204-
205-
if (facing != null) {
206-
facing.updateLook(source, entityIn);
207-
}
208-
209-
if (!(entityIn instanceof LivingEntity) || !((LivingEntity) entityIn).isElytraFlying()) {
210-
entityIn.setMotion(entityIn.getMotion().mul(1.0D, 0.0D, 1.0D));
211-
entityIn.onGround = true;
212-
}
213-
214-
}
215-
216118
static class Facing {
217119
private final Vec3d position;
218120
private final Entity entity;
@@ -240,7 +142,14 @@ public void updateLook(CommandSource source, Entity entityIn) {
240142
} else {
241143
entityIn.lookAt(source.getEntityAnchorType(), this.position);
242144
}
145+
}
146+
}
243147

148+
private static void commitBack(CommandSource source) {
149+
try {
150+
BackLocationAPI.INSTANCE.commit(source.asPlayer());
151+
} catch (CommandSyntaxException e) {
152+
// Suppressed. Sorry.
244153
}
245154
}
246155
}

0 commit comments

Comments
 (0)