Skip to content

Commit 4fd986c

Browse files
committed
Add shard item
1 parent be02bb7 commit 4fd986c

File tree

7 files changed

+167
-0
lines changed

7 files changed

+167
-0
lines changed

src/main/java/net/modfest/scatteredshards/ScatteredShardsContent.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import net.minecraft.block.Block;
66
import net.minecraft.block.entity.BlockEntityType;
77
import net.minecraft.client.render.block.entity.BlockEntityRendererFactories;
8+
import net.minecraft.component.ComponentType;
89
import net.minecraft.item.BlockItem;
910
import net.minecraft.item.Item;
1011
import net.minecraft.registry.Registries;
@@ -13,24 +14,34 @@
1314
import net.modfest.scatteredshards.block.ShardBlock;
1415
import net.modfest.scatteredshards.block.ShardBlockEntity;
1516
import net.modfest.scatteredshards.client.render.ShardBlockEntityRenderer;
17+
import net.modfest.scatteredshards.item.ShardItem;
1618
import net.modfest.scatteredshards.item.ShardTablet;
1719

1820
public class ScatteredShardsContent {
1921
public static final Identifier SHARD_BLOCK_ID = ScatteredShards.id("shard_block");
2022
public static final Identifier SHARD_TABLET_ID = ScatteredShards.id("shard_tablet");
23+
public static final Identifier SHARD_ITEM_ID = ScatteredShards.id("shard_item");
2124

2225
public static final Block SHARD_BLOCK = new ShardBlock();
2326
public static final Item SHARD_BLOCK_ITEM = new BlockItem(SHARD_BLOCK, new Item.Settings());
2427

2528
public static final Item SHARD_TABLET = new ShardTablet(new Item.Settings());
29+
public static final Item SHARD_ITEM = new ShardItem(new Item.Settings());
2630

2731
public static final BlockEntityType<ShardBlockEntity> SHARD_BLOCKENTITY = BlockEntityType.Builder.create(ShardBlockEntity::new, SHARD_BLOCK).build();
2832

33+
public static final ComponentType<Identifier> SHARD_ID_COMPONENT = Registry.register(
34+
Registries.DATA_COMPONENT_TYPE,
35+
ScatteredShards.id("shard_id"),
36+
ComponentType.<Identifier>builder().codec(Identifier.CODEC).build()
37+
);
38+
2939
public static void register() {
3040
Registry.register(Registries.BLOCK, SHARD_BLOCK_ID, SHARD_BLOCK);
3141
Registry.register(Registries.ITEM, SHARD_BLOCK_ID, SHARD_BLOCK_ITEM);
3242
Registry.register(Registries.BLOCK_ENTITY_TYPE, SHARD_BLOCK_ID, SHARD_BLOCKENTITY);
3343
Registry.register(Registries.ITEM, SHARD_TABLET_ID, SHARD_TABLET);
44+
Registry.register(Registries.ITEM, SHARD_ITEM_ID, SHARD_ITEM);
3445
}
3546

3647
@Environment(EnvType.CLIENT)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package net.modfest.scatteredshards.command;
2+
3+
import com.mojang.brigadier.Command;
4+
import com.mojang.brigadier.context.CommandContext;
5+
import com.mojang.brigadier.exceptions.CommandSyntaxException;
6+
import com.mojang.brigadier.tree.CommandNode;
7+
import me.lucko.fabric.api.permissions.v0.Permissions;
8+
import net.minecraft.item.ItemStack;
9+
import net.minecraft.server.command.ServerCommandSource;
10+
import net.minecraft.server.network.ServerPlayerEntity;
11+
import net.minecraft.text.Text;
12+
import net.minecraft.util.Identifier;
13+
import net.modfest.scatteredshards.ScatteredShards;
14+
import net.modfest.scatteredshards.item.ShardItem;
15+
16+
public class ItemCommand {
17+
18+
public static int item(CommandContext<ServerCommandSource> ctx) throws CommandSyntaxException {
19+
ServerPlayerEntity player = ctx.getSource().getPlayerOrThrow();
20+
Identifier shardId = ctx.getArgument("shard_id", Identifier.class);
21+
22+
ItemStack stack = ShardItem.createShardItem(shardId);
23+
24+
player.getInventory().offerOrDrop(stack);
25+
26+
ctx.getSource().sendFeedback(() -> Text.stringifiedTranslatable("commands.scattered_shards.shard.item", shardId), false);
27+
return Command.SINGLE_SUCCESS;
28+
}
29+
30+
public static void register(CommandNode<ServerCommandSource> parent) {
31+
//Usage: /shard item <shard_id>
32+
CommandNode<ServerCommandSource> blockCommand = ShardCommandNodeHelper.literal("item")
33+
.requires(Permissions.require(ScatteredShards.permission("command.item"), 2))
34+
.build();
35+
CommandNode<ServerCommandSource> shardIdArgument = ShardCommandNodeHelper.shardId("shard_id")
36+
.executes(ItemCommand::item)
37+
.build();
38+
39+
parent.addChild(blockCommand);
40+
blockCommand.addChild(shardIdArgument);
41+
}
42+
}

src/main/java/net/modfest/scatteredshards/command/ShardCommand.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public static void register() {
3030
AwardCommand.register(shardNode);
3131
UncollectCommand.register(shardNode);
3232
BlockCommand.register(shardNode);
33+
ItemCommand.register(shardNode);
3334
LibraryCommand.register(shardNode);
3435
});
3536
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package net.modfest.scatteredshards.item;
2+
3+
import net.minecraft.entity.player.PlayerEntity;
4+
import net.minecraft.item.Item;
5+
import net.minecraft.item.ItemStack;
6+
import net.minecraft.item.tooltip.TooltipType;
7+
import net.minecraft.server.network.ServerPlayerEntity;
8+
import net.minecraft.text.Style;
9+
import net.minecraft.text.Text;
10+
import net.minecraft.util.Formatting;
11+
import net.minecraft.util.Hand;
12+
import net.minecraft.util.Identifier;
13+
import net.minecraft.util.TypedActionResult;
14+
import net.minecraft.world.World;
15+
import net.modfest.scatteredshards.ScatteredShardsContent;
16+
import net.modfest.scatteredshards.api.ScatteredShardsAPI;
17+
import net.modfest.scatteredshards.api.ShardLibrary;
18+
import net.modfest.scatteredshards.api.shard.Shard;
19+
import net.modfest.scatteredshards.api.shard.ShardType;
20+
21+
import java.util.List;
22+
import java.util.Optional;
23+
24+
public class ShardItem extends Item {
25+
public ShardItem(Settings settings) {
26+
super(settings);
27+
}
28+
29+
30+
/**
31+
* Creates a shard item
32+
*
33+
* @return the shard item
34+
*/
35+
public static ItemStack createShardItem(Identifier shardId) {
36+
ItemStack stack = new ItemStack(ScatteredShardsContent.SHARD_ITEM);
37+
stack.set(ScatteredShardsContent.SHARD_ID_COMPONENT, shardId);
38+
39+
return stack;
40+
}
41+
42+
@Override
43+
public TypedActionResult<ItemStack> use(World world, PlayerEntity player, Hand hand) {
44+
var itemStack = player.getStackInHand(hand);
45+
var id = itemStack.get(ScatteredShardsContent.SHARD_ID_COMPONENT);
46+
47+
if (world.isClient) {
48+
return TypedActionResult.consume(itemStack);
49+
}
50+
51+
itemStack.decrementUnlessCreative(1, player);
52+
53+
ShardLibrary library = ScatteredShardsAPI.getServerLibrary();
54+
Optional<Shard> toCollect = library.shards().get(id);
55+
56+
if (toCollect.isEmpty() || !(player instanceof ServerPlayerEntity serverPlayer)) {
57+
return TypedActionResult.fail(itemStack);
58+
}
59+
60+
ScatteredShardsAPI.triggerShardCollection(serverPlayer, id);
61+
return TypedActionResult.consume(itemStack);
62+
}
63+
64+
@Override
65+
public void appendTooltip(ItemStack stack, TooltipContext context, List<Text> tooltip, TooltipType type) {
66+
Identifier id = stack.get(ScatteredShardsContent.SHARD_ID_COMPONENT);
67+
68+
if (id == null) {
69+
return;
70+
}
71+
72+
ShardLibrary library = ScatteredShardsAPI.getClientLibrary();
73+
Optional<Shard> shard = library.shards().get(id);
74+
75+
if (shard.isEmpty()) {
76+
return;
77+
}
78+
79+
Identifier shardTypeId = shard.get().shardTypeId();
80+
ShardType shardType = library.shardTypes().get(shardTypeId).orElse(ShardType.MISSING);
81+
Text shardTypeDesc = ShardType.getDescription(shardTypeId).copy().fillStyle(Style.EMPTY.withColor(shardType.textColor()));
82+
83+
tooltip.add(shardTypeDesc);
84+
tooltip.add(Text.translatable("item.scattered_shards.shard_item.description").formatted(Formatting.GRAY));
85+
}
86+
87+
@Override
88+
public Text getName(ItemStack stack) {
89+
Identifier id = stack.get(ScatteredShardsContent.SHARD_ID_COMPONENT);
90+
91+
if (id == null) {
92+
return getName();
93+
}
94+
95+
ShardLibrary library = ScatteredShardsAPI.getClientLibrary();
96+
Optional<Shard> shard = library.shards().get(id);
97+
98+
if (shard.isEmpty()) {
99+
return getName();
100+
}
101+
102+
return shard.get().name();
103+
}
104+
}

src/main/resources/assets/scattered_shards/lang/en_us.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
"block.scattered_shards.shard_block": "Shard",
33

44
"item.scattered_shards.shard_tablet": "Shard Tablet",
5+
"item.scattered_shards.shard_item": "Shard",
6+
"item.scattered_shards.shard_item.description": "Awards shard when used",
57

68
"commands.scattered_shards.shard.collect": "Collected shard '%s'",
79
"commands.scattered_shards.shard.award": "Awarded shard '%s' to %s players",
810
"commands.scattered_shards.shard.award.none": "Tried to award shard '%s' but no players are eligible",
911
"commands.scattered_shards.shard.block": "Shard-block '%s' added to inventory",
12+
"commands.scattered_shards.shard.item": "Shard '%s' added to inventory",
1013
"commands.scattered_shards.shard.uncollect": "Un-Collected shard '%s'",
1114
"commands.scattered_shards.shard.uncollect.all": "Un-Collected %s shards",
1215
"commands.scattered_shards.shard.library.delete": "Deleted shard '%s' from the library",
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"parent": "item/generated",
3+
"textures": {
4+
"layer0": "scattered_shards:item/shard_item"
5+
}
6+
}
Loading

0 commit comments

Comments
 (0)