Skip to content

Commit

Permalink
Save location when players use the teleport command
Browse files Browse the repository at this point in the history
  • Loading branch information
DrexHD committed Sep 27, 2024
1 parent 92c3eb7 commit e1f8318
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import org.jetbrains.changelog.Changelog

plugins {
id 'fabric-loom' version '1.6-SNAPSHOT'
id 'fabric-loom' version '1.7-SNAPSHOT'
id "me.modmuss50.mod-publish-plugin" version "0.7.4"
id 'maven-publish'
id 'org.jetbrains.changelog' version '2.+'
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/me/drex/essentials/mixin/TeleportCommandMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package me.drex.essentials.mixin;

import me.drex.essentials.storage.DataStorage;
import me.drex.essentials.storage.PlayerData;
import me.drex.essentials.util.teleportation.Location;
import net.minecraft.commands.CommandSource;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.server.commands.TeleportCommand;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.RelativeMovement;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Coerce;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.Set;

@Mixin(TeleportCommand.class)
public class TeleportCommandMixin {

@Inject(
method = "performTeleport",
at = @At(
value = "INVOKE",
target = "Lnet/minecraft/world/entity/Entity;teleportTo(Lnet/minecraft/server/level/ServerLevel;DDDLjava/util/Set;FF)Z"
)
)
private static void onTeleport(CommandSourceStack commandSourceStack, Entity entity, ServerLevel serverLevel, double d, double e, double f, Set<RelativeMovement> set, float g, float h, @Coerce Object lookAt, CallbackInfo ci) {
CommandSource source = ((CommandSourceStackAccessor) commandSourceStack).getSource();
if (source instanceof ServerPlayer && entity instanceof ServerPlayer player) {
// The command was actually executed by a player and not *as* a player (e.g. from /execute as)
// And the affected entity was also a player
PlayerData playerData = DataStorage.updatePlayerData(player);
// Save location before teleportation
playerData.saveLocation(new Location(player));
}
}

}
1 change: 1 addition & 0 deletions src/main/resources/essentials.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"mixins": [
"CommandSourceStackAccessor",
"TeleportCommandMixin",
"async.IChunkMap",
"async.IServerChunkCache",
"async.ServerPlayerMixin",
Expand Down

0 comments on commit e1f8318

Please sign in to comment.