-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
dMaterial.java
894 lines (800 loc) · 32 KB
/
dMaterial.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
package net.aufdemrand.denizen.objects;
import net.aufdemrand.denizen.nms.NMSHandler;
import net.aufdemrand.denizen.nms.NMSVersion;
import net.aufdemrand.denizen.nms.abstracts.ModernBlockData;
import net.aufdemrand.denizen.nms.interfaces.BlockData;
import net.aufdemrand.denizen.objects.properties.material.MaterialAge;
import net.aufdemrand.denizen.objects.properties.material.MaterialLevel;
import net.aufdemrand.denizen.tags.BukkitTagContext;
import net.aufdemrand.denizen.utilities.blocks.OldMaterialsHelper;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.objects.*;
import net.aufdemrand.denizencore.objects.properties.PropertyParser;
import net.aufdemrand.denizencore.tags.Attribute;
import net.aufdemrand.denizencore.tags.TagContext;
import net.aufdemrand.denizencore.utilities.CoreUtilities;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.inventory.meta.BlockStateMeta;
import org.bukkit.material.MaterialData;
import java.util.HashMap;
public class dMaterial implements dObject, Adjustable {
// <--[language]
// @name dMaterial
// @group Object System
// @description
// A dMaterial represents a material (a type of block or item).
//
// For format info, see <@link language m@>
//
// -->
// <--[language]
// @name m@
// @group Object Fetcher System
// @description
// m@ refers to the 'object identifier' of a dMaterial. The 'm@' is notation for Denizen's Object
// Fetcher. The constructor for a dMaterial is the material type name.
// For example, 'm@stick'.
//
// For general info, see <@link language dMaterial>
//
// -->
/**
* Legacy dMaterial identities.
*/
private String forcedIdentity = null,
forcedIdentityLow = null;
/**
* Legacy dMaterial identities. Do not use.
*/
public dMaterial forceIdentifyAs(String string) {
forcedIdentity = string;
forcedIdentityLow = CoreUtilities.toLowerCase(string);
return this;
}
//////////////////
// OBJECT FETCHER
////////////////
public static dMaterial valueOf(String string) {
return valueOf(string, null);
}
/**
* Gets a Material Object from a string form.
*
* @param string the string
* @return a Material, or null if incorrectly formatted
*/
@Fetchable("m")
public static dMaterial valueOf(String string, TagContext context) {
///////
// Handle objects with properties through the object fetcher
if (ObjectFetcher.DESCRIBED_PATTERN.matcher(string).matches()) {
return ObjectFetcher.getObjectFrom(dMaterial.class, string, context);
}
string = string.toUpperCase();
if (string.startsWith("M@")) {
string = string.substring("M@".length());
}
if (string.equals("RANDOM")) {
return OldMaterialsHelper.getMaterialFrom(Material.values()[CoreUtilities.getRandom().nextInt(Material.values().length)]);
}
int index = string.indexOf(',');
if (index < 0) {
index = string.indexOf(':');
}
int data = 0;
if (index >= 0) {
data = aH.getIntegerFrom(string.substring(index + 1));
string = string.substring(0, index);
}
Material m = Material.getMaterial(string);
if (m == null && NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2)) {
m = Material.getMaterial(string, true);
if (m != null) {
m = Bukkit.getUnsafe().fromLegacy(m);
if (context == null || context.debug) {
dB.log("'" + string + "' is a legacy (pre-1.13) material name. It is now '" + m.name() + "'.");
}
}
}
if (m != null) {
if (index >= 0) {
if (context == null || context.debug) {
dB.log("Material ID and data magic number support is deprecated and WILL be removed in a future release. Use relevant properties instead.");
}
}
else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2)) {
return new dMaterial(m);
}
return OldMaterialsHelper.getMaterialFrom(m, data);
}
dMaterial mat = OldMaterialsHelper.all_dMaterials.get(string);
if (mat != null) {
if ((context == null || context.debug) && index >= 0) {
dB.log("Material ID and data magic number support is deprecated and WILL be removed in a future release. Use relevant properties instead.");
}
if (data == 0) {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2)) {
return new dMaterial(mat.material);
}
return mat;
}
return OldMaterialsHelper.getMaterialFrom(mat.material, data);
}
int matid = aH.getIntegerFrom(string);
if (matid != 0) {
// It's always an error (except in the 'matches' call) to use a material ID number instead of a name.
if (context != noDebugContext) {
dB.echoError("Material ID and data magic number support is deprecated and WILL be removed in a future release. Use material names instead.");
}
m = OldMaterialsHelper.getLegacyMaterial(matid);
if (m != null) {
return OldMaterialsHelper.getMaterialFrom(m, data);
}
}
return null;
}
public static dMaterial quickOfNamed(String string) {
string = string.toUpperCase();
int index = string.indexOf(',');
if (index < 0) {
index = string.indexOf(':');
}
int data = 0;
if (index >= 0) {
data = aH.getIntegerFrom(string.substring(index + 1));
string = string.substring(0, index);
}
Material m = Material.getMaterial(string);
if (m != null) {
return OldMaterialsHelper.getMaterialFrom(m, data);
}
dMaterial mat = OldMaterialsHelper.all_dMaterials.get(string);
if (mat != null) {
if (data == 0) {
return mat;
}
return OldMaterialsHelper.getMaterialFrom(mat.material, data);
}
return null;
}
public static TagContext noDebugContext = new BukkitTagContext(null, null, false, null, false, null);
/**
* Determine whether a string is a valid material.
*
* @param arg the string
* @return true if matched, otherwise false
*/
public static boolean matches(String arg) {
if (valueOf(arg, noDebugContext) != null) {
return true;
}
return false;
}
/**
* @param object object-fetchable String of a valid dMaterial, or a dMaterial object
* @return true if the dMaterials are the same.
*/
@Override
public boolean equals(Object object) {
if (object instanceof dMaterial) {
return getMaterial() == ((dMaterial) object).getMaterial()
&& getData((byte) 0) == ((dMaterial) object).getData((byte) 0);
}
else {
dMaterial parsed = valueOf(object.toString());
return equals(parsed);
}
}
public boolean matchesBlock(Block b) {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2)) {
return getMaterial() == b.getType();
}
return matchesMaterialData(b.getType().getNewData(b.getData()));
}
///////////////
// Constructors
/////////////
/**
* Legacy material format. Do not use.
*/
public dMaterial(Material material, int data) {
this.material = material;
if (data < 0) {
this.data = null;
}
else {
this.data = (byte) data;
}
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2)
&& material.isBlock()) {
modernData = new ModernBlockData(material);
}
}
public dMaterial(Material material) {
this(material, 0);
}
public dMaterial(BlockState state) {
this.material = state.getType();
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2)) {
this.modernData = new ModernBlockData(state);
}
else {
this.data = state.getRawData();
}
}
public dMaterial(BlockData block) {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2)) {
this.modernData = block.modern();
this.material = modernData.getMaterial();
}
else {
this.material = block.getMaterial();
this.data = block.getData();
}
}
public dMaterial(Block block) {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2)) {
this.modernData = new ModernBlockData(block);
this.material = modernData.getMaterial();
}
else {
this.material = block.getType();
this.data = block.getData();
}
}
public dMaterial(ModernBlockData data) {
this.modernData = data;
this.material = data.getMaterial();
}
/////////////////////
// INSTANCE FIELDS/METHODS
/////////////////
private Material material;
private Byte data = 0;
private ModernBlockData modernData;
public boolean hasModernData() {
return modernData != null;
}
public ModernBlockData getModernData() {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2)) {
return modernData;
}
throw new IllegalStateException("Modern block data handler is not available prior to MC 1.13.");
}
public void setModernData(ModernBlockData data) {
modernData = data;
}
public Material getMaterial() {
return material;
}
public BlockData getNmsBlockData() {
if (modernData != null) {
return NMSHandler.getInstance().getBlockHelper().getBlockData(modernData);
}
return NMSHandler.getInstance().getBlockHelper().getBlockData(getMaterial(), getData((byte) 0));
}
public String name() {
return material.name();
}
public byte getData(byte fallback) {
if (data == null) {
return fallback;
}
else {
return data;
}
}
public Byte getData() {
return data;
}
public boolean hasData() {
return data != null;
}
public boolean matchesMaterialData(MaterialData data) {
// If this material has data, check datas
if (hasData()) {
return (material == data.getItemType() && this.data == data.getData());
}
// Else, return matched itemType/materialType
else {
return material == data.getItemType();
}
}
public MaterialData getMaterialData() {
return new MaterialData(material, data != null ? data : 0);
}
public boolean isStructure() {
if (material == Material.CHORUS_PLANT) {
return true;
}
else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2)
&& (material == Material.RED_MUSHROOM_BLOCK || material == Material.BROWN_MUSHROOM_BLOCK)) {
return true;
}
else if (NMSHandler.getVersion().isAtMost(NMSVersion.v1_12_R1)) {
if (material == Material.RED_MUSHROOM || material == Material.BROWN_MUSHROOM) {
return true;
}
String name = material.name();
return name.equals("SAPLING") || name.equals("HUGE_MUSHROOM_1") || name.equals("HUGE_MUSHROOM_2");
}
return false;
}
String prefix = "material";
@Override
public String getPrefix() {
return prefix;
}
@Override
public String debug() {
return (prefix + "='<A>" + identify() + "<G>' ");
}
@Override
public boolean isUnique() {
return false;
}
@Override
public String getObjectType() {
return "Material";
}
@Override
public String identify() {
return "m@" + identifyNoIdentifier();
}
public String identifyFull() {
return "m@" + identifyFullNoIdentifier();
}
@Override
public String identifySimple() {
return "m@" + identifySimpleNoIdentifier();
}
public String identifyNoPropertiesNoIdentifier() {
if (forcedIdentity != null) {
return forcedIdentityLow;
}
if (NMSHandler.getVersion().isAtMost(NMSVersion.v1_12_R1) && getData() != null && getData() > 0) {
return CoreUtilities.toLowerCase(material.name()) + "," + getData();
}
return CoreUtilities.toLowerCase(material.name());
}
public String identifyNoIdentifier() {
if (forcedIdentity != null) {
return forcedIdentityLow;
}
if (NMSHandler.getVersion().isAtMost(NMSVersion.v1_12_R1) && getData() != null && getData() > 0) {
return CoreUtilities.toLowerCase(material.name()) + "," + getData();
}
return CoreUtilities.toLowerCase(material.name()) + PropertyParser.getPropertiesString(this);
}
public String identifySimpleNoIdentifier() {
if (forcedIdentity != null) {
return forcedIdentityLow;
}
return CoreUtilities.toLowerCase(material.name());
}
public String identifyFullNoIdentifier() {
if (forcedIdentity != null) {
return forcedIdentityLow + (getData() != null ? "," + getData() : "");
}
if (NMSHandler.getVersion().isAtMost(NMSVersion.v1_12_R1) && getData() != null && getData() > 0) {
return CoreUtilities.toLowerCase(material.name()) + "," + getData();
}
return CoreUtilities.toLowerCase(material.name()) + PropertyParser.getPropertiesString(this);
}
@Override
public String toString() {
return identify();
}
public String realName() {
if (forcedIdentity != null) {
return forcedIdentityLow;
}
return CoreUtilities.toLowerCase(material.name());
}
@Override
public dObject setPrefix(String prefix) {
if (prefix != null) {
this.prefix = prefix;
}
return this;
}
public static void registerTags() {
registerTag("id", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
dB.echoError("Material ID and data magic number support is deprecated and WILL be removed in a future release. Use material names instead.");
return new Element(((dMaterial) object).material.getId())
.getAttribute(attribute.fulfill(1));
}
});
registerTag("data", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
if (attribute.context == null || attribute.context.debug) {
dB.log("Material ID and data magic number support is deprecated and WILL be removed in a future release. Use relevant properties instead.");
}
return new Element(((dMaterial) object).getData())
.getAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <m@material.is_ageable>
// @returns Element(Boolean)
// @group properties
// @description
// Returns whether the material is an ageable material.
// When this returns true, <@link tag m@material.age>, <@link tag m@material.maximum_age>,
// and <@link mechanism dMaterial.age> are accessible.
// -->
registerTag("is_ageable", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(MaterialAge.describes(object))
.getAttribute(attribute.fulfill(1));
}
});
registerTag("is_plant", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(MaterialAge.describes(object))
.getAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <m@material.is_levelable>
// @returns Element(Boolean)
// @group properties
// @description
// Returns whether the material is a levelable material.
// When this returns true, <@link tag m@material.level>, <@link tag m@material.maximum_level>,
// and <@link mechanism dMaterial.level> are accessible.
// -->
registerTag("is_levelable", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(MaterialLevel.describes(object))
.getAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <m@material.has_gravity>
// @returns Element(Boolean)
// @description
// Returns whether the material is affected by gravity.
// -->
registerTag("has_gravity", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(((dMaterial) object).material.hasGravity())
.getAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <m@material.is_block>
// @returns Element(Boolean)
// @description
// Returns whether the material is a placeable block.
// -->
registerTag("is_block", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(((dMaterial) object).material.isBlock())
.getAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <m@material.is_burnable>
// @returns Element(Boolean)
// @description
// Returns whether the material is a block that can burn away.
// -->
registerTag("is_burnable", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(((dMaterial) object).material.isBurnable())
.getAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <m@material.is_edible>
// @returns Element(Boolean)
// @description
// Returns whether the material is edible.
// -->
registerTag("is_edible", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(((dMaterial) object).material.isEdible())
.getAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <m@material.is_flammable>
// @returns Element(Boolean)
// @description
// Returns whether the material is a block that can catch fire.
// -->
registerTag("is_flammable", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(((dMaterial) object).material.isFlammable())
.getAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <m@material.is_occluding>
// @returns Element(Boolean)
// @description
// Returns whether the material is a block that completely blocks vision.
// -->
registerTag("is_occluding", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(((dMaterial) object).material.isOccluding())
.getAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <m@material.is_record>
// @returns Element(Boolean)
// @description
// Returns whether the material is a playable music disc.
// -->
registerTag("is_record", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(((dMaterial) object).material.isRecord())
.getAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <m@material.is_solid>
// @returns Element(Boolean)
// @description
// Returns whether the material is a block that is solid (cannot be walked through).
// -->
registerTag("is_solid", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(((dMaterial) object).material.isSolid())
.getAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <m@material.is_transparent>
// @returns Element(Boolean)
// @description
// Returns whether the material is a block that does not block any light.
// -->
registerTag("is_transparent", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(((dMaterial) object).material.isTransparent())
.getAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <m@material.max_durability>
// @returns Element(Number)
// @description
// Returns the maximum durability of this material.
// -->
registerTag("max_durability", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(((dMaterial) object).material.getMaxDurability())
.getAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <m@material.block_resistance>
// @returns Element(Decimal)
// @mechanism dMaterial.block_resistance
// @description
// Returns the explosion resistance for all blocks of this material type.
// -->
registerTag("block_resistance", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
dMaterial material = (dMaterial) object;
if (!NMSHandler.getInstance().getBlockHelper().hasBlock(material.getMaterial())) {
dB.echoError("Provided material does not have a placeable block.");
return null;
}
return new Element(NMSHandler.getInstance().getBlockHelper().getBlockResistance(material.getMaterial()))
.getAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <m@material.hardness>
// @returns Element(Decimal)
// @description
// Returns the value representing how hard a material, used as a basis for calculating the time it takes to break.
// -->
registerTag("hardness", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
dMaterial material = (dMaterial) object;
if (!material.getMaterial().isBlock()) {
dB.echoError("Provided material does not have a placeable block.");
return null;
}
return new Element(material.getMaterial().getHardness())
.getAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <m@material.max_stack_size>
// @returns Element(Number)
// @description
// Returns the maximum amount of this material that can be held in a stack.
// -->
registerTag("max_stack_size", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(((dMaterial) object).material.getMaxStackSize())
.getAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <m@material.is_made_of[<material>]>
// @returns Element(Boolean)
// @description
// Returns true if the material is a variety of the specified material.
// Example: <m@red_wool.is_made_of[m@wool]> will return true.
// Invalid for 1.13+ servers.
// -->
registerTag("is_made_of", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
dMaterial compared = dMaterial.valueOf(attribute.getContext(1));
return new Element(compared != null && ((dMaterial) object).material == compared.getMaterial())
.getAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <m@material.bukkit_enum>
// @returns Element
// @description
// Returns the bukkit Material enum value. For example: <m@birch_sapling.bukkit_enum>
// will return 'sapling'
// Unneeded for 1.13+ servers.
// -->
registerTag("bukkit_enum", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(((dMaterial) object).material.name())
.getAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <m@material.name>
// @returns Element
// @description
// Returns the name of the material.
// -->
registerTag("name", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element(((dMaterial) object).forcedIdentity != null ? ((dMaterial) object).forcedIdentityLow :
CoreUtilities.toLowerCase(((dMaterial) object).material.name()))
.getAttribute(attribute.fulfill(1));
}
});
// <--[tag]
// @attribute <m@material.full>
// @returns Element
// @description
// Returns the material's full identification.
// Irrelevant on modern (1.13+) servers.
// -->
registerTag("full", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
if (((dMaterial) object).hasData()) {
return new Element(((dMaterial) object).identifyFull())
.getAttribute(attribute.fulfill(1));
}
else {
return new Element(((dMaterial) object).identify())
.getAttribute(attribute.fulfill(1));
}
}
});
// <--[tag]
// @attribute <m@material.item>
// @returns dItem
// @description
// Returns an item of the material.
// -->
registerTag("item", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
dMaterial material = (dMaterial) object;
dItem item = new dItem(material, 1);
attribute = attribute.fulfill(1);
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2)) {
// Special patch for older material-item tags.
if (!attribute.isComplete()) {
String tag = attribute.getAttribute(1);
String returned = CoreUtilities.autoPropertyTag(object, attribute);
if (returned != null) {
dB.echoError("Usage of outdated 'material.item." + tag + "' tag should be replaced by 'material." + tag + "' (with '.item' removed).");
return returned;
}
}
if (item.getItemStack().hasItemMeta() && item.getItemStack().getItemMeta() instanceof BlockStateMeta) {
((BlockStateMeta) item.getItemStack().getItemMeta()).setBlockState(material.modernData.getBlockState());
}
}
return item.getAttribute(attribute);
}
});
// <--[tag]
// @attribute <m@material.type>
// @returns Element
// @description
// Always returns 'Material' for dMaterial objects. All objects fetchable by the Object Fetcher will return the
// type of object that is fulfilling this attribute.
// -->
registerTag("type", new TagRunnable() {
@Override
public String run(Attribute attribute, dObject object) {
return new Element("Material").getAttribute(attribute.fulfill(1));
}
});
}
public static HashMap<String, TagRunnable> registeredTags = new HashMap<>();
public static void registerTag(String name, TagRunnable runnable) {
if (runnable.name == null) {
runnable.name = name;
}
registeredTags.put(name, runnable);
}
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
// TODO: Scrap getAttribute, make this functionality a core system
String attrLow = CoreUtilities.toLowerCase(attribute.getAttributeWithoutContext(1));
TagRunnable tr = registeredTags.get(attrLow);
if (tr != null) {
if (!tr.name.equals(attrLow)) {
net.aufdemrand.denizencore.utilities.debugging.dB.echoError(attribute.getScriptEntry() != null ? attribute.getScriptEntry().getResidingQueue() : null,
"Using deprecated form of tag '" + tr.name + "': '" + attrLow + "'.");
}
return tr.run(attribute, this);
}
String returned = CoreUtilities.autoPropertyTag(this, attribute);
if (returned != null) {
return returned;
}
return new Element(identify()).getAttribute(attribute.fulfill(0));
}
@Override
public void applyProperty(Mechanism mechanism) {
adjust(mechanism);
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object dMaterial
// @name block_resistance
// @input Element(Decimal)
// @description
// Sets the explosion resistance for all blocks of this material type.
// @tags
// <m@material.block_resistance>
// -->
if (!mechanism.isProperty && mechanism.matches("block_resistance") && mechanism.requireFloat()) {
if (!NMSHandler.getInstance().getBlockHelper().setBlockResistance(material, mechanism.getValue().asFloat())) {
dB.echoError("Provided material does not have a placeable block.");
}
}
CoreUtilities.autoPropertyMechanism(this, mechanism);
}
}