-
Notifications
You must be signed in to change notification settings - Fork 529
/
SlimefunItem.java
1205 lines (1056 loc) · 43.4 KB
/
SlimefunItem.java
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package io.github.thebusybiscuit.slimefun4.api.items;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import org.apache.commons.lang.Validate;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.permissions.Permission;
import io.github.bakedlibs.dough.collections.OptionalMap;
import io.github.bakedlibs.dough.items.ItemUtils;
import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion;
import io.github.thebusybiscuit.slimefun4.api.SlimefunAddon;
import io.github.thebusybiscuit.slimefun4.api.SlimefunBranch;
import io.github.thebusybiscuit.slimefun4.api.exceptions.IdConflictException;
import io.github.thebusybiscuit.slimefun4.api.exceptions.IncompatibleItemHandlerException;
import io.github.thebusybiscuit.slimefun4.api.exceptions.MissingDependencyException;
import io.github.thebusybiscuit.slimefun4.api.exceptions.UnregisteredItemException;
import io.github.thebusybiscuit.slimefun4.api.exceptions.WrongItemStackException;
import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile;
import io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType;
import io.github.thebusybiscuit.slimefun4.api.researches.Research;
import io.github.thebusybiscuit.slimefun4.core.SlimefunRegistry;
import io.github.thebusybiscuit.slimefun4.core.attributes.NotConfigurable;
import io.github.thebusybiscuit.slimefun4.core.attributes.Placeable;
import io.github.thebusybiscuit.slimefun4.core.attributes.Radioactive;
import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuide;
import io.github.thebusybiscuit.slimefun4.core.handlers.GlobalItemHandler;
import io.github.thebusybiscuit.slimefun4.implementation.Slimefun;
import io.github.thebusybiscuit.slimefun4.implementation.items.VanillaItem;
import io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.enchanting.AutoDisenchanter;
import io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.enchanting.AutoEnchanter;
import me.mrCookieSlime.Slimefun.Objects.handlers.BlockTicker;
/**
* A {@link SlimefunItem} is a custom item registered by a {@link SlimefunAddon}.
* This class defines the behaviours of the item, you can assign an {@link ItemHandler}
* to give the item functionality.
* In contrast to that the {@link SlimefunItemStack} defines the look and feel of the item.
*
* Remember to call {@link #register(SlimefunAddon)} on your {@link SlimefunItem} for it
* to appear in the {@link SlimefunGuide}.
*
* @author TheBusyBiscuit
* @author Poslovitch
*
* @see SlimefunItemStack
* @see SlimefunAddon
*
*/
public class SlimefunItem implements Placeable {
/**
* This is our item id.
*/
private final String id;
/**
* This is the original {@link ItemStack} that represents this item.
* It is immutable and should always be cloned, never used directly.
*/
private final ItemStack itemStackTemplate;
/**
* This is a reference to the {@link SlimefunAddon} that registered this
* {@link SlimefunItem}, if the item has not been registered yet, it will be null.
*/
protected SlimefunAddon addon;
/**
* This is the state of this {@link SlimefunItem}.
*/
private ItemState state = ItemState.UNREGISTERED;
/**
* This is the {@link ItemGroup} in which this {@link SlimefunItem} can be found.
*/
private ItemGroup itemGroup;
/**
* This is a reference to the associated {@link Research}, can be null.
*/
private Research research;
private ItemStack[] recipe;
private RecipeType recipeType;
protected ItemStack recipeOutput;
protected boolean enchantable = true;
protected boolean disenchantable = true;
protected boolean hidden = false;
protected boolean useableInWorkbench = false;
private Optional<String> wikiURL = Optional.empty();
private final OptionalMap<Class<? extends ItemHandler>, ItemHandler> itemHandlers = new OptionalMap<>(HashMap::new);
private final Set<ItemSetting<?>> itemSettings = new HashSet<>();
private boolean ticking = false;
private BlockTicker blockTicker;
/**
* This creates a new {@link SlimefunItem} from the given arguments.
*
* @param itemGroup
* The {@link ItemGroup} this {@link SlimefunItem} belongs to
* @param item
* The {@link SlimefunItemStack} that describes the visual features of our {@link SlimefunItem}
* @param recipeType
* the {@link RecipeType} that determines how this {@link SlimefunItem} is crafted
* @param recipe
* An Array representing the recipe of this {@link SlimefunItem}
*/
@ParametersAreNonnullByDefault
public SlimefunItem(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) {
this(itemGroup, item, recipeType, recipe, null);
}
/**
* This creates a new {@link SlimefunItem} from the given arguments.
*
* @param itemGroup
* The {@link ItemGroup} this {@link SlimefunItem} belongs to
* @param item
* The {@link SlimefunItemStack} that describes the visual features of our {@link SlimefunItem}
* @param recipeType
* the {@link RecipeType} that determines how this {@link SlimefunItem} is crafted
* @param recipe
* An Array representing the recipe of this {@link SlimefunItem}
* @param recipeOutput
* The result of crafting this item
*/
@ParametersAreNonnullByDefault
public SlimefunItem(ItemGroup itemGroup, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, @Nullable ItemStack recipeOutput) {
Validate.notNull(itemGroup, "'itemGroup' is not allowed to be null!");
Validate.notNull(item, "'item' is not allowed to be null!");
Validate.notNull(recipeType, "'recipeType' is not allowed to be null!");
this.itemGroup = itemGroup;
this.itemStackTemplate = item;
this.id = item.getItemId();
this.recipeType = recipeType;
this.recipe = recipe;
this.recipeOutput = recipeOutput;
}
// Previously deprecated constructor, now only for internal purposes
@ParametersAreNonnullByDefault
protected SlimefunItem(ItemGroup itemGroup, ItemStack item, String id, RecipeType recipeType, ItemStack[] recipe) {
Validate.notNull(itemGroup, "'itemGroup' is not allowed to be null!");
Validate.notNull(item, "'item' is not allowed to be null!");
Validate.notNull(id, "'id' is not allowed to be null!");
Validate.notNull(recipeType, "'recipeType' is not allowed to be null!");
this.itemGroup = itemGroup;
this.itemStackTemplate = item;
this.id = id;
this.recipeType = recipeType;
this.recipe = recipe;
}
/**
* Returns the identifier of this {@link SlimefunItem}.
*
* @return the identifier of this {@link SlimefunItem}
*/
public final @Nonnull String getId() {
return id;
}
/**
* This method returns the {@link ItemState} this {@link SlimefunItem}
* is currently in. This can be used to determine whether a {@link SlimefunItem}
* is enabled or disabled.
*
* {@link VanillaItem} represents a special case here.
*
* @return The {@link ItemState} of this {@link SlimefunItem}
*/
public @Nonnull ItemState getState() {
return state;
}
/**
* This returns the {@link ItemStack} of this {@link SlimefunItem}.
* The {@link ItemStack} describes the look and feel of this {@link SlimefunItem}.
*
* @return The {@link ItemStack} that this {@link SlimefunItem} represents
*/
public @Nonnull ItemStack getItem() {
return itemStackTemplate;
}
/**
* This returns the {@link ItemGroup} of our {@link SlimefunItem}, every {@link SlimefunItem}
* is associated with exactly one {@link ItemGroup}.
*
* @return The {@link ItemGroup} that this {@link SlimefunItem} belongs to
*/
public @Nonnull ItemGroup getItemGroup() {
return itemGroup;
}
/**
* Retrieve the recipe for this {@link SlimefunItem}.
*
* @return An {@link ItemStack} array of 9 which represents the recipe for this {@link SlimefunItem}
*/
public @Nonnull ItemStack[] getRecipe() {
return recipe;
}
/**
* This method returns the {@link RecipeType}.
* The {@link RecipeType} determines how this {@link SlimefunItem} is crafted.
*
* @return The {@link RecipeType} of this {@link SlimefunItem}
*/
public @Nonnull RecipeType getRecipeType() {
return recipeType;
}
/**
* This method returns the result of crafting this {@link SlimefunItem}
*
* @return The recipe output of this {@link SlimefunItem}
*/
public @Nonnull ItemStack getRecipeOutput() {
return recipeOutput != null ? recipeOutput.clone() : itemStackTemplate.clone();
}
/**
* This method returns the {@link Research} this {@link SlimefunItem} is linked to.
* This will be null if the item is not linked to any {@link Research}
*
* @return The linked {@link Research} or null
*/
public final @Nullable Research getResearch() {
return research;
}
/**
* This returns whether this {@link SlimefunItem} has a {@link Research}
* assigned to it.
* It is equivalent to a null check performed on {@link #getResearch()}.
*
* @return Whether this {@link SlimefunItem} has a {@link Research}
*/
public final boolean hasResearch() {
return research != null;
}
/**
* This returns a {@link Set} containing all instances of {@link ItemSetting} for this {@link SlimefunItem}.
*
* @return A {@link Set} of every {@link ItemSetting} for this {@link SlimefunItem}
*/
public @Nonnull Set<ItemSetting<?>> getItemSettings() {
return itemSettings;
}
/**
* This method returns an {@link Optional} holding an {@link ItemSetting} with the given
* key and data type. Or an empty {@link Optional} if this {@link SlimefunItem} has no such {@link ItemSetting}.
*
* @param <T>
* The Type of value stored in this {@link ItemSetting}
* @param key
* The key of this {@link ItemSetting}
* @param c
* The {@link Class} of the type of value stored by this setting
*
* @return An {@link Optional} describing the result
*/
@SuppressWarnings("unchecked")
public @Nonnull <T> Optional<ItemSetting<T>> getItemSetting(@Nonnull String key, @Nonnull Class<T> c) {
for (ItemSetting<?> setting : itemSettings) {
if (setting.getKey().equals(key) && setting.isType(c)) {
return Optional.of((ItemSetting<T>) setting);
}
}
return Optional.empty();
}
/**
* This returns whether or not this {@link SlimefunItem} is allowed to be used in
* an {@link AutoEnchanter}.
*
* @return Whether this {@link SlimefunItem} can be enchanted.
*/
public boolean isEnchantable() {
return enchantable;
}
/**
* This returns whether or not this {@link SlimefunItem} is allowed to be used in
* an {@link AutoDisenchanter}.
*
* @return Whether this {@link SlimefunItem} can be disenchanted.
*/
public boolean isDisenchantable() {
return disenchantable;
}
/**
* This method returns whether this {@link SlimefunItem} was hidden from the
* {@link SlimefunGuide}.
*
* @return Whether this {@link SlimefunItem} is hidden.
*/
public final boolean isHidden() {
return hidden;
}
/**
* This method will forcefully hide this {@link SlimefunItem} from the {@link SlimefunGuide}.
*
* @param hidden
* Whether to hide this {@link SlimefunItem} or not
*/
public void setHidden(boolean hidden) {
if (this.hidden != hidden) {
this.hidden = hidden;
if (state == ItemState.ENABLED) {
if (hidden) {
itemGroup.remove(this);
} else {
itemGroup.add(this);
}
}
}
}
/**
* This method returns whether this {@link SlimefunItem} is disabled.
*
* @return Whether this {@link SlimefunItem} is disabled.
*/
public boolean isDisabled() {
if (state == ItemState.UNREGISTERED) {
error("isDisabled() cannot be called before registering the item", new UnregisteredItemException(this));
return false;
}
return state != ItemState.ENABLED;
}
/**
* This method returns whether this {@link SlimefunItem} is disabled
* for that specific {@link World}.
* Note that if the item is disabled globally, this method will still return false.
*
* @param world
* The {@link World} to check
*
* @return Whether this {@link SlimefunItem} is disabled in that world (or in general).
*/
public boolean isDisabledIn(@Nonnull World world) {
if (state == ItemState.UNREGISTERED) {
error("isDisabled(World) cannot be called before registering the item", new UnregisteredItemException(this));
return false;
}
// Check if the Item is disabled globally or in this specific world
return isDisabled() || !Slimefun.getWorldSettingsService().isEnabled(world, this);
}
/**
* This method returns the {@link SlimefunAddon} that registered this
* {@link SlimefunItem}. If this Item is from Slimefun itself, the current
* instance of {@link Slimefun} will be returned.
* Use an instanceof check to account for that.
*
* @return The {@link SlimefunAddon} that registered this {@link SlimefunItem}
*/
public final @Nonnull SlimefunAddon getAddon() {
if (addon == null) {
throw new UnregisteredItemException(this);
}
return addon;
}
public BlockTicker getBlockTicker() {
return blockTicker;
}
/**
* This method registers this {@link SlimefunItem}.
* Always call this method after your {@link SlimefunItem} has been initialized.
* Never call it more than once!
*
* @param addon
* The {@link SlimefunAddon} that this {@link SlimefunItem} belongs to.
*/
public void register(@Nonnull SlimefunAddon addon) {
Validate.notNull(addon, "A SlimefunAddon cannot be null!");
Validate.notNull(addon.getJavaPlugin(), "SlimefunAddon#getJavaPlugin() is not allowed to return null!");
this.addon = addon;
try {
checkDependencies(addon);
checkForConflicts();
preRegister();
if (recipe == null) {
recipe = new ItemStack[9];
} else if (recipe.length < 9) {
recipe = Arrays.copyOf(recipe, 9);
}
Slimefun.getRegistry().getAllSlimefunItems().add(this);
Slimefun.getRegistry().getSlimefunItemIds().put(id, this);
// Items that are "not-configurable" cannot be configured.
if (!(this instanceof NotConfigurable)) {
Slimefun.getItemCfg().setDefaultValue(id + ".enabled", true);
Slimefun.getItemCfg().setDefaultValue(id + ".can-be-used-in-workbenches", useableInWorkbench);
Slimefun.getItemCfg().setDefaultValue(id + ".hide-in-guide", hidden);
Slimefun.getItemCfg().setDefaultValue(id + ".allow-enchanting", enchantable);
Slimefun.getItemCfg().setDefaultValue(id + ".allow-disenchanting", disenchantable);
// Load all item settings
for (ItemSetting<?> setting : itemSettings) {
setting.reload();
}
}
if (ticking && !Slimefun.getCfg().getBoolean("URID.enable-tickers")) {
state = ItemState.DISABLED;
return;
}
if (this instanceof NotConfigurable) {
/*
* Not-configurable items will be enabled.
* Any other settings will remain as default.
*/
state = ItemState.ENABLED;
} else if (Slimefun.getItemCfg().getBoolean(id + ".enabled")) {
// The item has been enabled.
state = ItemState.ENABLED;
useableInWorkbench = Slimefun.getItemCfg().getBoolean(id + ".can-be-used-in-workbenches");
hidden = Slimefun.getItemCfg().getBoolean(id + ".hide-in-guide");
enchantable = Slimefun.getItemCfg().getBoolean(id + ".allow-enchanting");
disenchantable = Slimefun.getItemCfg().getBoolean(id + ".allow-disenchanting");
} else if (this instanceof VanillaItem) {
// This item is a vanilla "mock" but was disabled.
state = ItemState.VANILLA_FALLBACK;
} else {
// The item has been disabled.
state = ItemState.DISABLED;
}
// Now we can be certain this item should be enabled
if (state == ItemState.ENABLED) {
onEnable();
} else {
// Clear item handlers if we are disabled so that calling them isn't possible later on
for (ItemHandler handler : this.itemHandlers.values()) {
if (handler instanceof BlockTicker) {
Slimefun.getRegistry().getTickerBlocks().remove(getId());
}
}
this.itemHandlers.clear();
}
// Lock the SlimefunItemStack from any accidental manipulations
if (itemStackTemplate instanceof SlimefunItemStack stack && isItemStackImmutable()) {
stack.lock();
}
postRegister();
// handle runtime-registrations / auto-loading
if (Slimefun.getRegistry().isAutoLoadingEnabled() && state == ItemState.ENABLED) {
info("Item was registered during runtime.");
load();
}
} catch (Exception x) {
error("Registering " + toString() + " has failed!", x);
}
}
/**
* This method is called when this {@link SlimefunItem} is currently being registered
* and we are certain that it will be enabled.
*
* <strong>This method is for internal purposes, like {@link ItemGroup} registration only</strong>
*/
private final void onEnable() {
// Register the ItemGroup too if it hasn't been registered yet
if (!itemGroup.isRegistered()) {
itemGroup.register(addon);
}
// Send out deprecation warnings for any classes or interfaces
checkForDeprecations(getClass());
// Check for an illegal stack size
if (itemStackTemplate.getAmount() != 1) {
// @formatter:off
warn("This item has an illegal stack size: " + itemStackTemplate.getAmount()
+ ". An Item size of 1 is recommended. Please inform the author(s) of " + addon.getName()
+ " to fix this. Crafting Results with amounts of higher should be handled"
+ " via the recipeOutput parameter!");
// @formatter:on
}
// Add it to the list of enabled items
Slimefun.getRegistry().getEnabledSlimefunItems().add(this);
// Load our Item Handlers
loadItemHandlers();
// Properly mark this Item as radioactive
if (this instanceof Radioactive) {
Slimefun.getRegistry().getRadioactiveItems().add(this);
}
}
private void loadItemHandlers() {
for (ItemHandler handler : itemHandlers.values()) {
Optional<IncompatibleItemHandlerException> exception = handler.validate(this);
// Check if the validation caused an exception.
if (exception.isPresent()) {
throw exception.get();
} else {
/*
* Make developers or at least Server admins aware that an Item
* is using a deprecated ItemHandler
*/
checkForDeprecations(handler.getClass());
}
/*
* If this ItemHandler is "global" (not bound to this SlimefunItem),
* we add it to the list of global Item handlers
*/
if (handler instanceof GlobalItemHandler) {
SlimefunRegistry registry = Slimefun.getRegistry();
registry.getGlobalItemHandlers(handler.getIdentifier()).add(handler);
}
}
}
/**
* This method returns whether the original {@link SlimefunItemStack} of this
* {@link SlimefunItem} is immutable.
*
* If <code>true</code> is returned, then any changes to the original {@link SlimefunItemStack}
* will be rejected with a {@link WrongItemStackException}.
* This ensures integrity so developers don't accidentally damage the wrong {@link ItemStack}.
*
* @return Whether the original {@link SlimefunItemStack} is immutable.
*/
protected boolean isItemStackImmutable() {
return true;
}
/**
* This method checks if the dependencies have been set up correctly.
*
* @param addon
* The {@link SlimefunAddon} trying to register this {@link SlimefunItem}
*/
private void checkDependencies(@Nonnull SlimefunAddon addon) {
if (!addon.hasDependency("Slimefun")) {
throw new MissingDependencyException(addon, "Slimefun");
}
}
/**
* This method checks for id conflicts.
*/
private void checkForConflicts() {
SlimefunItem conflictingItem = getById(id);
if (conflictingItem != null) {
throw new IdConflictException(this, conflictingItem);
}
}
/**
* This method checks recursively for all {@link Class} parents to look for any {@link Deprecated}
* elements.
*
* If a {@link Deprecated} element was found, a warning message will be printed.
*
* @param c
* The {@link Class} from which to start this operation.
*/
private void checkForDeprecations(@Nullable Class<?> c) {
if (Slimefun.getUpdater().getBranch() == SlimefunBranch.DEVELOPMENT) {
/*
* This method is currently way too spammy with all the restructuring going on...
* Since DEV builds are anyway under "development", things may be relocated.
* So we fire these only for stable versions, since devs should update then, so
* it's the perfect moment to tell them to act.
*/
return;
}
/*
* We do not wanna throw an Exception here since this could also mean that.
* We have reached the end of the Class hierarchy
*/
if (c != null) {
// Check if this Class is deprecated
if (c.isAnnotationPresent(Deprecated.class)) {
warn("The inherited Class \"" + c.getName() + "\" has been deprecated. Check the documentation for more details!");
}
for (Class<?> parent : c.getInterfaces()) {
// Check if this Interface is deprecated
if (parent.isAnnotationPresent(Deprecated.class)) {
warn("The implemented Interface \"" + parent.getName() + "\" has been deprecated. Check the documentation for more details!");
}
}
// Recursively lookup the super class
checkForDeprecations(c.getSuperclass());
}
}
/**
* This method will set the {@link Research} of this {@link SlimefunItem}.
* You don't have to call this method if your {@link SlimefunItem} was linked to your {@link Research}
* using {@link Research#addItems(SlimefunItem...)}
*
* @param research
* The new {@link Research} for this {@link SlimefunItem}, or null
*/
public void setResearch(@Nullable Research research) {
if (this.research != null) {
this.research.getAffectedItems().remove(this);
}
if (research != null) {
research.getAffectedItems().add(this);
}
this.research = research;
}
/**
* Sets the recipe for this {@link SlimefunItem}.
*
* @param recipe
* The recipe for this {@link ItemStack}
*/
public void setRecipe(@Nonnull ItemStack[] recipe) {
if (recipe == null || recipe.length != 9) {
throw new IllegalArgumentException("Recipes must be of length 9");
}
this.recipe = recipe;
}
/**
* Sets the {@link RecipeType} for this {@link SlimefunItem}.
*
* @param type
* The {@link RecipeType} for this {@link SlimefunItem}
*/
public void setRecipeType(@Nonnull RecipeType type) {
Validate.notNull(type, "The RecipeType is not allowed to be null!");
this.recipeType = type;
}
/**
* This sets the {@link ItemGroup} in which this {@link SlimefunItem} will be displayed.
*
* @param itemGroup
* The new {@link ItemGroup}
*/
public void setItemGroup(@Nonnull ItemGroup itemGroup) {
Validate.notNull(itemGroup, "The ItemGroup is not allowed to be null!");
this.itemGroup.remove(this);
itemGroup.add(this);
this.itemGroup = itemGroup;
}
/**
* This method will set the result of crafting this {@link SlimefunItem}.
* If null is passed, then it will use the default item as the recipe result.
*
* @param output
* The {@link ItemStack} that will be the result of crafting this {@link SlimefunItem}
*/
public void setRecipeOutput(@Nullable ItemStack output) {
this.recipeOutput = output;
}
/**
* This method returns whether or not this {@link SlimefunItem} is allowed to
* be used in a Crafting Table.
*
* Items of type {@link VanillaItem} may be used in workbenches for example.
*
* @see #setUseableInWorkbench(boolean)
*
* @return Whether this {@link SlimefunItem} may be used in a Workbench.
*/
public boolean isUseableInWorkbench() {
return useableInWorkbench;
}
/**
* This sets whether or not this {@link SlimefunItem} is allowed to be
* used in a normal Crafting Table.
*
* @param useable
* Whether this {@link SlimefunItem} should be useable in a workbench
*
* @return This instance of {@link SlimefunItem}
*/
public @Nonnull SlimefunItem setUseableInWorkbench(boolean useable) {
this.useableInWorkbench = useable;
return this;
}
/**
* This method checks whether the provided {@link ItemStack} represents
* this {@link SlimefunItem}.
*
* @param item
* The {@link ItemStack} to compare
*
* @return Whether the given {@link ItemStack} represents this {@link SlimefunItem}
*/
public boolean isItem(@Nullable ItemStack item) {
if (item == null) {
return false;
}
// If the given item is a SlimefunitemStack, simply compare the id
if (item instanceof SlimefunItemStack stack) {
return getId().equals(stack.getItemId());
}
if (item.hasItemMeta()) {
Optional<String> itemId = Slimefun.getItemDataService().getItemData(item);
if (itemId.isPresent()) {
return getId().equals(itemId.get());
}
}
return false;
}
/**
* This method is used for internal purposes only.
*/
public void load() {
if (!hidden) {
itemGroup.add(this);
}
recipeType.register(recipe, getRecipeOutput());
}
/**
* This method will add any given {@link ItemHandler} to this {@link SlimefunItem}.
* Note that this will not work after the {@link SlimefunItem} was registered.
*
* @param handlers
* Any {@link ItemHandler} that should be added to this {@link SlimefunItem}
*/
public final void addItemHandler(ItemHandler... handlers) {
Validate.notEmpty(handlers, "You cannot add zero handlers...");
Validate.noNullElements(handlers, "You cannot add any 'null' ItemHandler!");
// Make sure they are added before the item was registered.
if (state != ItemState.UNREGISTERED) {
throw new UnsupportedOperationException("You cannot add an ItemHandler after the SlimefunItem was registered.");
}
for (ItemHandler handler : handlers) {
itemHandlers.put(handler.getIdentifier(), handler);
// Tickers are a special case (at the moment at least)
if (handler instanceof BlockTicker ticker) {
ticking = true;
Slimefun.getRegistry().getTickerBlocks().add(getId());
blockTicker = ticker;
}
}
}
/**
* This method will add any given {@link ItemSetting} to this {@link SlimefunItem}.
* Note that this will not work after the {@link SlimefunItem} was registered.
*
* @param settings
* Any {@link ItemSetting} that should be added to this {@link SlimefunItem}
*/
public final void addItemSetting(ItemSetting<?>... settings) {
Validate.notEmpty(settings, "You cannot add zero settings...");
Validate.noNullElements(settings, "You cannot add any 'null' ItemSettings!");
if (state != ItemState.UNREGISTERED) {
throw new UnsupportedOperationException("You cannot add an ItemSetting after the SlimefunItem was registered.");
}
if (this instanceof NotConfigurable) {
throw new UnsupportedOperationException("This Item has been marked as NotConfigurable and cannot accept Item Settings!");
}
for (ItemSetting<?> setting : settings) {
if (setting != null) {
// Prevent two Item Settings with the same key
for (ItemSetting<?> existingSetting : itemSettings) {
if (existingSetting.getKey().equals(setting.getKey())) {
throw new IllegalArgumentException("This Item has already an ItemSetting with this key: " + setting.getKey());
}
}
itemSettings.add(setting);
}
}
}
/**
* This method is called before {@link #register(SlimefunAddon)}.
* Override this method to add any additional setup, adding an {@link ItemHandler} for example.
*/
public void preRegister() {
// Override this method to execute code before the Item has been registered
// Useful for calls to addItemHandler(...)
}
/**
* This method is called after {@link #register(SlimefunAddon)}.
* Override this method to add any additional setup that needs to happen after
* the original registration of this {@link SlimefunItem}.
*/
public void postRegister() {
// Override this method to execute code after the Item has been registered
// Useful for calls to Slimefun.getItemValue(...)
}
/**
* This method will assign the given wiki page to this Item.
* Note that you only need to provide the page name itself,
* the URL to our wiki is prepended automatically.
*
* @param page
* The associated wiki page
*/
public final void addOfficialWikipage(@Nonnull String page) {
Validate.notNull(page, "Wiki page cannot be null.");
wikiURL = Optional.of("https://github.com/Slimefun/Slimefun4/wiki/" + page);
}
/**
* This method returns the wiki page that has been assigned to this item.
* It will return null, if no wiki page was found.
*
* @see SlimefunItem#addOfficialWikipage(String)
*
* @return This item's wiki page
*/
public @Nonnull Optional<String> getWikipage() {
return wikiURL;
}
/**
* This method will return this Item's Name (The name that is displayed when
* hovering over this {@link ItemStack} in an {@link Inventory}).
*
* @return This item's name in {@link ItemStack} form
*/
public final @Nonnull String getItemName() {
if (itemStackTemplate instanceof SlimefunItemStack) {
Optional<String> name = ((SlimefunItemStack) itemStackTemplate).getItemMetaSnapshot().getDisplayName();
if (name.isPresent()) {
return name.get();
}
}
return ItemUtils.getItemName(itemStackTemplate);
}
/**
* This method returns a Set of item handlers associated with this Item.
*
* @return The Set of item handlers
*/
public @Nonnull Collection<ItemHandler> getHandlers() {
return itemHandlers.values();
}
/**
* This method calls every {@link ItemHandler} of the given {@link Class}
* and performs the action as specified via the {@link Consumer}.
*
* @param c
* The {@link Class} of the {@link ItemHandler} to call.
* @param callable
* A {@link Consumer} that is called for any found {@link ItemHandler}.
* @param <T>
* The type of {@link ItemHandler} to call.
*
* @return Whether or not an {@link ItemHandler} was found.
*/
@ParametersAreNonnullByDefault
public <T extends ItemHandler> boolean callItemHandler(Class<T> c, Consumer<T> callable) {
Optional<ItemHandler> handler = itemHandlers.get(c);
if (handler.isPresent()) {
try {
callable.accept(c.cast(handler.get()));
} catch (Exception | LinkageError x) {
error("Could not pass \"" + c.getSimpleName() + "\" for " + toString(), x);
}
return true;
}
return false;
}
/**
* This returns whether or not we are scheduling a ticking task for this block.
*
* @return Whether this {@link SlimefunItem} is a ticking block
*/
public boolean isTicking() {
return ticking;
}
@Override
public String toString() {
if (addon == null) {
return getClass().getSimpleName() + " - '" + id + "'";
} else {
return getClass().getSimpleName() + " - '" + id + "' (" + addon.getName() + " v" + addon.getPluginVersion() + ')';
}
}
@Override
public @Nonnull Collection<ItemStack> getDrops() {
return Arrays.asList(itemStackTemplate.clone());
}
@Override
public @Nonnull Collection<ItemStack> getDrops(Player p) {
return getDrops();
}
/**
* This will send an info message to the console and signal that this message came
* from this {@link SlimefunItem}, the message will be sent using the {@link Logger}
* of the {@link SlimefunAddon} which registered this {@link SlimefunItem}.
*
* @param message
* The message to send
*/
@ParametersAreNonnullByDefault
public void info(String message) {
Validate.notNull(addon, "Cannot log a message for an unregistered item!");
String msg = toString() + ": " + message;
addon.getLogger().log(Level.INFO, msg);
}
/**
* This will send a warning to the console and signal that this warning came from
* this {@link SlimefunItem}, the warning will be sent using the {@link Logger}
* of the {@link SlimefunAddon} which registered this {@link SlimefunItem}.
*
* @param message
* The message to send