mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2026-01-04 15:41:38 +00:00
优化物品获取
This commit is contained in:
@@ -27,6 +27,7 @@ import net.momirealms.craftengine.core.util.*;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.event.HandlerList;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.incendo.cloud.suggestion.Suggestion;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -402,6 +403,7 @@ public class BukkitItemManager extends AbstractItemManager<ItemStack> {
|
||||
Object resourceLocation = FastNMS.INSTANCE.method$Registry$getKey(MBuiltInRegistries.ITEM, item);
|
||||
Key itemKey = KeyUtils.resourceLocationToKey(resourceLocation);
|
||||
VANILLA_ITEMS.add(itemKey);
|
||||
super.cachedVanillaItemSuggestions.add(Suggestion.suggestion(itemKey.asString()));
|
||||
UniqueKey uniqueKey = UniqueKey.create(itemKey);
|
||||
Object mcHolder = FastNMS.INSTANCE.method$Registry$getHolderByResourceKey(MBuiltInRegistries.ITEM, FastNMS.INSTANCE.method$ResourceKey$create(MRegistries.ITEM, resourceLocation)).get();
|
||||
Set<Object> tags = (Set<Object>) CoreReflections.field$Holder$Reference$tags.get(mcHolder);
|
||||
|
||||
@@ -64,6 +64,7 @@ public class BukkitCommandManager extends AbstractCommandManager<CommandSender>
|
||||
new SendResourcePackCommand(this, plugin),
|
||||
new DebugSaveDefaultResourcesCommand(this, plugin),
|
||||
new DebugCleanCacheCommand(this, plugin)
|
||||
// new OverrideGiveCommand(this, plugin)
|
||||
));
|
||||
final LegacyPaperCommandManager<CommandSender> manager = (LegacyPaperCommandManager<CommandSender>) getCommandManager();
|
||||
manager.settings().set(ManagerSetting.ALLOW_UNSAFE_REGISTRATION, true);
|
||||
|
||||
@@ -2,7 +2,6 @@ package net.momirealms.craftengine.bukkit.plugin.command.feature;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.momirealms.craftengine.bukkit.block.BukkitBlockManager;
|
||||
import net.momirealms.craftengine.bukkit.nms.FastNMS;
|
||||
import net.momirealms.craftengine.bukkit.plugin.command.BukkitCommandFeature;
|
||||
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections;
|
||||
import net.momirealms.craftengine.bukkit.util.BlockStateUtils;
|
||||
@@ -11,7 +10,6 @@ import net.momirealms.craftengine.core.block.ImmutableBlockState;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
import net.momirealms.craftengine.core.plugin.command.CraftEngineCommandManager;
|
||||
import net.momirealms.craftengine.core.plugin.command.sender.Sender;
|
||||
import net.momirealms.craftengine.core.plugin.config.Config;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package net.momirealms.craftengine.bukkit.plugin.command.feature;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.momirealms.craftengine.bukkit.api.BukkitAdaptors;
|
||||
import net.momirealms.craftengine.bukkit.api.CraftEngineItems;
|
||||
import net.momirealms.craftengine.bukkit.item.BukkitItemManager;
|
||||
import net.momirealms.craftengine.bukkit.plugin.command.BukkitCommandFeature;
|
||||
import net.momirealms.craftengine.bukkit.util.PlayerUtils;
|
||||
import net.momirealms.craftengine.core.item.CustomItem;
|
||||
import net.momirealms.craftengine.core.item.Item;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
import net.momirealms.craftengine.core.plugin.command.CraftEngineCommandManager;
|
||||
import net.momirealms.craftengine.core.plugin.command.FlagKeys;
|
||||
@@ -37,18 +39,16 @@ public class GetItemCommand extends BukkitCommandFeature<CommandSender> {
|
||||
return builder
|
||||
.senderType(Player.class)
|
||||
.flag(FlagKeys.SILENT_FLAG)
|
||||
.flag(FlagKeys.TO_INVENTORY_FLAG)
|
||||
.required("id", NamespacedKeyParser.namespacedKeyComponent().suggestionProvider(new SuggestionProvider<>() {
|
||||
@Override
|
||||
public @NonNull CompletableFuture<? extends @NonNull Iterable<? extends @NonNull Suggestion>> suggestionsFuture(@NonNull CommandContext<Object> context, @NonNull CommandInput input) {
|
||||
return CompletableFuture.completedFuture(plugin().itemManager().cachedSuggestions());
|
||||
return CompletableFuture.completedFuture(plugin().itemManager().cachedCustomItemSuggestions());
|
||||
}
|
||||
}))
|
||||
.optional("amount", IntegerParser.integerParser(1, 6400))
|
||||
.optional("amount", IntegerParser.integerParser(1, 9999))
|
||||
.handler(context -> {
|
||||
Player player = context.sender();
|
||||
int amount = context.getOrDefault("amount", 1);
|
||||
boolean toInv = context.flags().hasFlag(FlagKeys.TO_INVENTORY);
|
||||
NamespacedKey namespacedKey = context.get("id");
|
||||
Key itemId = Key.of(namespacedKey.namespace(), namespacedKey.value());
|
||||
CustomItem<ItemStack> customItem = CraftEngineItems.byId(itemId);
|
||||
@@ -61,24 +61,16 @@ public class GetItemCommand extends BukkitCommandFeature<CommandSender> {
|
||||
itemId = customItem.id();
|
||||
}
|
||||
}
|
||||
ItemStack builtItem = customItem.buildItemStack(plugin().adapt(context.sender()));
|
||||
int amountToGive = amount;
|
||||
int maxStack = builtItem.getMaxStackSize();
|
||||
while (amountToGive > 0) {
|
||||
int perStackSize = Math.min(maxStack, amountToGive);
|
||||
amountToGive -= perStackSize;
|
||||
ItemStack more = builtItem.clone();
|
||||
more.setAmount(perStackSize);
|
||||
if (toInv) {
|
||||
PlayerUtils.putItemsToInventory(player.getInventory(), more, more.getAmount());
|
||||
} else {
|
||||
PlayerUtils.dropItem(player, more, false, true, false);
|
||||
}
|
||||
Item<ItemStack> builtItem = customItem.buildItem(BukkitAdaptors.adapt(player));
|
||||
if (builtItem != null) {
|
||||
PlayerUtils.giveItem(player, amount, builtItem);
|
||||
}
|
||||
handleFeedback(context, MessageConstants.COMMAND_ITEM_GET_SUCCESS, Component.text(amount), Component.text(itemId.toString()));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public String getFeatureID() {
|
||||
return "get_item";
|
||||
|
||||
@@ -1,17 +1,18 @@
|
||||
package net.momirealms.craftengine.bukkit.plugin.command.feature;
|
||||
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.momirealms.craftengine.bukkit.api.BukkitAdaptors;
|
||||
import net.momirealms.craftengine.bukkit.api.CraftEngineItems;
|
||||
import net.momirealms.craftengine.bukkit.item.BukkitItemManager;
|
||||
import net.momirealms.craftengine.bukkit.plugin.command.BukkitCommandFeature;
|
||||
import net.momirealms.craftengine.bukkit.util.PlayerUtils;
|
||||
import net.momirealms.craftengine.core.item.CustomItem;
|
||||
import net.momirealms.craftengine.core.item.Item;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
import net.momirealms.craftengine.core.plugin.command.CraftEngineCommandManager;
|
||||
import net.momirealms.craftengine.core.plugin.command.FlagKeys;
|
||||
import net.momirealms.craftengine.core.plugin.locale.MessageConstants;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.VersionHelper;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -40,19 +41,17 @@ public class GiveItemCommand extends BukkitCommandFeature<CommandSender> {
|
||||
public Command.Builder<? extends CommandSender> assembleCommand(org.incendo.cloud.CommandManager<CommandSender> manager, Command.Builder<CommandSender> builder) {
|
||||
return builder
|
||||
.flag(FlagKeys.SILENT_FLAG)
|
||||
.flag(FlagKeys.TO_INVENTORY_FLAG)
|
||||
.required("player", MultiplePlayerSelectorParser.multiplePlayerSelectorParser(true))
|
||||
.required("id", NamespacedKeyParser.namespacedKeyComponent().suggestionProvider(new SuggestionProvider<>() {
|
||||
@Override
|
||||
public @NonNull CompletableFuture<? extends @NonNull Iterable<? extends @NonNull Suggestion>> suggestionsFuture(@NonNull CommandContext<Object> context, @NonNull CommandInput input) {
|
||||
return CompletableFuture.completedFuture(plugin().itemManager().cachedSuggestions());
|
||||
return CompletableFuture.completedFuture(plugin().itemManager().cachedCustomItemSuggestions());
|
||||
}
|
||||
}))
|
||||
.optional("amount", IntegerParser.integerParser(1, 6400))
|
||||
.optional("amount", IntegerParser.integerParser(1, 9999))
|
||||
.handler(context -> {
|
||||
MultiplePlayerSelector selector = context.get("player");
|
||||
int amount = context.getOrDefault("amount", 1);
|
||||
boolean toInv = context.flags().hasFlag(FlagKeys.TO_INVENTORY);
|
||||
NamespacedKey namespacedKey = context.get("id");
|
||||
Key itemId = Key.of(namespacedKey.namespace(), namespacedKey.value());
|
||||
CustomItem<ItemStack> customItem = CraftEngineItems.byId(itemId);
|
||||
@@ -67,30 +66,9 @@ public class GiveItemCommand extends BukkitCommandFeature<CommandSender> {
|
||||
}
|
||||
Collection<Player> players = selector.values();
|
||||
for (Player player : players) {
|
||||
ItemStack builtItem = customItem.buildItemStack(plugin().adapt(player));
|
||||
if (builtItem == null) {
|
||||
return;
|
||||
}
|
||||
int amountToGive = amount;
|
||||
int maxStack = builtItem.getMaxStackSize();
|
||||
while (amountToGive > 0) {
|
||||
int perStackSize = Math.min(maxStack, amountToGive);
|
||||
amountToGive -= perStackSize;
|
||||
ItemStack more = builtItem.clone();
|
||||
more.setAmount(perStackSize);
|
||||
if (toInv) {
|
||||
if (VersionHelper.isFolia()) {
|
||||
player.getScheduler().run(plugin().javaPlugin(), (t) -> PlayerUtils.putItemsToInventory(player.getInventory(), more, more.getAmount()), () -> {});
|
||||
} else {
|
||||
PlayerUtils.putItemsToInventory(player.getInventory(), more, more.getAmount());
|
||||
}
|
||||
} else {
|
||||
if (VersionHelper.isFolia()) {
|
||||
player.getScheduler().run(plugin().javaPlugin(), (t) -> PlayerUtils.dropItem(player, more, false, true, false), () -> {});
|
||||
} else {
|
||||
PlayerUtils.dropItem(player, more, false, true, false);
|
||||
}
|
||||
}
|
||||
Item<ItemStack> builtItem = customItem.buildItem(BukkitAdaptors.adapt(player));
|
||||
if (builtItem != null) {
|
||||
PlayerUtils.giveItem(player, amount, builtItem);
|
||||
}
|
||||
}
|
||||
if (players.size() == 1) {
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
//package net.momirealms.craftengine.bukkit.plugin.command.feature;
|
||||
//
|
||||
//import net.kyori.adventure.text.Component;
|
||||
//import net.momirealms.craftengine.bukkit.api.BukkitAdaptors;
|
||||
//import net.momirealms.craftengine.bukkit.item.BukkitItemManager;
|
||||
//import net.momirealms.craftengine.bukkit.plugin.command.BukkitCommandFeature;
|
||||
//import net.momirealms.craftengine.bukkit.util.PlayerUtils;
|
||||
//import net.momirealms.craftengine.core.item.Item;
|
||||
//import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
//import net.momirealms.craftengine.core.plugin.command.CraftEngineCommandManager;
|
||||
//import net.momirealms.craftengine.core.plugin.locale.MessageConstants;
|
||||
//import net.momirealms.craftengine.core.util.Key;
|
||||
//import org.bukkit.NamespacedKey;
|
||||
//import org.bukkit.command.CommandSender;
|
||||
//import org.bukkit.entity.Player;
|
||||
//import org.bukkit.inventory.ItemStack;
|
||||
//import org.checkerframework.checker.nullness.qual.NonNull;
|
||||
//import org.incendo.cloud.Command;
|
||||
//import org.incendo.cloud.bukkit.data.MultiplePlayerSelector;
|
||||
//import org.incendo.cloud.bukkit.parser.NamespacedKeyParser;
|
||||
//import org.incendo.cloud.bukkit.parser.selector.MultiplePlayerSelectorParser;
|
||||
//import org.incendo.cloud.context.CommandContext;
|
||||
//import org.incendo.cloud.context.CommandInput;
|
||||
//import org.incendo.cloud.parser.standard.IntegerParser;
|
||||
//import org.incendo.cloud.suggestion.Suggestion;
|
||||
//import org.incendo.cloud.suggestion.SuggestionProvider;
|
||||
//
|
||||
//import java.util.Collection;
|
||||
//import java.util.concurrent.CompletableFuture;
|
||||
//
|
||||
//public class OverrideGiveCommand extends BukkitCommandFeature<CommandSender> {
|
||||
//
|
||||
// public OverrideGiveCommand(CraftEngineCommandManager<CommandSender> commandManager, CraftEngine plugin) {
|
||||
// super(commandManager, plugin);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public Command.Builder<? extends CommandSender> assembleCommand(org.incendo.cloud.CommandManager<CommandSender> manager, Command.Builder<CommandSender> builder) {
|
||||
// return builder
|
||||
// .required("player", MultiplePlayerSelectorParser.multiplePlayerSelectorParser(true))
|
||||
// .required("id", NamespacedKeyParser.namespacedKeyComponent().suggestionProvider(new SuggestionProvider<>() {
|
||||
// @Override
|
||||
// public @NonNull CompletableFuture<? extends @NonNull Iterable<? extends @NonNull Suggestion>> suggestionsFuture(@NonNull CommandContext<Object> context, @NonNull CommandInput input) {
|
||||
// return CompletableFuture.completedFuture(plugin().itemManager().cachedAllItemSuggestions());
|
||||
// }
|
||||
// }))
|
||||
// .optional("amount", IntegerParser.integerParser(1, 6400))
|
||||
// .handler(context -> {
|
||||
// MultiplePlayerSelector selector = context.get("player");
|
||||
// int amount = context.getOrDefault("amount", 1);
|
||||
// NamespacedKey namespacedKey = context.get("id");
|
||||
// Key itemId = Key.of(namespacedKey.namespace(), namespacedKey.value());
|
||||
// Collection<Player> players = selector.values();
|
||||
//
|
||||
// Component anyItemDisplayName = null;
|
||||
//
|
||||
// for (Player player : players) {
|
||||
// Item<ItemStack> builtItem = BukkitItemManager.instance().createWrappedItem(itemId, BukkitAdaptors.adapt(player));
|
||||
// if (builtItem == null) {
|
||||
// return;
|
||||
// }
|
||||
// int maxStack = builtItem.maxStackSize();
|
||||
// anyItemDisplayName = builtItem.hoverNameComponent()
|
||||
// .orElseGet(() -> {
|
||||
// if (builtItem.isCustomItem()) {
|
||||
// return Component.text(itemId.asString());
|
||||
// } else {
|
||||
// return Component.translatable("item.minecraft." + itemId.value());
|
||||
// }
|
||||
// });
|
||||
//
|
||||
// if (amount > maxStack * 100) {
|
||||
// plugin().senderFactory().wrap(context.sender()).sendMessage(Component.translatable("commands.give.failed.toomanyitems", Component.text(maxStack * 100), anyItemDisplayName);
|
||||
// return;
|
||||
// }
|
||||
//
|
||||
// PlayerUtils.giveItem(player, amount, builtItem);
|
||||
// }
|
||||
// if (players.size() == 1) {
|
||||
// plugin().senderFactory().wrap(context.sender()).sendMessage(Component.translatable("commands.give.success.single", Component.text(amount), anyItemDisplayName, ));
|
||||
// } else if (players.size() > 1) {
|
||||
// handleFeedback(context, MessageConstants.COMMAND_ITEM_GIVE_SUCCESS_MULTIPLE, Component.text(amount), Component.text(itemId.toString()), Component.text(players.size()));
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public String getFeatureID() {
|
||||
// return "override_minecraft_give";
|
||||
// }
|
||||
//}
|
||||
@@ -37,7 +37,7 @@ public class SearchRecipeAdminCommand extends BukkitCommandFeature<CommandSender
|
||||
.required("id", NamespacedKeyParser.namespacedKeyComponent().suggestionProvider(new SuggestionProvider<>() {
|
||||
@Override
|
||||
public @NonNull CompletableFuture<? extends @NonNull Iterable<? extends @NonNull Suggestion>> suggestionsFuture(@NonNull CommandContext<Object> context, @NonNull CommandInput input) {
|
||||
return CompletableFuture.completedFuture(plugin().itemManager().cachedSuggestions());
|
||||
return CompletableFuture.completedFuture(plugin().itemManager().cachedCustomItemSuggestions());
|
||||
}
|
||||
}))
|
||||
.handler(context -> {
|
||||
|
||||
@@ -37,7 +37,7 @@ public class SearchUsageAdminCommand extends BukkitCommandFeature<CommandSender>
|
||||
.required("id", NamespacedKeyParser.namespacedKeyComponent().suggestionProvider(new SuggestionProvider<>() {
|
||||
@Override
|
||||
public @NonNull CompletableFuture<? extends @NonNull Iterable<? extends @NonNull Suggestion>> suggestionsFuture(@NonNull CommandContext<Object> context, @NonNull CommandInput input) {
|
||||
return CompletableFuture.completedFuture(plugin().itemManager().cachedSuggestions());
|
||||
return CompletableFuture.completedFuture(plugin().itemManager().cachedCustomItemSuggestions());
|
||||
}
|
||||
}))
|
||||
.handler(context -> {
|
||||
|
||||
@@ -5,8 +5,6 @@ import com.google.common.collect.Lists;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.papermc.paper.registry.RegistryAccess;
|
||||
import io.papermc.paper.registry.RegistryKey;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.momirealms.craftengine.bukkit.api.CraftEngineBlocks;
|
||||
import net.momirealms.craftengine.bukkit.block.entity.BlockEntityHolder;
|
||||
@@ -378,9 +376,10 @@ public class BukkitServerPlayer extends Player {
|
||||
platformPlayer().playSound(new Location(null, pos.x(), pos.y(), pos.z()), sound.toString(), SoundUtils.toBukkit(source), volume, pitch);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void giveItem(Item<?> item) {
|
||||
PlayerUtils.giveItem(platformPlayer(), (ItemStack) item.getItem(), item.count());
|
||||
PlayerUtils.giveItem(platformPlayer(), item.count(), (Item<ItemStack>) item);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -7,148 +7,49 @@ import net.momirealms.craftengine.bukkit.plugin.BukkitCraftEngine;
|
||||
import net.momirealms.craftengine.bukkit.plugin.network.BukkitNetworkManager;
|
||||
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections;
|
||||
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.NetworkReflections;
|
||||
import net.momirealms.craftengine.core.item.Item;
|
||||
import net.momirealms.craftengine.core.util.RandomUtils;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Item;
|
||||
import net.momirealms.craftengine.core.util.VersionHelper;
|
||||
import org.bukkit.Sound;
|
||||
import org.bukkit.SoundCategory;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.PlayerInventory;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
import org.bukkit.util.Vector;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
public final class PlayerUtils {
|
||||
private PlayerUtils() {
|
||||
}
|
||||
|
||||
public static void dropItem(@NotNull Player player, @NotNull ItemStack itemStack, boolean retainOwnership, boolean noPickUpDelay, boolean throwRandomly) {
|
||||
requireNonNull(player, "player");
|
||||
requireNonNull(itemStack, "itemStack");
|
||||
Location location = player.getLocation().clone();
|
||||
Item item = player.getWorld().dropItem(player.getEyeLocation().clone().subtract(new Vector(0, 0.3, 0)), itemStack);
|
||||
item.setPickupDelay(noPickUpDelay ? 0 : 40);
|
||||
item.setOwner(player.getUniqueId());
|
||||
if (retainOwnership) {
|
||||
item.setThrower(player.getUniqueId());
|
||||
public static void giveItem(Player player, int amount, Item<ItemStack> original) {
|
||||
int amountToGive = amount;
|
||||
int maxStack = original.maxStackSize();
|
||||
while (amountToGive > 0) {
|
||||
int perStackSize = Math.min(maxStack, amountToGive);
|
||||
amountToGive -= perStackSize;
|
||||
PlayerUtils.giveItem(player, original, original.copyWithCount(perStackSize));
|
||||
}
|
||||
if (throwRandomly) {
|
||||
double d1 = RandomUtils.generateRandomDouble(0, 1) * 0.5f;
|
||||
double d2 = RandomUtils.generateRandomDouble(0, 1) * (Math.PI * 2);
|
||||
item.setVelocity(new Vector(-Math.sin(d2) * d1, 0.2f, Math.cos(d2) * d1));
|
||||
}
|
||||
|
||||
public static void giveItem(Player player, Item<ItemStack> original, Item<ItemStack> item) {
|
||||
Object serverPlayer = FastNMS.INSTANCE.method$CraftPlayer$getHandle(player);
|
||||
Object inventory = FastNMS.INSTANCE.method$Player$getInventory(serverPlayer);
|
||||
boolean flag = FastNMS.INSTANCE.method$Inventory$add(inventory, item.getLiteralObject());
|
||||
if (flag && item.isEmpty()) {
|
||||
Object droppedItem = FastNMS.INSTANCE.method$ServerPlayer$drop(serverPlayer, original.copyWithCount(1).getLiteralObject(), false, false, false, null);
|
||||
if (droppedItem != null) {
|
||||
FastNMS.INSTANCE.method$ItemEntity$makeFakeItem(droppedItem);
|
||||
}
|
||||
player.getWorld().playSound(player.getLocation(), Sound.ENTITY_ITEM_PICKUP, SoundCategory.PLAYERS, 0.2F, ((RandomUtils.generateRandomFloat(0, 1) - RandomUtils.generateRandomFloat(0, 1)) * 0.7F + 1.0F) * 2.0F);
|
||||
FastNMS.INSTANCE.method$AbstractContainerMenu$broadcastChanges(FastNMS.INSTANCE.field$Player$containerMenu(serverPlayer));
|
||||
} else {
|
||||
double d1 = Math.sin(location.getPitch() * (Math.PI / 180));
|
||||
double d2 = RandomUtils.generateRandomDouble(0, 0.02);
|
||||
double d3 = RandomUtils.generateRandomDouble(0, 1) * (Math.PI * 2);
|
||||
Vector vector = location.getDirection().multiply(0.3).setY(-d1 * 0.3 + 0.1 + (RandomUtils.generateRandomDouble(0, 1) - RandomUtils.generateRandomDouble(0, 1)) * 0.1);
|
||||
vector.add(new Vector(Math.cos(d3) * d2, 0, Math.sin(d3) * d2));
|
||||
item.setVelocity(vector);
|
||||
}
|
||||
}
|
||||
|
||||
public static int putItemsToInventory(Inventory inventory, ItemStack itemStack, int amount) {
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
int maxStackSize = itemStack.getMaxStackSize();
|
||||
for (ItemStack other : inventory.getStorageContents()) {
|
||||
if (other != null) {
|
||||
if (other.getType() == itemStack.getType() && other.getItemMeta().equals(meta)) {
|
||||
if (other.getAmount() < maxStackSize) {
|
||||
int delta = maxStackSize - other.getAmount();
|
||||
if (amount > delta) {
|
||||
other.setAmount(maxStackSize);
|
||||
amount -= delta;
|
||||
} else {
|
||||
other.setAmount(amount + other.getAmount());
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Object droppedItem = FastNMS.INSTANCE.method$ServerPlayer$drop(serverPlayer, item.getLiteralObject(), false, false, !VersionHelper.isOrAbove1_21_5(), null);
|
||||
if (droppedItem != null) {
|
||||
FastNMS.INSTANCE.method$ItemEntity$setNoPickUpDelay(droppedItem);
|
||||
FastNMS.INSTANCE.method$ItemEntity$setTarget(droppedItem, player.getUniqueId());
|
||||
}
|
||||
}
|
||||
|
||||
if (amount > 0) {
|
||||
for (ItemStack other : inventory.getStorageContents()) {
|
||||
if (other == null) {
|
||||
if (amount > maxStackSize) {
|
||||
amount -= maxStackSize;
|
||||
ItemStack cloned = itemStack.clone();
|
||||
cloned.setAmount(maxStackSize);
|
||||
inventory.addItem(cloned);
|
||||
} else {
|
||||
ItemStack cloned = itemStack.clone();
|
||||
cloned.setAmount(amount);
|
||||
inventory.addItem(cloned);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return amount;
|
||||
}
|
||||
|
||||
public static int giveItem(Player player, ItemStack itemStack, int amount) {
|
||||
PlayerInventory inventory = player.getInventory();
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
int maxStackSize = itemStack.getMaxStackSize();
|
||||
if (amount > maxStackSize * 100) {
|
||||
amount = maxStackSize * 100;
|
||||
}
|
||||
int actualAmount = amount;
|
||||
for (ItemStack other : inventory.getStorageContents()) {
|
||||
if (other != null) {
|
||||
if (other.getType() == itemStack.getType() && other.getItemMeta().equals(meta)) {
|
||||
if (other.getAmount() < maxStackSize) {
|
||||
int delta = maxStackSize - other.getAmount();
|
||||
if (amount > delta) {
|
||||
other.setAmount(maxStackSize);
|
||||
amount -= delta;
|
||||
} else {
|
||||
other.setAmount(amount + other.getAmount());
|
||||
return actualAmount;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (amount > 0) {
|
||||
for (ItemStack other : inventory.getStorageContents()) {
|
||||
if (other == null) {
|
||||
if (amount > maxStackSize) {
|
||||
amount -= maxStackSize;
|
||||
ItemStack cloned = itemStack.clone();
|
||||
cloned.setAmount(maxStackSize);
|
||||
inventory.addItem(cloned);
|
||||
} else {
|
||||
ItemStack cloned = itemStack.clone();
|
||||
cloned.setAmount(amount);
|
||||
inventory.addItem(cloned);
|
||||
return actualAmount;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (amount > 0) {
|
||||
for (int i = 0; i < amount / maxStackSize; i++) {
|
||||
ItemStack cloned = itemStack.clone();
|
||||
cloned.setAmount(maxStackSize);
|
||||
player.getWorld().dropItem(player.getLocation(), cloned);
|
||||
}
|
||||
int left = amount % maxStackSize;
|
||||
if (left != 0) {
|
||||
ItemStack cloned = itemStack.clone();
|
||||
cloned.setAmount(left);
|
||||
player.getWorld().dropItem(player.getLocation(), cloned);
|
||||
}
|
||||
}
|
||||
|
||||
return actualAmount;
|
||||
}
|
||||
|
||||
public static void sendTotemAnimation(Player player, ItemStack totem) {
|
||||
|
||||
@@ -59,7 +59,9 @@ public abstract class AbstractItemManager<I> extends AbstractModelGenerator impl
|
||||
protected final Map<Key, TreeMap<Integer, ModernItemModel>> modernOverrides = new HashMap<>();
|
||||
protected final Map<Key, Equipment> equipments = new HashMap<>();
|
||||
// Cached command suggestions
|
||||
protected final List<Suggestion> cachedSuggestions = new ArrayList<>();
|
||||
protected final List<Suggestion> cachedCustomItemSuggestions = new ArrayList<>();
|
||||
protected final List<Suggestion> cachedAllItemSuggestions = new ArrayList<>();
|
||||
protected final List<Suggestion> cachedVanillaItemSuggestions = new ArrayList<>();
|
||||
protected final List<Suggestion> cachedTotemSuggestions = new ArrayList<>();
|
||||
|
||||
protected AbstractItemManager(CraftEngine plugin) {
|
||||
@@ -130,7 +132,8 @@ public abstract class AbstractItemManager<I> extends AbstractModelGenerator impl
|
||||
super.clearModelsToGenerate();
|
||||
this.customItemsById.clear();
|
||||
this.customItemsByPath.clear();
|
||||
this.cachedSuggestions.clear();
|
||||
this.cachedCustomItemSuggestions.clear();
|
||||
this.cachedAllItemSuggestions.clear();
|
||||
this.cachedTotemSuggestions.clear();
|
||||
this.legacyOverrides.clear();
|
||||
this.modernOverrides.clear();
|
||||
@@ -181,7 +184,7 @@ public abstract class AbstractItemManager<I> extends AbstractModelGenerator impl
|
||||
this.customItemsByPath.put(id.value(), customItem);
|
||||
if (!customItem.isVanillaItem()) {
|
||||
// cache command suggestions
|
||||
this.cachedSuggestions.add(Suggestion.suggestion(id.toString()));
|
||||
this.cachedCustomItemSuggestions.add(Suggestion.suggestion(id.toString()));
|
||||
// totem animations
|
||||
if (VersionHelper.isOrAbove1_21_2()) {
|
||||
this.cachedTotemSuggestions.add(Suggestion.suggestion(id.toString()));
|
||||
@@ -208,8 +211,13 @@ public abstract class AbstractItemManager<I> extends AbstractModelGenerator impl
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Suggestion> cachedSuggestions() {
|
||||
return Collections.unmodifiableCollection(this.cachedSuggestions);
|
||||
public Collection<Suggestion> cachedCustomItemSuggestions() {
|
||||
return Collections.unmodifiableCollection(this.cachedCustomItemSuggestions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Suggestion> cachedAllItemSuggestions() {
|
||||
return Collections.unmodifiableCollection(this.cachedAllItemSuggestions);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -239,6 +247,12 @@ public abstract class AbstractItemManager<I> extends AbstractModelGenerator impl
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delayedLoad() {
|
||||
this.cachedAllItemSuggestions.addAll(this.cachedVanillaItemSuggestions);
|
||||
this.cachedAllItemSuggestions.addAll(this.cachedCustomItemSuggestions);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<Key, CustomItem<I>> loadedItems() {
|
||||
return Collections.unmodifiableMap(this.customItemsById);
|
||||
|
||||
@@ -102,7 +102,9 @@ public interface ItemManager<T> extends Manageable, ModelGenerator {
|
||||
|
||||
int fuelTime(Key id);
|
||||
|
||||
Collection<Suggestion> cachedSuggestions();
|
||||
Collection<Suggestion> cachedCustomItemSuggestions();
|
||||
|
||||
Collection<Suggestion> cachedAllItemSuggestions();
|
||||
|
||||
Collection<Suggestion> cachedTotemSuggestions();
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ byte_buddy_version=1.17.5
|
||||
ahocorasick_version=0.6.3
|
||||
snake_yaml_version=2.5
|
||||
anti_grief_version=0.20
|
||||
nms_helper_version=1.0.98
|
||||
nms_helper_version=1.0.100
|
||||
evalex_version=3.5.0
|
||||
reactive_streams_version=1.0.4
|
||||
amazon_awssdk_version=2.34.5
|
||||
|
||||
Reference in New Issue
Block a user