|
| 1 | +/* |
| 2 | + * Copyright (C) 2021 spnda |
| 3 | + * This file is part of BlockProt <https://github.com/spnda/BlockProt>. |
| 4 | + * |
| 5 | + * BlockProt is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * BlockProt is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with BlockProt. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +package de.sean.blockprot.nbt; |
| 20 | + |
| 21 | +import org.jetbrains.annotations.NotNull; |
| 22 | +import org.jetbrains.annotations.Nullable; |
| 23 | + |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Optional; |
| 27 | +import java.util.UUID; |
| 28 | +import java.util.stream.Collectors; |
| 29 | +import java.util.stream.Stream; |
| 30 | + |
| 31 | +/** |
| 32 | + * A block handler to get values and settings from a single lockable |
| 33 | + * block. |
| 34 | + * |
| 35 | + * @since 0.2.3 |
| 36 | + */ |
| 37 | +public abstract class IBlockNBTHandler<T, K, F extends IFriendHandler<?, ?>> extends INBTHandler<T> { |
| 38 | + protected static final boolean DEFAULT_REDSTONE = true; |
| 39 | + |
| 40 | + protected static final String OWNER_ATTRIBUTE = "splugin_owner"; |
| 41 | + |
| 42 | + protected static final String OLD_LOCK_ATTRIBUTE = "splugin_lock"; |
| 43 | + |
| 44 | + protected static final String LOCK_ATTRIBUTE = "blockprot_friends"; |
| 45 | + |
| 46 | + protected static final String REDSTONE_ATTRIBUTE = "splugin_lock_redstone"; |
| 47 | + |
| 48 | + /** |
| 49 | + * Reads the current owner from the NBT container. |
| 50 | + * |
| 51 | + * @return The owner as a UUID-String read from the container, or an empty String. |
| 52 | + * @since 0.2.3 |
| 53 | + */ |
| 54 | + @NotNull |
| 55 | + public abstract String getOwner(); |
| 56 | + |
| 57 | + /** |
| 58 | + * Set the current owner of this block. |
| 59 | + * |
| 60 | + * @param owner The new owner for this block. Should |
| 61 | + * be a valid UUID. |
| 62 | + * @since 0.2.3 |
| 63 | + */ |
| 64 | + public abstract void setOwner(@NotNull final String owner); |
| 65 | + |
| 66 | + /** |
| 67 | + * Gets a {@link Stream} of {@link IFriendHandler} for this block. |
| 68 | + * |
| 69 | + * @return A stream of friend handlers for all NBT compounds under |
| 70 | + * the friend key. |
| 71 | + * @since 0.3.0 |
| 72 | + */ |
| 73 | + @NotNull |
| 74 | + public abstract Stream<F> getFriendsStream(); |
| 75 | + |
| 76 | + /** |
| 77 | + * Gets a {@link List} of friends for this block. |
| 78 | + * |
| 79 | + * @return A list of {@link IFriendHandler} to read |
| 80 | + * additional data for each friend. |
| 81 | + * @since 0.3.0 |
| 82 | + */ |
| 83 | + @NotNull |
| 84 | + public List<F> getFriends() { |
| 85 | + return getFriendsStream().collect(Collectors.toList()); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Set a new list of FriendHandler for the friends list. |
| 90 | + * |
| 91 | + * @param access The new list of friends to use. |
| 92 | + * @since 0.3.0 |
| 93 | + */ |
| 94 | + public abstract void setFriends(@NotNull final List<F> access); |
| 95 | + |
| 96 | + /** |
| 97 | + * Filters the results of {@link #getFriends()} for any entry which |
| 98 | + * id qualifies for {@link String#equals(Object)}. |
| 99 | + * |
| 100 | + * @param id The String ID to check for. Usually a UUID as a String as {@link UUID#toString()}. |
| 101 | + * @return The first {@link IFriendHandler} found, or none. |
| 102 | + * @since 0.3.0 |
| 103 | + */ |
| 104 | + @NotNull |
| 105 | + public Optional<F> getFriend(@NotNull final String id) { |
| 106 | + return getFriendsStream() |
| 107 | + .filter((f) -> f.getName().equals(id)) |
| 108 | + .findFirst(); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Adds a new friend to the NBT. |
| 113 | + * |
| 114 | + * @param friend The friend to add. |
| 115 | + * @since 0.3.0 |
| 116 | + */ |
| 117 | + public abstract void addFriend(@NotNull final String friend); |
| 118 | + |
| 119 | + /** |
| 120 | + * Removes a friend from the NBT. |
| 121 | + * |
| 122 | + * @param friend The friend to remove. |
| 123 | + * @since 0.3.0 |
| 124 | + */ |
| 125 | + public abstract void removeFriend(@NotNull final String friend); |
| 126 | + |
| 127 | + /** |
| 128 | + * If true, redstone should be allowed for this block and should not be blocked. |
| 129 | + * If redstone has not been set for this block yet, the default value is true |
| 130 | + * |
| 131 | + * @return Whether redstone should be allowed or not. |
| 132 | + * @since 0.2.3 |
| 133 | + */ |
| 134 | + public abstract boolean getRedstone(); |
| 135 | + |
| 136 | + /** |
| 137 | + * Set the new value for redstone. See {@link #getRedstone()} for more |
| 138 | + * details on the values. |
| 139 | + * |
| 140 | + * @param redstone The boolean value to set. |
| 141 | + * @since 0.2.3 |
| 142 | + */ |
| 143 | + public abstract void setRedstone(final boolean redstone); |
| 144 | + |
| 145 | + /** |
| 146 | + * This applies any changes to this container to a possible other |
| 147 | + * half. For example doors consist from two blocks, as do double |
| 148 | + * chests. Without this call, all methods will modify only the local, |
| 149 | + * current block. |
| 150 | + * <p> |
| 151 | + * This method is specifically not called on each modification of NBT, |
| 152 | + * as this would be a massive, unnecessary performance penalty. |
| 153 | + * |
| 154 | + * @since 0.4.6 |
| 155 | + */ |
| 156 | + public abstract void applyToOtherContainer(); |
| 157 | + |
| 158 | + /** |
| 159 | + * Locks this block for given {@code player} as the owner. |
| 160 | + * |
| 161 | + * @param player The player to set as an owner. |
| 162 | + * @return A {@code L} whether or not the block was successfully locked, |
| 163 | + * else there might have been issues with permissions. |
| 164 | + * @since 0.4.6 |
| 165 | + */ |
| 166 | + @NotNull |
| 167 | + public abstract LockReturnValue lockBlock(@NotNull final K player); |
| 168 | + |
| 169 | + /** |
| 170 | + * Locks redstone for this block. |
| 171 | + * |
| 172 | + * @param player The player requesting this command, should be the owner. |
| 173 | + * @param value The value we want to set it to. If null, we just flip |
| 174 | + * the current value. |
| 175 | + * @return A {@code L} whether or not the redstone was switched |
| 176 | + * successfully. |
| 177 | + * @since 0.4.6 |
| 178 | + */ |
| 179 | + @NotNull |
| 180 | + public abstract LockReturnValue lockRedstoneForBlock(@NotNull final String player, @Nullable final Boolean value); |
| 181 | + |
| 182 | + /** |
| 183 | + * Whether or not this block is protected. This is evaluated by checking |
| 184 | + * if an owner exists and if any friends have been added to the block. |
| 185 | + * |
| 186 | + * @return True, if this block is not protected and there is no owner. |
| 187 | + * @since 0.2.3 |
| 188 | + */ |
| 189 | + public boolean isNotProtected() { |
| 190 | + return getOwner().isEmpty() && getFriends().isEmpty(); |
| 191 | + } |
| 192 | + |
| 193 | + /** |
| 194 | + * @return True, if this block is protected. |
| 195 | + * @see #isNotProtected() |
| 196 | + * @since 0.2.3 |
| 197 | + */ |
| 198 | + public boolean isProtected() { |
| 199 | + return !isNotProtected(); |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * Checks whether or not given {@code player} is the owner of this block. |
| 204 | + * |
| 205 | + * @param player A String representing a players UUID. |
| 206 | + * @return Whether or not {@code player} is the owner of this block. |
| 207 | + * @since 0.2.3 |
| 208 | + */ |
| 209 | + public boolean isOwner(@NotNull final String player) { |
| 210 | + return getOwner().equals(player); |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Checks whether or not given {@code player} can access this block. |
| 215 | + * |
| 216 | + * @param player The player to check for. |
| 217 | + * @return True, if {@code player} can access this block. |
| 218 | + * @since 0.2.3 |
| 219 | + */ |
| 220 | + public boolean canAccess(@NotNull final String player) { |
| 221 | + Optional<F> friend = getFriend(player); |
| 222 | + return !isProtected() || (getOwner().equals(player) || (friend.isPresent() && friend.get().canRead())); |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * Checks whether or not {@code friends} contains {@code friend}. |
| 227 | + * |
| 228 | + * @param friends A list of all friends we want to filter. |
| 229 | + * @param friend The UUID of a player we want to check for. |
| 230 | + * @return True, if the list does contain that friend. |
| 231 | + * @since 0.3.0 |
| 232 | + */ |
| 233 | + protected boolean containsFriend(@NotNull final List<F> friends, @NotNull final String friend) { |
| 234 | + return friends |
| 235 | + .stream() |
| 236 | + .anyMatch((f) -> f.getName().equals(friend)); |
| 237 | + } |
| 238 | + |
| 239 | + /** |
| 240 | + * Clears all values from this block and resets it to the |
| 241 | + * defaults. |
| 242 | + * |
| 243 | + * @since 0.3.2 |
| 244 | + */ |
| 245 | + public void clear() { |
| 246 | + this.setOwner(""); |
| 247 | + this.setFriends(Collections.emptyList()); |
| 248 | + this.setRedstone(DEFAULT_REDSTONE); |
| 249 | + } |
| 250 | + |
| 251 | + /** |
| 252 | + * Modifies the friends of this block for given {@code action}. |
| 253 | + * |
| 254 | + * @param player The player requesting this command, should be the owner. |
| 255 | + * @param friend The friend do to {@code action} with. |
| 256 | + * @param action The action we should perform with {@code friend} on this block. |
| 257 | + * @return A {@code L} whether or not the friends were modified |
| 258 | + * successfully. |
| 259 | + * @since 0.4.6 |
| 260 | + */ |
| 261 | + @NotNull |
| 262 | + public abstract LockReturnValue modifyFriends(@NotNull final String player, @NotNull final String friend, @NotNull final FriendModifyAction action); |
| 263 | + |
| 264 | + /** |
| 265 | + * Merges this handler with another {@link INBTHandler}. |
| 266 | + * |
| 267 | + * @param handler The handler to merge with. If {@code handler} is not an instance |
| 268 | + * of {@link IBlockNBTHandler}, this will do nothing. |
| 269 | + * @since 0.3.2 |
| 270 | + */ |
| 271 | + @Override |
| 272 | + public void mergeHandler(@NotNull INBTHandler<?> handler) { |
| 273 | + if (!(handler instanceof IBlockNBTHandler)) return; |
| 274 | + final IBlockNBTHandler<?, ?, F> blockNBTHandler = (IBlockNBTHandler<?, ?, F>) handler; |
| 275 | + this.setOwner(blockNBTHandler.getOwner()); |
| 276 | + this.setFriends(blockNBTHandler.getFriends()); |
| 277 | + this.setRedstone(blockNBTHandler.getRedstone()); |
| 278 | + } |
| 279 | +} |
0 commit comments