Skip to content
This repository has been archived by the owner on Dec 28, 2023. It is now read-only.

Commit

Permalink
1.0-RC2
Browse files Browse the repository at this point in the history
---
Fix some bugs
- When the RMC is broken and installed, the hardening time is not initialized
- Processing is not implemented when ArchKingItem falls
  • Loading branch information
deveworld committed Aug 21, 2023
1 parent 4e3efbd commit 6c16390
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 17 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = "dev.worldsw"
version = "1.0-RC"
version = "1.0-RC2"
description = "The plugin for the architecture king(YouTuber)."

repositories {
Expand Down
17 changes: 17 additions & 0 deletions src/main/kotlin/dev/worldsw/archKing/data/DataManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class DataManager(private val plugin: ArchKingPlugin) {
companion object {
const val REBARS = "rebars"
const val CUSTOM_ITEM = "custom_item"
const val READY_MIXED_CONCRETE_HARD = "rmc_hard"
}

fun init() {
Expand All @@ -39,6 +40,22 @@ class DataManager(private val plugin: ArchKingPlugin) {
}
}

fun addMemory(property: String, inputProperty: String, value: JsonElement) {
getMemory(property).asJsonObject.add(inputProperty, value)
}

fun getMemory(property: String): JsonElement {
return memory.get(property)
}

fun getMemory(property: String, secondProperty: String): JsonElement? {
return memory.get(property).asJsonObject?.get(secondProperty)
}

fun removeMemory(property: String, removeProperty: String) {
getMemory(property).asJsonObject.remove(removeProperty)
}

fun addData(property: String, inputProperty: String, value: JsonElement) {
getData(property).asJsonObject.add(inputProperty, value)
}
Expand Down
69 changes: 53 additions & 16 deletions src/main/kotlin/dev/worldsw/archKing/event/EventHandle.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.worldsw.archKing.event

import com.google.gson.JsonObject
import dev.worldsw.archKing.ArchKingPlugin
import dev.worldsw.archKing.data.DataManager
import dev.worldsw.archKing.item.ArchKingItem
Expand All @@ -18,52 +19,67 @@ import org.bukkit.event.block.BlockBreakEvent
import org.bukkit.event.block.BlockPistonExtendEvent
import org.bukkit.event.block.BlockPistonRetractEvent
import org.bukkit.event.block.BlockPlaceEvent
import org.bukkit.event.entity.EntityChangeBlockEvent
import org.bukkit.event.entity.EntityDamageByEntityEvent
import org.bukkit.event.entity.EntityPickupItemEvent
import org.bukkit.event.world.WorldSaveEvent
import org.bukkit.inventory.ItemStack
import org.bukkit.persistence.PersistentDataContainer
import org.bukkit.persistence.PersistentDataType


class EventHandle(private val plugin: ArchKingPlugin) : Listener {
private fun rmcToc(block: Block) {
plugin.server.scheduler.scheduleSyncDelayedTask(plugin, {
val rmc = plugin.server.scheduler.scheduleSyncDelayedTask(plugin, {
val dataArchKingItem = plugin.itemManager.getCustomBlockData(block) ?: return@scheduleSyncDelayedTask
if (dataArchKingItem != ArchKingItem.READY_MIXED_CONCRETE) return@scheduleSyncDelayedTask

plugin.itemManager.removeCustomBlockData(block)
plugin.itemManager.addCustomBlockData(block, ArchKingItem.CONCRETE)
block.type = plugin.itemManager.getItem(ArchKingItem.CONCRETE).type
}, (3600 + (-600..600).random()).toLong())
val data = JsonObject()
data.addProperty("schedule", rmc)
plugin.dataManager.addMemory(DataManager.READY_MIXED_CONCRETE_HARD, block.location.toString(), data)
}

@EventHandler
fun onWorldSaveEvent(event: WorldSaveEvent) {
plugin.dataManager.saveData()
}

private fun onArchKingItemPlace(data: PersistentDataContainer, block: Block) {
val dataArchKingItem = data.getOrDefault(
NamespacedKey(plugin, ArchKingItem.CUSTOM_ITEM),
PersistentDataType.INTEGER,
ArchKingItem.NOT_CUSTOM_ITEM
)
if (dataArchKingItem == ArchKingItem.NOT_CUSTOM_ITEM) return
plugin.itemManager.addCustomBlockData(block, dataArchKingItem)
if (dataArchKingItem == ArchKingItem.READY_MIXED_CONCRETE) rmcToc(block)
}

@EventHandler
fun onBlockPlaceEvent(event: BlockPlaceEvent) {
if (event.block.type == Material.IRON_BARS) {
plugin.rebarHandler.onPlaceRebar(event.block)
event.isCancelled = true
if (event.player.gameMode != GameMode.CREATIVE) event.player.inventory.removeItem(ItemStack(Material.IRON_BARS))
}

val dataArchKingItem = event.itemInHand.itemMeta.persistentDataContainer
.getOrDefault(NamespacedKey(plugin, ArchKingItem.CUSTOM_ITEM), PersistentDataType.INTEGER, ArchKingItem.NOT_CUSTOM_ITEM)
if (dataArchKingItem == ArchKingItem.NOT_CUSTOM_ITEM) return
plugin.itemManager.addCustomBlockData(event.block, dataArchKingItem)
if (dataArchKingItem == ArchKingItem.READY_MIXED_CONCRETE) rmcToc(event.block)
onArchKingItemPlace(event.itemInHand.itemMeta.persistentDataContainer, event.block)
}

private fun handleBlockMove(block: Block, direction: BlockFace) {
private fun handleBlockMove(block: Block, newBlock: Block) {
val dataArchKingItem = plugin.itemManager.getCustomBlockData(block) ?: return
plugin.itemManager.removeCustomBlockData(block)
val newBlock = block.getRelative(direction)
plugin.itemManager.addCustomBlockData(newBlock, dataArchKingItem)
}

private fun handleBlockMove(block: Block, direction: BlockFace) {
val newBlock = block.getRelative(direction)
handleBlockMove(block, newBlock)
}

@EventHandler
fun onBlockPistonExtendEvent(event: BlockPistonExtendEvent) {
for (block in event.blocks) handleBlockMove(block, event.direction)
Expand All @@ -74,11 +90,33 @@ class EventHandle(private val plugin: ArchKingPlugin) : Listener {
for (block in event.blocks) handleBlockMove(block, event.direction)
}

@EventHandler
fun onEntityChangeBlockEvent(event: EntityChangeBlockEvent) {
if (event.block.type == Material.AIR) {
onArchKingItemPlace(event.entity.persistentDataContainer, event.block)
} else {
val dataArchKingItem = plugin.itemManager.getCustomBlockData(event.block) ?: return
plugin.itemManager.removeCustomBlockData(event.block)
event.entity.persistentDataContainer.set(
NamespacedKey(plugin, ArchKingItem.CUSTOM_ITEM),
PersistentDataType.INTEGER,
dataArchKingItem
)
}
}

private fun onRMCBreak(block: Block) {
val data = plugin.dataManager.getMemory(DataManager.READY_MIXED_CONCRETE_HARD, block.location.toString()) ?: return
plugin.dataManager.removeMemory(DataManager.READY_MIXED_CONCRETE_HARD, block.location.toString())
plugin.server.scheduler.cancelTask(data.asJsonObject.get("schedule").asInt)
}

@EventHandler
fun onBlockBreakEvent(event: BlockBreakEvent) {
if (!event.isDropItems) return
val dataArchKingItem = plugin.itemManager.getCustomBlockData(event.block) ?: return
plugin.itemManager.removeCustomBlockData(event.block)
if (dataArchKingItem == ArchKingItem.READY_MIXED_CONCRETE) onRMCBreak(event.block)

val player = event.player
if (event.player.gameMode == GameMode.CREATIVE) return
Expand All @@ -96,6 +134,7 @@ class EventHandle(private val plugin: ArchKingPlugin) : Listener {
if (event.drops.size == 0) return
val dataArchKingItem = plugin.itemManager.getCustomBlockData(event.block) ?: return
plugin.itemManager.removeCustomBlockData(event.block)
if (dataArchKingItem == ArchKingItem.READY_MIXED_CONCRETE) onRMCBreak(event.block)

val dropItems = plugin.itemManager.getItem(dataArchKingItem, 1)
event.block.drops.clear()
Expand All @@ -112,12 +151,10 @@ class EventHandle(private val plugin: ArchKingPlugin) : Listener {
@EventHandler
fun onEntityDamageByEntityEvent(event: EntityDamageByEntityEvent) {
if (event.damager !is Player) return
val location = event.entity.location.toBlockLocation().toString()
if (plugin.dataManager.getData(DataManager.REBARS).asJsonObject.has(location)) {
plugin.rebarHandler.onBreakRebar(event.entity)
val player = (event.damager as Player)
player.playSound(event.entity.location, Sound.BLOCK_STONE_BREAK, 1f, 1f)
if (player.gameMode != GameMode.CREATIVE) player.inventory.addItem(ItemStack(Material.IRON_BARS))
}
plugin.dataManager.getData(DataManager.REBARS, event.entity.location.toBlockLocation().toString()) ?: return
plugin.rebarHandler.onBreakRebar(event.entity)
val player = (event.damager as Player)
player.playSound(event.entity.location, Sound.BLOCK_STONE_BREAK, 1f, 1f)
if (player.gameMode != GameMode.CREATIVE) player.inventory.addItem(ItemStack(Material.IRON_BARS))
}
}

0 comments on commit 6c16390

Please sign in to comment.