9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-28 19:39:11 +00:00

add new furniture place command

This commit is contained in:
XiaoMoMi
2025-03-26 17:12:04 +08:00
parent c625aad284
commit 15f1f00356
11 changed files with 152 additions and 12 deletions

View File

@@ -37,6 +37,20 @@ public class CraftEngineFurniture {
return BukkitFurnitureManager.instance().getFurniture(id).orElse(null);
}
/**
* Places furniture at the certain location
*
* @param location location
* @param furnitureId furniture to place
* @return the loaded furniture
*/
@Nullable
public static LoadedFurniture place(Location location, Key furnitureId) {
CustomFurniture furniture = byId(furnitureId);
if (furniture == null) return null;
return place(location, furnitureId, furniture.getAnyPlacement());
}
/**
* Places furniture at the certain location
*

View File

@@ -16,6 +16,7 @@ import org.bukkit.entity.*;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.persistence.PersistentDataType;
import org.incendo.cloud.suggestion.Suggestion;
import org.jetbrains.annotations.NotNull;
import org.joml.Vector3f;
@@ -41,6 +42,8 @@ public class BukkitFurnitureManager implements FurnitureManager {
private final FurnitureEventListener furnitureEventListener;
// tick task
private SchedulerTask tickTask;
// Cached command suggestions
private final List<Suggestion> cachedSuggestions = new ArrayList<>();
public static BukkitFurnitureManager instance() {
return instance;
@@ -54,10 +57,14 @@ public class BukkitFurnitureManager implements FurnitureManager {
}
public LoadedFurniture place(CustomFurniture furniture, Location location, AnchorType anchorType, boolean playSound) {
if (furniture.isAllowedPlacement(anchorType)) {
anchorType = furniture.getAnyPlacement();
}
AnchorType finalAnchorType = anchorType;
Entity furnitureEntity = EntityUtils.spawnEntity(location.getWorld(), location, EntityType.ITEM_DISPLAY, entity -> {
ItemDisplay display = (ItemDisplay) entity;
display.getPersistentDataContainer().set(BukkitFurnitureManager.FURNITURE_KEY, PersistentDataType.STRING, furniture.id().toString());
display.getPersistentDataContainer().set(BukkitFurnitureManager.FURNITURE_ANCHOR_KEY, PersistentDataType.STRING, anchorType.name());
display.getPersistentDataContainer().set(BukkitFurnitureManager.FURNITURE_ANCHOR_KEY, PersistentDataType.STRING, finalAnchorType.name());
handleEntityLoadEarly(display);
});
if (playSound) {
@@ -67,6 +74,24 @@ public class BukkitFurnitureManager implements FurnitureManager {
return getLoadedFurnitureByBaseEntityId(furnitureEntity.getEntityId());
}
@Override
public void delayedLoad() {
this.initSuggestions();
}
@Override
public void initSuggestions() {
this.cachedSuggestions.clear();
for (Key key : this.byId.keySet()) {
this.cachedSuggestions.add(Suggestion.suggestion(key.toString()));
}
}
@Override
public Collection<Suggestion> cachedSuggestions() {
return Collections.unmodifiableCollection(this.cachedSuggestions);
}
@SuppressWarnings("unchecked")
@Override
public void parseSection(Pack pack, Path path, Key id, Map<String, Object> section) {

View File

@@ -44,6 +44,7 @@ public class BukkitCommandManager extends AbstractCommandManager<CommandSender>
new DebugRealStateUsageCommand(this, plugin),
new DebugItemDataCommand(this, plugin),
new DebugSetBlockCommand(this, plugin),
new DebugSpawnFurnitureCommand(this, plugin),
new DebugTargetBlockCommand(this, plugin)
));
final LegacyPaperCommandManager<CommandSender> manager = (LegacyPaperCommandManager<CommandSender>) getCommandManager();

View File

@@ -0,0 +1,67 @@
package net.momirealms.craftengine.bukkit.plugin.command.feature;
import net.momirealms.craftengine.bukkit.api.CraftEngineFurniture;
import net.momirealms.craftengine.bukkit.entity.furniture.BukkitFurnitureManager;
import net.momirealms.craftengine.bukkit.plugin.command.BukkitCommandFeature;
import net.momirealms.craftengine.bukkit.util.KeyUtils;
import net.momirealms.craftengine.core.entity.furniture.AnchorType;
import net.momirealms.craftengine.core.entity.furniture.CustomFurniture;
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.util.Key;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.command.CommandSender;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.incendo.cloud.Command;
import org.incendo.cloud.bukkit.parser.NamespacedKeyParser;
import org.incendo.cloud.bukkit.parser.location.LocationParser;
import org.incendo.cloud.context.CommandContext;
import org.incendo.cloud.context.CommandInput;
import org.incendo.cloud.parser.standard.EnumParser;
import org.incendo.cloud.suggestion.Suggestion;
import org.incendo.cloud.suggestion.SuggestionProvider;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
public class DebugSpawnFurnitureCommand extends BukkitCommandFeature<CommandSender> {
public DebugSpawnFurnitureCommand(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("location", LocationParser.locationParser())
.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().furnitureManager().cachedSuggestions());
}
}))
.optional("anchor-type", EnumParser.enumParser(AnchorType.class))
.flag(FlagKeys.SILENT_FLAG)
.handler(context -> {
NamespacedKey namespacedKey = context.get("id");
Key id = KeyUtils.namespacedKey2Key(namespacedKey);
BukkitFurnitureManager furnitureManager = BukkitFurnitureManager.instance();
Optional<CustomFurniture> optionalCustomFurniture = furnitureManager.getFurniture(id);
if (optionalCustomFurniture.isEmpty()) {
return;
}
Location location = context.get("location");
CustomFurniture customFurniture = optionalCustomFurniture.get();
AnchorType anchorType = (AnchorType) context.optional("anchor-type").orElse(customFurniture.getAnyPlacement());
boolean playSound = context.flags().hasFlag("silent");
furnitureManager.place(customFurniture, location, anchorType, !playSound);
});
}
@Override
public String getFeatureID() {
return "debug_spawn_furniture";
}
}

View File

@@ -589,16 +589,6 @@ public class BukkitInjector {
}
}
public static class PalettedContainerMethodInterceptor {
public static final PalettedContainerMethodInterceptor INSTANCE = new PalettedContainerMethodInterceptor();
@RuntimeType
public Object intercept(@This Object thisObj, @AllArguments Object[] args, @Origin Method method) throws Throwable {
InjectedPalettedContainerHolder holder = (InjectedPalettedContainerHolder) thisObj;
return method.invoke(holder.target(), args);
}
}
public static class GetAndSetInterceptor {
public static final GetAndSetInterceptor INSTANCE = new GetAndSetInterceptor();

View File

@@ -0,0 +1,6 @@
package net.momirealms.craftengine.bukkit.util;
public class OptimizedReflections {
}