9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-22 08:29:21 +00:00

增强1.21.2的item model生成

This commit is contained in:
XiaoMoMi
2025-05-15 03:17:21 +08:00
parent c047e5a83c
commit ec2298fe6d
7 changed files with 79 additions and 48 deletions

View File

@@ -311,3 +311,5 @@ warning.config.function.message.missing_message: "<yellow>Issue found in file <a
warning.config.selector.missing_type: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is missing the required 'type' argument for selector.</yellow>"
warning.config.selector.invalid_type: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is using an invalid selector type '<arg:2>'.</yellow>"
warning.config.selector.invalid_target: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is using an invalid selector target '<arg:2>'.</yellow>"
warning.config.resource_pack.item_model.conflict: "<yellow>Failed to generate item model for '<arg:0>' because this item model has been occupied by a vanilla item.</yellow>"
warning.config.resource_pack.item_model.v1_21_2.conflict: "<yellow>Failed to generate item model(v1.21.2) for '<arg:0>'.</yellow>"

View File

@@ -132,18 +132,17 @@ public class BlockEventListener implements Listener {
if (itemInHand != null) {
Optional<CustomItem<ItemStack>> optionalCustomItem = itemInHand.getCustomItem();
if (optionalCustomItem.isPresent()) {
Cancellable dummy = Cancellable.dummy();
Cancellable cancellable = Cancellable.of(event::isCancelled, event::setCancelled);
optionalCustomItem.get().execute(
PlayerOptionalContext.of(serverPlayer, ContextHolder.builder()
.withParameter(DirectContextParameters.BLOCK, new BukkitBlockInWorld(block))
.withParameter(DirectContextParameters.POSITION, position)
.withParameter(DirectContextParameters.PLAYER, serverPlayer)
.withParameter(DirectContextParameters.EVENT, dummy)
.withParameter(DirectContextParameters.EVENT, cancellable)
.withOptionalParameter(DirectContextParameters.ITEM_IN_HAND, itemInHand)
), EventTrigger.BREAK
);
if (dummy.isCancelled()) {
event.setCancelled(true);
if (cancellable.isCancelled()) {
return;
}
}
@@ -166,7 +165,7 @@ public class BlockEventListener implements Listener {
}
// execute functions
Cancellable cancellable = Cancellable.dummy();
Cancellable cancellable = Cancellable.of(event::isCancelled, event::setCancelled);
PlayerOptionalContext context = PlayerOptionalContext.of(serverPlayer, ContextHolder.builder()
.withParameter(DirectContextParameters.BLOCK, new BukkitBlockInWorld(block))
.withParameter(DirectContextParameters.BLOCK_STATE, state)
@@ -176,7 +175,6 @@ public class BlockEventListener implements Listener {
);
state.owner().value().execute(context, EventTrigger.BREAK);
if (cancellable.isCancelled()) {
event.setCancelled(true);
return;
}

View File

@@ -3,7 +3,6 @@ package net.momirealms.craftengine.bukkit.entity.data;
import net.momirealms.craftengine.core.util.VersionHelper;
public class InteractionEntityData<T> extends BaseEntityData<T> {
// Interaction only
public static final InteractionEntityData<Float> Width = of(8, EntityDataValue.Serializers$FLOAT, 1F);
public static final InteractionEntityData<Float> Height = of(9, EntityDataValue.Serializers$FLOAT, 1F);
public static final InteractionEntityData<Boolean> Responsive = of(10, EntityDataValue.Serializers$BOOLEAN, false);

View File

@@ -3,7 +3,6 @@ package net.momirealms.craftengine.bukkit.entity.data;
import net.momirealms.craftengine.bukkit.util.Reflections;
public class TextDisplayEntityData<T> extends DisplayEntityData<T> {
// Text display only
public static final DisplayEntityData<Object> Text = of(23, EntityDataValue.Serializers$COMPONENT, Reflections.instance$Component$empty);
public static final DisplayEntityData<Integer> LineWidth = of(24, EntityDataValue.Serializers$INT, 200);
public static final DisplayEntityData<Integer> BackgroundColor = of(25, EntityDataValue.Serializers$INT, 0x40000000);

View File

@@ -5,7 +5,6 @@ import net.momirealms.craftengine.bukkit.util.Reflections;
public class ThrowableItemProjectileData<T> extends BaseEntityData<T> {
public static final ThrowableItemProjectileData<Object> ItemStack = new ThrowableItemProjectileData<>(8, EntityDataValue.Serializers$ITEM_STACK, Reflections.instance$ItemStack$Air);
public ThrowableItemProjectileData(int id, Object serializer, T defaultValue) {
super(id, serializer, defaultValue);
}

View File

@@ -560,13 +560,13 @@ public abstract class AbstractPackManager implements PackManager {
}
this.generateFonts(generatedPackPath);
this.generateItemModels(generatedPackPath, this.plugin.itemManager());
this.generateItemModels(generatedPackPath, this.plugin.blockManager());
this.generateBlockOverrides(generatedPackPath);
this.generateLegacyItemOverrides(generatedPackPath);
this.generateModernItemOverrides(generatedPackPath);
this.generateModernItemModels1_21_2(generatedPackPath);
this.generateModernItemModels1_21_4(generatedPackPath);
this.generateBlockOverrides(generatedPackPath);
this.generateItemModels(generatedPackPath, this.plugin.itemManager());
this.generateItemModels(generatedPackPath, this.plugin.blockManager());
this.generateOverrideSounds(generatedPackPath);
this.generateCustomSounds(generatedPackPath);
this.generateClientLang(generatedPackPath);
@@ -911,60 +911,67 @@ public abstract class AbstractPackManager implements PackManager {
if (Config.packMaxVersion() < 21.19f) return;
if (Config.packMinVersion() > 21.39f) return;
boolean has = false;
for (Map.Entry<Key, List<LegacyOverridesModel>> entry : plugin.itemManager().modernItemModels1_21_2().entrySet()) {
has = true;
Key key = entry.getKey();
// 此段代码生成1.21.2专用的item model文件情况非常复杂
for (Map.Entry<Key, List<LegacyOverridesModel>> entry : this.plugin.itemManager().modernItemModels1_21_2().entrySet()) {
Key itemModelPath = entry.getKey();
List<LegacyOverridesModel> legacyOverridesModels = entry.getValue();
boolean first = true;
JsonObject jsonObject = new JsonObject();
JsonArray overrides = new JsonArray();
for (LegacyOverridesModel model : legacyOverridesModels) {
if (first) {
jsonObject.addProperty("parent", model.model());
if (model.hasPredicate()) {
overrides.add(model.toLegacyPredicateElement());
}
first = false;
} else {
overrides.add(model.toLegacyPredicateElement());
}
}
if (!overrides.isEmpty()) {
jsonObject.add("overrides", overrides);
// 检测item model合法性
if (PRESET_MODERN_MODELS_ITEM.containsKey(itemModelPath) || PRESET_LEGACY_MODELS_ITEM.containsKey(itemModelPath)) {
TranslationManager.instance().log("warning.config.resource_pack.item_model.conflict", itemModelPath.asString());
continue;
}
// 要检查目标生成路径是否已经存在模型如果存在模型应该只为其生成overrides
Path itemPath = generatedPackPath
.resolve("assets")
.resolve(key.namespace())
.resolve(itemModelPath.namespace())
.resolve("models")
.resolve("item")
.resolve(key.value() + ".json");
if (Files.exists(itemPath)) {
plugin.logger().warn("Failed to generate item model for [" + key + "] because " + itemPath.toAbsolutePath() + " already exists");
} else {
if (PRESET_MODERN_MODELS_ITEM.containsKey(key) || PRESET_LEGACY_MODELS_ITEM.containsKey(key)) {
plugin.logger().warn("Failed to generate item model for [" + key + "] because it conflicts with vanilla item");
.resolve(itemModelPath.value() + ".json");
boolean modelExists = Files.exists(itemPath);
JsonObject itemJson;
if (modelExists) {
// 路径已经存在了,那么就应该把模型读入
try {
itemJson = GsonHelper.readJsonFile(itemPath).getAsJsonObject();
// 野心真大已经自己写了overrides那么不管你了
if (itemJson.has("overrides")) {
continue;
}
JsonArray overrides = new JsonArray();
for (LegacyOverridesModel legacyOverridesModel : legacyOverridesModels) {
overrides.add(legacyOverridesModel.toLegacyPredicateElement());
}
itemJson.add("overrides", overrides);
} catch (IOException e) {
this.plugin.logger().warn("Failed to read item json " + itemPath.toAbsolutePath());
continue;
}
} else {
// 如果路径不存在则需要我们创建一个json对象并对接model的路径
itemJson = new JsonObject();
LegacyOverridesModel firstModel = legacyOverridesModels.getFirst();
itemJson.addProperty("parent", firstModel.model());
JsonArray overrides = new JsonArray();
for (LegacyOverridesModel legacyOverridesModel : legacyOverridesModels) {
overrides.add(legacyOverridesModel.toLegacyPredicateElement());
}
itemJson.add("overrides", overrides);
}
try {
Files.createDirectories(itemPath.getParent());
} catch (IOException e) {
plugin.logger().severe("Error creating " + itemPath.toAbsolutePath());
plugin.logger().severe("Error creating " + itemPath.toAbsolutePath(), e);
continue;
}
try (BufferedWriter writer = Files.newBufferedWriter(itemPath)) {
GsonHelper.get().toJson(jsonObject, writer);
GsonHelper.get().toJson(itemJson, writer);
} catch (IOException e) {
plugin.logger().warn("Failed to save item model for [" + key + "]");
plugin.logger().warn("Failed to save item model for " + itemModelPath, e);
}
}
// TODO it later
// if (Config.packMinVersion() < 21.19f && has) {
// plugin.logger().warn("You are using 'item-model' component for some models which requires 1.21.2+ client. But the min-supported-version set in 'config.yml' is " + "1." + Config.packMinVersion());
// }
}
private void generateModernItemModels1_21_4(Path generatedPackPath) {

View File

@@ -1,5 +1,8 @@
package net.momirealms.craftengine.core.util;
import java.util.function.Consumer;
import java.util.function.Supplier;
public interface Cancellable {
boolean isCancelled();
@@ -10,6 +13,10 @@ public interface Cancellable {
return new Dummy();
}
static Cancellable of(Supplier<Boolean> isCancelled, Consumer<Boolean> setCancelled) {
return new Simple(isCancelled, setCancelled);
}
class Dummy implements Cancellable {
private boolean cancelled;
@@ -23,4 +30,24 @@ public interface Cancellable {
this.cancelled = cancel;
}
}
class Simple implements Cancellable {
private final Supplier<Boolean> isCancelled;
private final Consumer<Boolean> setCancelled;
public Simple(Supplier<Boolean> isCancelled, Consumer<Boolean> setCancelled) {
this.isCancelled = isCancelled;
this.setCancelled = setCancelled;
}
@Override
public boolean isCancelled() {
return this.isCancelled.get();
}
@Override
public void setCancelled(boolean cancel) {
this.setCancelled.accept(cancel);
}
}
}