001/* 002 * PlotSquared, a land and world management plugin for Minecraft. 003 * Copyright (C) IntellectualSites <https://intellectualsites.com> 004 * Copyright (C) IntellectualSites team and contributors 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 018 */ 019package com.plotsquared.core.plot.flag.implementations; 020 021import com.plotsquared.core.configuration.caption.TranslatableCaption; 022import com.plotsquared.core.player.PlotPlayer; 023import com.plotsquared.core.plot.Plot; 024import com.plotsquared.core.plot.flag.FlagParseException; 025import com.plotsquared.core.plot.flag.PlotFlag; 026import net.kyori.adventure.text.minimessage.Template; 027import org.checkerframework.checker.nullness.qual.NonNull; 028import org.checkerframework.checker.nullness.qual.Nullable; 029 030import java.util.Arrays; 031import java.util.Collection; 032 033public class DenyTeleportFlag extends PlotFlag<DenyTeleportFlag.DeniedGroup, DenyTeleportFlag> { 034 035 public static final DenyTeleportFlag DENY_TELEPORT_FLAG_NONE = 036 new DenyTeleportFlag(DeniedGroup.NONE); 037 038 /** 039 * Construct a new flag instance. 040 * 041 * @param value Flag value 042 */ 043 protected DenyTeleportFlag(@NonNull DeniedGroup value) { 044 super( 045 value, 046 TranslatableCaption.of("flags.flag_category_enum"), 047 TranslatableCaption.of("flags.flag_description_deny_teleport") 048 ); 049 } 050 051 public static boolean allowsTeleport(PlotPlayer<?> player, Plot plot) { 052 final DeniedGroup value = plot.getFlag(DenyTeleportFlag.class); 053 if (value == DeniedGroup.NONE) { 054 return true; 055 } 056 final boolean result; 057 switch (value) { 058 case TRUSTED: 059 result = !plot.getTrusted().contains(player.getUUID()); 060 break; 061 case MEMBERS: 062 result = !plot.getMembers().contains(player.getUUID()); 063 break; 064 case NONMEMBERS: 065 result = plot.isAdded(player.getUUID()); 066 break; 067 case NONTRUSTED: 068 result = 069 plot.getTrusted().contains(player.getUUID()) || plot.isOwner(player.getUUID()); 070 break; 071 case NONOWNERS: 072 result = plot.isOwner(player.getUUID()); 073 break; 074 default: 075 return true; 076 } 077 return result || player.hasPermission("plots.admin.entry.denied"); 078 } 079 080 @Override 081 public DenyTeleportFlag parse(@NonNull String input) throws FlagParseException { 082 final DeniedGroup group = DeniedGroup.fromString(input); 083 if (group == null) { 084 throw new FlagParseException(this, input, TranslatableCaption.of("flags.flag_error_enum"), 085 Template.of("list", "members, nonmembers, trusted, nontrusted, nonowners") 086 ); 087 } 088 return flagOf(group); 089 } 090 091 @Override 092 public DenyTeleportFlag merge(@NonNull DeniedGroup newValue) { 093 if (getValue().ordinal() < newValue.ordinal()) { 094 return flagOf(newValue); 095 } 096 return this; 097 } 098 099 @Override 100 public String toString() { 101 return this.getValue().name(); 102 } 103 104 @Override 105 public String getExample() { 106 return "trusted"; 107 } 108 109 @Override 110 protected DenyTeleportFlag flagOf(@NonNull DeniedGroup value) { 111 return new DenyTeleportFlag(value); 112 } 113 114 @Override 115 public Collection<String> getTabCompletions() { 116 return Arrays.asList("none", "members", "trusted", "nonmembers", "nontrusted", "nonowners"); 117 } 118 119 public enum DeniedGroup { 120 NONE, 121 MEMBERS, 122 TRUSTED, 123 NONMEMBERS, 124 NONTRUSTED, 125 NONOWNERS; 126 127 public static @Nullable DeniedGroup fromString(final @NonNull String string) { 128 for (final DeniedGroup group : values()) { 129 if (group.name().equalsIgnoreCase(string)) { 130 return group; 131 } 132 } 133 return null; 134 } 135 } 136 137}