|
| 1 | +/* |
| 2 | + * Copyright (c) 2016, Kevin Phoenix |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * |
| 8 | + * 1. Redistributions of source code must retain the above copyright notice, this |
| 9 | + * list of conditions and the following disclaimer. |
| 10 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 11 | + * this list of conditions and the following disclaimer in the documentation |
| 12 | + * and/or other materials provided with the distribution. |
| 13 | + * |
| 14 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 15 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 17 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 18 | + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 19 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 20 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 21 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 23 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | + */ |
| 25 | + |
| 26 | +package in.twizmwaz.cardinal.module.apply; |
| 27 | + |
| 28 | +import ee.ellytr.chat.component.formattable.UnlocalizedComponent; |
| 29 | +import in.twizmwaz.cardinal.module.filter.Filter; |
| 30 | +import in.twizmwaz.cardinal.module.filter.FilterState; |
| 31 | +import in.twizmwaz.cardinal.module.kit.Kit; |
| 32 | +import in.twizmwaz.cardinal.module.kit.KitRemovable; |
| 33 | +import in.twizmwaz.cardinal.module.region.Region; |
| 34 | +import in.twizmwaz.cardinal.module.region.RegionBounds; |
| 35 | +import in.twizmwaz.cardinal.util.Channels; |
| 36 | +import in.twizmwaz.cardinal.util.Components; |
| 37 | +import lombok.AccessLevel; |
| 38 | +import lombok.AllArgsConstructor; |
| 39 | +import lombok.Getter; |
| 40 | +import net.md_5.bungee.api.chat.BaseComponent; |
| 41 | +import org.bukkit.block.Block; |
| 42 | +import org.bukkit.entity.Player; |
| 43 | +import org.bukkit.util.Vector; |
| 44 | + |
| 45 | +import java.util.Collection; |
| 46 | + |
| 47 | +@AllArgsConstructor(access = AccessLevel.PRIVATE) |
| 48 | +public class AppliedRegion implements Region, Filter, KitRemovable { |
| 49 | + |
| 50 | + @Getter |
| 51 | + private final ApplyType type; |
| 52 | + private final Region region; |
| 53 | + private final Filter filter; |
| 54 | + |
| 55 | + private final Kit kit; |
| 56 | + private final Vector velocity; |
| 57 | + @Getter |
| 58 | + private final BaseComponent message; |
| 59 | + @Getter |
| 60 | + private final boolean earlyWarning; |
| 61 | + |
| 62 | + public AppliedRegion(ApplyType type, Region region, Filter filter, String message) { |
| 63 | + this(type, region, filter, message, false); |
| 64 | + } |
| 65 | + |
| 66 | + public AppliedRegion(ApplyType type, Region region, Filter filter, String message, boolean earlyWarning) { |
| 67 | + this(type, region, filter, message == null ? null : new UnlocalizedComponent(message), earlyWarning); |
| 68 | + } |
| 69 | + |
| 70 | + public AppliedRegion(ApplyType type, Region region, Filter filter, BaseComponent message) { |
| 71 | + this(type, region, filter, message, false); |
| 72 | + } |
| 73 | + |
| 74 | + public AppliedRegion(ApplyType type, Region region, Filter filter, BaseComponent message, boolean earlyWarning) { |
| 75 | + this(type, region, filter, null, null, message == null ? null : Components.getWarningComponent(message), |
| 76 | + earlyWarning); |
| 77 | + } |
| 78 | + |
| 79 | + public AppliedRegion(ApplyType type, Region region, Filter filter, Kit kit) { |
| 80 | + this(type, region, filter, kit, null, null, false); |
| 81 | + } |
| 82 | + |
| 83 | + public AppliedRegion(Region region, Filter filter, Vector velocity) { |
| 84 | + this(ApplyType.VELOCITY, region, filter, null, velocity, null, false); |
| 85 | + } |
| 86 | + |
| 87 | + public boolean isType(ApplyType type) { |
| 88 | + return this.type.equals(type); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Sends the message for this region to the player as long as both message and player aren't null. |
| 93 | + * @param player The player to send the message to. |
| 94 | + */ |
| 95 | + public void sendMessage(Player player) { |
| 96 | + if (message != null && player != null) { |
| 97 | + Channels.getPlayerChannel(player).sendMessage(message); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Applies the effects in this region (kits or velocity) to a player if the filter allows it. |
| 103 | + * @param player The player to apply effect on. |
| 104 | + * @param objects The object to pass to the filter. |
| 105 | + */ |
| 106 | + public void applyEffects(Player player, Object... objects) { |
| 107 | + if (evaluate(objects).toBoolean()) { |
| 108 | + if (isType(ApplyType.VELOCITY)) { |
| 109 | + player.applyImpulse(velocity); |
| 110 | + } else if (kit != null) { |
| 111 | + kit.apply(player, false); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + /* KitRemovable interface */ |
| 117 | + @Override |
| 118 | + public void apply(Player player, boolean force) { |
| 119 | + kit.apply(player, force); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public void remove(Player player) { |
| 124 | + if (isType(ApplyType.KIT_LEND) && kit instanceof KitRemovable) { |
| 125 | + ((KitRemovable) kit).remove(player); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /* Filter interface */ |
| 130 | + @Override |
| 131 | + public FilterState evaluate(Object... objects) { |
| 132 | + return filter.evaluate(objects); |
| 133 | + } |
| 134 | + |
| 135 | + /* Region interface */ |
| 136 | + @Override |
| 137 | + public boolean contains(Vector vector) { |
| 138 | + return region.contains(vector); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public boolean isRandomizable() { |
| 143 | + return region.isRandomizable(); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public boolean isBounded() { |
| 148 | + return region.isBounded(); |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public RegionBounds getBounds() { |
| 153 | + return region.getBounds(); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public Collection<Block> getBlocks() { |
| 158 | + return region.getBlocks(); |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public Vector getRandomPoint() { |
| 163 | + return region.getRandomPoint(); |
| 164 | + } |
| 165 | + |
| 166 | +} |
0 commit comments