transferClaim and permission commands

This commit is contained in:
Flemmli97 2020-09-03 18:00:37 +02:00
parent 123cd321c2
commit 53c2d1f9b5
8 changed files with 163 additions and 31 deletions

View File

@ -1,3 +1,9 @@
Flan 1.0.4
======================
- BisUmTo: Add addClaim command to create claims via commands
- Add transferClaim command to transfer owner of a claim to another player
- Add editPermission command to edit claim permissions via commands
Flan 1.0.3 Flan 1.0.3
====================== ======================
- Add permission to toggle firespreading in claims - Add permission to toggle firespreading in claims

View File

@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx2G
loader_version=0.9.1+build.205 loader_version=0.9.1+build.205
# Mod Properties # Mod Properties
mod_version = 1.0.3 mod_version = 1.0.4
maven_group = com.flemmli97.flan maven_group = com.flemmli97.flan
archives_base_name = flan archives_base_name = flan

View File

@ -114,7 +114,7 @@ public class Claim {
public void toggleAdminClaim(ServerPlayerEntity player, boolean flag) { public void toggleAdminClaim(ServerPlayerEntity player, boolean flag) {
if (!flag) if (!flag)
this.transferOwner(player); this.transferOwner(player.getUuid());
else { else {
this.owner = null; this.owner = null;
this.subClaims.forEach(claim -> claim.owner = null); this.subClaims.forEach(claim -> claim.owner = null);
@ -126,9 +126,9 @@ public class Claim {
return this.owner == null; return this.owner == null;
} }
public void transferOwner(ServerPlayerEntity player) { public void transferOwner(UUID player) {
this.owner = player.getUuid(); this.owner = player;
this.subClaims.forEach(claim -> claim.owner = player.getUuid()); this.subClaims.forEach(claim -> claim.owner = player);
this.setDirty(true); this.setDirty(true);
} }
@ -186,14 +186,12 @@ public class Claim {
PlayerClaimData data = PlayerClaimData.get(player); PlayerClaimData data = PlayerClaimData.get(player);
if ((this.isAdminClaim() && player.hasPermissionLevel(2)) || data.isAdminIgnoreClaim()) if ((this.isAdminClaim() && player.hasPermissionLevel(2)) || data.isAdminIgnoreClaim())
return true; return true;
for (Claim claim : this.subClaims) { if (perm != EnumPermission.EDITCLAIM && perm != EnumPermission.EDITPERMS)
if (claim.insideClaim(pos)) { for (Claim claim : this.subClaims) {
if (perm != EnumPermission.EDITCLAIM && perm != EnumPermission.EDITPERMS) if (claim.insideClaim(pos)) {
return claim.canInteract(player, perm, pos, message); return claim.canInteract(player, perm, pos, message);
else if (claim.canInteract(player, perm, pos, message)) }
return true;
} }
}
if (this.playersGroups.containsKey(player.getUuid())) { if (this.playersGroups.containsKey(player.getUuid())) {
EnumMap<EnumPermission, Boolean> map = this.permissions.get(this.playersGroups.get(player.getUuid())); EnumMap<EnumPermission, Boolean> map = this.permissions.get(this.playersGroups.get(player.getUuid()));
if (map != null && map.containsKey(perm)) { if (map != null && map.containsKey(perm)) {
@ -413,7 +411,7 @@ public class Claim {
this.minZ = pos.get(2).getAsInt(); this.minZ = pos.get(2).getAsInt();
this.maxZ = pos.get(3).getAsInt(); this.maxZ = pos.get(3).getAsInt();
this.minY = pos.get(4).getAsInt(); this.minY = pos.get(4).getAsInt();
if (obj.has("AdminClaim") ? obj.get("AdminClaim").getAsBoolean() : false) if (obj.has("AdminClaim") && obj.get("AdminClaim").getAsBoolean())
this.owner = null; this.owner = null;
else else
this.owner = uuid; this.owner = uuid;

View File

@ -190,6 +190,23 @@ public class ClaimStorage {
}); });
} }
public boolean transferOwner(Claim claim, ServerPlayerEntity player, UUID newOwner) {
if (!player.getUuid().equals(claim.getOwner()))
return false;
this.playerClaimMap.merge(claim.getOwner(), Sets.newHashSet(), (old, val) -> {
old.remove(claim);
return old;
});
this.dirty.add(claim.getOwner());
claim.transferOwner(newOwner);
this.playerClaimMap.merge(claim.getOwner(), Sets.newHashSet(claim), (old, val) -> {
old.add(claim);
return old;
});
this.dirty.add(claim.getOwner());
return true;
}
public Collection<Claim> allClaimsFromPlayer(UUID player) { public Collection<Claim> allClaimsFromPlayer(UUID player) {
return this.playerClaimMap.containsKey(player) ? ImmutableSet.copyOf(this.playerClaimMap.get(player)) : ImmutableSet.of(); return this.playerClaimMap.containsKey(player) ? ImmutableSet.copyOf(this.playerClaimMap.get(player)) : ImmutableSet.of();
} }

View File

@ -50,11 +50,13 @@ public class CommandClaim {
CommandManager.literal("addClaim").then(CommandManager.argument("from", BlockPosArgumentType.blockPos()).then(CommandManager.argument("to", BlockPosArgumentType.blockPos()).executes(CommandClaim::addClaim))), CommandManager.literal("addClaim").then(CommandManager.argument("from", BlockPosArgumentType.blockPos()).then(CommandManager.argument("to", BlockPosArgumentType.blockPos()).executes(CommandClaim::addClaim))),
CommandManager.literal("menu").executes(CommandClaim::openMenu), CommandManager.literal("menu").executes(CommandClaim::openMenu),
CommandManager.literal("claimInfo").executes(CommandClaim::claimInfo), CommandManager.literal("claimInfo").executes(CommandClaim::claimInfo),
CommandManager.literal("transferClaim").then(CommandManager.argument("player", GameProfileArgumentType.gameProfile()).executes(CommandClaim::transferClaim)),
CommandManager.literal("delete").executes(CommandClaim::deleteClaim), CommandManager.literal("delete").executes(CommandClaim::deleteClaim),
CommandManager.literal("deleteAll").executes(CommandClaim::deleteAllClaim), CommandManager.literal("deleteAll").executes(CommandClaim::deleteAllClaim),
CommandManager.literal("deleteSubClaim").executes(CommandClaim::deleteSubClaim), CommandManager.literal("deleteSubClaim").executes(CommandClaim::deleteSubClaim),
CommandManager.literal("deleteAllSubClaims").executes(CommandClaim::deleteAllSubClaim), CommandManager.literal("deleteAllSubClaims").executes(CommandClaim::deleteAllSubClaim),
CommandManager.literal("list").executes(CommandClaim::listClaims).then(CommandManager.argument("player", GameProfileArgumentType.gameProfile()).requires(src -> src.hasPermissionLevel(ConfigHandler.config.permissionLevel)).executes(cmd -> listClaims(cmd, GameProfileArgumentType.getProfileArgument(cmd, "player")))), CommandManager.literal("list").executes(CommandClaim::listClaims).then(CommandManager.argument("player", GameProfileArgumentType.gameProfile()).requires(src -> src.hasPermissionLevel(ConfigHandler.config.permissionLevel))
.executes(cmd -> listClaims(cmd, GameProfileArgumentType.getProfileArgument(cmd, "player")))),
CommandManager.literal("switchMode").executes(CommandClaim::switchClaimMode), CommandManager.literal("switchMode").executes(CommandClaim::switchClaimMode),
CommandManager.literal("adminMode").requires(src -> src.hasPermissionLevel(ConfigHandler.config.permissionLevel)).executes(CommandClaim::switchAdminMode), CommandManager.literal("adminMode").requires(src -> src.hasPermissionLevel(ConfigHandler.config.permissionLevel)).executes(CommandClaim::switchAdminMode),
CommandManager.literal("readGriefPrevention").requires(src -> src.hasPermissionLevel(ConfigHandler.config.permissionLevel)).executes(CommandClaim::readGriefPreventionData), CommandManager.literal("readGriefPrevention").requires(src -> src.hasPermissionLevel(ConfigHandler.config.permissionLevel)).executes(CommandClaim::readGriefPreventionData),
@ -80,17 +82,24 @@ public class CommandClaim {
ServerCommandSource src = context.getSource(); ServerCommandSource src = context.getSource();
ClaimStorage storage = ClaimStorage.get(src.getWorld()); ClaimStorage storage = ClaimStorage.get(src.getWorld());
Claim claim = storage.getClaimAt(src.getPlayer().getBlockPos()); Claim claim = storage.getClaimAt(src.getPlayer().getBlockPos());
if (claim != null && claim.canInteract(src.getPlayer(), EnumPermission.EDITCLAIM, src.getPlayer().getBlockPos())) { if (claim != null && claim.canInteract(src.getPlayer(), EnumPermission.EDITPERMS, src.getPlayer().getBlockPos())) {
list = claim.playersFromGroup(player.getServer(), ""); list = claim.playersFromGroup(player.getServer(), "");
} }
return CommandSource.suggestMatching(list, build); return CommandSource.suggestMatching(list, build);
}).executes(CommandClaim::removePlayer)))) }).executes(CommandClaim::removePlayer))))),
))); addToMainCommand(CommandManager.literal("permission"),
CommandManager.literal("global").then(CommandManager.argument("permission", StringArgumentType.word()).suggests((ctx, b) -> permSuggestions(ctx, b, false))
.then(CommandManager.argument("toggle", StringArgumentType.word()).suggests((ctx, b) -> CommandSource.suggestMatching(new String[]{"default", "true", "false"}, b)).executes(CommandClaim::editGlobalPerm))),
CommandManager.literal("group").then(CommandManager.argument("group", StringArgumentType.word()).suggests(CommandClaim::groupSuggestion)
.then(CommandManager.argument("permission", StringArgumentType.word()).suggests((ctx, b) -> permSuggestions(ctx, b, true))
.then(CommandManager.argument("toggle", StringArgumentType.word())
.suggests((ctx, b) -> CommandSource.suggestMatching(new String[]{"default", "true", "false"}, b)).executes(CommandClaim::editGroupPerm)))))
));
} }
private static <S, T extends ArgumentBuilder<S, T>> T addToMainCommand(T main, ArgumentBuilder<S, T>... other) { private static <S, T extends ArgumentBuilder<S, T>> T addToMainCommand(T main, T... other) {
if (other != null) if (other != null)
for (ArgumentBuilder<S, T> o : other) for (T o : other)
main.then(o); main.then(o);
return main; return main;
} }
@ -106,11 +115,32 @@ public class CommandClaim {
ClaimStorage storage = ClaimStorage.get(player.getServerWorld()); ClaimStorage storage = ClaimStorage.get(player.getServerWorld());
BlockPos from = BlockPosArgumentType.getLoadedBlockPos(context, "from"); BlockPos from = BlockPosArgumentType.getLoadedBlockPos(context, "from");
BlockPos to = BlockPosArgumentType.getLoadedBlockPos(context, "to"); BlockPos to = BlockPosArgumentType.getLoadedBlockPos(context, "to");
storage.createClaim(from, to, player); storage.createClaim(from, to, player);
return Command.SINGLE_SUCCESS; return Command.SINGLE_SUCCESS;
} }
private static int transferClaim(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
ServerPlayerEntity player = context.getSource().getPlayer();
Collection<GameProfile> profs = GameProfileArgumentType.getProfileArgument(context, "player");
if (profs.size() != 1) {
context.getSource().sendFeedback(PermHelper.simpleColoredText(ConfigHandler.lang.onlyOnePlayer, Formatting.RED), false);
return 0;
}
GameProfile prof = profs.iterator().next();
ClaimStorage storage = ClaimStorage.get(player.getServerWorld());
Claim claim = storage.getClaimAt(player.getBlockPos());
if (claim == null) {
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.noClaim, Formatting.RED), false);
return 0;
}
if (!storage.transferOwner(claim, player, prof.getId())) {
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.ownerTransferFail, Formatting.RED), false);
return 0;
}
player.sendMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.ownerTransferSuccess, prof.getName()), Formatting.GOLD), false);
return Command.SINGLE_SUCCESS;
}
private static int openMenu(CommandContext<ServerCommandSource> context) throws CommandSyntaxException { private static int openMenu(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
ServerPlayerEntity player = context.getSource().getPlayer(); ServerPlayerEntity player = context.getSource().getPlayer();
PlayerClaimData data = PlayerClaimData.get(player); PlayerClaimData data = PlayerClaimData.get(player);
@ -137,8 +167,10 @@ public class CommandClaim {
ServerPlayerEntity player = context.getSource().getPlayer(); ServerPlayerEntity player = context.getSource().getPlayer();
Claim claim = ClaimStorage.get(player.getServerWorld()).getClaimAt(player.getBlockPos()); Claim claim = ClaimStorage.get(player.getServerWorld()).getClaimAt(player.getBlockPos());
PlayerClaimData data = PlayerClaimData.get(player); PlayerClaimData data = PlayerClaimData.get(player);
if (claim == null) if (claim == null) {
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.noClaim, Formatting.RED), false);
return 0; return 0;
}
if (data.getEditMode() == EnumEditMode.SUBCLAIM) { if (data.getEditMode() == EnumEditMode.SUBCLAIM) {
Claim sub = claim.getSubClaim(player.getBlockPos()); Claim sub = claim.getSubClaim(player.getBlockPos());
if (sub != null) { if (sub != null) {
@ -203,7 +235,7 @@ public class CommandClaim {
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.noClaim, Formatting.RED), false); player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.noClaim, Formatting.RED), false);
return 0; return 0;
} }
boolean check = PermHelper.check(player, player.getBlockPos(), sub, EnumPermission.EDITCLAIM, b -> { boolean check = PermHelper.check(player, player.getBlockPos(), claim, EnumPermission.EDITCLAIM, b -> {
if (!b.isPresent()) if (!b.isPresent())
PermHelper.noClaimMessage(player); PermHelper.noClaimMessage(player);
else if (!b.get()) else if (!b.get())
@ -333,7 +365,7 @@ public class CommandClaim {
return Command.SINGLE_SUCCESS; return Command.SINGLE_SUCCESS;
} }
private static int listAdminClaims(CommandContext<ServerCommandSource> context) throws CommandSyntaxException { private static int listAdminClaims(CommandContext<ServerCommandSource> context) {
ServerCommandSource src = context.getSource(); ServerCommandSource src = context.getSource();
Collection<Claim> claims = ClaimStorage.get(src.getWorld()).getAdminClaims(); Collection<Claim> claims = ClaimStorage.get(src.getWorld()).getAdminClaims();
src.sendFeedback(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.listAdminClaims, src.getWorld().getRegistryKey().getValue()), Formatting.GOLD), false); src.sendFeedback(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.listAdminClaims, src.getWorld().getRegistryKey().getValue()), Formatting.GOLD), false);
@ -373,7 +405,7 @@ public class CommandClaim {
List<String> list = Lists.newArrayList(); List<String> list = Lists.newArrayList();
ClaimStorage storage = ClaimStorage.get(player.getServerWorld()); ClaimStorage storage = ClaimStorage.get(player.getServerWorld());
Claim claim = storage.getClaimAt(player.getBlockPos()); Claim claim = storage.getClaimAt(player.getBlockPos());
if (claim != null && claim.canInteract(player, EnumPermission.EDITCLAIM, player.getBlockPos())) { if (claim != null && claim.canInteract(player, EnumPermission.EDITPERMS, player.getBlockPos())) {
list = claim.groups(); list = claim.groups();
} }
return CommandSource.suggestMatching(list, build); return CommandSource.suggestMatching(list, build);
@ -408,7 +440,7 @@ public class CommandClaim {
if (claim.groups().contains(group)) { if (claim.groups().contains(group)) {
player.sendMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.groupExist, group), Formatting.RED), false); player.sendMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.groupExist, group), Formatting.RED), false);
return 0; return 0;
} else if (claim.editPerms(player, group, EnumPermission.EDITCLAIM, -1)) } else if (claim.editPerms(player, group, EnumPermission.EDITPERMS, -1))
player.sendMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.groupAdd, group), Formatting.GOLD), false); player.sendMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.groupAdd, group), Formatting.GOLD), false);
else { else {
PermHelper.genericNoPermMessage(player); PermHelper.genericNoPermMessage(player);
@ -455,4 +487,82 @@ public class CommandClaim {
player.sendMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.playerModifyNo, group, modified), Formatting.RED), false); player.sendMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.playerModifyNo, group, modified), Formatting.RED), false);
return Command.SINGLE_SUCCESS; return Command.SINGLE_SUCCESS;
} }
private static CompletableFuture<Suggestions> permSuggestions(CommandContext<ServerCommandSource> context, SuggestionsBuilder build, boolean group) {
for (EnumPermission perm : EnumPermission.values()) {
if (!group || !perm.isAlwaysGlobalPerm())
build.suggest(perm.toString());
}
return build.buildFuture();
}
private static int editGlobalPerm(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
int mode = 0;
switch (StringArgumentType.getString(context, "toggle")) {
case "true":
mode = 1;
break;
case "false":
mode = 0;
break;
case "default":
mode = -1;
break;
}
return editPerms(context, null, mode);
}
private static int editGroupPerm(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
int mode = 0;
switch (StringArgumentType.getString(context, "toggle")) {
case "true":
mode = 1;
break;
case "false":
mode = 0;
break;
case "default":
mode = -1;
break;
}
return editPerms(context, StringArgumentType.getString(context, "group"), mode);
}
private static int editPerms(CommandContext<ServerCommandSource> context, String group, int mode) throws CommandSyntaxException {
ServerPlayerEntity player = context.getSource().getPlayer();
Claim claim = ClaimStorage.get(player.getServerWorld()).getClaimAt(player.getBlockPos());
PlayerClaimData data = PlayerClaimData.get(player);
if (data.getEditMode() == EnumEditMode.SUBCLAIM) {
Claim sub = claim.getSubClaim(player.getBlockPos());
if (sub != null)
claim = sub;
}
if (claim == null) {
PermHelper.noClaimMessage(player);
return 0;
}
if (!claim.canInteract(player, EnumPermission.EDITPERMS, player.getBlockPos())) {
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.noPermission, Formatting.DARK_RED), false);
return 0;
}
EnumPermission perm;
String p = StringArgumentType.getString(context, "permission");
try {
perm = EnumPermission.valueOf(p);
if (group != null && perm.isAlwaysGlobalPerm())
throw new IllegalArgumentException();
} catch (IllegalArgumentException e) {
player.sendMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.noSuchPerm, p), Formatting.DARK_RED), false);
return 0;
}
String setPerm = mode == 1 ? "true" : mode == 0 ? "false" : "default";
if (group == null) {
claim.editGlobalPerms(perm, mode);
player.sendMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.editPerm, perm, setPerm), Formatting.GOLD), false);
} else {
claim.editPerms(player, group, perm, mode);
player.sendMessage(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.editPermGroup, perm, group, setPerm), Formatting.GOLD), false);
}
return Command.SINGLE_SUCCESS;
}
} }

