Skip to content

Commit

Permalink
Implemented the Pierce Bomb power-up (fix #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Minemobs committed Apr 5, 2024
1 parent 0c85632 commit 58cb3b9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 19 deletions.
10 changes: 7 additions & 3 deletions src/main/java/fr/sunderia/bomberman/Powerup.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import net.minestom.server.attribute.AttributeModifier;
import net.minestom.server.attribute.AttributeOperation;
import net.minestom.server.entity.Player;
import net.minestom.server.tag.Tag;

import java.util.function.Consumer;

Expand All @@ -18,9 +17,14 @@ enum Powerup {
SPEED_UP(p -> incrementSpeed(p, 0.1f / 8)),
//MIN SPEED -0.025
SPEED_DOWN(p -> incrementSpeed(p, -0.025f / 4)),
BOXING_GLOVE(p -> p.setTag(PowerupTags.BOXING_GLOVE.getTag(Boolean.class), true))
BOXING_GLOVE(p -> setTag(p, PowerupTags.BOXING_GLOVE)),
PIERCE(p -> setTag(p, PowerupTags.PIERCE))
;

private static void setTag(Player player, PowerupTags tag) {
player.setTag(tag.getBool(), true);
}

private final Consumer<Player> effect;

Powerup(Consumer<Player> effect) {
Expand All @@ -40,4 +44,4 @@ private static void incrementSpeed(Player player, float amount) {
private static void incrementPower(Player p, int increment) {
Bomberman.Companion.getPowerMap().compute(p.getUuid(), (k, currentPower) -> Math.min(Math.max(currentPower + increment, 1), 8));
}
}
}
22 changes: 16 additions & 6 deletions src/main/java/fr/sunderia/bomberman/PrimedTntEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minestom.server.item.Material;
import net.minestom.server.network.packet.server.play.ParticlePacket;
import net.minestom.server.particle.Particle;
import net.minestom.server.scoreboard.Team;
import net.minestom.server.utils.PacketUtils;

/**
Expand All @@ -27,7 +28,8 @@ public class PrimedTntEntity extends Entity {
private final Player player;
private final Sound tntHiss = Sound.sound(Key.key("entity.tnt.primed"), Sound.Source.BLOCK, 1f, 1f);
private final Sound explosionSound = Sound.sound(Key.key("entity.generic.explode"), Sound.Source.BLOCK, 1f, 1f);
private Pos spawnPos;
private boolean pierce;
public static Team pierceTeam;

public Player getPlayer() {
return player;
Expand Down Expand Up @@ -73,7 +75,8 @@ private void breakBlocks(int power, boolean isX, boolean negative) {
if (getInstance().getBlock(newPos.add(0, 1, 0)).id() == Block.BARRIER.id())
getInstance().setBlock(newPos.add(0, 1, 0), Block.AIR);
}
break;
if(!isAPierceBomb()) break;
if(id == Block.STONE.id()) break;
}
}
}
Expand All @@ -83,9 +86,6 @@ private void dropPowerup(Pos pos) {
return;
int index = Random.Default.nextInt(Powerup.values().length);
Powerup powerup = Powerup.values()[index];
if(powerup == Powerup.BOXING_GLOVE) {
Bomberman.Companion.getLogger().info("Spawned boxing glove");
}
ItemStack is = ItemStack.of(Material.NAUTILUS_SHELL).withMeta(meta -> meta.customModelData(index + 1)
.displayName(Component.text(powerup.name().replace("_", " ").toLowerCase())));
ItemEntity item = new ItemEntity(is);
Expand All @@ -108,7 +108,6 @@ public void setFuseTime(int fuseTime) {
@Override
public void spawn() {
super.getViewersAsAudience().playSound(tntHiss);
this.spawnPos = getPosition();
}

@Override
Expand All @@ -119,5 +118,16 @@ public void update(long time) {
explode();
getInstance().setBlock(this.position, Block.AIR);
remove();
pierceTeam.removeMember(this.uuid.toString());
}

public boolean isAPierceBomb() {
return pierce;
}

public void setPierce(boolean pierce) {
this.setGlowing(true);
pierceTeam.addMember(this.uuid.toString());
this.pierce = pierce;
}
}
18 changes: 13 additions & 5 deletions src/main/kotlin/fr/sunderia/bomberman/Bomberman.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import fr.sunderia.bomberman.party.Party
import fr.sunderia.bomberman.party.PartyCommand
import fr.sunderia.bomberman.utils.Axis
import fr.sunderia.bomberman.utils.Cooldown
import fr.sunderia.bomberman.utils.PositionUtils
import fr.sunderia.bomberman.utils.PositionUtils.Companion.removeBlockAt
import fr.sunderia.bomberman.utils.PositionUtils.Companion.setBlockAt
import fr.sunderia.bomberman.utils.PowerupTags
Expand Down Expand Up @@ -39,6 +38,7 @@ import net.minestom.server.network.NetworkBuffer
import net.minestom.server.network.packet.client.play.ClientPlayerDiggingPacket
import net.minestom.server.network.packet.server.play.ChangeGameStatePacket
import net.minestom.server.network.packet.server.play.SetCooldownPacket
import net.minestom.server.scoreboard.TeamBuilder
import net.minestom.server.tag.Tag
import net.minestom.server.timer.TaskSchedule
import net.minestom.server.utils.NamespaceID
Expand All @@ -52,6 +52,7 @@ import java.util.*
import java.util.logging.Level
import java.util.logging.Logger


fun main() {
val game = Bomberman()
val mc = MinecraftServer.init()
Expand Down Expand Up @@ -86,6 +87,8 @@ class Bomberman {
commandManager.register(PartyCommand())
commandManager.register(GameCommand())

PrimedTntEntity.pierceTeam = TeamBuilder("Pierce TNT", MinecraftServer.getTeamManager()).teamColor(NamedTextColor.AQUA).build()

try {
ByteArrayOutputStream().use { os ->
URI.create("https://raw.githubusercontent.com/Sunderia/Bomberman/main/bomberman.zip").toURL().openStream().use { input ->
Expand All @@ -99,6 +102,7 @@ class Bomberman {
logger.info("Bomberman starting")
}

@Suppress("UnstableApiUsage", "NestedLambdaShadowedImplicitParameter")
private fun registerListeners(container: InstanceContainer) {
val spawn = Pos(.0, 45.0, .0)
val extensionNode = MinecraftServer.getGlobalEventHandler()
Expand Down Expand Up @@ -128,7 +132,9 @@ class Bomberman {
MinecraftServer.getPacketListenerManager().setPlayListener(ClientPlayerDiggingPacket::class.java) { packet: ClientPlayerDiggingPacket, player: Player ->
val instance = player.instance
if(player.gameMode != GameMode.ADVENTURE || packet.status != ClientPlayerDiggingPacket.Status.STARTED_DIGGING) return@setPlayListener PlayerDiggingListener.playerDiggingListener(packet, player)
if(!player.hasTag(PowerupTags.BOXING_GLOVE.tag)) return@setPlayListener
logger.info("Digging")
if(!player.hasTag(PowerupTags.BOXING_GLOVE.getBool())) return@setPlayListener
logger.info("Has effect: ")
if(!instance.getBlock(packet.blockPosition).compare(Block.BARRIER)) return@setPlayListener
if(packet.blockFace == BlockFace.TOP || packet.blockFace == BlockFace.BOTTOM) return@setPlayListener
val tnt = instance.entities
Expand Down Expand Up @@ -195,7 +201,6 @@ class Bomberman {
))
}


gameNode.addListener(PlayerDeathEvent::class.java) { event ->
if(!event.instance.hasTag(Tag.Boolean("game"))) return@addListener
val player = event.player
Expand Down Expand Up @@ -238,13 +243,16 @@ class Bomberman {
it.block = Block.BARRIER

if (player.gameMode == GameMode.ADVENTURE) {
val timeInSeconds = 1
val timeInSeconds = 2
val c = Cooldown(it.player.uuid, "tnt", timeInSeconds)
c.start()
val packet = SetCooldownPacket(Material.TNT.id(), timeInSeconds * 20)
it.player.playerConnection.sendPacket(packet)
}
PrimedTntEntity(it.player, it.instance, it.blockPosition.add(0.5, 0.0, 0.5))
val tnt = PrimedTntEntity(it.player, it.instance, it.blockPosition.add(0.5, 0.0, 0.5))
if(player.hasTag(PowerupTags.PIERCE.getBool())) {
tnt.setPierce(true)
}
}

extensionNode.addChild(lobbyNode)
Expand Down
5 changes: 3 additions & 2 deletions src/main/kotlin/fr/sunderia/bomberman/party/Game.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import fr.sunderia.bomberman.Bomberman.Companion.powerMap
import fr.sunderia.bomberman.InstanceCreator.Companion.createInstanceContainer
import fr.sunderia.bomberman.InstanceCreator.Companion.generateStructure
import fr.sunderia.bomberman.utils.PowerupTags
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.Component.text
import net.kyori.adventure.text.format.NamedTextColor
import net.minestom.server.MinecraftServer
Expand Down Expand Up @@ -77,7 +76,9 @@ data class Game(val instance: InstanceContainer, val scheduler: SchedulerManager
for (uuid in uuids) {
p.getAttribute(Attribute.MOVEMENT_SPEED).removeModifier(uuid!!)
}
p.removeTag(PowerupTags.BOXING_GLOVE.tag)
PowerupTags.entries.forEach {
p.removeTag(it.getBool())
}
}
instance.entities.stream()
.filter { e: Entity ->
Expand Down
11 changes: 8 additions & 3 deletions src/main/kotlin/fr/sunderia/bomberman/utils/PowerupTags.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ package fr.sunderia.bomberman.utils

import net.minestom.server.tag.Tag

enum class PowerupTags(val tag: Tag<*>) {
enum class PowerupTags(private val tag: Tag<*>) {
BOXING_GLOVE(Tag.Boolean("boxing_glove")),
PIERCE(Tag.Boolean("pierce")),
;

@Suppress("UNCHECKED_CAST")
fun <T> getTag(clazz: Class<T>): Tag<T> {
@Suppress("UNCHECKED_CAST", "UNUSED_PARAMETER")
private fun <T> getTag(clazz: Class<T>): Tag<T> {
return this.tag as Tag<T>
}

fun getBool(): Tag<Boolean> {
return this.getTag(Boolean::class.java)
}
}

0 comments on commit 58cb3b9

Please sign in to comment.