Skip to content

Commit

Permalink
Rework page Migration, finalize item capturer
Browse files Browse the repository at this point in the history
  • Loading branch information
gabber235 committed Nov 23, 2023
1 parent 17f18ea commit f9b48dc
Show file tree
Hide file tree
Showing 24 changed files with 1,047 additions and 705 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
package me.gabber235.typewriter.entries.action

import com.github.shynixn.mccoroutine.bukkit.launch
import lirand.api.extensions.inventory.meta
import com.google.gson.JsonObject
import lirand.api.extensions.other.set
import me.gabber235.typewriter.adapters.Colors
import me.gabber235.typewriter.adapters.Entry
import me.gabber235.typewriter.adapters.modifiers.Colored
import me.gabber235.typewriter.adapters.modifiers.Help
import me.gabber235.typewriter.adapters.modifiers.MultiLine
import me.gabber235.typewriter.adapters.modifiers.Placeholder
import me.gabber235.typewriter.entry.Criteria
import me.gabber235.typewriter.entry.Modifier
import me.gabber235.typewriter.entry.*
import me.gabber235.typewriter.entry.entries.ActionEntry
import me.gabber235.typewriter.plugin
import me.gabber235.typewriter.utils.Icons
import me.gabber235.typewriter.utils.asMini
import me.gabber235.typewriter.utils.Item
import me.gabber235.typewriter.utils.optional
import org.bukkit.Location
import org.bukkit.Material
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.meta.ItemMeta
import java.util.*

@Entry("drop_item", "Drop an item at location, or on player", Colors.RED, Icons.DROPBOX)
Expand All @@ -39,39 +35,41 @@ class DropItemActionEntry(
override val modifiers: List<Modifier>,
override val triggers: List<String> = emptyList(),
@Help("The item to drop.")
private val material: Material = Material.AIR,
@Help("The amount of items to drop.")
private val amount: Int = 1,
@Colored
@Placeholder
@Help("The display name of the item. (Defaults to the material's display name)")
// The display name of the item to drop. If not specified, the item will have its default display name.
private val displayName: String = "",
@MultiLine
@Colored
@Placeholder
@Help("The lore of the item. (Defaults to the item's lore)")
// The lore of the item to drop. If not specified, the item will have its default lore.
private val lore: String,
val item: Item = Item.Empty,
@Help("The location to drop the item. (Defaults to the player's location)")
// The location to drop the item at. If this field is left blank, the item will be dropped at the location of the player triggering the action.
private val location: Optional<Location> = Optional.empty(),
) : ActionEntry {
override fun execute(player: Player) {
super.execute(player)
val item = ItemStack(material, amount).meta<ItemMeta> {
if (this@DropItemActionEntry.displayName.isNotBlank()) displayName(this@DropItemActionEntry.displayName.asMini())
if (this@DropItemActionEntry.lore.isNotBlank()) {
lore(this@DropItemActionEntry.lore.split("\n").map { "<gray>$it".asMini() })
}
}
// Run on main thread
plugin.launch {
if (location.isPresent) {
location.get().world.dropItem(location.get(), item)
location.get().world.dropItem(location.get(), item.build(player))
} else {
player.location.world.dropItem(player.location, item)
player.location.world.dropItem(player.location, item.build(player))
}
}
}
}
}

@EntryMigration(DropItemActionEntry::class, "0.4.0")
private fun migrate040(json: JsonObject, context: EntryMigratorContext): JsonObject {
val data = JsonObject()
data.copyAllBut(json, "material", "amount", "displayName", "lore")

val material = json.getAndParse<Material>("material", context.gson).optional
val amount = json.getAndParse<Int>("amount", context.gson).optional
val displayName = json.getAndParse<String>("displayName", context.gson).optional
val lore = json.getAndParse<String>("lore", context.gson).optional

val item = Item(
material = material,
amount = amount,
name = displayName,
lore = lore,
)
data["item"] = context.gson.toJsonTree(item)

return data
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package me.gabber235.typewriter.entries.action

import com.google.gson.JsonObject
import lirand.api.extensions.other.set
import me.gabber235.typewriter.adapters.Colors
import me.gabber235.typewriter.adapters.Entry
import me.gabber235.typewriter.adapters.modifiers.Help
import me.gabber235.typewriter.entry.Criteria
import me.gabber235.typewriter.entry.Modifier
import me.gabber235.typewriter.entry.*
import me.gabber235.typewriter.entry.entries.ActionEntry
import me.gabber235.typewriter.utils.Icons
import me.gabber235.typewriter.utils.Item
import me.gabber235.typewriter.utils.optional
import org.bukkit.Material
import org.bukkit.entity.Player

@Entry("give_item", "Give an item to the player", Colors.RED, Icons.WAND_SPARKLES)
Expand All @@ -32,4 +35,25 @@ class GiveItemActionEntry(

player.inventory.addItem(item.build(player))
}
}

