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

events读取

This commit is contained in:
XiaoMoMi
2025-05-10 19:12:25 +08:00
parent 7725c3c841
commit d7f8f88106
9 changed files with 70 additions and 22 deletions

View File

@@ -297,4 +297,5 @@ warning.config.conflict_matcher.all_of.missing_terms: "<yellow>Issue found in co
warning.config.conflict_matcher.any_of.missing_terms: "<yellow>Issue found in config.yml at 'resource-pack.duplicated-files-handler' - Missing required 'terms' argument for 'any_of' matcher.</yellow>"
warning.config.conflict_resolution.missing_type: "<yellow>Issue found in config.yml at 'resource-pack.duplicated-files-handler' - Missing required 'type' argument for one of the resolutions.</yellow>"
warning.config.conflict_resolution.invalid_type: "<yellow>Issue found in config.yml at 'resource-pack.duplicated-files-handler' - One of the resolutions is using the invalid type '<arg:0>'.</yellow>"
warning.config.function.command.missing_command: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is missing the required 'command' argument for 'command' function.</yellow>"
warning.config.function.command.missing_command: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is missing the required 'command' argument for 'command' function.</yellow>"
warning.config.event.invalid_trigger: "<yellow>Issue found in file <arg:0> - The config '<arg:1>' is using an invalid event trigger '<arg:2>'.</yellow>"

View File

@@ -26,6 +26,10 @@ import net.momirealms.craftengine.core.pack.model.generation.ModelGeneration;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.plugin.config.Config;
import net.momirealms.craftengine.core.plugin.config.ConfigParser;
import net.momirealms.craftengine.core.plugin.context.PlayerBlockActionContext;
import net.momirealms.craftengine.core.plugin.context.function.Function;
import net.momirealms.craftengine.core.plugin.event.BlockEventFunctions;
import net.momirealms.craftengine.core.plugin.event.EventTrigger;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.plugin.locale.TranslationManager;
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
@@ -421,6 +425,37 @@ public class BukkitBlockManager extends AbstractBlockManager {
}
}
Object eventsObj = ResourceConfigUtils.get(section, "events");
EnumMap<EventTrigger, List<Function<PlayerBlockActionContext>>> events = new EnumMap<>(EventTrigger.class);
if (eventsObj instanceof Map<?, ?> eventsSection) {
Map<String, Object> eventsSectionMap = MiscUtils.castToMap(eventsSection, false);
for (Map.Entry<String, Object> eventEntry : eventsSectionMap.entrySet()) {
try {
EventTrigger eventTrigger = EventTrigger.valueOf(eventEntry.getKey().toUpperCase(Locale.ENGLISH));
if (eventEntry.getValue() instanceof List<?> list) {
if (list.size() == 1) {
events.put(eventTrigger, List.of(BlockEventFunctions.fromMap(MiscUtils.castToMap(list.get(0), false))));
} else if (list.size() == 2) {
events.put(eventTrigger, List.of(
BlockEventFunctions.fromMap(MiscUtils.castToMap(list.get(0), false)),
BlockEventFunctions.fromMap(MiscUtils.castToMap(list.get(1), false))
));
} else {
List<Function<PlayerBlockActionContext>> eventsList = new ArrayList<>();
for (Object event : list) {
eventsList.add(BlockEventFunctions.fromMap(MiscUtils.castToMap(event, false)));
}
events.put(eventTrigger, eventsList);
}
} else if (eventEntry.getValue() instanceof Map<?, ?> eventSection) {
events.put(eventTrigger, List.of(BlockEventFunctions.fromMap(MiscUtils.castToMap(eventSection, false))));
}
} catch (IllegalArgumentException e) {
throw new LocalizedResourceConfigException("warning.config.event.invalid_trigger", eventEntry.getKey());
}
}
}
Map<String, Object> behaviors = MiscUtils.castToMap(section.getOrDefault("behavior", Map.of()), false);
CustomBlock block = BukkitCustomBlock.builder(id)
.appearances(appearances)
@@ -429,6 +464,7 @@ public class BukkitBlockManager extends AbstractBlockManager {
.properties(properties)
.settings(settings)
.behavior(behaviors)
.events(events)
.build();
// bind appearance and real state

View File

@@ -9,6 +9,9 @@ import net.momirealms.craftengine.core.block.*;
import net.momirealms.craftengine.core.block.properties.Property;
import net.momirealms.craftengine.core.loot.LootTable;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.plugin.context.PlayerBlockActionContext;
import net.momirealms.craftengine.core.plugin.context.function.Function;
import net.momirealms.craftengine.core.plugin.event.EventTrigger;
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
import net.momirealms.craftengine.core.registry.Holder;
import net.momirealms.craftengine.core.registry.WritableRegistry;
@@ -19,12 +22,11 @@ import net.momirealms.craftengine.core.util.VersionHelper;
import net.momirealms.craftengine.shared.ObjectHolder;
import net.momirealms.craftengine.shared.block.BlockBehavior;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.reflect.Field;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.*;
public class BukkitCustomBlock extends CustomBlock {
@@ -35,10 +37,11 @@ public class BukkitCustomBlock extends CustomBlock {
Map<String, Integer> appearances,
Map<String, VariantState> variantMapper,
BlockSettings settings,
Map<String, Object> behavior,
@NotNull EnumMap<EventTrigger, List<Function<PlayerBlockActionContext>>> events,
@Nullable Map<String, Object> behavior,
@Nullable LootTable<?> lootTable
) {
super(id, holder, properties, appearances, variantMapper, settings, behavior, lootTable);
super(id, holder, properties, appearances, variantMapper, settings, events, behavior, lootTable);
}
@SuppressWarnings("unchecked")
@@ -158,7 +161,7 @@ public class BukkitCustomBlock extends CustomBlock {
// create or get block holder
Holder.Reference<CustomBlock> holder = BuiltInRegistries.BLOCK.get(id).orElseGet(() ->
((WritableRegistry<CustomBlock>) BuiltInRegistries.BLOCK).registerForHolder(new ResourceKey<>(BuiltInRegistries.BLOCK.key().location(), id)));
return new BukkitCustomBlock(id, holder, properties, appearances, variantMapper, settings, behavior, lootTable);
return new BukkitCustomBlock(id, holder, properties, appearances, variantMapper, settings, events, behavior, lootTable);
}
}
}

