|
| 1 | +/*------------------------------------------------------------------------------ |
| 2 | + - Adapt is a Skill/Integration plugin for Minecraft Bukkit Servers |
| 3 | + - Copyright (c) 2022 Arcane Arts (Volmit Software) |
| 4 | + - |
| 5 | + - This program 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 | + - This program 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 this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + -----------------------------------------------------------------------------*/ |
| 18 | + |
| 19 | +package com.volmit.adapt.content.adaptation.ranged; |
| 20 | + |
| 21 | +import com.volmit.adapt.Adapt; |
| 22 | +import com.volmit.adapt.api.adaptation.SimpleAdaptation; |
| 23 | +import com.volmit.adapt.api.recipe.AdaptRecipe; |
| 24 | +import com.volmit.adapt.api.recipe.MaterialChar; |
| 25 | +import com.volmit.adapt.content.item.BoundSnowBall; |
| 26 | +import com.volmit.adapt.util.C; |
| 27 | +import com.volmit.adapt.util.Element; |
| 28 | +import com.volmit.adapt.util.J; |
| 29 | +import com.volmit.adapt.util.Localizer; |
| 30 | +import lombok.NoArgsConstructor; |
| 31 | +import org.bukkit.*; |
| 32 | +import org.bukkit.block.Block; |
| 33 | +import org.bukkit.block.data.BlockData; |
| 34 | +import org.bukkit.entity.Entity; |
| 35 | +import org.bukkit.entity.Player; |
| 36 | +import org.bukkit.entity.Snowball; |
| 37 | +import org.bukkit.event.EventHandler; |
| 38 | +import org.bukkit.event.EventPriority; |
| 39 | +import org.bukkit.event.block.BlockBreakEvent; |
| 40 | +import org.bukkit.event.block.BlockExplodeEvent; |
| 41 | +import org.bukkit.event.block.BlockPistonExtendEvent; |
| 42 | +import org.bukkit.event.block.BlockPistonRetractEvent; |
| 43 | +import org.bukkit.event.entity.EntityExplodeEvent; |
| 44 | +import org.bukkit.event.entity.ProjectileHitEvent; |
| 45 | +import org.bukkit.event.entity.ProjectileLaunchEvent; |
| 46 | + |
| 47 | +import java.util.*; |
| 48 | + |
| 49 | +public class RangedWebBomb extends SimpleAdaptation<RangedWebBomb.Config> { |
| 50 | + private static final BlockData AIR = Material.AIR.createBlockData(); |
| 51 | + private static final BlockData BLOCK = Material.COBWEB.createBlockData(); |
| 52 | + private final Map<Entity, Player> activeSnowballs; |
| 53 | + private final Set<Block> activeBlocks; |
| 54 | + |
| 55 | + public RangedWebBomb() { |
| 56 | + super("ranged-webshot"); |
| 57 | + registerConfiguration(Config.class); |
| 58 | + setDescription(Localizer.dLocalize("ranged", "webshot", "description")); |
| 59 | + setDisplayName(Localizer.dLocalize("ranged", "webshot", "name")); |
| 60 | + setIcon(Material.COBWEB); |
| 61 | + setBaseCost(getConfig().baseCost); |
| 62 | + setMaxLevel(getConfig().maxLevel); |
| 63 | + setInterval(4900); |
| 64 | + setInitialCost(getConfig().initialCost); |
| 65 | + setCostFactor(getConfig().costFactor); |
| 66 | + registerRecipe(AdaptRecipe.shaped() |
| 67 | + .key("ranged-web-bomb") |
| 68 | + .ingredient(new MaterialChar('I', Material.COBWEB)) |
| 69 | + .ingredient(new MaterialChar('S', Material.SNOWBALL)) |
| 70 | + .shapes(List.of( |
| 71 | + "III", |
| 72 | + "ISI", |
| 73 | + "III")) |
| 74 | + .result(BoundSnowBall.io.withData(new BoundSnowBall.Data(null))) |
| 75 | + .build()); |
| 76 | + activeBlocks = new HashSet<>(); |
| 77 | + activeSnowballs = new HashMap<>(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void addStats(int level, Element v) { |
| 82 | + v.addLore(C.GREEN + "+ " + Localizer.dLocalize("ranged", "webshot", "lore1")); |
| 83 | + v.addLore(C.YELLOW + "+ " + level + C.GRAY + " " + Localizer.dLocalize("ranged", "webshot", "lore2")); |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + @EventHandler |
| 88 | + public void on(ProjectileHitEvent e) { |
| 89 | + if (e.isCancelled()) { |
| 90 | + return; |
| 91 | + } |
| 92 | + Block block; |
| 93 | + |
| 94 | + if (e.getHitEntity() != null) { |
| 95 | + block = e.getHitEntity().getLocation().add(0, 1, 0).getBlock(); |
| 96 | + } else if (e.getHitBlock() != null) { |
| 97 | + block = e.getHitBlock().getLocation().add(0, 1, 0).getBlock(); |
| 98 | + } else { |
| 99 | + block = e.getEntity().getLocation().add(0, 1, 0).getBlock(); |
| 100 | + } |
| 101 | + |
| 102 | + if (e.getEntity().getShooter() instanceof Player p && e.getEntity() instanceof Snowball snowball && hasAdaptation(p)) { |
| 103 | + vfxSingleCubeOutline(block, Particle.REVERSE_PORTAL); |
| 104 | + Adapt.verbose("Snowball Got: " + snowball.getEntityId() + " " + snowball.getUniqueId()); |
| 105 | + if (activeSnowballs.containsKey(Bukkit.getEntity(snowball.getUniqueId()))) { |
| 106 | + Adapt.verbose("Detected snowball hit"); |
| 107 | + activeSnowballs.remove(snowball); |
| 108 | + snowball.remove(); |
| 109 | + Set<Block> locs = new HashSet<>(); |
| 110 | + locs.add(block.getLocation().add(0, 1, 0).getBlock()); |
| 111 | + locs.add(block.getLocation().add(0, -1, 0).getBlock()); |
| 112 | + locs.add(block.getLocation().add(0, 0, 1).getBlock()); |
| 113 | + locs.add(block.getLocation().add(0, 0, -1).getBlock()); |
| 114 | + locs.add(block.getLocation().add(1, 0, 0).getBlock()); |
| 115 | + locs.add(block.getLocation().add(-1, 0, 0).getBlock()); |
| 116 | + |
| 117 | + for (Block i : locs) { |
| 118 | + addFoundation(i, getLevel(p)); |
| 119 | + } |
| 120 | + |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + |
| 126 | + @EventHandler |
| 127 | + public void on(ProjectileLaunchEvent e) { |
| 128 | + if (e.isCancelled()) { |
| 129 | + return; |
| 130 | + } |
| 131 | + if (e.getEntity().getShooter() instanceof Player p && e.getEntity() instanceof Snowball snowball && hasAdaptation(p)) { |
| 132 | + Adapt.verbose("Snowball Launched: " + snowball.getEntityId() + " " + snowball.getUniqueId()); |
| 133 | + if (BoundSnowBall.isBindableItem(snowball.getItem())) { |
| 134 | + Adapt.verbose("Snowball is bound"); |
| 135 | + activeSnowballs.put(snowball, p); |
| 136 | + } else { |
| 137 | + Adapt.verbose("Snowball is not bound"); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public void addFoundation(Block block, int seconds) { |
| 143 | + if (!block.getType().isAir()) { |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + J.s(() -> { |
| 148 | + block.setBlockData(BLOCK); |
| 149 | + activeBlocks.add(block); |
| 150 | + }); |
| 151 | + block.getWorld().playSound(block.getLocation(), Sound.BLOCK_ROOTED_DIRT_PLACE, 1.0f, 1.0f); |
| 152 | + if (getConfig().showParticles) { |
| 153 | + |
| 154 | + vfxSingleCubeOutline(block, Particle.CLOUD); |
| 155 | + vfxSingleCubeOutline(block, Particle.WHITE_ASH); |
| 156 | + } |
| 157 | + J.a(() -> removeFoundation(block), seconds * 16); |
| 158 | + } |
| 159 | + |
| 160 | + public void removeFoundation(Block block) { |
| 161 | + if (!block.getBlockData().equals(BLOCK)) { |
| 162 | + return; |
| 163 | + } |
| 164 | + |
| 165 | + J.s(() -> { |
| 166 | + block.setBlockData(AIR); |
| 167 | + activeBlocks.remove(block); |
| 168 | + }); |
| 169 | + block.getWorld().playSound(block.getLocation(), Sound.BLOCK_ROOTED_DIRT_BREAK, 1.0f, 1.0f); |
| 170 | + if (getConfig().showParticles) { |
| 171 | + vfxSingleCubeOutline(block, Particle.ENCHANTMENT_TABLE); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + |
| 176 | + //prevent piston from moving blocks // Dupe fix |
| 177 | + @EventHandler(priority = EventPriority.HIGHEST) |
| 178 | + public void on(BlockPistonExtendEvent e) { |
| 179 | + if (e.isCancelled()) { |
| 180 | + return; |
| 181 | + } |
| 182 | + e.getBlocks().forEach(b -> { |
| 183 | + if (activeBlocks.contains(b)) { |
| 184 | + Adapt.verbose("Cancelled Piston Extend on Adaptation Foundation Block"); |
| 185 | + e.setCancelled(true); |
| 186 | + } |
| 187 | + }); |
| 188 | + } |
| 189 | + |
| 190 | + //prevent piston from pulling blocks // Dupe fix |
| 191 | + @EventHandler(priority = EventPriority.HIGHEST) |
| 192 | + public void on(BlockPistonRetractEvent e) { |
| 193 | + if (e.isCancelled()) { |
| 194 | + return; |
| 195 | + } |
| 196 | + e.getBlocks().forEach(b -> { |
| 197 | + if (activeBlocks.contains(b)) { |
| 198 | + Adapt.verbose("Cancelled Piston Retract on Adaptation Foundation Block"); |
| 199 | + e.setCancelled(true); |
| 200 | + } |
| 201 | + }); |
| 202 | + } |
| 203 | + |
| 204 | + //prevent TNT from destroying blocks // Dupe fix |
| 205 | + @EventHandler(priority = EventPriority.HIGHEST) |
| 206 | + public void on(BlockExplodeEvent e) { |
| 207 | + if (e.isCancelled()) { |
| 208 | + return; |
| 209 | + } |
| 210 | + if (activeBlocks.contains(e.getBlock())) { |
| 211 | + Adapt.verbose("Cancelled Block Explosion on Adaptation Foundation Block"); |
| 212 | + e.setCancelled(true); |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + //prevent block from being destroyed // Dupe fix |
| 217 | + @EventHandler(priority = EventPriority.HIGHEST) |
| 218 | + public void on(BlockBreakEvent e) { |
| 219 | + if (activeBlocks.contains(e.getBlock())) { |
| 220 | + e.setCancelled(true); |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + //prevent Entities from destroying blocks // Dupe fix |
| 225 | + @EventHandler(priority = EventPriority.HIGHEST) |
| 226 | + public void on(EntityExplodeEvent e) { |
| 227 | + if (e.isCancelled()) { |
| 228 | + return; |
| 229 | + } |
| 230 | + e.blockList().removeIf(activeBlocks::contains); |
| 231 | + } |
| 232 | + |
| 233 | + |
| 234 | + @Override |
| 235 | + public void onTick() { |
| 236 | + |
| 237 | + } |
| 238 | + |
| 239 | + @Override |
| 240 | + public boolean isEnabled() { |
| 241 | + return getConfig().enabled; |
| 242 | + } |
| 243 | + |
| 244 | + @Override |
| 245 | + public boolean isPermanent() { |
| 246 | + return getConfig().permanent; |
| 247 | + } |
| 248 | + |
| 249 | + @NoArgsConstructor |
| 250 | + protected static class Config { |
| 251 | + boolean permanent = false; |
| 252 | + boolean enabled = true; |
| 253 | + boolean showParticles = true; |
| 254 | + int baseCost = 5; |
| 255 | + int maxLevel = 5; |
| 256 | + int initialCost = 1; |
| 257 | + double costFactor = 1.5; |
| 258 | + } |
| 259 | +} |
0 commit comments