move permission check inside the claim menu. potion permission close #49

This commit is contained in:
Flemmli97 2021-06-13 15:51:22 +02:00
parent 8fbec0bd43
commit 261c94ec86
6 changed files with 98 additions and 46 deletions

View File

@ -29,6 +29,7 @@ public class PermissionRegistry {
public static ClaimPermission EDITCLAIM = register(new ClaimPermission("EDITCLAIM", () -> new ItemStack(ConfigHandler.config.claimingItem), "Gives permission to edit (resize, delete...) the claim"));
public static ClaimPermission EDITPERMS = register(new ClaimPermission("EDITPERMS", () -> new ItemStack(Items.COMMAND_BLOCK), "Gives permission to change the claims permissions"));
public static ClaimPermission EDITPOTIONS = register(new ClaimPermission("EDITPOTIONS", () -> new ItemStack(Items.POTION), "Gives permission to edit the claims potion effect"));
public static ClaimPermission BREAK = register(new ClaimPermission("BREAK", () -> new ItemStack(Items.DIAMOND_PICKAXE), "Permission to break blocks in the claim"));
public static ClaimPermission PLACE = register(new ClaimPermission("PLACE", () -> new ItemStack(Items.GRASS_BLOCK), "Permission to place blocks in the claim"));
public static ClaimPermission OPENCONTAINER = register(new ClaimPermission("OPENCONTAINER", () -> new ItemStack(Items.CHEST), "Permission to open containers", "(chest, furnace etc.)"));

View File

@ -179,23 +179,26 @@ public class CommandClaim {
}
private static int openMenu(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
try {
ServerPlayerEntity player = context.getSource().getPlayer();
PlayerClaimData data = PlayerClaimData.get(player);
if (data.getEditMode() == EnumEditMode.DEFAULT) {
Claim claim = PermHelper.checkReturn(player, PermissionRegistry.EDITPERMS, PermHelper.genericNoPermMessage(player));
if (claim == null)
Claim claim = ClaimStorage.get(player.getServerWorld()).getClaimAt(player.getBlockPos());
if (claim == null) {
PermHelper.noClaimMessage(player);
return 0;
}
if (data.getEditMode() == EnumEditMode.DEFAULT) {
ClaimMenuScreenHandler.openClaimMenu(player, claim);
data.addDisplayClaim(claim, EnumDisplayType.MAIN, player.getBlockPos().getY());
} else {
Claim claim = ClaimStorage.get(player.getServerWorld()).getClaimAt(player.getBlockPos());
Claim sub = claim.getSubClaim(player.getBlockPos());
if (sub != null && (claim.canInteract(player, PermissionRegistry.EDITPERMS, player.getBlockPos()) || sub.canInteract(player, PermissionRegistry.EDITPERMS, player.getBlockPos())))
if (sub != null)
ClaimMenuScreenHandler.openClaimMenu(player, sub);
else if (claim.canInteract(player, PermissionRegistry.EDITPERMS, player.getBlockPos()))
ClaimMenuScreenHandler.openClaimMenu(player, claim);
else
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.noPermission, Formatting.DARK_RED), false);
ClaimMenuScreenHandler.openClaimMenu(player, claim);
}
} catch (Exception e) {
e.printStackTrace();
}
return Command.SINGLE_SUCCESS;
}

View File

