From 341aa9ebcee9df67e5edbb0c8fc31e55017083db Mon Sep 17 00:00:00 2001 From: Flemmli97 Date: Sat, 12 Jun 2021 23:21:38 +0200 Subject: [PATCH] add sethome and teleport command (for claims) --- Changelog.txt | 6 +- .../flan/api/PermissionRegistry.java | 1 + .../io/github/flemmli97/flan/claim/Claim.java | 33 +++++++++++ .../flemmli97/flan/commands/CommandClaim.java | 58 +++++++++++++++++++ .../github/flemmli97/flan/config/Config.java | 1 + .../flemmli97/flan/config/LangCommands.java | 2 + .../flemmli97/flan/config/LangConfig.java | 6 +- .../flan/event/EntityInteractEvents.java | 2 +- .../permissionapi/CommandPermission.java | 3 + .../flan/player/PlayerClaimData.java | 24 ++++++-- .../fabric/ClaimPermissionCheckImpl.java | 2 - .../forgeevent/BlockInteractEventsForge.java | 1 - gradle.properties | 2 +- 13 files changed, 130 insertions(+), 11 deletions(-) diff --git a/Changelog.txt b/Changelog.txt index ee77ebd..c3eb52e 100644 --- a/Changelog.txt +++ b/Changelog.txt @@ -1,7 +1,11 @@ -Flan 1.4.3 +Flan 1.5.0 ====================== - Fix some items checking for wrong position and thus are able to interact in claims - Fix enderpearl phasing through blocks +- Add claim homes set via /flan setHome in a claim to the players position. + The claim permission to allow others do that too is EDITCLAIM +- Add /flan teleport to tp to a claims home. + Default global value is ALLFALSE so disabled. Flan 1.4.2 ====================== diff --git a/common/src/main/java/io/github/flemmli97/flan/api/PermissionRegistry.java b/common/src/main/java/io/github/flemmli97/flan/api/PermissionRegistry.java index c8a9052..a7ec041 100644 --- a/common/src/main/java/io/github/flemmli97/flan/api/PermissionRegistry.java +++ b/common/src/main/java/io/github/flemmli97/flan/api/PermissionRegistry.java @@ -68,6 +68,7 @@ public class PermissionRegistry { public static ClaimPermission PICKUP = register(new ClaimPermission("PICKUP", () -> new ItemStack(Items.BRICK), true, "Allow the pickup of items")); public static ClaimPermission FLIGHT = register(new ClaimPermission("FLIGHT", () -> new ItemStack(Items.FEATHER), true, "Allow non creative flight")); public static ClaimPermission CANSTAY = register(new ClaimPermission("CANSTAY", () -> new ItemStack(Items.PAPER), true, "Allow player to enter your claim")); + public static ClaimPermission TELEPORT = register(new ClaimPermission("TELEPORT", () -> new ItemStack(Items.END_PORTAL_FRAME), false, "Allow player to teleport to your claim home position")); public static ClaimPermission HURTPLAYER = global(new ClaimPermission("HURTPLAYER", () -> new ItemStack(Items.DIAMOND_SWORD), "Permission to hurt other players")); public static ClaimPermission EXPLOSIONS = global(new ClaimPermission("EXPLOSIONS", () -> new ItemStack(Items.TNT), "Toggle explosions in claim")); diff --git a/common/src/main/java/io/github/flemmli97/flan/claim/Claim.java b/common/src/main/java/io/github/flemmli97/flan/claim/Claim.java index 67d172d..be1557e 100644 --- a/common/src/main/java/io/github/flemmli97/flan/claim/Claim.java +++ b/common/src/main/java/io/github/flemmli97/flan/claim/Claim.java @@ -19,6 +19,8 @@ import net.minecraft.text.Text; import net.minecraft.util.ActionResult; import net.minecraft.util.Formatting; import net.minecraft.util.math.BlockPos; +import net.minecraft.world.Heightmap; +import net.minecraft.world.chunk.ChunkStatus; import java.util.ArrayList; import java.util.Arrays; @@ -38,6 +40,7 @@ public class Claim implements IPermissionContainer { private UUID claimID; private String claimName = ""; + private BlockPos homePos; private final Map globalPerm = new HashMap<>(); private final Map> permissions = new HashMap<>(); @@ -80,6 +83,7 @@ public class Claim implements IPermissionContainer { this.minY = Math.max(0, minY); this.owner = creator; this.world = world; + this.homePos = this.getInitCenterPos(); this.setDirty(true); PermissionRegistry.getPerms().stream().filter(perm -> perm.defaultVal).forEach(perm -> this.globalPerm.put(perm, true)); if (setDefaultGroups) @@ -92,6 +96,12 @@ public class Claim implements IPermissionContainer { return claim; } + private BlockPos getInitCenterPos() { + BlockPos center = new BlockPos(this.minX + (this.maxX - this.minX) * 0.5, 0, this.minZ + (this.maxZ - this.minZ) * 0.5); + int y = this.world.getChunk(center.getX() >> 4, center.getZ() >> 4, ChunkStatus.HEIGHTMAPS).sampleHeightmap(Heightmap.Type.MOTION_BLOCKING, center.getX() & 15, center.getZ() & 15); + return new BlockPos(center.getX(), y + 1, center.getZ()); + } + public void setClaimID(UUID uuid) { this.claimID = uuid; this.setDirty(true); @@ -443,6 +453,18 @@ public class Claim implements IPermissionContainer { return l; } + public boolean setHomePos(BlockPos homePos) { + if (this.insideClaim(homePos)) { + this.homePos = homePos; + return true; + } + return false; + } + + public BlockPos getHomePos() { + return this.homePos; + } + /** * Only marks non sub claims */ @@ -467,6 +489,12 @@ public class Claim implements IPermissionContainer { this.minZ = pos.get(2).getAsInt(); this.maxZ = pos.get(3).getAsInt(); this.minY = pos.get(4).getAsInt(); + JsonArray home = ConfigHandler.arryFromJson(obj, "Home"); + if (home.size() != 3) + this.homePos = this.getInitCenterPos(); + else { + this.homePos = new BlockPos(home.get(0).getAsInt(), home.get(1).getAsInt(), home.get(2).getAsInt()); + } if (ConfigHandler.fromJson(obj, "AdminClaim", false)) this.owner = null; else @@ -526,6 +554,11 @@ public class Claim implements IPermissionContainer { pos.add(this.maxZ); pos.add(this.minY); obj.add("PosxXzZY", pos); + JsonArray home = new JsonArray(); + home.add(this.homePos.getX()); + home.add(this.homePos.getY()); + home.add(this.homePos.getZ()); + obj.add("Home", home); if (this.parent != null) obj.addProperty("Parent", this.parent.toString()); if (!this.globalPerm.isEmpty()) { diff --git a/common/src/main/java/io/github/flemmli97/flan/commands/CommandClaim.java b/common/src/main/java/io/github/flemmli97/flan/commands/CommandClaim.java index 449726b..ef0f86c 100644 --- a/common/src/main/java/io/github/flemmli97/flan/commands/CommandClaim.java +++ b/common/src/main/java/io/github/flemmli97/flan/commands/CommandClaim.java @@ -9,6 +9,7 @@ import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.exceptions.SimpleCommandExceptionType; import com.mojang.brigadier.suggestion.Suggestions; import com.mojang.brigadier.suggestion.SuggestionsBuilder; import io.github.flemmli97.flan.api.ClaimPermission; @@ -46,6 +47,7 @@ import java.util.Map; import java.util.UUID; import java.util.concurrent.CompletableFuture; import java.util.regex.Pattern; +import java.util.stream.Collectors; public class CommandClaim { @@ -54,6 +56,7 @@ public class CommandClaim { .then(CommandManager.literal("reload").requires(src -> CommandPermission.perm(src, CommandPermission.cmdReload, true)).executes(CommandClaim::reloadConfig)) .then(CommandManager.literal("addClaim").requires(src -> CommandPermission.perm(src, CommandPermission.claimCreate)).then(CommandManager.argument("from", BlockPosArgumentType.blockPos()).then(CommandManager.argument("to", BlockPosArgumentType.blockPos()).executes(CommandClaim::addClaim)))) .then(CommandManager.literal("menu").requires(src -> CommandPermission.perm(src, CommandPermission.cmdMenu)).executes(CommandClaim::openMenu)) + .then(CommandManager.literal("setHome").requires(src -> CommandPermission.perm(src, CommandPermission.cmdHome)).executes(CommandClaim::setClaimHome)) .then(CommandManager.literal("trapped").requires(src -> CommandPermission.perm(src, CommandPermission.cmdTrapped)).executes(CommandClaim::trapped)) .then(CommandManager.literal("name").requires(src -> CommandPermission.perm(src, CommandPermission.cmdName)).then(CommandManager.argument("name", StringArgumentType.string()).executes(CommandClaim::nameClaim))) .then(CommandManager.literal("unlockDrops").executes(CommandClaim::unlockDrops) @@ -101,6 +104,11 @@ public class CommandClaim { } return CommandSource.suggestMatching(list, build); }).executes(CommandClaim::removePlayer)))))) + .then(CommandManager.literal("teleport").requires(src -> CommandPermission.perm(src, CommandPermission.cmdTeleport)) + .then(CommandManager.literal("self").then(CommandManager.argument("claim", StringArgumentType.word()).suggests((ctx, b) -> CommandClaim.claimSuggestions(ctx, b, ctx.getSource().getPlayer().getUuid())) + .executes(CommandClaim::teleport))) + .then(CommandManager.literal("other").then(CommandManager.argument("player", GameProfileArgumentType.gameProfile()).then(CommandManager.argument("claim", StringArgumentType.word()).suggests((ctx, b) -> CommandClaim.claimSuggestions(ctx, b, CommandClaim.singleProfile(ctx, "player").getId())) + .executes(src -> CommandClaim.teleport(src, CommandClaim.singleProfile(src, "player").getId())))))) .then(CommandManager.literal("permission").requires(src -> CommandPermission.perm(src, CommandPermission.cmdPermission)) .then(CommandManager.literal("personal").then(CommandManager.argument("group", StringArgumentType.string()).suggests(CommandClaim::personalGroupSuggestion) .then(CommandManager.argument("permission", StringArgumentType.word()).suggests((ctx, b) -> permSuggestions(ctx, b, true)) @@ -744,4 +752,54 @@ public class CommandClaim { player.sendMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.editPersonalGroup, group, perm, setPerm), Formatting.GOLD), false); return Command.SINGLE_SUCCESS; } + + public static int setClaimHome(CommandContext context) throws CommandSyntaxException { + ServerPlayerEntity player = context.getSource().getPlayer(); + Claim claim = PermHelper.checkReturn(player, PermissionRegistry.EDITCLAIM, PermHelper.genericNoPermMessage(player)); + if (claim == null) + return 0; + claim.setHomePos(player.getBlockPos()); + context.getSource().sendFeedback(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.setHome, player.getBlockPos()), Formatting.GOLD), false); + return Command.SINGLE_SUCCESS; + } + + public static int teleport(CommandContext context) throws CommandSyntaxException { + return teleport(context, context.getSource().getPlayer().getUuid()); + } + + public static int teleport(CommandContext context, UUID owner) throws CommandSyntaxException { + ServerPlayerEntity player = context.getSource().getPlayer(); + String name = StringArgumentType.getString(context, "claim"); + return ClaimStorage.get(player.getServerWorld()).allClaimsFromPlayer(owner) + .stream().filter(claim -> { + if (claim.getClaimName().isEmpty()) + return claim.getClaimID().toString().equals(name); + return claim.getClaimName().equals(name); + }).findFirst().map(claim -> { + BlockPos pos = claim.getHomePos(); + if (claim.canInteract(player, PermissionRegistry.TELEPORT, pos, false)) { + PlayerClaimData data = PlayerClaimData.get(player); + if (data.setTeleportTo(pos)) { + context.getSource().sendFeedback(PermHelper.simpleColoredText(ConfigHandler.lang.teleportHome, Formatting.GOLD), false); + return Command.SINGLE_SUCCESS; + } + context.getSource().sendFeedback(PermHelper.simpleColoredText(ConfigHandler.lang.teleportHomeFail, Formatting.RED), false); + } else + context.getSource().sendFeedback(PermHelper.simpleColoredText(ConfigHandler.lang.noPermissionSimple, Formatting.DARK_RED), false); + return 0; + }).orElse(0); + } + + public static CompletableFuture claimSuggestions(CommandContext context, SuggestionsBuilder build, UUID owner) { + return CommandSource.suggestMatching(ClaimStorage.get(context.getSource().getWorld()).allClaimsFromPlayer(owner) + .stream().map(claim -> claim.getClaimName().isEmpty() ? claim.getClaimID().toString() : claim.getClaimName()).collect(Collectors.toList()), build); + } + + public static GameProfile singleProfile(CommandContext context, String arg) throws CommandSyntaxException { + Collection profs = GameProfileArgumentType.getProfileArgument(context, arg); + if (profs.size() != 1) { + throw new SimpleCommandExceptionType(() -> ConfigHandler.lang.onlyOnePlayer).create(); + } + return profs.stream().findFirst().get(); + } } diff --git a/common/src/main/java/io/github/flemmli97/flan/config/Config.java b/common/src/main/java/io/github/flemmli97/flan/config/Config.java index 2d80bf5..e501fb7 100644 --- a/common/src/main/java/io/github/flemmli97/flan/config/Config.java +++ b/common/src/main/java/io/github/flemmli97/flan/config/Config.java @@ -80,6 +80,7 @@ public class Config { private final Map> globalDefaultPerms = createHashMap(map -> map.put("*", createHashMap(perms -> { perms.put(PermissionRegistry.FLIGHT, GlobalType.ALLTRUE); perms.put(PermissionRegistry.MOBSPAWN, GlobalType.ALLFALSE); + perms.put(PermissionRegistry.TELEPORT, GlobalType.ALLFALSE); }))); public Config(MinecraftServer server) { diff --git a/common/src/main/java/io/github/flemmli97/flan/config/LangCommands.java b/common/src/main/java/io/github/flemmli97/flan/config/LangCommands.java index f01d77a..241c472 100644 --- a/common/src/main/java/io/github/flemmli97/flan/config/LangCommands.java +++ b/common/src/main/java/io/github/flemmli97/flan/config/LangCommands.java @@ -26,6 +26,8 @@ public class LangCommands { map.put("buyBlocks", new String[]{"buyBlocks ", "Buys claimblocks. Needs gunpowder currency installed."}); map.put("trapped", new String[]{"trapped", "If in a claim not owned by the player attempts to teleport the player out of it after 5 seconds."}); map.put("unlockDrops", new String[]{"unlockDrops ", "Unlocks dropped items from death so other players can pick them up too. Or all of the given players (needs OP)"}); + map.put("setHome", new String[]{"setHome", "Standing in a claim with sufficient permission sets that claims home to the players position"}); + map.put("teleport", new String[]{"teleport self | (other ) ( | )", "Teleport to the given claims home position"}); map.put("reload", new String[]{"reload", "Reloads the config ingame."}); map.put("adminMode", new String[]{"adminMode", "Switches to admin mode ignoring all claims."}); diff --git a/common/src/main/java/io/github/flemmli97/flan/config/LangConfig.java b/common/src/main/java/io/github/flemmli97/flan/config/LangConfig.java index 3ce4bb7..9d25518 100644 --- a/common/src/main/java/io/github/flemmli97/flan/config/LangConfig.java +++ b/common/src/main/java/io/github/flemmli97/flan/config/LangConfig.java @@ -124,7 +124,7 @@ public class LangConfig { public String trappedRescue = "Rescuing. Don't move for 5 seconds"; public String trappedFail = "Rescue not necessary or already rescuing"; - public String trappedMove = "You moved. Aborting rescue"; + public String trappedMove = "You moved. Aborting teleport"; public String unlockDropsCmd = "Your deathitems are protected. Use %s to unlock them for other players"; public String unlockDrops = "Your deathitems are now unlocked for %s ticks"; @@ -134,6 +134,10 @@ public class LangConfig { public String claimNameUsed = "The owner of the claim already has another claim with the same name"; public String claimNameUsedSub = "One of the subclaim of this claim already has this name"; + public String setHome = "Claim home set to [x=%s,y=%s,z=%s]"; + public String teleportHome = "Teleporting to claim home. Don't move for 5 seconds"; + public String teleportHomeFail = "Teleport already happening"; + public LangCommands cmdLang = new LangCommands(); public LangConfig(MinecraftServer server) { diff --git a/common/src/main/java/io/github/flemmli97/flan/event/EntityInteractEvents.java b/common/src/main/java/io/github/flemmli97/flan/event/EntityInteractEvents.java index c9e075a..35c262d 100644 --- a/common/src/main/java/io/github/flemmli97/flan/event/EntityInteractEvents.java +++ b/common/src/main/java/io/github/flemmli97/flan/event/EntityInteractEvents.java @@ -148,7 +148,7 @@ public class EntityInteractEvents { pers.setShotFromCrossbow(false); ((IPersistentProjectileVars) pers).resetPiercingStatus(); } - if(proj instanceof EnderPearlEntity) + if (proj instanceof EnderPearlEntity) proj.remove(); //TODO: find a way to properly update chorus fruit break on hit //player.getServer().send(new ServerTask(player.getServer().getTicks()+2, ()->player.world.updateListeners(pos, state, state, 2))); diff --git a/common/src/main/java/io/github/flemmli97/flan/integration/permissionapi/CommandPermission.java b/common/src/main/java/io/github/flemmli97/flan/integration/permissionapi/CommandPermission.java index 771e203..9401730 100644 --- a/common/src/main/java/io/github/flemmli97/flan/integration/permissionapi/CommandPermission.java +++ b/common/src/main/java/io/github/flemmli97/flan/integration/permissionapi/CommandPermission.java @@ -41,6 +41,9 @@ public class CommandPermission { public static final String cmdUnlockAll = "flan.command.unlock.all"; public static final String cmdName = "flan.command.name"; + public static final String cmdHome = "flan.command.home"; + public static final String cmdTeleport = "flan.command.teleport"; + public static boolean perm(CommandSource src, String perm) { return perm(src, perm, false); } diff --git a/common/src/main/java/io/github/flemmli97/flan/player/PlayerClaimData.java b/common/src/main/java/io/github/flemmli97/flan/player/PlayerClaimData.java index 1594431..8cff1ef 100644 --- a/common/src/main/java/io/github/flemmli97/flan/player/PlayerClaimData.java +++ b/common/src/main/java/io/github/flemmli97/flan/player/PlayerClaimData.java @@ -41,6 +41,7 @@ public class PlayerClaimData { private int lastBlockTick, trappedTick = -1, deathPickupTick; private Vec3d trappedPos; + private BlockPos tpPos; private EnumEditMode mode = EnumEditMode.DEFAULT; private Claim editingClaim; private ClaimDisplay displayEditing; @@ -214,6 +215,16 @@ public class PlayerClaimData { return false; } + public boolean setTeleportTo(BlockPos tp) { + if (this.trappedTick < 0) { + this.trappedTick = 101; + this.trappedPos = this.player.getPos(); + this.tpPos = tp; + return true; + } + return false; + } + public void tick(Claim currentClaim, Consumer cons) { EntityInteractEvents.updateClaim(this.player, currentClaim, cons); boolean tool = this.player.getMainHandStack().getItem() == ConfigHandler.config.claimingItem @@ -253,10 +264,15 @@ public class PlayerClaimData { this.actionCooldown--; if (--this.trappedTick >= 0) { if (this.trappedTick == 0) { - Vec3d tp = TeleportUtils.getTeleportPos(this.player, this.player.getPos(), ClaimStorage.get(this.player.getServerWorld()), - ((IPlayerClaimImpl) this.player).getCurrentClaim().getDimensions(), - TeleportUtils.roundedBlockPos(this.player.getPos()).mutableCopy(), (claim, nPos) -> false); - this.player.teleport(tp.getX(), tp.getY(), tp.getZ()); + if (this.tpPos != null) { + this.player.teleport(this.tpPos.getX(), this.tpPos.getY(), this.tpPos.getZ()); + this.tpPos = null; + } else { + Vec3d tp = TeleportUtils.getTeleportPos(this.player, this.player.getPos(), ClaimStorage.get(this.player.getServerWorld()), + ((IPlayerClaimImpl) this.player).getCurrentClaim().getDimensions(), + TeleportUtils.roundedBlockPos(this.player.getPos()).mutableCopy(), (claim, nPos) -> false); + this.player.teleport(tp.getX(), tp.getY(), tp.getZ()); + } } else if (this.player.getPos().squaredDistanceTo(this.trappedPos) > 0.15) { this.trappedTick = -1; this.trappedPos = null; diff --git a/fabric/src/main/java/io/github/flemmli97/flan/claim/fabric/ClaimPermissionCheckImpl.java b/fabric/src/main/java/io/github/flemmli97/flan/claim/fabric/ClaimPermissionCheckImpl.java index c51fb5b..84769d2 100644 --- a/fabric/src/main/java/io/github/flemmli97/flan/claim/fabric/ClaimPermissionCheckImpl.java +++ b/fabric/src/main/java/io/github/flemmli97/flan/claim/fabric/ClaimPermissionCheckImpl.java @@ -2,8 +2,6 @@ package io.github.flemmli97.flan.claim.fabric; import io.github.flemmli97.flan.api.ClaimPermission; import io.github.flemmli97.flan.api.fabric.PermissionCheckEvent; -import net.fabricmc.fabric.api.event.Event; -import net.fabricmc.fabric.api.event.EventFactory; import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.util.ActionResult; import net.minecraft.util.math.BlockPos; diff --git a/forge/src/main/java/io/github/flemmli97/flan/forgeevent/BlockInteractEventsForge.java b/forge/src/main/java/io/github/flemmli97/flan/forgeevent/BlockInteractEventsForge.java index a70cc98..a745b84 100644 --- a/forge/src/main/java/io/github/flemmli97/flan/forgeevent/BlockInteractEventsForge.java +++ b/forge/src/main/java/io/github/flemmli97/flan/forgeevent/BlockInteractEventsForge.java @@ -1,7 +1,6 @@ package io.github.flemmli97.flan.forgeevent; import io.github.flemmli97.flan.event.BlockInteractEvents; -import net.minecraft.server.network.ServerPlayerInteractionManager; import net.minecraft.util.ActionResult; import net.minecraft.world.World; import net.minecraftforge.event.entity.player.PlayerInteractEvent; diff --git a/gradle.properties b/gradle.properties index 8796c88..f9da656 100644 --- a/gradle.properties +++ b/gradle.properties @@ -8,7 +8,7 @@ minecraft_version=1.16.5 yarn_mappings=1.16.5+build.9 loader_version=0.11.3 # Mod Properties -mod_version=1.4.3 +mod_version=1.5.0 maven_group=io.github.flemmli97 archives_base_name=flan # Dependencies