View File

@@ -6,6 +6,9 @@ import net.momirealms.craftengine.core.block.properties.Property;
import net.momirealms.craftengine.core.item.context.BlockPlaceContext;
import net.momirealms.craftengine.core.loot.LootTable;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.plugin.context.PlayerBlockActionContext;
import net.momirealms.craftengine.core.plugin.context.function.Function;
import net.momirealms.craftengine.core.plugin.event.EventTrigger;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.registry.Holder;
import net.momirealms.craftengine.core.util.Key;
@@ -15,10 +18,7 @@ import net.momirealms.sparrow.nbt.Tag;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.function.BiFunction;
public abstract class CustomBlock {
@@ -29,6 +29,7 @@ public abstract class CustomBlock {
protected final BlockBehavior behavior;
protected final List<BiFunction<BlockPlaceContext, ImmutableBlockState, ImmutableBlockState>> placements;
protected final ImmutableBlockState defaultState;
protected final EnumMap<EventTrigger, List<Function<PlayerBlockActionContext>>> events;
@Nullable
protected final LootTable<?> lootTable;
@@ -39,6 +40,7 @@ public abstract class CustomBlock {
@NotNull Map<String, Integer> appearances,
@NotNull Map<String, VariantState> variantMapper,
@NotNull BlockSettings settings,
@NotNull EnumMap<EventTrigger, List<Function<PlayerBlockActionContext>>> events,
@Nullable Map<String, Object> behavior,
@Nullable LootTable<?> lootTable
) {
@@ -48,6 +50,7 @@ public abstract class CustomBlock {
this.lootTable = lootTable;
this.properties = properties;
this.placements = new ArrayList<>();
this.events = events;
this.variantProvider = new BlockStateVariantProvider(holder, ImmutableBlockState::new, properties);
this.defaultState = this.variantProvider.getDefaultState();
this.behavior = BlockBehaviors.fromMap(this, behavior);
@@ -168,11 +171,17 @@ public abstract class CustomBlock {
protected BlockSettings settings;
protected Map<String, Object> behavior;
protected LootTable<?> lootTable;
protected EnumMap<EventTrigger, List<Function<PlayerBlockActionContext>>> events;
protected Builder(Key id) {
this.id = id;
}
public Builder events(EnumMap<EventTrigger, List<Function<PlayerBlockActionContext>>> events) {
this.events = events;
return this;
}
public Builder appearances(Map<String, Integer> appearances) {
this.appearances = appearances;
return this;

View File

@@ -1,8 +1,10 @@
package net.momirealms.craftengine.core.block;
import net.momirealms.craftengine.core.plugin.event.EventTrigger;
import net.momirealms.craftengine.core.registry.Holder;
import net.momirealms.craftengine.core.util.Key;
import java.util.EnumMap;
import java.util.Map;
public class EmptyBlock extends CustomBlock {
@@ -10,7 +12,7 @@ public class EmptyBlock extends CustomBlock {
public static ImmutableBlockState STATE;
public EmptyBlock(Key id, Holder.Reference<CustomBlock> holder) {
super(id, holder, Map.of(), Map.of(), Map.of(), BlockSettings.of(), null, null);
super(id, holder, Map.of(), Map.of(), Map.of(), BlockSettings.of(), new EnumMap<>(EventTrigger.class), null, null);
INSTANCE = this;
STATE = defaultState();
}

View File

@@ -1,10 +1,12 @@
package net.momirealms.craftengine.core.block;
import it.unimi.dsi.fastutil.objects.Reference2ObjectArrayMap;
import net.momirealms.craftengine.core.plugin.event.EventTrigger;
import net.momirealms.craftengine.core.registry.Holder;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.sparrow.nbt.CompoundTag;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
@@ -12,7 +14,7 @@ public class InactiveCustomBlock extends CustomBlock {
private final Map<CompoundTag, ImmutableBlockState> cachedData = new HashMap<>();
public InactiveCustomBlock(Key id, Holder.Reference<CustomBlock> holder) {
super(id, holder, Map.of(), Map.of(), Map.of(), BlockSettings.of(), null, null);
super(id, holder, Map.of(), Map.of(), Map.of(), BlockSettings.of(), new EnumMap<>(EventTrigger.class), null, null);
}
@Override

View File

@@ -28,7 +28,7 @@ public class ItemBehaviors {
Key key = Key.withDefaultNamespace(type, Key.DEFAULT_NAMESPACE);
ItemBehaviorFactory factory = BuiltInRegistries.ITEM_BEHAVIOR_FACTORY.getValue(key);
if (factory == null) {
throw new LocalizedResourceConfigException("warning.config.item.behavior.invalid_type", type.toString());
throw new LocalizedResourceConfigException("warning.config.item.behavior.invalid_type", type);
}
return factory.create(pack, path, id, map);
}

View File

@@ -4,5 +4,5 @@ public enum EventTrigger {
USE_ITEM,
USE_ITEM_ON,
CONSUME,
BREAK
}

View File

@@ -185,17 +185,12 @@ public class MCUtils {
return next;
}
public static int floor(float value) {
int i = (int) value;
return value < (float)i ? i - 1 : i;
}
public static byte packDegrees(float degrees) {
return (byte)floor(degrees * 256.0F / 360.0F);
return (byte) fastFloor(degrees * 256.0F / 360.0F);
}
public static float unpackDegrees(byte degrees) {
return (float)(degrees * 360) / 256.0F;
return (float) (degrees * 360) / 256.0F;
}
public static int clamp(int value, int min, int max) {