|
| 1 | +Just like integrated tags like the "LABY DEV/..." team tags addons can also add custom tags |
| 2 | +that seamlessly integrate with any other tags: |
| 3 | + |
| 4 | + |
| 5 | + |
| 6 | +## Snapshot extras |
| 7 | + |
| 8 | +This is what our Snapshot extra will look like for this example. For simplicity the implementation |
| 9 | +details for the Snapshot extra will be skipped in this example, please make sure to read |
| 10 | +the previous page [Entity Snapshots](entity-snapshots.md) beforehand. |
| 11 | + |
| 12 | +=== ":octicons-file-code-16: ExampleTagExtraKeys" |
| 13 | + ```java |
| 14 | + package org.example.myaddon.api.snapshot; |
| 15 | + |
| 16 | + import net.labymod.api.laby3d.renderer.snapshot.ExtraKey; |
| 17 | + |
| 18 | + public class ExampleTagExtraKeys { |
| 19 | + |
| 20 | + // The key must be something unique within your addon; it will automatically include your addon's namespace |
| 21 | + public static final ExtraKey<ExampleTagUserSnapshot> EXAMPLE_USER_TAG = ExtraKey.of("example_user_tag", ExampleTagUserSnapshot.class); |
| 22 | + |
| 23 | + // ... add more keys as required |
| 24 | + } |
| 25 | + ``` |
| 26 | + |
| 27 | +=== ":octicons-file-code-16: ExampleTagUserSnapshot" |
| 28 | + ```java |
| 29 | + package org.example.myaddon.api.snapshot; |
| 30 | + |
| 31 | + import net.labymod.api.client.component.Component; |
| 32 | + import net.labymod.api.laby3d.renderer.snapshot.LabySnapshot; |
| 33 | + |
| 34 | + public interface ExampleTagUserSnapshot extends LabySnapshot { |
| 35 | + |
| 36 | + int reports(); |
| 37 | + |
| 38 | + // ... put in whatever custom data you want |
| 39 | + } |
| 40 | + ``` |
| 41 | + |
| 42 | +## Component NameTags |
| 43 | + |
| 44 | +ComponentNameTags can be used to display a text component around the player name like so: |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +=== ":octicons-file-code-16: ExampleAddon" |
| 49 | + ```java |
| 50 | + package org.example.myaddon; |
| 51 | + |
| 52 | + @AddonMain |
| 53 | + public class ExampleAddon extends LabyAddon<ExampleConfiguration> { |
| 54 | + |
| 55 | + @Override |
| 56 | + protected void enable() { |
| 57 | + Laby.references().tagRegistry().register( |
| 58 | + "my_custom_component", |
| 59 | + PositionType.ABOVE_NAME, |
| 60 | + new MyCustomNameTag() |
| 61 | + ); |
| 62 | + } |
| 63 | + } |
| 64 | + ``` |
| 65 | + |
| 66 | +=== ":octicons-file-code-16: MyCustomNameTag" |
| 67 | + ```java |
| 68 | + package org.example.myaddon.tag; |
| 69 | + |
| 70 | + import org.example.myaddon.api.snapshot.ExampleTagExtraKeys; |
| 71 | + import org.example.myaddon.api.snapshot.ExampleUserSnapshot; |
| 72 | + import java.util.List; |
| 73 | + import net.labymod.api.client.component.Component; |
| 74 | + import net.labymod.api.client.entity.player.tag.tags.ComponentNameTag; |
| 75 | + import net.labymod.api.client.render.state.entity.EntitySnapshot; |
| 76 | + import org.jetbrains.annotations.NotNull; |
| 77 | + |
| 78 | + public class MyCustomNameTag extends ComponentNameTag { |
| 79 | + |
| 80 | + @Override |
| 81 | + protected @NotNull List<Component> buildComponents(EntitySnapshot snapshot) { |
| 82 | + if (!snapshot.has(ExampleTagExtraKeys.EXAMPLE_USER_TAG)) { |
| 83 | + return super.buildComponents(snapshot); |
| 84 | + } |
| 85 | + |
| 86 | + ExampleTagUserSnapshot userSnapshot = snapshot.get(ExampleTagExtraKeys.EXAMPLE_USER_TAG); |
| 87 | + int reports = userSnapshot.reports(); |
| 88 | + |
| 89 | + if (reports > 0) { |
| 90 | + return List.of(Component.text("Reports: " + reports)); |
| 91 | + } |
| 92 | + |
| 93 | + return List.of(); |
| 94 | + } |
| 95 | + |
| 96 | + // You can override this method to include your own custom logic, but make sure |
| 97 | + // to include the call to super.isVisible(); |
| 98 | + // The NameTag is always hidden as long as buildComponents() returns an empty list |
| 99 | + // @Override |
| 100 | + // public boolean isVisible() { |
| 101 | + // return super.isVisible() && <implement custom visibility logic>; |
| 102 | + // } |
| 103 | + |
| 104 | + // You can override this method to change the scale of your NameTag, default is 1.0 |
| 105 | + // @Override |
| 106 | + // public float getScale() { |
| 107 | + // return 0.5F; |
| 108 | + // } |
| 109 | + } |
| 110 | + ``` |
| 111 | + |
| 112 | +## Icon Tags |
| 113 | + |
| 114 | +IconTags can be used to display any icon around the player name like so: |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +=== ":octicons-file-code-16: ExampleAddon" |
| 119 | + ```java |
| 120 | + package org.example.myaddon; |
| 121 | + |
| 122 | + @AddonMain |
| 123 | + public class ExampleAddon extends LabyAddon<ExampleConfiguration> { |
| 124 | + |
| 125 | + @Override |
| 126 | + protected void enable() { |
| 127 | + Laby.references().tagRegistry().register( |
| 128 | + "my_custom_icon", |
| 129 | + PositionType.ABOVE_NAME, |
| 130 | + new MyCustomIconTag() |
| 131 | + ); |
| 132 | + } |
| 133 | + } |
| 134 | + ``` |
| 135 | + |
| 136 | +=== ":octicons-file-code-16: MyCustomIconTag" |
| 137 | + ```java |
| 138 | + package org.example.myaddon.tag; |
| 139 | + |
| 140 | + import org.example.myaddon.api.snapshot.ExampleTagExtraKeys; |
| 141 | + import net.labymod.api.client.entity.player.tag.tags.IconTag; |
| 142 | + import net.labymod.api.client.gui.icon.Icon; |
| 143 | + import net.labymod.api.client.render.state.entity.EntitySnapshot; |
| 144 | + |
| 145 | + public class MyCustomIconTag extends IconTag { |
| 146 | + |
| 147 | + public MyCustomIconTag() { |
| 148 | + super(36.5F, 8F); // width, height |
| 149 | + // super(8F); // or just size (if width == height) |
| 150 | + } |
| 151 | + |
| 152 | + // You can override this method to include your own custom logic, but make sure |
| 153 | + // to include the call to super.isVisible(); |
| 154 | + // The IconTag is always hidden as long as getIcon() returns null |
| 155 | + // @Override |
| 156 | + // public boolean isVisible() { |
| 157 | + // if (!this.snapshot.has(ExampleTagExtraKeys.EXAMPLE_USER_TAG)) { |
| 158 | + // return false; |
| 159 | + // } |
| 160 | + // |
| 161 | + // return super.isVisible() && this.snapshot.get(ExampleTagExtraKeys.EXAMPLE_USER_TAG).reports() > 0; |
| 162 | + // } |
| 163 | + |
| 164 | + @Override |
| 165 | + public Icon getIcon(EntitySnapshot snapshot) { |
| 166 | + // Return any Icon that you want to be rendered, |
| 167 | + // or null to hide the Tag for this Entity. |
| 168 | + // If the Icon is static and the same for each Entity you may |
| 169 | + // also pass it in the constructor and don't implement getIcon(). |
| 170 | + return Icon.url("https://labymod.net/page/tpl/assets/images/logo.png"); |
| 171 | + } |
| 172 | + } |
| 173 | + ``` |
0 commit comments