-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSplinter.java
More file actions
290 lines (250 loc) · 9.89 KB
/
Copy pathSplinter.java
File metadata and controls
290 lines (250 loc) · 9.89 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package xyz.holocons.mc.holoitemsrevamp.enchantment;
import com.destroystokyo.paper.MaterialSetTag;
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 org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Orientable;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import org.jetbrains.annotations.NotNull;
import xyz.holocons.mc.holoitemsrevamp.HoloItemsRevamp;
import xyz.holocons.mc.holoitemsrevamp.integration.Integrations;
import java.util.*;
public class Splinter extends CustomEnchantment implements EnchantmentAbility {
private final HoloItemsRevamp plugin;
private final MaterialSetTag COMPATIBLE_MATERIALS;
private static final Map<Player, ActiveSplinter> splinters = new HashMap<>();
public Splinter(HoloItemsRevamp plugin){
super(plugin, "splinter");
this.plugin = plugin;
var materialSetTagKey = new NamespacedKey(plugin, "splinter_materials");
this.COMPATIBLE_MATERIALS = new MaterialSetTag(materialSetTagKey, this::isCompatibleMaterial).lock();
}
@Override
public int getMaxLevel() {
return 1;
}
@Override
public boolean conflictsWith(@NotNull Enchantment enchantment) {
return false;
}
@Override
public boolean canEnchantItem(@NotNull ItemStack itemStack) {
return MaterialTags.AXES.isTagged(itemStack);
}
@Override
public @NotNull Component displayName(int i) {
return Component.text()
.color(NamedTextColor.GRAY)
.decoration(TextDecoration.ITALIC, false)
.append(Component.text("Splinter"))
.build();
}
@Override
public int getCostMultiplier() {
return 12;
}
@Override
public void onBlockBreak(BlockBreakEvent event, ItemStack itemStack) {
Block blockBroken = event.getBlock();
if(isInvalidSplinterType(blockBroken.getType()) || isInvalidSplinterLocation(blockBroken.getLocation())){
return;
}
// Presuming the block is a log - ie, it has an orientation.
var player = event.getPlayer();
tryCleanupSplinterData(player, blockBroken);
boolean isTree = isLogBlock(blockBroken.getType());
boolean isShroom = isShroomBlock(blockBroken.getType());
boolean isTrunk = isTrunkBlock(blockBroken);
if(isTree && isTrunk){
handleTreeTrunkBroken(player, blockBroken);
}
else if(isTree){
// && !isTrunk
// aka && isBranch
handleTreeBranchBroken(player, blockBroken);
}
else if(isShroom && isTrunk){
handleShroomStemBroken(player, blockBroken);
}
else if(isShroom){
// && !isTrunk
// aka && isBranch
handleShroomBlockBroken(player, blockBroken);
}
// else{} here shouldnt be reachable since anything else would be an invalid splinter type
}
/**
* Try to cleanup a player's Splinter data. If the data is scheduled for cleanup, but the newTrunk isn't a valid
* new trunk block, it will delete the data from the map entirely.
* @param player The player's
* @param newTrunk The proposed new trunk block
*/
private void tryCleanupSplinterData(Player player, Block newTrunk){
final var activeSplinter = splinters.get(player);
// if(!shouldCleanup)
if(activeSplinter != null && activeSplinter.scheduledSplinters != 0){
// there is still a splinter going, so don't cleanup until that one's done
return;
}
splinters.remove(player);
if(!isTrunkBlock(newTrunk)){
return;
}
ActiveSplinter newData = new ActiveSplinter();
newData.origin = newTrunk;
newData.remainingCharges = 32; // In the future we can make this based on enchLevel
newData.scheduledSplinters = 0;
splinters.put(player, newData);
}
private void handleTreeTrunkBroken(Player player, Block trunkBlock){
// TODO
}
private void handleTreeBranchBroken(Player player, Block branchBlock){
// TODO
}
private void handleShroomStemBroken(Player player, Block stemBlock){
// aka the shroom stem
// TODO
}
private void handleShroomBlockBroken(Player player, Block branchBlock){
// TODO
}
private void scheduleSplinterAbility(Player player, Block block) {
var data = splinters.get(player);
if(data.remainingCharges <= 0){
// No power left to schedule anything.
return;
}
int splinterDelay = data.scheduledSplinters;
// Update and save data
data.remainingCharges -= 1;
data.scheduledSplinters += 1;
splinters.put(player, data);
// Schedule a splinter
new BukkitRunnable(){
@Override
public void run() {
// Break the block
// TODO: Check if the player is holding a Splinter axe first.
// I got the itemStack to check for splinter, but idk how to check for splinter.
var activeItem = player.getActiveItem();
player.breakBlock(block);
// ... then update the data.
var updatedData = splinters.get(player);
updatedData.scheduledSplinters -= 1;
splinters.put(player, updatedData);
}
}.runTaskLater(plugin, splinterDelay);
}
/**
* Gets the potential next-branch blocks from this starting branch block. The trunk block is used since branches
* don't "curl in" on theirself, they continue to go farther from the trunk. If the branch is actually part
* of the trunk, all 8 adjacent blocks plus the 8 additional branches are returned.
*/
private List<Block> getPossibleBranches(Block trunk, Block branch){
// Since the main operations afaik will be append and iterator, this is slightly better than arraylist.
// Not by much.
List<Block> ret = new LinkedList<>();
int deltaX = branch.getX() - trunk.getX();
int deltaZ = branch.getZ() - trunk.getZ();
int[] relXs = getRelsToCheck(deltaX);
int[] relZs = getRelsToCheck(deltaZ);
boolean isShroom = isShroomBlock(branch.getType());
for(int relX : relXs){
for(int relZ : relZs){
if(relX == 0 && relZ == 0){
continue;
}
if(isShroom){
// Remember shroom branches also go downwards
ret.add(branch.getRelative(relX, -1, relZ));
}
ret.add(branch.getRelative(relX, 0, relZ));
ret.add(branch.getRelative(relX, 1, relZ));
}
}
return ret;
}
private int[] getRelsToCheck(int delta){
// "Get relatives to check"
if(delta > 0){
return new int[]{1};
}
else if(delta < 0){
return new int[]{-1};
}
else{
// start == 0
return new int[]{-1, 0, 1};
}
}
/**
* In summary: Log-splinters should only continue with other log-splinters, but shroom-splinters should be able
* to move from stem to mushroom block. (That said, mushroom block shouldn't be able to move back to stem.)
* Note: This will also return an empty list (aka "no legal next material") if the baseMaterial is not a legal
* splinter material.
*/
private List<Material> getLegalNextMaterials(Block block){
List<Material> ret = new LinkedList<>();
if(isLogBlock(block.getType())){
ret.add(block.getType());
}
else if(isShroomBlock(block.getType())){
ret.add(block.getType());
if(isTrunkBlock(block)){
// shroom trunks (aka stems) should also be able to go to shroom leaves (aka mushroom blocks)
ret.add(Material.RED_MUSHROOM_BLOCK);
ret.add(Material.BROWN_MUSHROOM_BLOCK);
}
}
// else: return empty list, aka unchanged
return ret;
}
private boolean isInvalidSplinterType(Material type) {
return !this.COMPATIBLE_MATERIALS.isTagged(type);
}
private boolean isInvalidSplinterLocation(Location loc){
return !Integrations.WORLDGUARD.canUseEnchantment(loc, Splinter.class);
}
private boolean isCompatibleMaterial(Material material) {
return isLogBlock(material) || isShroomBlock(material);
}
private boolean isLogBlock(Material material){
// Remember later: All log-blocks are orientable. (shrooms are compatible-material but not orientable.)
return Tag.LOGS.isTagged(material);
}
private boolean isShroomBlock(Material material){
return MaterialTags.MUSHROOM_BLOCKS.isTagged(material);
}
private boolean isTrunkBlock(Block block){
var material = block.getType();
if(isLogBlock(material)){
if(block.getBlockData() instanceof Orientable orientable){
return orientable.getAxis() == Axis.Y;
}
// non-orientable log shouldn't be possible, but just in case:
return false;
}
else if(isShroomBlock(material)){
return Material.MUSHROOM_STEM == material;
}
else{
return false;
}
}
private static class ActiveSplinter {
Block origin;
int remainingCharges;
int scheduledSplinters;
}
}