Skip to content

Commit

Permalink
feat: restrictEnclosureTp
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Jun 21, 2024
1 parent de20394 commit 4c46992
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ package com.github.zly2006.enclosure.access
import net.minecraft.entity.player.PlayerEntity
import net.minecraft.server.network.ServerPlayerEntity
import net.minecraft.text.Text
import java.util.*

@Suppress("INAPPLICABLE_JVM_NAME")
@JvmDefaultWithCompatibility
interface PlayerAccess {
interface MessageProvider {
fun get(player: ServerPlayerEntity?): Text?
}

@Suppress("INAPPLICABLE_JVM_NAME")
@get:JvmName("enclosure\$getVisitedEnclosures")
val visitedEnclosures: MutableSet<UUID>

@get:JvmName("enclosure\$getLastTeleportTime")
@set:JvmName("enclosure\$setLastTeleportTime")
var lastTeleportTime: Long

@Suppress("INAPPLICABLE_JVM_NAME")
@get:JvmName("enclosure\$getPermissionDeniedMsgTime")
@set:JvmName("enclosure\$setPermissionDeniedMsgTime")
var permissionDeniedMsgTime: Long
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/github/zly2006/enclosure/config/Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ public class Common {
public String leaveMessageHeader = "";
@SerializedName("check_update")
public boolean checkUpdate = true;
@SerializedName("restrict_enclosure_tp")
public boolean restrictEnclosureTp = false;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,18 @@
import com.github.zly2006.enclosure.utils.TrT;
import com.github.zly2006.enclosure.utils.Utils;
import com.github.zly2006.enclosure.utils.UtilsKt;
import com.google.common.collect.Sets;
import com.mojang.authlib.GameProfile;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtHelper;
import net.minecraft.nbt.NbtList;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
Expand All @@ -35,10 +40,13 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Optional;
import java.util.Set;
import java.util.UUID;

import static com.github.zly2006.enclosure.command.EnclosureCommandKt.CONSOLE;
import static com.github.zly2006.enclosure.utils.Permission.*;

@SuppressWarnings("UnreachableCode")
@Mixin(ServerPlayerEntity.class)
public abstract class MixinServerPlayerEntity extends PlayerEntity implements PlayerAccess {
@Shadow public ServerPlayNetworkHandler networkHandler;
Expand All @@ -47,6 +55,7 @@ public abstract class MixinServerPlayerEntity extends PlayerEntity implements Pl
@Unique @Nullable private EnclosureArea lastArea = null;
@Unique @Nullable private ServerWorld lastWorld;
@Unique private long permissionDeniedMsgTime = 0;
@Unique Set<UUID> visitedEnclosures = Sets.newHashSet();

@Override
public long enclosure$getPermissionDeniedMsgTime() {
Expand All @@ -58,6 +67,34 @@ public abstract class MixinServerPlayerEntity extends PlayerEntity implements Pl
this.permissionDeniedMsgTime = permissionDeniedMsgTime;
}

@Inject(
method = "readCustomDataFromNbt",
at = @At("HEAD")
)
private void readCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) {
visitedEnclosures = Sets.newHashSet();
if (nbt.contains("visited_enclosures")) {
NbtElement element = nbt.get("visited_enclosures");
if (element instanceof NbtList list) {
list.forEach(item -> visitedEnclosures.add(NbtHelper.toUuid(item)));
}
}
}

@Inject(
method = "writeCustomDataToNbt",
at = @At("HEAD")
)
private void writeCustomDataToNbt(NbtCompound nbt, CallbackInfo ci) {
NbtList list = new NbtList();
visitedEnclosures.forEach(uuid -> list.add(NbtHelper.fromUuid(uuid)));
nbt.put("visited_enclosures", list);
}

public Set<UUID> enclosure$getVisitedEnclosures() {
return visitedEnclosures;
}

@Shadow public abstract void sendMessage(Text message);

@Shadow public abstract void sendMessage(Text message, boolean overlay);
Expand Down Expand Up @@ -176,6 +213,7 @@ private void onTick(CallbackInfo ci) {
}
}
if (area != null) {
visitedEnclosures.add(area.getUuid());
if (!area.hasPerm(player, MOVE)) {
player.sendMessage(MOVE.getNoPermissionMsg(player));
if (area != lastArea && lastWorld != null && lastPos != null) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/com/github/zly2006/enclosure/EnclosureArea.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ open class EnclosureArea : PersistentState, EnclosureView {
protected set
override var father: PermissionHolder? = null
protected set
val uuid: UUID

override val fullName: String
get() = if (father != null) {
Expand Down Expand Up @@ -112,6 +113,11 @@ open class EnclosureArea : PersistentState, EnclosureView {
permissionsMap[UUID.fromString(playerUuid)] = perm
}
owner = compound.getUuid("owner")
uuid = if (compound.containsUuid("uuid")) {
compound.getUuid("uuid")
} else {
UUID.randomUUID()
}
}

operator fun Map<String, Boolean>.get(perm: Permission): Boolean? {
Expand Down Expand Up @@ -145,6 +151,7 @@ open class EnclosureArea : PersistentState, EnclosureView {
}
createdOn = System.currentTimeMillis()
teleportPos = Vec3d(centerX.toDouble() + 0.5, centerY.toDouble(), centerZ.toDouble() + 0.5)
uuid = UUID.randomUUID()
markDirty()
}

Expand Down Expand Up @@ -188,6 +195,7 @@ open class EnclosureArea : PersistentState, EnclosureView {
nbtTpPos.add(NbtDouble.of(teleportPos!!.getZ()))
nbt.put("tp_pos", nbtTpPos)
nbt.putUuid("owner", owner)
nbt.putUuid("uuid", uuid)
return nbt
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,30 @@ fun register(dispatcher: CommandDispatcher<ServerCommandSource>, access: Command
}
}
}
literal("visited") {
argument("player", EntityArgumentType.player()) {
literal("get") {
executes {
val player = EntityArgumentType.getPlayer(this, "player")
val visited = ServerMain.getAllEnclosures()
.filter { it.uuid in (player as PlayerAccess).visitedEnclosures }
val text = player.name.copy().append(" visited: ")
visited.forEach { e ->
text.append(e.serialize(SerializationSettings.Name, player))
.append("\n")
}
source.sendMessage(text)
}
}
literal("clear") {
executes {
val player = EntityArgumentType.getPlayer(this, "player")
(player as PlayerAccess).visitedEnclosures.clear()
source.sendMessage(Text.literal("Cleared"))
}
}
}
}
literal("closest") {
permission("enclosure.command.admin.closest", BuilderScope.Companion.DefaultPermission.OP)
executes {
Expand Down Expand Up @@ -832,6 +856,13 @@ fun register(dispatcher: CommandDispatcher<ServerCommandSource>, access: Command
), this
)
}
if (!source.hasPermissionLevel(4) && ServerMain.commonConfig.restrictEnclosureTp) {
if (source.player is PlayerAccess) {
if (!(source.player as PlayerAccess).visitedEnclosures.contains(area.uuid)) {
error(TrT.of("enclosure.message.unvisited_enclosure"), this)
}
}
}
(player as PlayerAccess).permissionDeniedMsgTime = System.currentTimeMillis()
if (ServerMain.commonConfig.showTeleportWarning) {
if (!isPositionSafe(area.world, area.teleportPos!!)) {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/assets/enclosure/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
"enclosure.message.unexpected_info": "Unexpected type: only enter|leave are accepted.",
"enclosure.message.unexpected_optional_boolean": "Unexpected value: please type true | false | none",
"enclosure.message.unknown_user": "unknown user",
"enclosure.message.unvisited_enclosure": "Unvisited enclosure! You will be able to teleport to this enclosure once you visit it.",
"enclosure.message.user_not_found": "User not found.",
"enclosure.permission.admin": "Gives admin permission to the player",
"enclosure.permission.all": "Gives all permissions to the player",
Expand Down

0 comments on commit 4c46992

Please sign in to comment.