@ -98,6 +98,7 @@ public class LangConfig {
public String screenPrevious = "Prev";
public String screenAdd = "Add";
public String screenBack = "Back";
public String screenNoPerm = "No Permission";
public String screenMenu = "Claim-Menu";
public String screenMenuSub = "SubClaim-Menu";

View File

@ -1,5 +1,7 @@
package io.github.flemmli97.flan.gui;
import io.github.flemmli97.flan.api.ClaimPermission;
import io.github.flemmli97.flan.api.PermissionRegistry;
import io.github.flemmli97.flan.claim.Claim;
import io.github.flemmli97.flan.claim.ClaimStorage;
import io.github.flemmli97.flan.claim.PermHelper;
@ -23,7 +25,7 @@ public class ClaimMenuScreenHandler extends ServerOnlyScreenHandler {
private final Claim claim;
private ClaimMenuScreenHandler(int syncId, PlayerInventory playerInventory, Claim claim) {
super(syncId, playerInventory, 1);
super(syncId, playerInventory, 1, claim);
this.claim = claim;
}
@ -44,6 +46,7 @@ public class ClaimMenuScreenHandler extends ServerOnlyScreenHandler {
@Override
protected void fillInventoryWith(PlayerEntity player, Inventory inv, Object... additionalData) {
Claim claim = (Claim) additionalData[0];
for (int i = 0; i < 9; i++) {
switch (i) {
case 0:
@ -54,21 +57,29 @@ public class ClaimMenuScreenHandler extends ServerOnlyScreenHandler {
case 2:
ItemStack perm = new ItemStack(Items.BEACON);
perm.setCustomName(ServerScreenHelper.coloredGuiText(ConfigHandler.lang.screenMenuGlobal, Formatting.GOLD));
if (player instanceof ServerPlayerEntity && !this.hasEditPerm(claim, (ServerPlayerEntity) player))
ServerScreenHelper.addLore(perm, ServerScreenHelper.coloredGuiText(ConfigHandler.lang.screenNoPerm, Formatting.DARK_RED));
inv.setStack(i, perm);
break;
case 3:
ItemStack group = new ItemStack(Items.WRITABLE_BOOK);
group.setCustomName(ServerScreenHelper.coloredGuiText(ConfigHandler.lang.screenMenuGroup, Formatting.GOLD));
if (player instanceof ServerPlayerEntity && !this.hasEditPerm(claim, (ServerPlayerEntity) player))
ServerScreenHelper.addLore(group, ServerScreenHelper.coloredGuiText(ConfigHandler.lang.screenNoPerm, Formatting.DARK_RED));
inv.setStack(i, group);
break;
case 4:
ItemStack potions = new ItemStack(Items.POTION);
potions.setCustomName(ServerScreenHelper.coloredGuiText(ConfigHandler.lang.screenMenuPotion, Formatting.GOLD));
if (player instanceof ServerPlayerEntity && !this.hasPerm(claim, (ServerPlayerEntity) player, PermissionRegistry.EDITPOTIONS))
ServerScreenHelper.addLore(potions, ServerScreenHelper.coloredGuiText(ConfigHandler.lang.screenNoPerm, Formatting.DARK_RED));
inv.setStack(i, potions);
break;
case 8:
ItemStack delete = new ItemStack(Items.BARRIER);
delete.setCustomName(ServerScreenHelper.coloredGuiText(ConfigHandler.lang.screenMenuDelete, Formatting.RED));
if (player instanceof ServerPlayerEntity && !this.hasPerm(claim, (ServerPlayerEntity) player, PermissionRegistry.EDITCLAIM))
ServerScreenHelper.addLore(delete, ServerScreenHelper.coloredGuiText(ConfigHandler.lang.screenNoPerm, Formatting.DARK_RED));
inv.setStack(i, delete);
break;
default:
@ -90,21 +101,31 @@ public class ClaimMenuScreenHandler extends ServerOnlyScreenHandler {
ServerScreenHelper.playSongToPlayer(player, SoundEvents.UI_BUTTON_CLICK, 1, 1f);
break;
case 2:
if (this.hasEditPerm(this.claim, player)) {
player.closeHandledScreen();
player.getServer().execute(() -> PermissionScreenHandler.openClaimMenu(player, this.claim, null));
ServerScreenHelper.playSongToPlayer(player, SoundEvents.UI_BUTTON_CLICK, 1, 1f);
} else
ServerScreenHelper.playSongToPlayer(player, SoundEvents.ENTITY_VILLAGER_NO, 1, 1f);
break;
case 3:
if (this.hasEditPerm(this.claim, player)) {
player.closeHandledScreen();
player.getServer().execute(() -> GroupScreenHandler.openGroupMenu(player, this.claim));
ServerScreenHelper.playSongToPlayer(player, SoundEvents.UI_BUTTON_CLICK, 1, 1f);
} else
ServerScreenHelper.playSongToPlayer(player, SoundEvents.ENTITY_VILLAGER_NO, 1, 1f);
break;
case 4:
if (this.hasPerm(this.claim, player, PermissionRegistry.EDITPOTIONS)) {
player.closeHandledScreen();
player.getServer().execute(() -> PotionEditScreenHandler.openPotionMenu(player, this.claim));
ServerScreenHelper.playSongToPlayer(player, SoundEvents.UI_BUTTON_CLICK, 1, 1f);
} else
ServerScreenHelper.playSongToPlayer(player, SoundEvents.ENTITY_VILLAGER_NO, 1, 1f);
break;
case 8:
if (this.hasPerm(this.claim, player, PermissionRegistry.EDITCLAIM)) {
player.closeHandledScreen();
player.getServer().execute(() -> ConfirmScreenHandler.openConfirmScreen(player, (bool) -> {
if (bool) {
@ -119,8 +140,21 @@ public class ClaimMenuScreenHandler extends ServerOnlyScreenHandler {
ServerScreenHelper.playSongToPlayer(player, SoundEvents.ENTITY_VILLAGER_NO, 1, 1f);
}
}));
} else
ServerScreenHelper.playSongToPlayer(player, SoundEvents.ENTITY_VILLAGER_NO, 1, 1f);
break;
}
return true;
}
private boolean hasEditPerm(Claim claim, ServerPlayerEntity player) {
return ((claim.parentClaim() != null && claim.parentClaim().canInteract(player, PermissionRegistry.EDITPERMS, player.getBlockPos()))
|| claim.canInteract(player, PermissionRegistry.EDITPERMS, player.getBlockPos()));
}
private boolean hasPerm(Claim claim, ServerPlayerEntity player, ClaimPermission perm) {
if (claim.parentClaim() != null)
return claim.parentClaim().canInteract(player, perm, player.getBlockPos());
return claim.canInteract(player, perm, player.getBlockPos());
}
}

View File

@ -12,7 +12,6 @@ import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.potion.PotionUtil;
import net.minecraft.potion.Potions;
import net.minecraft.screen.NamedScreenHandlerFactory;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.slot.Slot;

View File

@ -19,7 +19,9 @@ import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ServerScreenHelper {
@ -33,18 +35,18 @@ public class ServerScreenHelper {
public static ItemStack fromPermission(Claim claim, ClaimPermission perm, String group) {
ItemStack stack = perm.getItem();
stack.setCustomName(ServerScreenHelper.coloredGuiText(perm.id, Formatting.GOLD));
ListTag lore = new ListTag();
List<Text> lore = new ArrayList<>();
for (String pdesc : perm.desc) {
Text trans = ServerScreenHelper.coloredGuiText(pdesc, Formatting.YELLOW);
lore.add(StringTag.of(Text.Serializer.toJson(trans)));
lore.add(trans);
}
Config.GlobalType global = ConfigHandler.config.getGlobal(claim.getWorld(), perm);
if (!claim.isAdminClaim() && !global.canModify()) {
Text text = ServerScreenHelper.coloredGuiText(ConfigHandler.lang.screenUneditable, Formatting.DARK_RED);
lore.add(StringTag.of(Text.Serializer.toJson(text)));
lore.add(text);
String permFlag = String.valueOf(global.getValue());
Text text2 = ServerScreenHelper.coloredGuiText(String.format(ConfigHandler.lang.screenEnableText, permFlag), permFlag.equals("true") ? Formatting.GREEN : Formatting.RED);
lore.add(StringTag.of(Text.Serializer.toJson(text2)));
lore.add(text2);
} else {
String permFlag;
if (group == null) {
@ -77,9 +79,9 @@ public class ServerScreenHelper {
}
}
Text text = ServerScreenHelper.coloredGuiText(String.format(ConfigHandler.lang.screenEnableText, permFlag), permFlag.equals("true") ? Formatting.GREEN : Formatting.RED);
lore.add(StringTag.of(Text.Serializer.toJson(text)));
lore.add(text);
}
stack.getOrCreateSubTag("display").put("Lore", lore);
addLore(stack, lore);
return stack;
}
@ -120,4 +122,16 @@ public class ServerScreenHelper {
public static Text coloredGuiText(String text, Formatting... formattings) {
return new LiteralText(text).setStyle(Style.EMPTY.withItalic(false).withFormatting(formattings));
}
public static void addLore(ItemStack stack, Text text) {
ListTag lore = new ListTag();
lore.add(StringTag.of(Text.Serializer.toJson(text)));
stack.getOrCreateSubTag("display").put("Lore", lore);
}
public static void addLore(ItemStack stack, List<Text> texts) {
ListTag lore = new ListTag();
texts.forEach(text -> lore.add(StringTag.of(Text.Serializer.toJson(text))));
stack.getOrCreateSubTag("display").put("Lore", lore);
}
}