1 /*
2 * Copyright (c) 2017-2019 sel-project
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 *
22 */
23 /**
24 * Copyright: Copyright (c) 2017-2019 sel-project
25 * License: MIT
26 * Authors: Kripth
27 * Source: $(HTTP github.com/sel-project/selery/source/selery/item/miscellaneous.d, selery/item/miscellaneous.d)
28 */
29 module selery.item.miscellaneous;
30
31 import std.conv : to, ConvException;
32 import std.traits : isIntegral;
33
34 import sel.nbt.tags;
35
36 import selery.about : block_t, item_t;
37 import selery.block.block : Block;
38 import selery.block.blocks : Blocks;
39 import selery.item.item : SimpleItem;
40 import selery.item.slot : Slot;
41 import selery.math.vector : BlockPosition, face, entityPosition;
42 import selery.player.player : Player;
43
44 static import sul.items;
45
46 class BucketItem(sul.items.Item si, item_t[item_t] pickups) : SimpleItem!(si) {
47
48 alias sul = si;
49
50 public this(F...)(F args) {
51 super(args);
52 }
53
54 public final override pure nothrow @property @safe @nogc bool placeable() {
55 return true;
56 }
57
58 public override bool onPlaced(Player player, BlockPosition position, uint tface) {
59 auto pos = position.face(tface);
60 auto pick = player.world[pos].id in pickups;
61 if(pick) {
62 player.world[pos] = Blocks.air;
63 Slot slot = Slot(player.world.items.get(*pick), 1);
64 if(player.inventory.held.count == 1) player.inventory.held = slot;
65 else if(!player.inventory.add(slot).empty) player.drop(slot);
66 return true;
67 } else {
68 return false;
69 }
70 }
71
72 alias slot this;
73
74 }
75
76 class FilledBucketItem(sul.items.Item si, block_t place, item_t residue) : SimpleItem!(si) {
77
78 alias sul = si;
79
80 public this(F...)(F args) {
81 super(args);
82 }
83
84 public final override pure nothrow @property @safe @nogc bool placeable() {
85 return true;
86 }
87
88 public override bool onPlaced(Player player, BlockPosition position, uint tface) {
89 Block rep = player.world[position];
90 if(!rep.replaceable) {
91 position = face(position, tface);
92 rep = player.world[position];
93 if(rep != Blocks.air) return false;
94 }
95 player.world[position] = place;
96 player.inventory.held = Slot(player.world.items.get(residue), 1);
97 return true;
98 }
99
100 alias slot this;
101
102 }
103
104 class MapItem(sul.items.Item si) : SimpleItem!(si) {
105
106 alias sul = si;
107
108 private ushort m_map_id;
109
110 public this(E...)(E args) {
111 static if(E.length > 0 && isIntegral!(E[0])) {
112 static if(E.length > 1) super(args[1..$]);
113 this.mapId = args[0] & ushort.max;
114 } else {
115 super(args);
116 }
117 }
118
119 public override pure nothrow @property @safe @nogc ushort javaMeta() {
120 return this.mapId;
121 }
122
123 public pure nothrow @property @safe @nogc ushort mapId() {
124 return this.m_map_id;
125 }
126
127 public @property @safe ushort mapId(ushort mapId) {
128 if(this.m_pe_tag is null) this.m_pe_tag = new Compound();
129 this.m_pe_tag["map_uuid"] = to!string(mapId);
130 return this.m_map_id = mapId;
131 }
132
133 public override void parseBedrockCompound(Compound compound) {
134 super.parseBedrockCompound(compound);
135 compound = compound.get!Compound("", compound);
136 if(compound.has!String("map_uuid")) {
137 try {
138 this.mapId = to!ushort(compound.getValue!String("map_uuid", ""));
139 } catch(ConvException e) {}
140 }
141 }
142
143 alias slot this;
144
145 }