Skip to content

Commit

Permalink
fix citizens remove bug
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed Oct 9, 2023
1 parent 2ad43dd commit 700ac07
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 24 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
}

val questAdderGroup = "kor.toxicity.questadder"
val questAdderVersion = "1.1.4"
val questAdderVersion = "1.1.5"

val adventureVersion = "4.14.0"
val platformVersion = "4.3.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ import java.util.concurrent.ThreadLocalRandom
class QuestAdderBukkit: JavaPlugin(), QuestAdderPlugin {
companion object: QuestAdder {

const val VERSION = "1.1.4"
const val VERSION = "1.1.5"

private val listener = object : Listener {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package kor.toxicity.questadder.manager

import com.google.gson.JsonObject
import kor.toxicity.questadder.QuestAdderBukkit
import kor.toxicity.questadder.api.QuestAdder
import kor.toxicity.questadder.api.event.*
import kor.toxicity.questadder.api.gui.GuiData
import kor.toxicity.questadder.api.gui.MouseButton
Expand Down Expand Up @@ -67,7 +66,8 @@ object DialogManager: QuestAdderManager {
private val shopMap = ConcurrentHashMap<String, Shop>()

private val questNpcMap = HashMap<String, QuestNPC>()
private val actualNPCMap = HashMap<UUID, ActualNPC>()
private val npcIdMap = HashMap<Int, QuestNPC>()
private val actualNPCMap = ConcurrentHashMap<UUID, ActualNPC>()

private val exampleMap = mapOf(
"actions" to "example-action.yml",
Expand All @@ -79,8 +79,8 @@ object DialogManager: QuestAdderManager {
fun getSelectedQuest(player: Player) = selectedQuestMap[player.uniqueId]

override fun start(adder: QuestAdderBukkit) {

Bukkit.getPluginManager().registerEvents(object : Listener {
val pluginManager = Bukkit.getPluginManager()
pluginManager.registerEvents(object : Listener {
@EventHandler
fun quit(e: PlayerQuitEvent) {
val player = e.player
Expand Down Expand Up @@ -119,11 +119,15 @@ object DialogManager: QuestAdderManager {
}
@EventHandler
fun click(e: PlayerInteractAtEntityEvent) {
val uuid = e.rightClicked.uniqueId
val player = e.player
actualNPCMap[uuid]?.let {

val npc = CitizensAPI.getNPCRegistry().getNPC(e.rightClicked) ?: return

actualNPCMap[npc.uniqueId]?.let {

val questNpc = it.questNPC
QuestAdderBukkit.getPlayerData(player)?.let { data ->

if (questNpc.dialogs.isNotEmpty()) {
val dialog = questNpc.dialogs[(data.npcIndexes.getOrPut(it.questNPC.npcKey) {
0
Expand All @@ -146,8 +150,9 @@ object DialogManager: QuestAdderManager {
}
@EventHandler
fun reload(e: CitizensReloadEvent) {
dialogReload(adder)
QuestAdderBukkit.reloadSync()
}

@EventHandler
fun itemClick(e: PlayerInteractEvent) {
e.item?.let {
Expand Down Expand Up @@ -467,12 +472,13 @@ object DialogManager: QuestAdderManager {
}
}
private fun registerNPC(npc: NPC) {
questNpcMap.values.firstOrNull {
npc.id == it.id
}?.let {
val actual = ActualNPC(npc,it)
actualNPCMap[npc.entity.uniqueId] = actual
senderMap[actual.questNPC.npcKey] = actual
npcIdMap[npc.id]?.let {
val uuid = npc.uniqueId
if (!actualNPCMap.containsKey(uuid)) {
val actual = ActualNPC(npc,it)
actualNPCMap[uuid] = actual
senderMap[actual.questNPC.npcKey] = actual
}
}
}

Expand Down Expand Up @@ -540,7 +546,11 @@ object DialogManager: QuestAdderManager {
val npcReader: (File,String,ConfigurationSection) -> Unit = { file, key, c ->
try {
if (senderMap.containsKey(key)) throw RuntimeException("name collision found: $key")
questNpcMap.put(key,QuestNPC(adder, file, key, c))?.let {
val npc = QuestNPC(adder, file, key, c)
npcIdMap.put(npc.id, npc)?.let {
QuestAdderBukkit.warn("npc id collision found: $key in ${file.name} and ${it.file.name}")
}
questNpcMap.put(key, npc)?.let {
QuestAdderBukkit.warn("npc name collision found: $key in ${file.name} and ${it.file.name}")
}
} catch (ex: Exception) {
Expand Down Expand Up @@ -611,6 +621,7 @@ object DialogManager: QuestAdderManager {
actualNPCMap.clear()
senderMap.clear()
shopMap.clear()
npcIdMap.clear()

fun loadConfig(name: String, reader: (File,String,ConfigurationSection) -> Unit) {
adder.loadFolder(name) { file, config ->
Expand Down Expand Up @@ -641,8 +652,10 @@ object DialogManager: QuestAdderManager {
loadConfig("senders", senderReader)
loadConfig("shops", shopReader)

CitizensAPI.getNPCRegistry().forEach {
if (it.isSpawned) registerNPC(it)
CitizensAPI.getNPCRegistries().forEach {
it.forEach { npc ->
registerNPC(npc)
}
}
Bukkit.getConsoleSender().run {
send("${actionMap.size} of actions has successfully loaded.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,16 @@ object ResourcePackManager: QuestAdderManager {
QuestAdderBukkit.Config.importOtherResourcePack.forEach {
val importFile = File(parent,it)
if (importFile.exists() && importFile.isDirectory) {
importFile.copyTo(build)
fun copy(from: File, to: File) {
if (from.isDirectory) {
from.listFiles()?.forEach { file ->
copy(file, File(to,file.name))
}
} else {
if (!to.exists()) from.copyTo(to)
}
}
copy(importFile, build)
} else {
QuestAdderBukkit.warn("this file doesn't exist or is not directory: ${importFile.name}")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ActualNPC(val npc: NPC, val questNPC: QuestNPC): IActualNPC {
private val playerDisplayMap = HashMap<UUID,PlayerDisplay>()

private val thread = QuestAdderBukkit.asyncTaskTimer(questNPC.thread,questNPC.thread) {
val entity = npc.entity
val entity = npc.entity ?: return@asyncTaskTimer
val loc = entity.location
val players = Bukkit.getOnlinePlayers().filter {
it.world == entity.world && loc.distance(it.location) <= questNPC.renderDistance
Expand Down Expand Up @@ -114,19 +114,20 @@ class ActualNPC(val npc: NPC, val questNPC: QuestNPC): IActualNPC {
}
fun update() {
val newState = getState()
val entity = npc.entity ?: return
if (state != newState) {
state = newState
thread?.remove()
thread = when (state) {
State.COMPLETE -> EntityThread(QuestAdderBukkit.nms.createArmorStand(player,npc.entity.location).apply {
State.COMPLETE -> EntityThread(QuestAdderBukkit.nms.createArmorStand(player,entity.location).apply {
setText(Component.empty())
setItem(ItemStack(QuestAdderBukkit.Config.defaultResourcePackItem).apply {
itemMeta = itemMeta?.apply {
setCustomModelData(4)
}
})
})
State.READY_TO_REQUEST -> EntityThread(QuestAdderBukkit.nms.createArmorStand(player,npc.entity.location).apply {
State.READY_TO_REQUEST -> EntityThread(QuestAdderBukkit.nms.createArmorStand(player,entity.location).apply {
setText(Component.empty())
setItem(ItemStack(QuestAdderBukkit.Config.defaultResourcePackItem).apply {
itemMeta = itemMeta?.apply {
Expand All @@ -142,7 +143,7 @@ class ActualNPC(val npc: NPC, val questNPC: QuestNPC): IActualNPC {
private inner class EntityThread(val entity: VirtualEntity) {

private val task = QuestAdderBukkit.asyncTaskTimer(1,1) {
entity.teleport(npc.entity.location.apply {
entity.teleport((npc.entity ?: return@asyncTaskTimer).location.apply {
pitch = 0F
yaw = player.location.yaw - 180F
})
Expand All @@ -168,4 +169,4 @@ class ActualNPC(val npc: NPC, val questNPC: QuestNPC): IActualNPC {
override fun toQuestNPC(): IQuestNPC {
return questNPC
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package kor.toxicity.questadder.util.action;

import kor.toxicity.questadder.api.QuestAdder;
import kor.toxicity.questadder.api.event.QuestAdderEvent;
import kor.toxicity.questadder.api.mechanic.AbstractAction;
import kor.toxicity.questadder.api.mechanic.ActionResult;
import kor.toxicity.questadder.api.util.DataField;
import org.bukkit.EntityEffect;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

public class ActEntityEffect extends AbstractAction {
@DataField(aliases = "e", throwIfNull = true)
public EntityEffect effect;

public ActEntityEffect(@NotNull QuestAdder adder) {
super(adder);
}

@Override
public void initialize() {
super.initialize();
if (!effect.getApplicable().isAssignableFrom(Player.class)) throw new RuntimeException("inapplicable effect: " + effect.name().toLowerCase());
}

@Override
public @NotNull ActionResult invoke(@NotNull Player player, @NotNull QuestAdderEvent event) {
player.playEffect(effect);
return ActionResult.SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ object ActionBuilder {

put("potion",ActPotion::class.java)
put("clearpotion",ActClearPotion::class.java)
put("entityeffect", ActEntityEffect::class.java)

put("clone", ActClone::class.java)
put("removeentity", ActRemoveEntity::class.java)
Expand Down

0 comments on commit 700ac07

Please sign in to comment.