/
ServerCommandUtil.java
247 lines (224 loc) · 8.83 KB
/
ServerCommandUtil.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
package mezz.jei.util;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.tree.CommandNode;
import com.mojang.brigadier.tree.RootCommandNode;
import mezz.jei.common.network.ServerPacketContext;
import mezz.jei.core.config.GiveMode;
import mezz.jei.core.config.IServerConfig;
import mezz.jei.common.network.IConnectionToClient;
import mezz.jei.common.network.packets.PacketCheatPermission;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.commands.arguments.item.ItemInput;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TranslatableComponent;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* Server-side-safe utilities for commands.
*/
public final class ServerCommandUtil {
private static final Logger LOGGER = LogManager.getLogger();
private ServerCommandUtil() {
}
public static String[] getGiveCommandParameters(Player sender, ItemStack itemStack, int amount) {
Component senderName = sender.getName();
Item item = itemStack.getItem();
ResourceLocation itemResourceLocation = item.getRegistryName();
if (itemResourceLocation == null) {
String stackInfo = ErrorUtil.getItemStackInfo(itemStack);
throw new IllegalArgumentException("item.getRegistryName() returned null for: " + stackInfo);
}
List<String> commandStrings = new ArrayList<>();
commandStrings.add(senderName.getString());
String itemArgument = itemResourceLocation.toString();
CompoundTag tagCompound = itemStack.getTag();
if (tagCompound != null) {
itemArgument += tagCompound;
}
commandStrings.add(itemArgument);
commandStrings.add(String.valueOf(amount));
return commandStrings.toArray(new String[0]);
}
public static boolean hasPermissionForCheatMode(ServerPlayer sender, IServerConfig serverConfig) {
if (serverConfig.isCheatModeEnabledForCreative() &&
sender.isCreative()) {
return true;
}
CommandSourceStack commandSource = sender.createCommandSourceStack();
if (serverConfig.isCheatModeEnabledForOp()) {
MinecraftServer minecraftServer = sender.getServer();
if (minecraftServer != null) {
int opPermissionLevel = minecraftServer.getOperatorUserPermissionLevel();
return commandSource.hasPermission(opPermissionLevel);
}
}
if (serverConfig.isCheatModeEnabledForGive()) {
CommandNode<CommandSourceStack> giveCommand = getGiveCommand(sender);
if (giveCommand != null) {
return giveCommand.canUse(commandSource);
}
}
return false;
}
/**
* Gives a player an item.
*/
public static void executeGive(
ServerPacketContext context,
ItemStack itemStack,
GiveMode giveMode
) {
ServerPlayer sender = context.player();
IServerConfig serverConfig = context.serverConfig();
if (hasPermissionForCheatMode(sender, serverConfig)) {
if (itemStack.isEmpty()) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Player '{} ({})' tried to give an empty ItemStack.", sender.getName(), sender.getUUID());
}
return;
}
if (giveMode == GiveMode.INVENTORY) {
giveToInventory(sender, itemStack);
} else if (giveMode == GiveMode.MOUSE_PICKUP) {
mousePickupItemStack(sender, itemStack);
}
} else {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Player '{} ({})' tried to cheat an ItemStack '{}' but does not have permission.", sender.getName(), sender.getUUID(), itemStack.getDisplayName());
}
IConnectionToClient connection = context.connection();
connection.sendPacketToClient(new PacketCheatPermission(false), sender);
}
}
public static void setHotbarSlot(
ServerPacketContext context,
ItemStack itemStack,
int hotbarSlot
) {
ServerPlayer sender = context.player();
IServerConfig serverConfig = context.serverConfig();
if (hasPermissionForCheatMode(sender, serverConfig)) {
if (itemStack.isEmpty()) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Player '{} ({})' tried to set an empty ItemStack to the hotbar slot: {}", sender.getName(), sender.getUUID(), hotbarSlot);
}
return;
}
if (!Inventory.isHotbarSlot(hotbarSlot)) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Player '{} ({})' tried to set slot that is not in the hotbar: {}", sender.getName(), sender.getUUID(), hotbarSlot);
}
return;
}
ItemStack stackInSlot = sender.getInventory().getItem(hotbarSlot);
if (ItemStack.matches(stackInSlot, itemStack)) {
return;
}
ItemStack itemStackCopy = itemStack.copy();
sender.getInventory().setItem(hotbarSlot, itemStack);
sender.level.playSound(null, sender.getX(), sender.getY(), sender.getZ(), SoundEvents.ITEM_PICKUP, SoundSource.PLAYERS, 0.2F, ((sender.getRandom().nextFloat() - sender.getRandom().nextFloat()) * 0.7F + 1.0F) * 2.0F);
sender.inventoryMenu.broadcastChanges();
notifyGive(sender, itemStackCopy);
} else {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Player '{} ({})' tried to cheat an item '{}' to their hotbar but does not have permission.", sender.getName(), sender.getUUID(), itemStack.getDisplayName());
}
IConnectionToClient connection = context.connection();
connection.sendPacketToClient(new PacketCheatPermission(false), sender);
}
}
public static void mousePickupItemStack(Player sender, ItemStack itemStack) {
AbstractContainerMenu containerMenu = sender.containerMenu;
ItemStack itemStackCopy = itemStack.copy();
ItemStack existingStack = containerMenu.getCarried();
final int giveCount;
if (canStack(existingStack, itemStack)) {
int newCount = Math.min(existingStack.getMaxStackSize(), existingStack.getCount() + itemStack.getCount());
giveCount = newCount - existingStack.getCount();
existingStack.setCount(newCount);
} else {
containerMenu.setCarried(itemStack);
giveCount = itemStack.getCount();
}
if (giveCount > 0) {
itemStackCopy.setCount(giveCount);
notifyGive(sender, itemStackCopy);
containerMenu.broadcastChanges();
}
}
private static boolean canStack(ItemStack a, ItemStack b) {
if (a.isEmpty() || !a.sameItem(b) || a.hasTag() != b.hasTag()) {
return false;
}
CompoundTag tag = a.getTag();
if (tag != null && !tag.equals(b.getTag())) {
return false;
}
return a.areCapsCompatible(b);
}
/**
* Gives a player an item.
*
* @see GiveCommand#giveItem(CommandSource, ItemInput, Collection, int)
*/
@SuppressWarnings("JavadocReference")
private static void giveToInventory(Player entityplayermp, ItemStack itemStack) {
ItemStack itemStackCopy = itemStack.copy();
boolean flag = entityplayermp.getInventory().add(itemStack);
if (flag && itemStack.isEmpty()) {
itemStack.setCount(1);
ItemEntity entityitem = entityplayermp.drop(itemStack, false);
if (entityitem != null) {
entityitem.makeFakeItem();
}
entityplayermp.level.playSound(null, entityplayermp.getX(), entityplayermp.getY(), entityplayermp.getZ(), SoundEvents.ITEM_PICKUP, SoundSource.PLAYERS, 0.2F, ((entityplayermp.getRandom().nextFloat() - entityplayermp.getRandom().nextFloat()) * 0.7F + 1.0F) * 2.0F);
entityplayermp.inventoryMenu.broadcastChanges();
} else {
ItemEntity entityitem = entityplayermp.drop(itemStack, false);
if (entityitem != null) {
entityitem.setNoPickUpDelay();
entityitem.setOwner(entityplayermp.getUUID());
}
}
notifyGive(entityplayermp, itemStackCopy);
}
private static void notifyGive(Player player, ItemStack stack) {
if (player.getServer() == null) {
return;
}
CommandSourceStack commandSource = player.createCommandSourceStack();
int count = stack.getCount();
Component stackTextComponent = stack.getDisplayName();
Component displayName = player.getDisplayName();
TranslatableComponent message = new TranslatableComponent("commands.give.success.single", count, stackTextComponent, displayName);
commandSource.sendSuccess(message, true);
}
@Nullable
private static CommandNode<CommandSourceStack> getGiveCommand(Player sender) {
MinecraftServer minecraftServer = sender.getServer();
if (minecraftServer == null) {
return null;
}
Commands commandManager = minecraftServer.getCommands();
CommandDispatcher<CommandSourceStack> dispatcher = commandManager.getDispatcher();
RootCommandNode<CommandSourceStack> root = dispatcher.getRoot();
return root.getChild("give");
}
}