minecraft_command_types/command/
loot.rs

1use crate::command::item_source::ItemSource;
2use crate::coordinate::Coordinates;
3use crate::entity_selector::EntitySelector;
4use crate::item::ItemStack;
5use crate::resource_location::ResourceLocation;
6use minecraft_command_types_derive::HasMacro;
7use std::fmt::{Display, Formatter};
8
9#[derive(Debug, Clone, Eq, PartialEq, Hash, HasMacro)]
10pub enum LootTarget {
11    Give(EntitySelector),
12    Insert(Coordinates),
13    Spawn(Coordinates),
14    Replace(ItemSource, String, Option<i32>),
15}
16
17impl Display for LootTarget {
18    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
19        match self {
20            LootTarget::Give(selector) => write!(f, "give {}", selector),
21            LootTarget::Insert(coords) => write!(f, "insert {}", coords),
22            LootTarget::Spawn(coords) => write!(f, "spawn {}", coords),
23            LootTarget::Replace(item_source, slot, count) => {
24                write!(f, "replace {} {}", item_source, slot)?;
25
26                if let Some(count) = count {
27                    write!(f, " {}", count)?;
28                }
29
30                Ok(())
31            }
32        }
33    }
34}
35
36#[derive(Debug, Clone, Eq, PartialEq, Hash, HasMacro)]
37pub enum LootItemSource {
38    Tool(ItemStack),
39    Mainhand,
40    Offhand,
41}
42
43impl Display for LootItemSource {
44    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
45        match self {
46            LootItemSource::Tool(tool) => tool.fmt(f),
47            LootItemSource::Mainhand => f.write_str("mainhand"),
48            LootItemSource::Offhand => f.write_str("offhand"),
49        }
50    }
51}
52
53#[derive(Debug, Clone, Eq, PartialEq, Hash, HasMacro)]
54pub enum LootSource {
55    Fish(ResourceLocation, Coordinates, Option<LootItemSource>),
56    Loot(ResourceLocation),
57    Kill(EntitySelector),
58    Mine(Coordinates, Option<LootItemSource>),
59}
60
61impl Display for LootSource {
62    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
63        match self {
64            LootSource::Fish(loot_table, pos, item_source) => {
65                write!(f, "fish {} {}", loot_table, pos)?;
66
67                if let Some(item_source) = item_source {
68                    write!(f, " {}", item_source)?;
69                }
70
71                Ok(())
72            }
73            LootSource::Loot(loot_table) => {
74                write!(f, "loot {}", loot_table)
75            }
76            LootSource::Kill(selector) => {
77                write!(f, "kill {}", selector)
78            }
79            LootSource::Mine(coordinates, item_source) => {
80                write!(f, "mine {}", coordinates)?;
81
82                if let Some(item_source) = item_source {
83                    write!(f, " {}", item_source)?;
84                }
85
86                Ok(())
87            }
88        }
89    }
90}