Skip to content

Instantly share code, notes, and snippets.

@MercerK
Last active September 3, 2021 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MercerK/473319ba8b47b4dcc10c5fea6a994442 to your computer and use it in GitHub Desktop.
Save MercerK/473319ba8b47b4dcc10c5fea6a994442 to your computer and use it in GitHub Desktop.
Open Villager Inventory
interface VillagerInventory {
__type: ObjectTypes.VillagerInventory
inventory: obiInventory
villager: obeVillager
player: obePlayer
}
class Mechanic {
inventories: VillagerInventory[] = []
public initializeCommands = () => {
core.command({
name: 'mobinv',
execute: (sender) => {
const player = sender.getServer().getPlayer(sender.getName())
const config = PlayerConfig(player) // Replace this with your persistence storage or store within class
const flag = config.get(Flags.Command_Mob_Inv) // Replace this with your persistence storage or store within class
if (flag) {
player.sendMessage(`MobInv: Disabled`)
config.unset(Flags.Command_Mob_Inv) // Replace this with your persistence storage or store within class
} else {
player.sendMessage(`MobInv: Select an NPC`)
config.set(Flags.Command_Mob_Inv, true) // Replace this with your persistence storage or store within class
}
},
}
public initializeEvents = () => {
core.event('org.bukkit.event.inventory.InventoryCloseEvent', (event) => {
const player = event.getPlayer() as any as obePlayer
const index = this.inventories.findIndex(
(e) => e.player.getUniqueId() === player.getUniqueId()
)
if (index === -1) return
const relatedInventory = this.inventories[index]
const newContents = [...relatedInventory.inventory.getContents()]
const villagerInventory = relatedInventory.villager.getInventory()
villagerInventory.clear()
newContents.forEach((e) => e && villagerInventory.addItem(e))
this.inventories.splice(index, 1)
PlayerConfig(player).unset(Flags.Command_Mob_Inv) // Replace this with your persistence storage or store within class
})
core.event('org.bukkit.event.player.PlayerInteractEntityEvent', (event) => {
const player = event.getPlayer()
const config = PlayerConfig(player)
if (!config.get(Flags.Command_Mob_Inv)) return
const entity = event.getRightClicked()
if (entity.getType() !== EntityType.VILLAGER) return
event.setCancelled(true)
const villager = entity as obeVillager
const villagerInventory = villager.getInventory()
const villagerItems = [...villagerInventory.getContents()]
const inventory = server.createInventory(undefined as any, 9)
villagerItems.forEach((item) => item && inventory.addItem(item))
player.openInventory(inventory)
this.inventories.push({
__type: ObjectTypes.VillagerInventory,
inventory,
villager,
player,
})
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment