package net.deliciousreya.minecraftportal import com.destroystokyo.paper.event.block.BlockDestroyEvent import net.deliciousreya.minecraftportal.model.DOOR_TYPES import net.deliciousreya.minecraftportal.model.PortalDataStore import net.deliciousreya.minecraftportal.model.PortalFrame import net.deliciousreya.minecraftportal.model.findPortalFrameConnectedTo import org.bukkit.Particle import org.bukkit.Sound import org.bukkit.block.Block import org.bukkit.block.data.type.Door import org.bukkit.event.EventHandler import org.bukkit.event.Listener import org.bukkit.event.block.* import org.bukkit.event.entity.EntityChangeBlockEvent import org.bukkit.event.entity.EntityExplodeEvent import org.bukkit.event.player.PlayerInteractEvent import org.bukkit.plugin.java.JavaPlugin import org.bukkit.potion.PotionEffect import org.bukkit.potion.PotionEffectType class MinecraftPortalPlugin() : JavaPlugin(), Listener { val portals: PortalDataStore = PortalDataStore() override fun onEnable() { super.onEnable() server.pluginManager.registerEvents(this, this) } @EventHandler fun onBlockPlaced(e: BlockPlaceEvent) { if (e.block.type in PortalFrame.State.INACTIVE.allValidBlocks) { val newPortal = findPortalFrameConnectedTo( e.block, PortalFrame.State.INACTIVE ) if (newPortal != null) { val replacedPortal = portals.activateAndReplacePortal(newPortal) newPortal.activate() replacedPortal?.deactivate() } } } @EventHandler fun onBlockDestroy(e:BlockDestroyEvent) { onDestroyedBlock(e.block) } @EventHandler fun onEntityExplosion(e:EntityExplodeEvent) { e.blockList().forEach(::onDestroyedBlock) } @EventHandler fun onBlockExplosion(e:BlockExplodeEvent) { onDestroyedBlock(e.block) e.blockList().forEach(::onDestroyedBlock) } @EventHandler fun onBlockBurn(e: BlockBurnEvent) { onDestroyedBlock(e.block) } @EventHandler fun onBlockFade(e: BlockFadeEvent) { onDestroyedBlock(e.block) } @EventHandler fun onBlockBreak(e:BlockBreakEvent) { onDestroyedBlock(e.block) } @EventHandler fun onEntityChangeBlock(e: EntityChangeBlockEvent) { onDestroyedBlock(e.block) } @EventHandler fun onPlayerInteract(e: PlayerInteractEvent) { if (e.clickedBlock == null || e.action != Action.RIGHT_CLICK_BLOCK) { return } val block = e.clickedBlock!! if(block.type !in DOOR_TYPES) { return } val portalScanResults = findPortalFrameConnectedTo( block, PortalFrame.State.ACTIVE ) ?: return val door = block.blockData as Door if (!portalScanResults.isStandingInPortal(e.player.location)) { e.isCancelled = true return } if (!door.isOpen) { // door is about to be opened, so undrug the player e.player.stopSound(Sound.BLOCK_PORTAL_TRIGGER) e.player.removePotionEffect(PotionEffectType.BLINDNESS) e.player.removePotionEffect(PotionEffectType.CONFUSION) e.player.removePotionEffect(PotionEffectType.INVISIBILITY) } else { // door is about to be closed, so drug the player e.player.playSound(e.player.location, Sound.BLOCK_PORTAL_TRIGGER, 20f, 1f) e.player.addPotionEffect(PotionEffect(PotionEffectType.CONFUSION, 200, 1, false, false, false)) e.player.addPotionEffect(PotionEffect(PotionEffectType.BLINDNESS, 200, 1, false, false, false)) e.player.addPotionEffect(PotionEffect(PotionEffectType.INVISIBILITY, 200, 1, false, false, false)) } } fun onDestroyedBlock(block: Block) { if (block.type !in PortalFrame.State.ACTIVE.allValidBlocks) { return } val oldPortal = findPortalFrameConnectedTo( block, PortalFrame.State.ACTIVE ) if (oldPortal != null && portals.deactivatePortal(oldPortal)) { oldPortal.deactivate() } } }