@EntryMigration(GiveItemActionEntry::class, "0.4.0")
private fun migrate040(json: JsonObject, context: EntryMigratorContext): JsonObject {
val data = JsonObject()
data.copyAllBut(json, "material", "amount", "displayName", "lore")

val material = json.getAndParse<Material>("material", context.gson).optional
val amount = json.getAndParse<Int>("amount", context.gson).optional
val displayName = json.getAndParse<String>("displayName", context.gson).optional
val lore = json.getAndParse<String>("lore", context.gson).optional

val item = Item(
material = material,
amount = amount,
name = displayName,
lore = lore,
)
data["item"] = context.gson.toJsonTree(item)

return data
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
package me.gabber235.typewriter.entries.action

import lirand.api.extensions.inventory.meta
import com.google.gson.JsonObject
import lirand.api.extensions.other.set
import me.gabber235.typewriter.adapters.Colors
import me.gabber235.typewriter.adapters.Entry
import me.gabber235.typewriter.adapters.modifiers.Colored
import me.gabber235.typewriter.adapters.modifiers.Help
import me.gabber235.typewriter.adapters.modifiers.Placeholder
import me.gabber235.typewriter.entry.Criteria
import me.gabber235.typewriter.entry.Modifier
import me.gabber235.typewriter.entry.*
import me.gabber235.typewriter.entry.entries.ActionEntry
import me.gabber235.typewriter.utils.Icons
import me.gabber235.typewriter.utils.asMini
import me.gabber235.typewriter.utils.Item
import me.gabber235.typewriter.utils.optional
import org.bukkit.Material
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
import java.util.*

@Entry("remove_item", "Remove an item from the players inventory", Colors.RED, Icons.WAND_SPARKLES)
/**
* The `Remove Item Action` is an action that removes an item from the player's inventory.
* This action provides you with the ability to remove items from the player's inventory in response to specific events.
* <Admonition type="caution">
* This action will try to remove "as much as possible" but does not verify if the player has enough items in their inventory.
* If you want to guarantee that the player has enough items in their inventory, add an
* <Link to='../fact/inventory_item_count_fact'>Inventory Item Count Fact</Link> to the criteria.
* </Admonition>
*
* ## How could this be used?
*
* This can be used when `giving` an NPC an item, and you want to remove the item from the player's inventory.
* Or when you want to remove an item from the player's inventory when they complete a quest or achievement.
* Or when you want to remove a key from the player's inventory after they use it to unlock a door.
*/
class RemoveItemActionEntry(
override val id: String = "",
Expand All @@ -33,32 +36,32 @@ class RemoveItemActionEntry(
override val modifiers: List<Modifier>,
override val triggers: List<String> = emptyList(),
@Help("The item to remove.")
private val material: Material = Material.AIR,
@Help("The amount of items to remove.")
private val amount: Int = 1,
@Help("Does the player need to have the exact amount of items?")
private val exactAmount: Boolean = false,
@Placeholder
@Colored
@Help("The name of the item.")
// If the name is given, the item must have the same name to be removed.
private val itemName: Optional<String> = Optional.empty(),
val item: Item = Item.Empty,
) : ActionEntry {
override fun execute(player: Player) {
super.execute(player)

val item = ItemStack(material, amount).meta {
itemName.ifPresent {
displayName(it.asMini())
}
}
if (exactAmount) {
if (player.inventory.containsAtLeast(item, amount)) {
player.inventory.removeItemAnySlot(item)
}
} else {
player.inventory.removeItemAnySlot(item)
}
player.inventory.removeItemAnySlot(item.build(player))
}
}

@EntryMigration(RemoveItemActionEntry::class, "0.4.0")
private fun migrate040(json: JsonObject, context: EntryMigratorContext): JsonObject {
val data = JsonObject()
data.copyAllBut(json, "material", "amount", "itemName")

val material = json.getAndParse<Material>("material", context.gson).optional
val amount = json.getAndParse<Int>("amount", context.gson).optional
val displayName = json.getAndParse<Optional<String>>("itemName", context.gson).optional

val item = Item(
material = material,
amount = amount,
name = displayName,
)
data["item"] = context.gson.toJsonTree(item)

return data
}


Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package me.gabber235.typewriter.entries.event

import com.google.gson.JsonObject
import lirand.api.extensions.other.set
import me.gabber235.typewriter.adapters.Colors
import me.gabber235.typewriter.adapters.Entry
import me.gabber235.typewriter.adapters.modifiers.Help
import me.gabber235.typewriter.adapters.modifiers.MaterialProperties
import me.gabber235.typewriter.adapters.modifiers.MaterialProperty.ITEM
import me.gabber235.typewriter.entry.EntryListener
import me.gabber235.typewriter.entry.Query
import me.gabber235.typewriter.entry.*
import me.gabber235.typewriter.entry.entries.EventEntry
import me.gabber235.typewriter.entry.startDialogueWithOrNextDialogue
import me.gabber235.typewriter.utils.Icons
import me.gabber235.typewriter.utils.Item
import me.gabber235.typewriter.utils.optional
import org.bukkit.Material
import org.bukkit.entity.Player
import org.bukkit.event.entity.EntityPickupItemEvent
Expand All @@ -26,9 +26,8 @@ class PickupItemEventEntry(
override val id: String = "",
override val name: String = "",
override val triggers: List<String> = emptyList(),
@MaterialProperties(ITEM)
@Help("The item that was picked up.")
val material: Material = Material.STONE,
@Help("The item to listen for.")
val item: Item = Item.Empty,
) : EventEntry

@EntryListener(PickupItemEventEntry::class)
Expand All @@ -38,6 +37,20 @@ fun onPickupItem(event: EntityPickupItemEvent, query: Query<PickupItemEventEntry
val player = event.entity as Player

query findWhere { entry ->
entry.material == event.item.itemStack.type
entry.item.isSameAs(player, event.item.itemStack)
} startDialogueWithOrNextDialogue player
}
}

@EntryMigration(PickupItemEventEntry::class, "0.4.0")
private fun migrate040(json: JsonObject, context: EntryMigratorContext): JsonObject {
val data = JsonObject()
data.copyAllBut(json, "material")

val material = json.getAndParse<Material>("material", context.gson).optional
val item = Item(
material = material,
)
data["item"] = context.gson.toJsonTree(item)

return data
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package me.gabber235.typewriter.entries.fact

import com.google.gson.JsonObject
import lirand.api.extensions.other.set
import lirand.api.extensions.server.server
import me.gabber235.typewriter.adapters.Colors
import me.gabber235.typewriter.adapters.Entry
import me.gabber235.typewriter.adapters.modifiers.Help
import me.gabber235.typewriter.entry.EntryMigration
import me.gabber235.typewriter.entry.EntryMigratorContext
import me.gabber235.typewriter.entry.copyAllBut
import me.gabber235.typewriter.entry.entries.ReadableFactEntry
import me.gabber235.typewriter.entry.getAndParse
import me.gabber235.typewriter.facts.Fact
import me.gabber235.typewriter.utils.Icons
import me.gabber235.typewriter.utils.asMini
import me.gabber235.typewriter.utils.Item
import me.gabber235.typewriter.utils.optional
import org.bukkit.Material
import org.bukkit.inventory.ItemStack
import java.util.*

@Entry(
Expand All @@ -32,26 +38,29 @@ class InventoryItemCountFact(
override val id: String = "",
override val name: String = "",
override val comment: String = "",
@Help("The material the item needs to be.")
// If specified, only items with this material will be counted.
private val material: Optional<Material> = Optional.empty(),
@Help("The name of the item.")
// If specified, only items with this name will be counted.
private val itemName: Optional<String> = Optional.empty(),
@Help("The item to check for.")
val item: Item = Item.Empty,
) : ReadableFactEntry {
private fun isValid(item: ItemStack): Boolean {
if (material.isPresent) {
if (item.type != material.get()) return false
}
if (itemName.isPresent) {
if (item.itemMeta?.displayName() != itemName.get().asMini()) return false
}
return true
}

override fun read(playerId: UUID): Fact {
val player = server.getPlayer(playerId) ?: return Fact(id, 0)
val amount = player.inventory.contents.filterNotNull().filter { isValid(it) }.sumOf { it.amount }
val amount = player.inventory.contents.filterNotNull().filter { item.isSameAs(player, it) }.sumOf { it.amount }
return Fact(id, amount)
}
}

@EntryMigration(InventoryItemCountFact::class, "0.4.0")
private fun migrate040(json: JsonObject, context: EntryMigratorContext): JsonObject {
val data = JsonObject()
data.copyAllBut(json, "material", "itemName")

val material = json.getAndParse<Optional<Material>>("material", context.gson).optional
val displayName = json.getAndParse<Optional<String>>("itemName", context.gson).optional

val item = Item(
material = material,
name = displayName,
)
data["item"] = context.gson.toJsonTree(item)

return data
}
6 changes: 6 additions & 0 deletions app/lib/widgets/inspector/header.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ import "package:typewriter/utils/extensions.dart";
import "package:typewriter/widgets/components/app/writers.dart";
import "package:typewriter/widgets/inspector/editors.dart";
import "package:typewriter/widgets/inspector/headers/capture_action.dart";
import "package:typewriter/widgets/inspector/headers/colored_action.dart";
import "package:typewriter/widgets/inspector/headers/entry_selector_action.dart";
import "package:typewriter/widgets/inspector/headers/help_action.dart";
import "package:typewriter/widgets/inspector/headers/length_action.dart";
import "package:typewriter/widgets/inspector/headers/multiline_action.dart";
import "package:typewriter/widgets/inspector/headers/placeholder_action.dart";
import "package:typewriter/widgets/inspector/section_title.dart";

part "header.g.dart";
Expand Down Expand Up @@ -165,6 +168,9 @@ class Header extends InheritedWidget {
@riverpod
List<HeaderActionFilter> headerActionFilters(HeaderActionFiltersRef ref) => [
HelpHeaderActionFilter(),
ColoredHeaderActionFilter(),
PlaceholderHeaderActionFilter(),
RegexHeaderActionFilter(),
LengthHeaderActionFilter(),
EntrySelectorHeaderActionFilter(),
CaptureHeaderActionFilter(),
Expand Down
Loading

0 comments on commit f9b48dc

Please sign in to comment.