-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGemKnife.java
More file actions
213 lines (188 loc) · 8.24 KB
/
Copy pathGemKnife.java
File metadata and controls
213 lines (188 loc) · 8.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package xyz.holocons.mc.holoitemsrevamp.enchantment;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.function.Predicate;
import com.sk89q.worldedit.blocks.Blocks;
import org.bukkit.Material;
import org.bukkit.block.ShulkerBox;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.util.Consumer;
import org.jetbrains.annotations.NotNull;
import com.destroystokyo.paper.MaterialTags;
import com.strangeone101.holoitemsapi.enchantment.CustomEnchantment;
import com.strangeone101.holoitemsapi.enchantment.EnchantmentAbility;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import xyz.holocons.mc.holoitemsrevamp.HoloItemsRevamp;
public class GemKnife extends CustomEnchantment implements EnchantmentAbility {
/**
* When you GemKnife a Block, what item will get spit out/output?
*/
private static final Map<Material, Material> COMPATIBLE_MATERIALS = Map.ofEntries(
Map.entry(Material.COAL_ORE, Material.COAL_BLOCK),
Map.entry(Material.DEEPSLATE_COAL_ORE, Material.COAL_BLOCK),
Map.entry(Material.IRON_ORE, Material.IRON_BLOCK),
Map.entry(Material.DEEPSLATE_IRON_ORE, Material.IRON_BLOCK),
Map.entry(Material.GOLD_ORE, Material.GOLD_BLOCK),
Map.entry(Material.DEEPSLATE_GOLD_ORE, Material.GOLD_BLOCK),
Map.entry(Material.NETHER_GOLD_ORE, Material.GOLD_BLOCK),
Map.entry(Material.GILDED_BLACKSTONE, Material.GOLD_BLOCK),
Map.entry(Material.REDSTONE_ORE, Material.REDSTONE_BLOCK),
Map.entry(Material.DEEPSLATE_REDSTONE_ORE, Material.REDSTONE_BLOCK),
Map.entry(Material.LAPIS_ORE, Material.LAPIS_BLOCK),
Map.entry(Material.DEEPSLATE_LAPIS_ORE, Material.LAPIS_BLOCK),
Map.entry(Material.NETHER_QUARTZ_ORE, Material.QUARTZ_BLOCK),
Map.entry(Material.GLOWSTONE, Material.GLOWSTONE));
/**
* How much of each material do you get per-netherite-ingot?
*/
private static final Map<Material, Integer> MATERIALS_PER_INGOT = Map.ofEntries(
Map.entry(Material.COAL_BLOCK, 64),
Map.entry(Material.IRON_BLOCK, 64),
Map.entry(Material.GOLD_BLOCK, 64),
Map.entry(Material.REDSTONE_BLOCK, 64),
Map.entry(Material.LAPIS_BLOCK, 64),
Map.entry(Material.QUARTZ_BLOCK, 64),
Map.entry(Material.GLOWSTONE, 64)
);
public GemKnife(HoloItemsRevamp plugin) {
super(plugin, "gem_knife");
}
@Override
public int getMaxLevel() {
return 1;
}
@Override
public boolean conflictsWith(@NotNull Enchantment enchantment) {
return false;
}
@Override
public boolean canEnchantItem(@NotNull ItemStack item) {
return item.getType() == Material.EMERALD;
}
@Override
public @NotNull Component displayName(int level) {
return Component.text("Gem Knife", NamedTextColor.GRAY)
.decoration(TextDecoration.ITALIC, false);
}
@Override
public int getCostMultiplier() {
// TODO: should this be infinity instead?
return 12;
}
@Override
public void onPlayerInteract(PlayerInteractEvent event, ItemStack itemStack) {
final var clickedBlock = event.getClickedBlock();
if (clickedBlock == null) {
return;
}
final var materialToDrop = COMPATIBLE_MATERIALS.get(clickedBlock.getType());
if (materialToDrop == null) {
return;
}
final var amountPerIngot = MATERIALS_PER_INGOT.get(materialToDrop);
if(amountPerIngot == null) {
// Shouldn't be possible, but just in case.
return;
}
final var ingotsTaken = removeFromInventory(
event.getPlayer().getInventory(), 1, Material.NETHERITE_INGOT);
final var amountToDrop = amountPerIngot * ingotsTaken;
clickedBlock.getWorld().dropItemNaturally(clickedBlock.getLocation(),
new ItemStack(materialToDrop, amountToDrop));
}
/**
* Removes an item from an inventory. Will recursively check into shulker boxes.
* @param inventory The inventory to scan for an item
* @param maxToTake The maximum amount of the item to remove from the inventory
* @param materialToTake What kind of material should be searched for/taken
* @return How many of the item was successfully removed from the inventory
*/
private static int removeFromInventory(Inventory inventory, final int maxToTake, Material materialToTake) {
int amountToTake = maxToTake;
for(ItemStack stack : inventory.getContents()) {
if(stack == null || stack.isEmpty()) {
continue;
}
// TODO: Check for "Battery" HoloItem
// That, or getShulkerBoxInventory (renamed to getBatteryInventory?) should do that.
final var shulkerInv = getShulkerBoxInventory(stack);
if(shulkerInv != null) {
final var finalAmountToTake = amountToTake;
final var amountTakenFromShulker = editShulkerBoxInventoryWithReturn(stack, editableShulkerInv ->
removeFromInventory(editableShulkerInv, finalAmountToTake, materialToTake));
if(amountTakenFromShulker != null) {
amountToTake -= amountTakenFromShulker;
}
if(amountToTake == 0) {
return maxToTake;
}
continue;
}
if(stack.getType() != materialToTake) {
continue;
}
final var stackSize = stack.getAmount();
if(stackSize >= amountToTake) {
// This stack has enough items.
stack.setAmount(stackSize - amountToTake);
return maxToTake;
}
else {
// This stack does not have enough items; absorb the entire stack.
amountToTake -= stackSize;
stack.setType(Material.AIR);
}
}
return maxToTake - amountToTake;
}
/**
* Edit the inventory of a shulkerbox. After the consumer is called,
* the shulker's inventory is saved, including any edits made.
*
* @param itemStack The (possible) shulkerbox to edit
* @param action The action to perform on the shulker's inventory.
*/
private static void editShulkerBoxInventory(ItemStack itemStack, Consumer<Inventory> action) {
itemStack.editMeta(BlockStateMeta.class, blockStateMeta -> {
if (blockStateMeta.getBlockState() instanceof ShulkerBox shulkerBox) {
action.accept(shulkerBox.getInventory());
}
});
}
/**
* Edit the inventory of a shulkerbox. After the consumer is called,
* the shulker's inventory is saved, including any edits made.
*
* @param itemStack The (possible) shulkerbox to edit
* @param action The action to perform on the shulker's inventory
* @return The return value of action, or null if the action was not called.
* @param <R> The return type of the function
*/
private static <R> R editShulkerBoxInventoryWithReturn(ItemStack itemStack, Function<Inventory, R> action) {
if(!(itemStack.getItemMeta() instanceof BlockStateMeta blockStateMeta)) {
return null;
}
if(!(blockStateMeta.getBlockState() instanceof ShulkerBox shulkerBox)) {
return null;
}
R returnValue = action.apply(shulkerBox.getInventory());
blockStateMeta.setBlockState(shulkerBox);
itemStack.setItemMeta(blockStateMeta);
return returnValue;
}
private static Inventory getShulkerBoxInventory(final ItemStack itemStack) {
if (itemStack.getItemMeta() instanceof BlockStateMeta blockStateMeta
&& blockStateMeta.getBlockState() instanceof ShulkerBox shulkerBox) {
return shulkerBox.getInventory();
} else {
return null;
}
}
}