Skip to content

Commit 30bdcb9

Browse files
Name command add remove
1 parent 72cc814 commit 30bdcb9

File tree

4 files changed

+215
-36
lines changed

4 files changed

+215
-36
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// 1.21.4 -999999999-01-01T00:00:00 Commands+/Language (en_us)
2-
b42c4b6c1c9254c414a0561bef2cf6b85bbf5709 assets/commandsplus/lang/en_us.json
2+
9c64e6a9c0139a062644ee582eb8dd5329b94203 assets/commandsplus/lang/en_us.json

src/main/generated/assets/commandsplus/lang/en_us.json

+8-4
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,14 @@
5151
"commands.hunger.set.food.success": "Set food to %s",
5252
"commands.hunger.set.saturation.failed": "Failed to set saturation",
5353
"commands.hunger.set.saturation.success": "Set saturation to %s",
54-
"commands.name.entity.failed": "Failed to name entity",
55-
"commands.name.entity.success": "Named entity to \"%s\"",
56-
"commands.name.item.failed": "Failed to name item",
57-
"commands.name.item.success": "Named item to \"%s\"",
54+
"commands.name.entity.name.failed": "Failed to name entity",
55+
"commands.name.entity.name.success": "Named entity to \"%s\"",
56+
"commands.name.entity.remove.failed": "Failed to remove entity name",
57+
"commands.name.entity.remove.success": "Removed entity name",
58+
"commands.name.item.name.failed": "Failed to name item",
59+
"commands.name.item.name.success": "Named item to \"%s\"",
60+
"commands.name.item.remove.failed": "Failed to remove item name",
61+
"commands.name.item.remove.success": "Removed item name",
5862
"commands.setowner.failed": "Failed to set pet's owner",
5963
"commands.setowner.success": "Set pet's owner to %s",
6064
"commands.toggledownfall.success": "Toggled downfall",

src/main/java/com/thepinkhacker/commandsplus/data/lang/LanguageProvider.java

+21-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.thepinkhacker.commandsplus.data.lang;
22