View File

@ -21,6 +21,8 @@ public class LangConfig {
public String listClaims = "Listing all claims:"; public String listClaims = "Listing all claims:";
public String listAdminClaims = "Listing all admin-claims in %1:"; public String listAdminClaims = "Listing all admin-claims in %1:";
public String onlyOnePlayer = "Only one player can be used as argument"; public String onlyOnePlayer = "Only one player can be used as argument";
public String ownerTransferSuccess = "New Claimowner now: %s";
public String ownerTransferFail = "Only the owner may transfer claims";
public String noPermission = "You don't have the required permissions to do that here!"; public String noPermission = "You don't have the required permissions to do that here!";
public String noPermissionSimple = "Sorry you can't do that here!"; public String noPermissionSimple = "Sorry you can't do that here!";
@ -53,6 +55,9 @@ public class LangConfig {
public String deleteClaimError = "You can't delete this claim here"; public String deleteClaimError = "You can't delete this claim here";
public String deleteSubClaim = "Subclaim deleted"; public String deleteSubClaim = "Subclaim deleted";
public String deleteSubClaimAll = "All Subclaims from this claim deleted"; public String deleteSubClaimAll = "All Subclaims from this claim deleted";
public String noSuchPerm = "No such Permission %s";
public String editPerm = "%1$s now set to %2$s";
public String editPermGroup = "%1$s for %2$s now set to %3$s";
public String adminMode = "Adminmode (Ignore Claims) set to: %s"; public String adminMode = "Adminmode (Ignore Claims) set to: %s";
public String adminDeleteAll = "Deleted all claims for following players: %s"; public String adminDeleteAll = "Deleted all claims for following players: %s";

View File

@ -64,15 +64,11 @@ public class WorldEvents {
public static boolean canStartRaid(ServerPlayerEntity player) { public static boolean canStartRaid(ServerPlayerEntity player) {
Claim claim = ClaimStorage.get(player.getServerWorld()).getClaimAt(player.getBlockPos()); Claim claim = ClaimStorage.get(player.getServerWorld()).getClaimAt(player.getBlockPos());
if (claim != null && !claim.canInteract(player, EnumPermission.RAID, player.getBlockPos())) return claim == null || claim.canInteract(player, EnumPermission.RAID, player.getBlockPos());
return false;
return true;
} }
public static boolean canFireSpread(ServerWorld world, BlockPos pos) { public static boolean canFireSpread(ServerWorld world, BlockPos pos) {
Claim claim = ClaimStorage.get(world).getClaimAt(pos); Claim claim = ClaimStorage.get(world).getClaimAt(pos);
if (claim != null && !claim.canInteract(null, EnumPermission.FIRESPREAD, pos)) return claim == null || claim.canInteract(null, EnumPermission.FIRESPREAD, pos);
return false;
return true;
} }
} }

View File

@ -95,7 +95,7 @@ public class GroupScreenHandler extends ServerOnlyScreenHandler {
if (index == 3) { if (index == 3) {
player.closeHandledScreen(); player.closeHandledScreen();
player.getServer().execute(() -> StringResultScreenHandler.createNewStringResult(player, this.claim, (s) -> { player.getServer().execute(() -> StringResultScreenHandler.createNewStringResult(player, this.claim, (s) -> {
this.claim.editPerms(player, s, EnumPermission.EDITCLAIM, -1); this.claim.editPerms(player, s, EnumPermission.EDITPERMS, -1);
player.closeHandledScreen(); player.closeHandledScreen();
player.getServer().execute(() -> GroupScreenHandler.openGroupMenu(player, this.claim)); player.getServer().execute(() -> GroupScreenHandler.openGroupMenu(player, this.claim));
ServerScreenHelper.playSongToPlayer(player, SoundEvents.BLOCK_ANVIL_USE, 1, 1f); ServerScreenHelper.playSongToPlayer(player, SoundEvents.BLOCK_ANVIL_USE, 1, 1f);