-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathAttachmentItem.java
More file actions
86 lines (78 loc) · 3.46 KB
/
Copy pathAttachmentItem.java
File metadata and controls
86 lines (78 loc) · 3.46 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
package com.tacz.guns.item;
import com.tacz.guns.api.TimelessAPI;
import com.tacz.guns.api.item.IAttachment;
import com.tacz.guns.api.item.attachment.AttachmentType;
import com.tacz.guns.api.item.builder.AttachmentItemBuilder;
import com.tacz.guns.api.item.nbt.AttachmentItemDataAccessor;
import com.tacz.guns.client.renderer.item.AttachmentItemRenderer;
import com.tacz.guns.client.resource.index.ClientAttachmentIndex;
import com.tacz.guns.inventory.tooltip.AttachmentItemTooltip;
import com.tacz.guns.resource.index.CommonAttachmentIndex;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer;
import net.minecraft.core.NonNullList;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.tooltip.TooltipComponent;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.extensions.common.IClientItemExtensions;
import javax.annotation.Nonnull;
import java.util.Optional;
import java.util.function.Consumer;
public class AttachmentItem extends Item implements AttachmentItemDataAccessor {
public AttachmentItem() {
super(new Properties().stacksTo(1));
}
@Override
@Nonnull
@OnlyIn(Dist.CLIENT)
public Component getName(@Nonnull ItemStack stack) {
ResourceLocation attachmentId = this.getAttachmentId(stack);
Optional<ClientAttachmentIndex> attachmentIndex = TimelessAPI.getClientAttachmentIndex(attachmentId);
if (attachmentIndex.isPresent()) {
return Component.translatable(attachmentIndex.get().getName());
}
return super.getName(stack);
}
public static NonNullList<ItemStack> fillItemCategory(AttachmentType type) {
NonNullList<ItemStack> stacks = NonNullList.create();
TimelessAPI.getAllCommonAttachmentIndex().forEach(entry -> {
if (entry.getValue().getPojo().isHidden()) {
return;
}
if (type.equals(entry.getValue().getType())) {
ItemStack itemStack = AttachmentItemBuilder.create().setId(entry.getKey()).build();
stacks.add(itemStack);
}
});
return stacks;
}
@Override
public void initializeClient(Consumer<IClientItemExtensions> consumer) {
consumer.accept(new IClientItemExtensions() {
@Override
public BlockEntityWithoutLevelRenderer getCustomRenderer() {
Minecraft minecraft = Minecraft.getInstance();
return new AttachmentItemRenderer(minecraft.getBlockEntityRenderDispatcher(), minecraft.getEntityModels());
}
});
}
@Override
@Nonnull
public AttachmentType getType(ItemStack attachmentStack) {
IAttachment iAttachment = IAttachment.getIAttachmentOrNull(attachmentStack);
if (iAttachment != null) {
ResourceLocation id = iAttachment.getAttachmentId(attachmentStack);
return TimelessAPI.getCommonAttachmentIndex(id).map(CommonAttachmentIndex::getType).orElse(AttachmentType.NONE);
} else {
return AttachmentType.NONE;
}
}
@Override
public Optional<TooltipComponent> getTooltipImage(ItemStack stack) {
return Optional.of(new AttachmentItemTooltip(stack, this.getAttachmentId(stack), this.getType(stack)));
}
}