mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-22 16:39:28 +00:00
增强1.21.2的item model生成
This commit is contained in:
@@ -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.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_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.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>"
|
||||||
@@ -132,18 +132,17 @@ public class BlockEventListener implements Listener {
|
|||||||
if (itemInHand != null) {
|
if (itemInHand != null) {
|
||||||
Optional<CustomItem<ItemStack>> optionalCustomItem = itemInHand.getCustomItem();
|
Optional<CustomItem<ItemStack>> optionalCustomItem = itemInHand.getCustomItem();
|
||||||
if (optionalCustomItem.isPresent()) {
|
if (optionalCustomItem.isPresent()) {
|
||||||
Cancellable dummy = Cancellable.dummy();
|
Cancellable cancellable = Cancellable.of(event::isCancelled, event::setCancelled);
|
||||||
optionalCustomItem.get().execute(
|
optionalCustomItem.get().execute(
|
||||||
PlayerOptionalContext.of(serverPlayer, ContextHolder.builder()
|
PlayerOptionalContext.of(serverPlayer, ContextHolder.builder()
|
||||||
.withParameter(DirectContextParameters.BLOCK, new BukkitBlockInWorld(block))
|
.withParameter(DirectContextParameters.BLOCK, new BukkitBlockInWorld(block))
|
||||||
.withParameter(DirectContextParameters.POSITION, position)
|
.withParameter(DirectContextParameters.POSITION, position)
|
||||||
.withParameter(DirectContextParameters.PLAYER, serverPlayer)
|
.withParameter(DirectContextParameters.PLAYER, serverPlayer)
|
||||||
.withParameter(DirectContextParameters.EVENT, dummy)
|
.withParameter(DirectContextParameters.EVENT, cancellable)
|
||||||
.withOptionalParameter(DirectContextParameters.ITEM_IN_HAND, itemInHand)
|
.withOptionalParameter(DirectContextParameters.ITEM_IN_HAND, itemInHand)
|
||||||
), EventTrigger.BREAK
|
), EventTrigger.BREAK
|
||||||
);
|
);
|
||||||
if (dummy.isCancelled()) {
|
if (cancellable.isCancelled()) {
|
||||||
event.setCancelled(true);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -166,7 +165,7 @@ public class BlockEventListener implements Listener {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// execute functions
|
// execute functions
|
||||||
Cancellable cancellable = Cancellable.dummy();
|
Cancellable cancellable = Cancellable.of(event::isCancelled, event::setCancelled);
|
||||||
PlayerOptionalContext context = PlayerOptionalContext.of(serverPlayer, ContextHolder.builder()
|
PlayerOptionalContext context = PlayerOptionalContext.of(serverPlayer, ContextHolder.builder()
|
||||||
.withParameter(DirectContextParameters.BLOCK, new BukkitBlockInWorld(block))
|
.withParameter(DirectContextParameters.BLOCK, new BukkitBlockInWorld(block))
|
||||||
.withParameter(DirectContextParameters.BLOCK_STATE, state)
|
.withParameter(DirectContextParameters.BLOCK_STATE, state)
|
||||||
@@ -176,7 +175,6 @@ public class BlockEventListener implements Listener {
|
|||||||
);
|
);
|
||||||
state.owner().value().execute(context, EventTrigger.BREAK);
|
state.owner().value().execute(context, EventTrigger.BREAK);
|
||||||
if (cancellable.isCancelled()) {
|
if (cancellable.isCancelled()) {
|
||||||
event.setCancelled(true);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package net.momirealms.craftengine.bukkit.entity.data;
|
|||||||
import net.momirealms.craftengine.core.util.VersionHelper;
|
import net.momirealms.craftengine.core.util.VersionHelper;
|
||||||
|
|
||||||
public class InteractionEntityData<T> extends BaseEntityData<T> {
|
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> Width = of(8, EntityDataValue.Serializers$FLOAT, 1F);
|
||||||
public static final InteractionEntityData<Float> Height = of(9, 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);
|
public static final InteractionEntityData<Boolean> Responsive = of(10, EntityDataValue.Serializers$BOOLEAN, false);
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package net.momirealms.craftengine.bukkit.entity.data;
|
|||||||
import net.momirealms.craftengine.bukkit.util.Reflections;
|
import net.momirealms.craftengine.bukkit.util.Reflections;
|
||||||
|
|
||||||
public class TextDisplayEntityData<T> extends DisplayEntityData<T> {
|
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<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> LineWidth = of(24, EntityDataValue.Serializers$INT, 200);
|
||||||
public static final DisplayEntityData<Integer> BackgroundColor = of(25, EntityDataValue.Serializers$INT, 0x40000000);
|
public static final DisplayEntityData<Integer> BackgroundColor = of(25, EntityDataValue.Serializers$INT, 0x40000000);
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import net.momirealms.craftengine.bukkit.util.Reflections;
|
|||||||
public class ThrowableItemProjectileData<T> extends BaseEntityData<T> {
|
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 static final ThrowableItemProjectileData<Object> ItemStack = new ThrowableItemProjectileData<>(8, EntityDataValue.Serializers$ITEM_STACK, Reflections.instance$ItemStack$Air);
|
||||||
|
|
||||||
|
|
||||||
public ThrowableItemProjectileData(int id, Object serializer, T defaultValue) {
|
public ThrowableItemProjectileData(int id, Object serializer, T defaultValue) {
|
||||||
super(id, serializer, defaultValue);
|
super(id, serializer, defaultValue);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -560,13 +560,13 @@ public abstract class AbstractPackManager implements PackManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.generateFonts(generatedPackPath);
|
this.generateFonts(generatedPackPath);
|
||||||
|
this.generateItemModels(generatedPackPath, this.plugin.itemManager());
|
||||||
|
this.generateItemModels(generatedPackPath, this.plugin.blockManager());
|
||||||
|
this.generateBlockOverrides(generatedPackPath);
|
||||||
this.generateLegacyItemOverrides(generatedPackPath);
|
this.generateLegacyItemOverrides(generatedPackPath);
|
||||||
this.generateModernItemOverrides(generatedPackPath);
|
this.generateModernItemOverrides(generatedPackPath);
|
||||||
this.generateModernItemModels1_21_2(generatedPackPath);
|
this.generateModernItemModels1_21_2(generatedPackPath);
|
||||||
this.generateModernItemModels1_21_4(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.generateOverrideSounds(generatedPackPath);
|
||||||
this.generateCustomSounds(generatedPackPath);
|
this.generateCustomSounds(generatedPackPath);
|
||||||
this.generateClientLang(generatedPackPath);
|
this.generateClientLang(generatedPackPath);
|
||||||
@@ -911,60 +911,67 @@ public abstract class AbstractPackManager implements PackManager {
|
|||||||
if (Config.packMaxVersion() < 21.19f) return;
|
if (Config.packMaxVersion() < 21.19f) return;
|
||||||
if (Config.packMinVersion() > 21.39f) return;
|
if (Config.packMinVersion() > 21.39f) return;
|
||||||
|
|
||||||
boolean has = false;
|
// 此段代码生成1.21.2专用的item model文件,情况非常复杂!
|
||||||
for (Map.Entry<Key, List<LegacyOverridesModel>> entry : plugin.itemManager().modernItemModels1_21_2().entrySet()) {
|
for (Map.Entry<Key, List<LegacyOverridesModel>> entry : this.plugin.itemManager().modernItemModels1_21_2().entrySet()) {
|
||||||
has = true;
|
Key itemModelPath = entry.getKey();
|
||||||
Key key = entry.getKey();
|
|
||||||
List<LegacyOverridesModel> legacyOverridesModels = entry.getValue();
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
Path itemPath = generatedPackPath
|
// 检测item model合法性
|
||||||
.resolve("assets")
|
if (PRESET_MODERN_MODELS_ITEM.containsKey(itemModelPath) || PRESET_LEGACY_MODELS_ITEM.containsKey(itemModelPath)) {
|
||||||
.resolve(key.namespace())
|
TranslationManager.instance().log("warning.config.resource_pack.item_model.conflict", itemModelPath.asString());
|
||||||
.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");
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 要检查目标生成路径是否已经存在模型,如果存在模型,应该只为其生成overrides
|
||||||
|
Path itemPath = generatedPackPath
|
||||||
|
.resolve("assets")
|
||||||
|
.resolve(itemModelPath.namespace())
|
||||||
|
.resolve("models")
|
||||||
|
.resolve("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 {
|
try {
|
||||||
Files.createDirectories(itemPath.getParent());
|
Files.createDirectories(itemPath.getParent());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
plugin.logger().severe("Error creating " + itemPath.toAbsolutePath());
|
plugin.logger().severe("Error creating " + itemPath.toAbsolutePath(), e);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
try (BufferedWriter writer = Files.newBufferedWriter(itemPath)) {
|
try (BufferedWriter writer = Files.newBufferedWriter(itemPath)) {
|
||||||
GsonHelper.get().toJson(jsonObject, writer);
|
GsonHelper.get().toJson(itemJson, writer);
|
||||||
} catch (IOException e) {
|
} 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) {
|
private void generateModernItemModels1_21_4(Path generatedPackPath) {
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
package net.momirealms.craftengine.core.util;
|
package net.momirealms.craftengine.core.util;
|
||||||
|
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
public interface Cancellable {
|
public interface Cancellable {
|
||||||
|
|
||||||
boolean isCancelled();
|
boolean isCancelled();
|
||||||
@@ -10,6 +13,10 @@ public interface Cancellable {
|
|||||||
return new Dummy();
|
return new Dummy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Cancellable of(Supplier<Boolean> isCancelled, Consumer<Boolean> setCancelled) {
|
||||||
|
return new Simple(isCancelled, setCancelled);
|
||||||
|
}
|
||||||
|
|
||||||
class Dummy implements Cancellable {
|
class Dummy implements Cancellable {
|
||||||
private boolean cancelled;
|
private boolean cancelled;
|
||||||
|
|
||||||
@@ -23,4 +30,24 @@ public interface Cancellable {
|
|||||||
this.cancelled = cancel;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user