Skip to content

Commit

Permalink
Add /back compat for vanilla tp (#126)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Robertz <[email protected]>
  • Loading branch information
Lyfts and Dream-Master authored Oct 30, 2024
1 parent 56a89e3 commit 748275c
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 16 deletions.
5 changes: 5 additions & 0 deletions src/main/java/serverutils/ServerUtilitiesPermissions.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public class ServerUtilitiesPermissions {
public static final String TPA_BACK = "serverutilities.back.tpa";
public static final String RTP_BACK = "serverutilities.back.rtp";
public static final String RESPAWN_BACK = "serverutilities.back.respawn";
public static final String VANILLA_TP_BACK = "serverutilities.back.tp";
public static final String BACK_BACK = "serverutilities.back.back";

// Claims //
Expand Down Expand Up @@ -228,6 +229,10 @@ public static void registerPermissions() {
PermissionAPI
.registerNode(RTP_BACK, DefaultPermissionLevel.OP, "Allow player back to last time where /rtp is used");
PermissionAPI.registerNode(RESPAWN_BACK, DefaultPermissionLevel.ALL, "Allow player back to last death point");
PermissionAPI.registerNode(
VANILLA_TP_BACK,
DefaultPermissionLevel.OP,
"Allow player back to last point where /tp is used");
PermissionAPI.registerNode(
CLAIMS_JOURNEYMAP,
DefaultPermissionLevel.ALL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public enum Timer {
BACK(TeleportType.BACK),
SPAWN(TeleportType.SPAWN),
TPA(TeleportType.TPA),
RTP(TeleportType.RTP);
RTP(TeleportType.RTP),
VANILLA_TP(TeleportType.VANILLA_TP);

public static final Timer[] VALUES = values();

Expand Down
8 changes: 3 additions & 5 deletions src/main/java/serverutils/data/TeleportTracker.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Arrays;
import java.util.Collections;
import java.util.Objects;

import net.minecraft.nbt.NBTTagCompound;

Expand Down Expand Up @@ -35,7 +36,7 @@ public TeleportLog getLastDeath() {
}

private TeleportLog[] getSortedLogs() {
TeleportLog[] toSort = Arrays.stream(logs).filter((l) -> l != null).toArray(TeleportLog[]::new);
TeleportLog[] toSort = Arrays.stream(logs).filter(Objects::nonNull).toArray(TeleportLog[]::new);
Arrays.sort(toSort, Collections.reverseOrder());
return toSort;
}
Expand Down Expand Up @@ -71,9 +72,6 @@ public NBTTagCompound serializeNBT() {

@Override
public void deserializeNBT(NBTTagCompound nbt) {
if (nbt == null) {
return;
}
for (int i = 0; i < logs.length; i++) {
logs[i] = new TeleportLog(nbt.getCompoundTag(String.valueOf(i)));
}
Expand All @@ -85,7 +83,7 @@ public String toString() {
builder.append("{");
for (int i = 0; i < logs.length; i++) {
final TeleportLog l = logs[i];
builder.append(l.teleportType.toString() + ":" + l.getBlockDimPos());
builder.append(l.teleportType.toString()).append(":").append(l.getBlockDimPos());
if (i != logs.length - 1) {
builder.append(",");
}
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/serverutils/data/TeleportType.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package serverutils.data;

import javax.annotation.Nullable;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import serverutils.ServerUtilitiesPermissions;

Expand All @@ -18,27 +19,28 @@ public enum TeleportType {
ServerUtilitiesPermissions.TPA_COOLDOWN),
RTP(ServerUtilitiesPermissions.RTP_BACK, ServerUtilitiesPermissions.RTP_WARMUP,
ServerUtilitiesPermissions.RTP_COOLDOWN),
RESPAWN(ServerUtilitiesPermissions.RESPAWN_BACK, null, null);
RESPAWN(ServerUtilitiesPermissions.RESPAWN_BACK, null, null),
VANILLA_TP(ServerUtilitiesPermissions.VANILLA_TP_BACK, null, null);

private String permission;
private String warmup;
private String cooldown;
private final String permission;
private final String warmup;
private final String cooldown;

TeleportType(String node, @Nullable String warmup, @Nullable String cooldown) {
TeleportType(@NotNull String node, @Nullable String warmup, @Nullable String cooldown) {
this.permission = node;
this.warmup = warmup;
this.cooldown = cooldown;
}

public String getPermission() {
public @NotNull String getPermission() {
return this.permission;
}

public String getWarmupPermission() {
public @Nullable String getWarmupPermission() {
return this.warmup;
}

public String getCooldownPermission() {
public @Nullable String getCooldownPermission() {
return this.cooldown;
}
}
5 changes: 4 additions & 1 deletion src/main/java/serverutils/mixin/Mixins.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package serverutils.mixin;

import static serverutils.ServerUtilitiesConfig.commands;
import static serverutils.ServerUtilitiesConfig.ranks;
import static serverutils.mixin.TargetedMod.VANILLA;

Expand All @@ -20,7 +21,9 @@ public enum Mixins {
.setPhase(Phase.EARLY).setApplyIf(() -> ranks.enabled && ranks.command_permissions)
.addMixinClasses("minecraft.MixinCommandBase", "minecraft.MixinCommandHandler", "minecraft.MixinICommand")),
REPLACE_TAB_NAMES(new Builder("Replace tab menu names").addTargetedMod(VANILLA).setSide(Side.CLIENT)
.setPhase(Phase.EARLY).addMixinClasses("forge.MixinGuiIngameForge")),;
.setPhase(Phase.EARLY).addMixinClasses("forge.MixinGuiIngameForge")),
VANILLA_TP_BACK_COMPAT(new Builder("/back compat for the vanilla /tp").addTargetedMod(VANILLA).setSide(Side.BOTH)
.setPhase(Phase.EARLY).setApplyIf(() -> commands.back).addMixinClasses("minecraft.MixinCommandTeleport")),;

private final List<String> mixinClasses;
private final Supplier<Boolean> applyIf;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package serverutils.mixins.early.minecraft;

import net.minecraft.command.server.CommandTeleport;
import net.minecraft.entity.player.EntityPlayerMP;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import com.llamalad7.mixinextras.sugar.Local;

import serverutils.data.ServerUtilitiesPlayerData;
import serverutils.data.TeleportType;
import serverutils.lib.data.Universe;
import serverutils.lib.math.BlockDimPos;

@Mixin(CommandTeleport.class)
public abstract class MixinCommandTeleport {

@Inject(
method = "processCommand",
at = { @At(
value = "INVOKE",
target = "Lnet/minecraft/network/NetHandlerPlayServer;setPlayerLocation(DDDFF)V"),
@At(
value = "INVOKE",
target = "Lnet/minecraft/entity/player/EntityPlayerMP;setPositionAndUpdate(DDD)V") })
private void serverutilities$backCompat(CallbackInfo ci, @Local(ordinal = 0) EntityPlayerMP player) {
ServerUtilitiesPlayerData data = ServerUtilitiesPlayerData.get(Universe.get().getPlayer(player));
data.setLastTeleport(TeleportType.VANILLA_TP, new BlockDimPos(player));
}
}

0 comments on commit 748275c

Please sign in to comment.