3+
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
4+
import com.thepinkhacker.commandsplus.server.command.NameCommand;
35
import com.thepinkhacker.commandsplus.world.GameRuleManager;
46
import net.fabricmc.fabric.api.datagen.v1.FabricDataOutput;
57
import net.fabricmc.fabric.api.datagen.v1.provider.FabricLanguageProvider;
@@ -15,25 +17,30 @@ public LanguageProvider(FabricDataOutput dataOutput, CompletableFuture<RegistryW
1517

1618
@Override
1719
public void generateTranslations(RegistryWrapper.WrapperLookup lookup, TranslationBuilder builder) {
18-
addGamerule(
20+
add(
1921
builder,
2022
GameRuleManager.DO_ENDERMAN_PICKUP,
2123
"Do Enderman Pickup",
2224
"Allow Endermen to pickup blocks."
2325
);
24-
addGamerule(
26+
add(
2527
builder,
2628
GameRuleManager.DO_ENDERMAN_PLACE,
2729
"Do Enderman Place",
2830
"Allow Endermen to place blocks."
2931
);
30-
addGamerule(
32+
add(
3133
builder,
3234
GameRuleManager.ITEM_DESPAWN_AGE,
3335
"Item Despawn Age",
3436
"Controls how long it takes for an item to despawn."
3537
);
3638

39+
add(builder, NameCommand.ENTITY_FAILED, "Failed to name entity");
40+
add(builder, NameCommand.ENTITY_REMOVE_FAILED, "Failed to remove entity name");
41+
add(builder, NameCommand.ITEM_FAILED, "Failed to name item");
42+
add(builder, NameCommand.ITEM_REMOVE_FAILED, "Failed to remove item name");
43+
3744
GenericTranslationBuilder.of(builder)
3845
.child(GenericTranslationBuilder.Node.of("commands")
3946
.child(GenericTranslationBuilder.Node.of("clearspawnpoint")
@@ -160,14 +167,14 @@ public void generateTranslations(RegistryWrapper.WrapperLookup lookup, Translati
160167
)
161168
.child(GenericTranslationBuilder.Node.of("name")
162169
.child(GenericTranslationBuilder.Node.of("entity")
163-
.child("failed", "Failed to name entity")
164170
// TODO: Color name
165-
.child("success", "Named entity to \"%s\"")
171+
.child("name.success", "Named entity to \"%s\"")
172+
.child("remove.success", "Removed entity name")
166173
)
167174
.child(GenericTranslationBuilder.Node.of("item")
168-
.child("failed", "Failed to name item")
169175
// TODO: Color name
170-
.child("success", "Named item to \"%s\"")
176+
.child("name.success", "Named item to \"%s\"")
177+
.child("remove.success", "Removed item name")
171178
)
172179
)
173180
.child(GenericTranslationBuilder.Node.of("setowner")
@@ -181,17 +188,21 @@ public void generateTranslations(RegistryWrapper.WrapperLookup lookup, Translati
181188
.build();
182189
}
183190

184-
private static void addGamerule(
191+
private static void add(
185192
TranslationBuilder builder,
186193
GameRules.Key<?> gamerule,
187194
String title,
188195
String description
189196
) {
190-
addGamerule(builder, gamerule, title);
197+
add(builder, gamerule, title);
191198
builder.add(gamerule.getTranslationKey() + ".description", description);
192199
}
193200

194-
private static void addGamerule(TranslationBuilder builder, GameRules.Key<?> gamerule, String title) {
201+
private static void add(TranslationBuilder builder, GameRules.Key<?> gamerule, String title) {
195202
builder.add(gamerule.getTranslationKey(), title);
196203
}
204+
205+
private static void add(TranslationBuilder builder, SimpleCommandExceptionType exception, String value) {
206+
builder.add(exception.toString(), value);
207+
}
197208
}

src/main/java/com/thepinkhacker/commandsplus/server/command/NameCommand.java

+185-21
Original file line numberDiff line numberDiff line change
@@ -20,88 +20,252 @@
2020
import java.util.Collection;
2121

2222
public class NameCommand implements CommandRegistrationCallback {
23+
public static final SimpleCommandExceptionType ITEM_FAILED = new SimpleCommandExceptionType(Text.translatable("commands.name.item.name.failed"));
24+
public static final SimpleCommandExceptionType ITEM_REMOVE_FAILED = new SimpleCommandExceptionType(Text.translatable("commands.name.item.remove.failed"));
25+
public static final SimpleCommandExceptionType ENTITY_FAILED = new SimpleCommandExceptionType(Text.translatable("commands.name.entity.name.failed"));
26+
public static final SimpleCommandExceptionType ENTITY_REMOVE_FAILED = new SimpleCommandExceptionType(Text.translatable("commands.name.entity.remove.failed"));
27+
2328
@Override
24-
public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandRegistryAccess registryAccess, CommandManager.RegistrationEnvironment environment) {
29+
public void register(
30+
CommandDispatcher<ServerCommandSource> dispatcher,
31+
CommandRegistryAccess registryAccess,
32+
CommandManager.RegistrationEnvironment environment
33+
) {
2534
dispatcher.register(CommandManager.literal("name")
2635
.requires(source -> source.hasPermissionLevel(2))
2736
.then(CommandManager.literal("item")
2837
.then(CommandManager.argument("targets", EntityArgumentType.entities())
38+
.executes(context -> removeNameItem(
39+
context.getSource(),
40+
EntityArgumentType.getEntities(context, "targets")
41+
))
2942
.then(CommandManager.argument("slot", ItemSlotArgumentType.itemSlot())
43+
.executes(context -> removeNameItem(
44+
context.getSource(),
45+
EntityArgumentType.getEntities(context, "targets"),
46+
ItemSlotArgumentType.getItemSlot(context, "slot")
47+
))
3048
.then(CommandManager.argument("name", MessageArgumentType.message())
3149
.executes(context -> nameItem(
3250
context.getSource(),
3351
EntityArgumentType.getEntities(context, "targets"),
3452
ItemSlotArgumentType.getItemSlot(context, "slot"),
35-
MessageArgumentType.getMessage(context, "name").getString()
53+
MessageArgumentType.getMessage(context, "name")
3654
))
3755
)
3856
)
3957
.then(CommandManager.argument("name", MessageArgumentType.message())
4058
.executes(context -> nameItem(
4159
context.getSource(),
4260
EntityArgumentType.getEntities(context, "targets"),
43-
0,
44-
MessageArgumentType.getMessage(context, "name").getString()
61+
MessageArgumentType.getMessage(context, "name")
4562
))
4663
)
4764
)
4865
)
4966
.then(CommandManager.literal("entity")
5067
.then(CommandManager.argument("targets", EntityArgumentType.entities())
68+
.executes(context -> removeNameEntity(
69+
context.getSource(),
70+
EntityArgumentType.getEntities(context, "targets")
71+
))
5172
.then(CommandManager.argument("name", MessageArgumentType.message())
5273
.executes(context -> nameEntity(
5374
context.getSource(),
5475
EntityArgumentType.getEntities(context, "targets"),
55-
MessageArgumentType.getMessage(context, "name").getString()
76+
MessageArgumentType.getMessage(context, "name")
5677
))
5778
)
5879
)
5980
)
6081
);
6182
}
6283

63-
private static int nameItem(ServerCommandSource source, Collection<? extends Entity> targets, int slot, String name) throws CommandSyntaxException {
84+
private static int nameItem(
85+
ServerCommandSource source,
86+
Collection<? extends Entity> targets,
87+
Text name
88+
) throws CommandSyntaxException {
89+
int i = 0;
90+
91+
for (Entity entity : targets) {
92+
if (nameItemTarget(entity, name)) {
93+
i++;
94+
}
95+
}
96+
97+
if (i > 0) {
98+
source.sendFeedback(
99+
() -> Text.translatable("commands.name.item.name.success", name),
100+
false
101+
);
102+
return i;
103+
} else {
104+
throw ITEM_FAILED.create();
105+
}
106+
}
107+
108+
private static int nameItem(
109+
ServerCommandSource source,
110+
Collection<? extends Entity> targets,
111+
int slot,
112+
Text name
113+
) throws CommandSyntaxException {
114+
int i = 0;
115+
116+
for (Entity entity : targets) {
117+
if (nameItemTarget(entity, slot, name)) {
118+
i++;
119+
}
120+
}
121+
122+
if (i > 0) {
123+
source.sendFeedback(
124+
() -> Text.translatable("commands.name.item.name.success", name),
125+
false
126+
);
127+
return i;
128+
} else {
129+
throw ITEM_FAILED.create();
130+
}
131+
}
132+
133+
private static boolean nameItemTarget(Entity entity, int slot, Text name) {
134+
StackReference stack = entity.getStackReference(slot);
135+
136+
if (stack == StackReference.EMPTY) return false;
137+
138+
return nameItemTarget(stack.get(), name);
139+
}
140+
141+
private static boolean nameItemTarget(Entity entity, Text name) {
142+
if (entity instanceof LivingEntity livingEntity) {
143+
ItemStack stack = livingEntity.getMainHandStack();
144+
return nameItemTarget(stack, name);
145+
} else {
146+
return false;
147+
}
148+
}
149+
150+
private static boolean nameItemTarget(ItemStack stack, Text name) {
151+
if (stack.isEmpty()) return false;
152+
153+
stack.set(DataComponentTypes.CUSTOM_NAME, name);
154+
return true;
155+
}
156+
157+
private static int removeNameItem(
158+
ServerCommandSource source,
159+
Collection<? extends Entity> targets
160+
) throws CommandSyntaxException {
64161
int i = 0;
65162

66163
for (Entity entity : targets) {
67-
StackReference stackReference = entity.getStackReference(slot);
164+
if (removeNameItemTarget(entity)) {
165+
i++;
166+
}
167+
}
168+
169+
if (i > 0) {
170+
source.sendFeedback(() -> Text.translatable("commands.name.item.remove.success"), false);
171+
return i;
172+
} else {
173+
throw ITEM_REMOVE_FAILED.create();
174+
}
175+
}
68176

69-
if (stackReference != StackReference.EMPTY) {
70-
ItemStack itemStack = stackReference.get();
177+
private static int removeNameItem(
178+
ServerCommandSource source,
179+
Collection<? extends Entity> targets,
180+
int slot
181+
) throws CommandSyntaxException {
182+
int i = 0;
71183

72-
if (!itemStack.isEmpty()) {
73-
itemStack.set(DataComponentTypes.ITEM_NAME, Text.of(name));
74-
i++;
75-
}
184+
for (Entity entity : targets) {
185+
if (removeNameItemTarget(entity, slot)) {
186+
i++;
76187
}
77188
}
78189

79190
if (i > 0) {
80-
source.sendFeedback(() -> Text.translatable("commands.name.item.success", name), false);
191+
source.sendFeedback(() -> Text.translatable("commands.name.item.remove.success"), false);
192+
return i;
81193
} else {
82-
throw new SimpleCommandExceptionType(Text.translatable("commands.name.item.failed")).create();
194+
throw ITEM_REMOVE_FAILED.create();
83195
}
196+
}
197+
198+
private static boolean removeNameItemTarget(Entity entity, int slot) {
199+
StackReference stack = entity.getStackReference(slot);
200+
201+
if (stack == StackReference.EMPTY) return false;
84202

85-
return i;
203+
return removeNameItemTarget(stack.get());
86204
}
87205

88-
public static int nameEntity(ServerCommandSource source, Collection<? extends Entity> targets, String name) throws CommandSyntaxException {
206+
private static boolean removeNameItemTarget(Entity entity) {
207+
if (entity instanceof LivingEntity livingEntity) {
208+
ItemStack stack = livingEntity.getMainHandStack();
209+
return removeNameItemTarget(stack);
210+
} else {
211+
return false;
212+
}
213+
}
214+
215+
private static boolean removeNameItemTarget(ItemStack stack) {
216+
if (stack.isEmpty()) return false;
217+
218+
return stack.remove(DataComponentTypes.CUSTOM_NAME) != null;
219+
}
220+
221+
public static int nameEntity(
222+
ServerCommandSource source,
223+
Collection<? extends Entity> targets,
224+
Text name
225+
) throws CommandSyntaxException {
89226
int i = 0;
90227

91228
for (Entity entity : targets) {
92229
if (entity instanceof LivingEntity livingEntity) {
93-
livingEntity.setCustomName(Text.of(name));
230+
livingEntity.setCustomName(name);
94231
livingEntity.setCustomNameVisible(true);
95232
i++;
96233
}
97234
}
98235

99236
if (i > 0) {
100-
source.sendFeedback(() -> Text.translatable("commands.name.entity.success", name), false);
237+
source.sendFeedback(
238+
() -> Text.translatable("commands.name.entity.name.success", name),
239+
false
240+
);
241+
return i;
101242
} else {
102-
throw new SimpleCommandExceptionType(Text.translatable("commands.name.entity.failed")).create();
243+
throw ENTITY_FAILED.create();
103244
}
245+
}
104246

105-
return i;
247+
public static int removeNameEntity(
248+
ServerCommandSource source,
249+
Collection<? extends Entity> targets
250+
) throws CommandSyntaxException {
251+
int i = 0;
252+
253+
for (Entity entity : targets) {
254+
if (entity.getCustomName() == null) continue;
255+
256+
entity.setCustomNameVisible(false);
257+
entity.setCustomName(null);
258+
i++;
259+
}
260+
261+
if (i > 0) {
262+
source.sendFeedback(
263+
() -> Text.translatable("commands.name.entity.remove.success"),
264+
false
265+
);
266+
return i;
267+
} else {
268+
throw ENTITY_REMOVE_FAILED.create();
269+
}
106270
}
107271
}

0 commit comments

Comments
 (0)