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

局部修改

This commit is contained in:
XiaoMoMi
2025-06-28 17:20:37 +08:00
parent 225cc7e067
commit 83bd27e774
58 changed files with 255 additions and 341 deletions

View File

@@ -6,9 +6,9 @@ import ch.njol.skript.classes.Serializer;
import ch.njol.skript.lang.ParseContext;
import ch.njol.skript.registrations.Classes;
import ch.njol.yggdrasil.Fields;
import net.momirealms.craftengine.core.block.BlockStateParser;
import net.momirealms.craftengine.core.block.ImmutableBlockState;
import net.momirealms.craftengine.core.block.UnsafeBlockStateMatcher;
import net.momirealms.craftengine.core.block.parser.BlockStateParser;
import org.jetbrains.annotations.Nullable;
import java.io.StreamCorruptedException;

View File

@@ -9,8 +9,8 @@ import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockType;
import com.sk89q.worldedit.world.block.BlockTypes;
import net.momirealms.craftengine.core.block.AbstractBlockManager;
import net.momirealms.craftengine.core.block.BlockStateParser;
import net.momirealms.craftengine.core.block.ImmutableBlockState;
import net.momirealms.craftengine.core.block.parser.BlockStateParser;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.ReflectionUtils;
import org.bukkit.Material;

View File

@@ -116,7 +116,7 @@ public final class CraftEngineBlocks {
if (success) {
FastNMS.INSTANCE.method$BlockStateBase$onPlace(blockState, worldServer, blockPos, oldBlockState, false);
if (playSound) {
SoundData data = block.sounds().placeSound();
SoundData data = block.settings().sounds().placeSound();
location.getWorld().playSound(location, data.id().toString(), SoundCategory.BLOCKS, data.volume().get(), data.pitch().get());
}
}
@@ -181,7 +181,7 @@ public final class CraftEngineBlocks {
}
}
if (playSound) {
world.playBlockSound(position, state.sounds().breakSound());
world.playBlockSound(position, state.settings().sounds().breakSound());
}
if (sendParticles) {
FastNMS.INSTANCE.method$Level$levelEvent(world.serverWorld(), WorldEvents.BLOCK_BREAK_EFFECT, LocationUtils.toBlockPos(location.getBlockX(), location.getBlockY(), location.getBlockZ()), state.customBlockState().registryId());

View File

@@ -41,7 +41,7 @@ import org.bukkit.inventory.ItemStack;
import java.util.Optional;
public class BlockEventListener implements Listener {
public final class BlockEventListener implements Listener {
private final BukkitCraftEngine plugin;
private final boolean enableNoteBlockCheck;
private final BukkitBlockManager manager;
@@ -175,7 +175,7 @@ public class BlockEventListener implements Listener {
}
// play sound
world.playBlockSound(position, state.sounds().breakSound());
world.playBlockSound(position, state.settings().sounds().breakSound());
}
} else {
// override vanilla block loots
@@ -263,7 +263,7 @@ public class BlockEventListener implements Listener {
if (cancellable.isCancelled()) {
return;
}
player.playSound(location, state.sounds().stepSound().id().toString(), SoundCategory.BLOCKS, state.sounds().stepSound().volume().get(), state.sounds().stepSound().pitch().get());
player.playSound(location, state.settings().sounds().stepSound().id().toString(), SoundCategory.BLOCKS, state.settings().sounds().stepSound().volume().get(), state.settings().sounds().stepSound().pitch().get());
} else if (Config.enableSoundSystem()) {
Object ownerBlock = BlockStateUtils.getBlockOwner(blockState);
if (this.manager.isBlockSoundRemoved(ownerBlock)) {

View File

@@ -25,6 +25,8 @@ import net.momirealms.craftengine.bukkit.util.KeyUtils;
import net.momirealms.craftengine.bukkit.util.RegistryUtils;
import net.momirealms.craftengine.bukkit.util.TagUtils;
import net.momirealms.craftengine.core.block.*;
import net.momirealms.craftengine.core.block.behavior.EmptyBlockBehavior;
import net.momirealms.craftengine.core.block.parser.BlockStateParser;
import net.momirealms.craftengine.core.block.properties.Properties;
import net.momirealms.craftengine.core.block.properties.Property;
import net.momirealms.craftengine.core.loot.LootTable;
@@ -65,7 +67,7 @@ import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
public class BukkitBlockManager extends AbstractBlockManager {
public final class BukkitBlockManager extends AbstractBlockManager {
private static BukkitBlockManager instance;
private final BukkitCraftEngine plugin;
private final BlockParser blockParser;
@@ -75,6 +77,7 @@ public class BukkitBlockManager extends AbstractBlockManager {
// Minecraft objects
// Cached new blocks $ holders
private Map<Key, Integer> internalId2StateId;
private Map<Key, DelegatingBlock> registeredBlocks;
private Map<Integer, Object> stateId2BlockHolder;
// This map is used to change the block states that are not necessarily needed into a certain block state
private Map<Integer, Integer> blockAppearanceMapper;
@@ -93,7 +96,7 @@ public class BukkitBlockManager extends AbstractBlockManager {
private BlockEventListener blockEventListener;
private FallingBlockRemoveListener fallingBlockRemoveListener;
// cached tag packet
protected Object cachedUpdateTagsPacket;
Object cachedUpdateTagsPacket;
private final List<Tuple<Object, Key, Boolean>> blocksToDeceive = new ArrayList<>();
@@ -144,6 +147,12 @@ public class BukkitBlockManager extends AbstractBlockManager {
super.unload();
if (EmptyBlock.STATE != null)
Arrays.fill(this.stateId2ImmutableBlockStates, EmptyBlock.STATE);
for (DelegatingBlock block : this.registeredBlocks.values()) {
block.behaviorDelegate().bindValue(EmptyBlockBehavior.INSTANCE);
block.shapeDelegate().bindValue(BukkitBlockShape.STONE);
DelegatingBlockState state = (DelegatingBlockState) FastNMS.INSTANCE.method$Block$defaultState(block);
state.setBlockState(null);
}
}
@Override
@@ -201,7 +210,7 @@ public class BukkitBlockManager extends AbstractBlockManager {
@Nullable
public Object getMinecraftBlockHolder(int stateId) {
return stateId2BlockHolder.get(stateId);
return this.stateId2BlockHolder.get(stateId);
}
@NotNull
@@ -297,6 +306,7 @@ public class BukkitBlockManager extends AbstractBlockManager {
ImmutableMap.Builder<Key, Integer> builder1 = ImmutableMap.builder();
ImmutableMap.Builder<Integer, Object> builder2 = ImmutableMap.builder();
ImmutableMap.Builder<Key, List<Integer>> builder3 = ImmutableMap.builder();
ImmutableMap.Builder<Key, DelegatingBlock> builder4 = ImmutableMap.builder();
Set<Object> affectedBlockSounds = new HashSet<>();
Map<Object, Pair<SoundData, SoundData>> affectedDoors = new IdentityHashMap<>();
Set<Object> affectedBlocks = new HashSet<>();
@@ -306,7 +316,7 @@ public class BukkitBlockManager extends AbstractBlockManager {
int counter = 0;
for (Map.Entry<Key, Integer> baseBlockAndItsCount : this.registeredRealBlockSlots.entrySet()) {
counter = registerBlockVariants(baseBlockAndItsCount, counter, builder1, builder2, builder3, affectedBlockSounds, order);
counter = registerBlockVariants(baseBlockAndItsCount, counter, builder1, builder2, builder3, builder4, affectedBlockSounds, order);
}
freezeRegistry();
@@ -315,6 +325,7 @@ public class BukkitBlockManager extends AbstractBlockManager {
this.internalId2StateId = builder1.build();
this.stateId2BlockHolder = builder2.build();
this.realBlockArranger = builder3.build();
this.registeredBlocks = builder4.build();
this.blockRegisterOrder = ImmutableList.copyOf(order);
for (Object block : (Iterable<Object>) MBuiltInRegistries.BLOCK) {
@@ -420,7 +431,7 @@ public class BukkitBlockManager extends AbstractBlockManager {
// read states
Map<String, Property<?>> properties;
Map<String, Integer> appearances;
Map<String, VariantState> variants;
Map<String, BlockStateVariant> variants;
Map<String, Object> stateSection = MiscUtils.castToMap(ResourceConfigUtils.requireNonNullOrThrow(ResourceConfigUtils.get(section, "state", "states"), "warning.config.block.missing_state"), true);
boolean singleState = !stateSection.containsKey("properties");
@@ -435,7 +446,7 @@ public class BukkitBlockManager extends AbstractBlockManager {
if (internalBlockRegistryId == -1) {
throw new LocalizedResourceConfigException("warning.config.block.state.invalid_real_id", internalBlockId.toString(), String.valueOf(availableAppearances(vanillaBlock.type()) - 1));
}
variants = Map.of("", new VariantState("", settings, internalBlockRegistryId));
variants = Map.of("", new BlockStateVariant("", settings, internalBlockRegistryId));
} else {
// properties
properties = getProperties(MiscUtils.castToMap(ResourceConfigUtils.requireNonNullOrThrow(stateSection.get("properties"), "warning.config.block.state.missing_properties"), true));
@@ -467,7 +478,7 @@ public class BukkitBlockManager extends AbstractBlockManager {
throw new LocalizedResourceConfigException("warning.config.block.state.invalid_real_id", internalBlockId.toString(), String.valueOf(availableAppearances(baseBlock) - 1));
}
Map<String, Object> anotherSetting = MiscUtils.castToMap(variantSection.get("settings"), true);
variants.put(variantNBT, new VariantState(appearance, anotherSetting == null ? settings : BlockSettings.ofFullCopy(settings, anotherSetting), internalBlockRegistryId));
variants.put(variantNBT, new BlockStateVariant(appearance, anotherSetting == null ? settings : BlockSettings.ofFullCopy(settings, anotherSetting), internalBlockRegistryId));
}
}
}
@@ -544,6 +555,9 @@ public class BukkitBlockManager extends AbstractBlockManager {
return new VanillaBlockState(blockId, propertyNBT, vanillaBlockStateRegistryId);
}
public record VanillaBlockState(Key type, String properties, int registryId) {
}
private JsonObject getVariantModel(Map<String, Object> singleModelMap) {
JsonObject json = new JsonObject();
String modelPath = ResourceConfigUtils.requireNonEmptyStringOrThrow(singleModelMap.get("path"), "warning.config.block.state.model.missing_path");
@@ -763,6 +777,7 @@ public class BukkitBlockManager extends AbstractBlockManager {
ImmutableMap.Builder<Key, Integer> builder1,
ImmutableMap.Builder<Integer, Object> builder2,
ImmutableMap.Builder<Key, List<Integer>> builder3,
ImmutableMap.Builder<Key, DelegatingBlock> builder4,
Set<Object> affectSoundTypes,
List<Key> order) throws Exception {
Key clientSideBlockType = blockWithCount.getKey();
@@ -803,6 +818,7 @@ public class BukkitBlockManager extends AbstractBlockManager {
builder1.put(realBlockKey, stateId);
builder2.put(stateId, blockHolder);
builder4.put(realBlockKey, (DelegatingBlock) newRealBlock);
stateIds.add(stateId);
this.blocksToDeceive.add(Tuple.of(newRealBlock, clientSideBlockType, isNoteBlock));

View File

@@ -1,10 +1,12 @@
package net.momirealms.craftengine.bukkit.block;
import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MBlocks;
import net.momirealms.craftengine.core.block.BlockShape;
import org.jetbrains.annotations.Nullable;
public class BukkitBlockShape implements BlockShape {
public final class BukkitBlockShape implements BlockShape {
public static final BukkitBlockShape STONE = new BukkitBlockShape(MBlocks.STONE$defaultState, null);
private final Object rawBlockState;
private final Object supportBlockState;

View File

@@ -13,6 +13,7 @@ import net.momirealms.craftengine.bukkit.util.SoundUtils;
import net.momirealms.craftengine.core.block.*;
import net.momirealms.craftengine.core.block.behavior.AbstractBlockBehavior;
import net.momirealms.craftengine.core.block.behavior.BlockBehaviors;
import net.momirealms.craftengine.core.block.behavior.EmptyBlockBehavior;
import net.momirealms.craftengine.core.block.properties.Property;
import net.momirealms.craftengine.core.loot.LootTable;
import net.momirealms.craftengine.core.plugin.CraftEngine;
@@ -30,16 +31,16 @@ import org.jetbrains.annotations.Nullable;
import java.util.*;
public class BukkitCustomBlock extends AbstractCustomBlock {
public final class BukkitCustomBlock extends AbstractCustomBlock {
private static final Object ALWAYS_FALSE = FastNMS.INSTANCE.method$StatePredicate$always(false);
private static final Object ALWAYS_TRUE = FastNMS.INSTANCE.method$StatePredicate$always(true);
protected BukkitCustomBlock(
private BukkitCustomBlock(
@NotNull Key id,
@NotNull Holder.Reference<CustomBlock> holder,
@NotNull Map<String, Property<?>> properties,
@NotNull Map<String, Integer> appearances,
@NotNull Map<String, VariantState> variantMapper,
@NotNull Map<String, BlockStateVariant> variantMapper,
@NotNull BlockSettings settings,
@NotNull Map<EventTrigger, List<Function<PlayerOptionalContext>>> events,
@Nullable List<Map<String, Object>> behavior,
@@ -81,8 +82,8 @@ public class BukkitCustomBlock extends AbstractCustomBlock {
CraftEngine.instance().logger().warn("Could not find custom block immutableBlockState for " + immutableBlockState + ". This might cause errors!");
continue;
}
CustomBlockStateHolder nmsState = (CustomBlockStateHolder) immutableBlockState.customBlockState().handle();
nmsState.setCustomBlockState(immutableBlockState);
DelegatingBlockState nmsState = (DelegatingBlockState) immutableBlockState.customBlockState().handle();
nmsState.setBlockState(immutableBlockState);
BlockSettings settings = immutableBlockState.settings();
// set block properties
@@ -108,8 +109,8 @@ public class BukkitCustomBlock extends AbstractCustomBlock {
CoreReflections.field$BlockStateBase$isViewBlocking.set(nmsState, settings.isViewBlocking() == Tristate.UNDEFINED ? settings.isSuffocating().asBoolean() ? ALWAYS_TRUE : ALWAYS_FALSE : (settings.isViewBlocking().asBoolean() ? ALWAYS_TRUE : ALWAYS_FALSE));
// set parent block properties
CraftEngineNMSBlock nmsBlock = (CraftEngineNMSBlock) BlockStateUtils.getBlockOwner(nmsState);
ObjectHolder<BlockShape> shapeHolder = nmsBlock.getShapeHolder();
DelegatingBlock nmsBlock = (DelegatingBlock) BlockStateUtils.getBlockOwner(nmsState);
ObjectHolder<BlockShape> shapeHolder = nmsBlock.shapeDelegate();
shapeHolder.bindValue(new BukkitBlockShape(immutableBlockState.vanillaBlockState().handle(), Optional.ofNullable(immutableBlockState.settings().supportShapeBlockState()).map(it -> {
try {
Object blockState = BlockStateUtils.blockDataToBlockState(Bukkit.createBlockData(it));
@@ -123,7 +124,7 @@ public class BukkitCustomBlock extends AbstractCustomBlock {
}
}).orElse(null)));
// bind behavior
ObjectHolder<BlockBehavior> behaviorHolder = nmsBlock.getBehaviorHolder();
ObjectHolder<BlockBehavior> behaviorHolder = nmsBlock.behaviorDelegate();
behaviorHolder.bindValue(super.behavior);
// set block side properties
CoreReflections.field$BlockBehaviour$explosionResistance.set(nmsBlock, settings.resistance());
@@ -196,7 +197,7 @@ public class BukkitCustomBlock extends AbstractCustomBlock {
protected final Key id;
protected Map<String, Property<?>> properties;
protected Map<String, Integer> appearances;
protected Map<String, VariantState> variantMapper;
protected Map<String, BlockStateVariant> variantMapper;
protected BlockSettings settings;
protected List<Map<String, Object>> behavior;
protected LootTable<?> lootTable;
@@ -243,7 +244,7 @@ public class BukkitCustomBlock extends AbstractCustomBlock {
}
@Override
public Builder variantMapper(Map<String, VariantState> variantMapper) {
public Builder variantMapper(Map<String, BlockStateVariant> variantMapper) {
this.variantMapper = variantMapper;
return this;
}

View File

@@ -17,7 +17,7 @@ import org.bukkit.event.Listener;
import java.util.Optional;
@SuppressWarnings("DuplicatedCode")
public class FallingBlockRemoveListener implements Listener {
public final class FallingBlockRemoveListener implements Listener {
@EventHandler
public void onFallingBlockBreak(org.bukkit.event.entity.EntityRemoveEvent event) {
@@ -41,7 +41,7 @@ public class FallingBlockRemoveListener implements Listener {
Object entityData = CoreReflections.field$Entity$entityData.get(fallingBlockEntity);
boolean isSilent = (boolean) CoreReflections.method$SynchedEntityData$get.invoke(entityData, CoreReflections.instance$Entity$DATA_SILENT);
if (!isSilent) {
world.playBlockSound(position, customState.sounds().destroySound());
world.playBlockSound(position, customState.settings().sounds().destroySound());
}
} catch (ReflectiveOperationException e) {
CraftEngine.instance().logger().warn("Failed to handle EntityRemoveEvent", e);

View File

@@ -34,7 +34,7 @@ public abstract class AbstractCanSurviveBlockBehavior extends BukkitBlockBehavio
if (!customState.isEmpty() && customState.owner().value() == this.customBlock) {
net.momirealms.craftengine.core.world.World world = new BukkitWorld(FastNMS.INSTANCE.method$Level$getCraftWorld(level));
WorldPosition position = new WorldPosition(world, Vec3d.atCenterOf(LocationUtils.fromBlockPos(blockPos)));
world.playBlockSound(position, customState.sounds().breakSound());
world.playBlockSound(position, customState.settings().sounds().breakSound());
FastNMS.INSTANCE.method$Level$destroyBlock(level, blockPos, true);
}
});
@@ -74,7 +74,7 @@ public abstract class AbstractCanSurviveBlockBehavior extends BukkitBlockBehavio
ImmutableBlockState customState = optionalCustomState.get();
net.momirealms.craftengine.core.world.World world = new BukkitWorld(FastNMS.INSTANCE.method$Level$getCraftWorld(level));
WorldPosition position = new WorldPosition(world, Vec3d.atCenterOf(pos));
world.playBlockSound(position, customState.sounds().breakSound());
world.playBlockSound(position, customState.settings().sounds().breakSound());
FastNMS.INSTANCE.method$Level$levelEvent(level, WorldEvents.BLOCK_BREAK_EFFECT, blockPos, customState.customBlockState().registryId());
return MBlocks.AIR$defaultState;
}

View File

@@ -1,7 +1,7 @@
package net.momirealms.craftengine.bukkit.block.behavior;
import net.momirealms.craftengine.core.block.EmptyBlockBehavior;
import net.momirealms.craftengine.core.block.behavior.BlockBehaviors;
import net.momirealms.craftengine.core.block.behavior.EmptyBlockBehavior;
import net.momirealms.craftengine.core.util.Key;
public class BukkitBlockBehaviors extends BlockBehaviors {

View File

@@ -110,7 +110,7 @@ public class DoorBlockBehavior extends AbstractCanSurviveBlockBehavior {
BlockPos pos = LocationUtils.fromBlockPos(blockPos);
net.momirealms.craftengine.core.world.World world = new BukkitWorld(FastNMS.INSTANCE.method$Level$getCraftWorld(level));
WorldPosition position = new WorldPosition(world, Vec3d.atCenterOf(pos));
world.playBlockSound(position, customState.sounds().breakSound());
world.playBlockSound(position, customState.settings().sounds().breakSound());
FastNMS.INSTANCE.method$Level$levelEvent(level, WorldEvents.BLOCK_BREAK_EFFECT, blockPos, customState.customBlockState().registryId());
return MBlocks.AIR$defaultState;
}

View File

@@ -97,7 +97,7 @@ public class FallingBlockBehavior extends BukkitBlockBehavior {
Object entityData = CoreReflections.field$Entity$entityData.get(fallingBlockEntity);
boolean isSilent = (boolean) CoreReflections.method$SynchedEntityData$get.invoke(entityData, CoreReflections.instance$Entity$DATA_SILENT);
if (!isSilent) {
world.playBlockSound(position, customState.sounds().destroySound());
world.playBlockSound(position, customState.settings().sounds().destroySound());
}
}
@@ -114,7 +114,7 @@ public class FallingBlockBehavior extends BukkitBlockBehavior {
if (immutableBlockState == null || immutableBlockState.isEmpty()) return;
if (!isSilent) {
net.momirealms.craftengine.core.world.World world = new BukkitWorld(FastNMS.INSTANCE.method$Level$getCraftWorld(level));
world.playBlockSound(Vec3d.atCenterOf(LocationUtils.fromBlockPos(pos)), immutableBlockState.sounds().landSound());
world.playBlockSound(Vec3d.atCenterOf(LocationUtils.fromBlockPos(pos)), immutableBlockState.settings().sounds().landSound());
}
}

View File

@@ -69,7 +69,7 @@ public class PressurePlateBlockBehavior extends BukkitBlockBehavior {
BlockPos pos = LocationUtils.fromBlockPos(blockPos);
net.momirealms.craftengine.core.world.World world = new BukkitWorld(FastNMS.INSTANCE.method$Level$getCraftWorld(level));
WorldPosition position = new WorldPosition(world, Vec3d.atCenterOf(pos));
world.playBlockSound(position, customState.sounds().breakSound());
world.playBlockSound(position, customState.settings().sounds().breakSound());
FastNMS.INSTANCE.method$Level$levelEvent(level, WorldEvents.BLOCK_BREAK_EFFECT, blockPos, customState.customBlockState().registryId());
return MBlocks.AIR$defaultState;
}

View File

@@ -163,7 +163,7 @@ public class BlockItemBehavior extends BlockBoundItemBehavior {
block.setPlacedBy(context, blockStateToPlace);
player.swingHand(context.getHand());
context.getLevel().playBlockSound(position, blockStateToPlace.sounds().placeSound());
context.getLevel().playBlockSound(position, blockStateToPlace.settings().sounds().placeSound());
world.sendGameEvent(bukkitPlayer, GameEvent.BLOCK_PLACE, new Vector(pos.x(), pos.y(), pos.z()));
return InteractionResult.SUCCESS;
}

View File

@@ -117,7 +117,7 @@ public class FurnitureItemBehavior extends ItemBehavior {
}
}
if (!BukkitCraftEngine.instance().antiGrief().canPlace(bukkitPlayer, furnitureLocation)) {
if (!BukkitCraftEngine.instance().antiGriefProvider().canPlace(bukkitPlayer, furnitureLocation)) {
return InteractionResult.FAIL;
}

View File

@@ -366,7 +366,7 @@ public class BukkitCraftEngine extends CraftEngine {
);
}
public AntiGriefLib antiGrief() {
public AntiGriefLib antiGriefProvider() {
if (this.antiGrief == null) {
this.antiGrief = AntiGriefLib.builder(this.javaPlugin)
.ignoreOP(true)

View File

@@ -2,8 +2,8 @@ package net.momirealms.craftengine.bukkit.plugin.command.feature;
import net.momirealms.craftengine.bukkit.plugin.command.BukkitCommandFeature;
import net.momirealms.craftengine.bukkit.util.BlockStateUtils;
import net.momirealms.craftengine.core.block.BlockStateParser;
import net.momirealms.craftengine.core.block.ImmutableBlockState;
import net.momirealms.craftengine.core.block.parser.BlockStateParser;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.plugin.command.CraftEngineCommandManager;
import net.momirealms.craftengine.core.plugin.command.FlagKeys;

View File

@@ -2,9 +2,9 @@ package net.momirealms.craftengine.bukkit.plugin.command.feature;
import net.momirealms.craftengine.bukkit.api.CraftEngineBlocks;
import net.momirealms.craftengine.bukkit.plugin.command.BukkitCommandFeature;
import net.momirealms.craftengine.core.block.BlockStateParser;
import net.momirealms.craftengine.core.block.ImmutableBlockState;
import net.momirealms.craftengine.core.block.UpdateOption;
import net.momirealms.craftengine.core.block.parser.BlockStateParser;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.plugin.command.CraftEngineCommandManager;
import org.bukkit.Location;

View File

@@ -18,7 +18,11 @@ import net.momirealms.craftengine.bukkit.nms.FastNMS;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflections;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MBlocks;
import net.momirealms.craftengine.bukkit.util.NoteBlockChainUpdateUtils;
import net.momirealms.craftengine.core.block.*;
import net.momirealms.craftengine.core.block.BlockBehavior;
import net.momirealms.craftengine.core.block.BlockKeys;
import net.momirealms.craftengine.core.block.BlockShape;
import net.momirealms.craftengine.core.block.DelegatingBlock;
import net.momirealms.craftengine.core.block.behavior.EmptyBlockBehavior;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.plugin.config.Config;
import net.momirealms.craftengine.core.util.Key;
@@ -55,14 +59,14 @@ public final class BlockGenerator {
.defineField("isClientSideNoteBlock", boolean.class, Visibility.PUBLIC)
.defineField("isClientSideTripwire", boolean.class, Visibility.PUBLIC)
// should always implement this interface
.implement(DelegatingBlock.class)
.implement(CoreReflections.clazz$Fallable)
.implement(CoreReflections.clazz$BonemealableBlock)
.implement(CoreReflections.clazz$SimpleWaterloggedBlock)
// internal interfaces
.implement(CraftEngineNMSBlock.class)
.method(ElementMatchers.named("getBehaviorHolder"))
.method(ElementMatchers.named("behaviorDelegate"))
.intercept(FieldAccessor.ofField("behaviorHolder"))
.method(ElementMatchers.named("getShapeHolder"))
.method(ElementMatchers.named("shapeDelegate"))
.intercept(FieldAccessor.ofField("shapeHolder"))
.method(ElementMatchers.named("isNoteBlock"))
.intercept(FieldAccessor.ofField("isClientSideNoteBlock"))
@@ -218,8 +222,8 @@ public final class BlockGenerator {
@RuntimeType
public Object intercept(@This Object thisObj, @AllArguments Object[] args, @SuperCall Callable<Object> superMethod) {
ObjectHolder<BlockBehavior> holder = ((BehaviorHolder) thisObj).getBehaviorHolder();
ChainUpdateBlockIndicator indicator = (ChainUpdateBlockIndicator) thisObj;
ObjectHolder<BlockBehavior> holder = ((DelegatingBlock) thisObj).behaviorDelegate();
DelegatingBlock indicator = (DelegatingBlock) thisObj;
// todo chain updater
if (indicator.isNoteBlock()) {
if (CoreReflections.clazz$ServerLevel.isInstance(args[levelIndex])) {
@@ -260,7 +264,7 @@ public final class BlockGenerator {
@RuntimeType
public Object intercept(@This Object thisObj, @AllArguments Object[] args, @SuperCall Callable<Object> superMethod) throws Exception {
ObjectHolder<BlockShape> holder = ((ShapeHolder) thisObj).getShapeHolder();
ObjectHolder<BlockShape> holder = ((DelegatingBlock) thisObj).shapeDelegate();
try {
return holder.value().getShape(thisObj, args);
} catch (Exception e) {
@@ -275,7 +279,7 @@ public final class BlockGenerator {
@RuntimeType
public Object intercept(@This Object thisObj, @AllArguments Object[] args, @SuperCall Callable<Object> superMethod) throws Exception {
ObjectHolder<BlockShape> holder = ((ShapeHolder) thisObj).getShapeHolder();
ObjectHolder<BlockShape> holder = ((DelegatingBlock) thisObj).shapeDelegate();
try {
return holder.value().getCollisionShape(thisObj, args);
} catch (Exception e) {
@@ -290,7 +294,7 @@ public final class BlockGenerator {
@RuntimeType
public Object intercept(@This Object thisObj, @AllArguments Object[] args, @SuperCall Callable<Object> superMethod) throws Exception {
ObjectHolder<BlockShape> holder = ((ShapeHolder) thisObj).getShapeHolder();
ObjectHolder<BlockShape> holder = ((DelegatingBlock) thisObj).shapeDelegate();
try {
return holder.value().getSupportShape(thisObj, args);
} catch (Exception e) {
@@ -305,7 +309,7 @@ public final class BlockGenerator {
@RuntimeType
public Object intercept(@This Object thisObj, @AllArguments Object[] args, @SuperCall Callable<Object> superMethod) throws Exception {
ObjectHolder<BlockBehavior> holder = ((BehaviorHolder) thisObj).getBehaviorHolder();
ObjectHolder<BlockBehavior> holder = ((DelegatingBlock) thisObj).behaviorDelegate();
try {
return holder.value().isPathFindable(thisObj, args, superMethod);
} catch (Exception e) {
@@ -320,7 +324,7 @@ public final class BlockGenerator {
@RuntimeType
public Object intercept(@This Object thisObj, @AllArguments Object[] args, @SuperCall Callable<Object> superMethod) throws Exception {
ObjectHolder<BlockBehavior> holder = ((BehaviorHolder) thisObj).getBehaviorHolder();
ObjectHolder<BlockBehavior> holder = ((DelegatingBlock) thisObj).behaviorDelegate();
try {
return holder.value().mirror(thisObj, args, superMethod);
} catch (Exception e) {
@@ -335,7 +339,7 @@ public final class BlockGenerator {
@RuntimeType
public Object intercept(@This Object thisObj, @AllArguments Object[] args, @SuperCall Callable<Object> superMethod) throws Exception {
ObjectHolder<BlockBehavior> holder = ((BehaviorHolder) thisObj).getBehaviorHolder();
ObjectHolder<BlockBehavior> holder = ((DelegatingBlock) thisObj).behaviorDelegate();
try {
return holder.value().rotate(thisObj, args, superMethod);
} catch (Exception e) {
@@ -350,7 +354,7 @@ public final class BlockGenerator {
@RuntimeType
public void intercept(@This Object thisObj, @AllArguments Object[] args, @SuperCall Callable<Object> superMethod) {
ObjectHolder<BlockBehavior> holder = ((BehaviorHolder) thisObj).getBehaviorHolder();
ObjectHolder<BlockBehavior> holder = ((DelegatingBlock) thisObj).behaviorDelegate();
try {
holder.value().randomTick(thisObj, args, superMethod);
} catch (Exception e) {
@@ -364,7 +368,7 @@ public final class BlockGenerator {
@RuntimeType
public void intercept(@This Object thisObj, @AllArguments Object[] args, @SuperCall Callable<Object> superMethod) {
ObjectHolder<BlockBehavior> holder = ((BehaviorHolder) thisObj).getBehaviorHolder();
ObjectHolder<BlockBehavior> holder = ((DelegatingBlock) thisObj).behaviorDelegate();
try {
holder.value().tick(thisObj, args, superMethod);
} catch (Exception e) {
@@ -378,7 +382,7 @@ public final class BlockGenerator {
@RuntimeType
public void intercept(@This Object thisObj, @AllArguments Object[] args, @SuperCall Callable<Object> superMethod) {
ObjectHolder<BlockBehavior> holder = ((BehaviorHolder) thisObj).getBehaviorHolder();
ObjectHolder<BlockBehavior> holder = ((DelegatingBlock) thisObj).behaviorDelegate();
try {
holder.value().onPlace(thisObj, args, superMethod);
} catch (Exception e) {
@@ -392,7 +396,7 @@ public final class BlockGenerator {
@RuntimeType
public void intercept(@This Object thisObj, @AllArguments Object[] args) {
ObjectHolder<BlockBehavior> holder = ((BehaviorHolder) thisObj).getBehaviorHolder();
ObjectHolder<BlockBehavior> holder = ((DelegatingBlock) thisObj).behaviorDelegate();
try {
holder.value().onLand(thisObj, args);
} catch (Exception e) {
@@ -406,7 +410,7 @@ public final class BlockGenerator {
@RuntimeType
public void intercept(@This Object thisObj, @AllArguments Object[] args) {
ObjectHolder<BlockBehavior> holder = ((BehaviorHolder) thisObj).getBehaviorHolder();
ObjectHolder<BlockBehavior> holder = ((DelegatingBlock) thisObj).behaviorDelegate();
try {
holder.value().onBrokenAfterFall(thisObj, args);
} catch (Exception e) {
@@ -420,7 +424,7 @@ public final class BlockGenerator {
@RuntimeType
public boolean intercept(@This Object thisObj, @AllArguments Object[] args, @SuperCall Callable<Object> superMethod) {
ObjectHolder<BlockBehavior> holder = ((BehaviorHolder) thisObj).getBehaviorHolder();
ObjectHolder<BlockBehavior> holder = ((DelegatingBlock) thisObj).behaviorDelegate();
try {
return holder.value().canSurvive(thisObj, args, superMethod);
} catch (Exception e) {
@@ -435,7 +439,7 @@ public final class BlockGenerator {
@RuntimeType
public boolean intercept(@This Object thisObj, @AllArguments Object[] args) {
ObjectHolder<BlockBehavior> holder = ((BehaviorHolder) thisObj).getBehaviorHolder();
ObjectHolder<BlockBehavior> holder = ((DelegatingBlock) thisObj).behaviorDelegate();
try {
return holder.value().isBoneMealSuccess(thisObj, args);
} catch (Exception e) {
@@ -450,7 +454,7 @@ public final class BlockGenerator {
@RuntimeType
public boolean intercept(@This Object thisObj, @AllArguments Object[] args) {
ObjectHolder<BlockBehavior> holder = ((BehaviorHolder) thisObj).getBehaviorHolder();
ObjectHolder<BlockBehavior> holder = ((DelegatingBlock) thisObj).behaviorDelegate();
try {
return holder.value().isValidBoneMealTarget(thisObj, args);
} catch (Exception e) {
@@ -465,7 +469,7 @@ public final class BlockGenerator {
@RuntimeType
public void intercept(@This Object thisObj, @AllArguments Object[] args) {
ObjectHolder<BlockBehavior> holder = ((BehaviorHolder) thisObj).getBehaviorHolder();
ObjectHolder<BlockBehavior> holder = ((DelegatingBlock) thisObj).behaviorDelegate();
try {
holder.value().performBoneMeal(thisObj, args);
} catch (Exception e) {
@@ -479,7 +483,7 @@ public final class BlockGenerator {
@RuntimeType
public void intercept(@This Object thisObj, @AllArguments Object[] args, @SuperCall Callable<Object> superMethod) {
ObjectHolder<BlockBehavior> holder = ((BehaviorHolder) thisObj).getBehaviorHolder();
ObjectHolder<BlockBehavior> holder = ((DelegatingBlock) thisObj).behaviorDelegate();
try {
holder.value().neighborChanged(thisObj, args, superMethod);
} catch (Exception e) {
@@ -493,7 +497,7 @@ public final class BlockGenerator {
@RuntimeType
public void intercept(@This Object thisObj, @AllArguments Object[] args, @SuperCall Callable<Object> superMethod) {
ObjectHolder<BlockBehavior> holder = ((BehaviorHolder) thisObj).getBehaviorHolder();
ObjectHolder<BlockBehavior> holder = ((DelegatingBlock) thisObj).behaviorDelegate();
try {
holder.value().onExplosionHit(thisObj, args, superMethod);
} catch (Exception e) {
@@ -507,7 +511,7 @@ public final class BlockGenerator {
@RuntimeType
public Object intercept(@This Object thisObj, @AllArguments Object[] args, @SuperCall Callable<Object> superMethod) {
ObjectHolder<BlockBehavior> holder = ((BehaviorHolder) thisObj).getBehaviorHolder();
ObjectHolder<BlockBehavior> holder = ((DelegatingBlock) thisObj).behaviorDelegate();
try {
return holder.value().pickupBlock(thisObj, args, () -> CoreReflections.instance$ItemStack$EMPTY);
} catch (Exception e) {
@@ -522,7 +526,7 @@ public final class BlockGenerator {
@RuntimeType
public boolean intercept(@This Object thisObj, @AllArguments Object[] args, @SuperCall Callable<Object> superMethod) throws Exception {
ObjectHolder<BlockBehavior> holder = ((BehaviorHolder) thisObj).getBehaviorHolder();
ObjectHolder<BlockBehavior> holder = ((DelegatingBlock) thisObj).behaviorDelegate();
try {
return holder.value().placeLiquid(thisObj, args, superMethod);
} catch (Exception e) {
@@ -537,7 +541,7 @@ public final class BlockGenerator {
@RuntimeType
public boolean intercept(@This Object thisObj, @AllArguments Object[] args, @SuperCall Callable<Object> superMethod) {
ObjectHolder<BlockBehavior> holder = ((BehaviorHolder) thisObj).getBehaviorHolder();
ObjectHolder<BlockBehavior> holder = ((DelegatingBlock) thisObj).behaviorDelegate();
try {
return holder.value().canPlaceLiquid(thisObj, args, superMethod);
} catch (Exception e) {
@@ -552,7 +556,7 @@ public final class BlockGenerator {
@RuntimeType
public int intercept(@This Object thisObj, @AllArguments Object[] args, @SuperCall Callable<Object> superMethod) {
ObjectHolder<BlockBehavior> holder = ((BehaviorHolder) thisObj).getBehaviorHolder();
ObjectHolder<BlockBehavior> holder = ((DelegatingBlock) thisObj).behaviorDelegate();
try {
return holder.value().getDirectSignal(thisObj, args, superMethod);
} catch (Exception e) {
@@ -567,7 +571,7 @@ public final class BlockGenerator {
@RuntimeType
public int intercept(@This Object thisObj, @AllArguments Object[] args, @SuperCall Callable<Object> superMethod) {
ObjectHolder<BlockBehavior> holder = ((BehaviorHolder) thisObj).getBehaviorHolder();
ObjectHolder<BlockBehavior> holder = ((DelegatingBlock) thisObj).behaviorDelegate();
try {
return holder.value().getSignal(thisObj, args, superMethod);
} catch (Exception e) {
@@ -582,7 +586,7 @@ public final class BlockGenerator {
@RuntimeType
public boolean intercept(@This Object thisObj, @AllArguments Object[] args, @SuperCall Callable<Object> superMethod) {
ObjectHolder<BlockBehavior> holder = ((BehaviorHolder) thisObj).getBehaviorHolder();
ObjectHolder<BlockBehavior> holder = ((DelegatingBlock) thisObj).behaviorDelegate();
try {
return holder.value().isSignalSource(thisObj, args, superMethod);
} catch (Exception e) {
@@ -597,7 +601,7 @@ public final class BlockGenerator {
@RuntimeType
public void intercept(@This Object thisObj, @AllArguments Object[] args, @SuperCall Callable<Object> superMethod) {
ObjectHolder<BlockBehavior> holder = ((BehaviorHolder) thisObj).getBehaviorHolder();
ObjectHolder<BlockBehavior> holder = ((DelegatingBlock) thisObj).behaviorDelegate();
try {
holder.value().affectNeighborsAfterRemoval(thisObj, args, superMethod);
} catch (Exception e) {
@@ -611,7 +615,7 @@ public final class BlockGenerator {
@RuntimeType
public void intercept(@This Object thisObj, @AllArguments Object[] args, @SuperCall Callable<Object> superMethod) {
ObjectHolder<BlockBehavior> holder = ((BehaviorHolder) thisObj).getBehaviorHolder();
ObjectHolder<BlockBehavior> holder = ((DelegatingBlock) thisObj).behaviorDelegate();
try {
holder.value().entityInside(thisObj, args, superMethod);
} catch (Exception e) {

View File

@@ -21,7 +21,7 @@ import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MLootContex
import net.momirealms.craftengine.bukkit.plugin.user.BukkitServerPlayer;
import net.momirealms.craftengine.bukkit.world.BukkitWorld;
import net.momirealms.craftengine.core.block.BlockSettings;
import net.momirealms.craftengine.core.block.CustomBlockStateHolder;
import net.momirealms.craftengine.core.block.DelegatingBlockState;
import net.momirealms.craftengine.core.block.ImmutableBlockState;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.plugin.context.ContextHolder;
@@ -48,10 +48,10 @@ public final class BlockStateGenerator {
.subclass(CoreReflections.clazz$BlockState, ConstructorStrategy.Default.IMITATE_SUPER_CLASS_OPENING)
.name(generatedStateClassName)
.defineField("immutableBlockState", ImmutableBlockState.class, Visibility.PUBLIC)
.implement(CustomBlockStateHolder.class)
.method(ElementMatchers.named("customBlockState"))
.implement(DelegatingBlockState.class)
.method(ElementMatchers.named("blockState"))
.intercept(FieldAccessor.ofField("immutableBlockState"))
.method(ElementMatchers.named("setCustomBlockState"))
.method(ElementMatchers.named("setBlockState"))
.intercept(FieldAccessor.ofField("immutableBlockState"))
.method(ElementMatchers.is(CoreReflections.method$BlockStateBase$getDrops))
.intercept(MethodDelegation.to(GetDropsInterceptor.INSTANCE));
@@ -77,7 +77,7 @@ public final class BlockStateGenerator {
@RuntimeType
public Object intercept(@This Object thisObj, @AllArguments Object[] args) {
ImmutableBlockState state = ((CustomBlockStateHolder) thisObj).customBlockState();
ImmutableBlockState state = ((DelegatingBlockState) thisObj).blockState();
if (state == null) return List.of();
Object builder = args[0];
Object vec3 = FastNMS.INSTANCE.method$LootParams$Builder$getOptionalParameter(builder, MLootContextParams.ORIGIN);

View File

@@ -1551,7 +1551,7 @@ public class PacketConsumers {
if (EventUtils.fireAndCheckCancel(preBreakEvent))
return;
if (!BukkitCraftEngine.instance().antiGrief().canBreak(platformPlayer, location))
if (!BukkitCraftEngine.instance().antiGriefProvider().canBreak(platformPlayer, location))
return;
FurnitureBreakEvent breakEvent = new FurnitureBreakEvent(serverPlayer.platformPlayer(), furniture);

View File

@@ -7,7 +7,7 @@ import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MBlocks;
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MBuiltInRegistries;
import net.momirealms.craftengine.core.block.BlockSettings;
import net.momirealms.craftengine.core.block.BlockStateWrapper;
import net.momirealms.craftengine.core.block.CustomBlockStateHolder;
import net.momirealms.craftengine.core.block.DelegatingBlockState;
import net.momirealms.craftengine.core.block.ImmutableBlockState;
import net.momirealms.craftengine.core.item.Item;
import net.momirealms.craftengine.core.util.Key;
@@ -117,11 +117,11 @@ public class BlockStateUtils {
}
public static boolean isVanillaBlock(Object state) {
return !(state instanceof CustomBlockStateHolder);
return !(state instanceof DelegatingBlockState);
}
public static boolean isCustomBlock(Object state) {
return state instanceof CustomBlockStateHolder;
return state instanceof DelegatingBlockState;
}
public static boolean isVanillaBlock(int id) {
@@ -133,8 +133,8 @@ public class BlockStateUtils {
}
public static Optional<ImmutableBlockState> getOptionalCustomBlockState(Object state) {
if (state instanceof CustomBlockStateHolder holder) {
return Optional.ofNullable(holder.customBlockState());
if (state instanceof DelegatingBlockState holder) {
return Optional.ofNullable(holder.blockState());
} else {
return Optional.empty();
}

View File

@@ -1,7 +1,8 @@
package net.momirealms.craftengine.core.block;
import com.google.common.collect.ImmutableMap;
import net.momirealms.craftengine.core.block.behavior.AbstractBlockBehavior;
import net.momirealms.craftengine.core.block.behavior.EmptyBlockBehavior;
import net.momirealms.craftengine.core.block.parser.BlockNbtParser;
import net.momirealms.craftengine.core.block.properties.Property;
import net.momirealms.craftengine.core.item.context.BlockPlaceContext;
import net.momirealms.craftengine.core.loot.LootTable;
@@ -37,7 +38,7 @@ public abstract class AbstractCustomBlock implements CustomBlock {
@NotNull Holder.Reference<CustomBlock> holder,
@NotNull Map<String, Property<?>> properties,
@NotNull Map<String, Integer> appearances,
@NotNull Map<String, VariantState> variantMapper,
@NotNull Map<String, BlockStateVariant> variantMapper,
@NotNull BlockSettings settings,
@NotNull Map<EventTrigger, List<Function<PlayerOptionalContext>>> events,
@NotNull List<Map<String, Object>> behaviorConfig,
@@ -57,14 +58,14 @@ public abstract class AbstractCustomBlock implements CustomBlock {
placements.add(Property.createStateForPlacement(propertyEntry.getKey(), propertyEntry.getValue()));
}
this.placementFunction = composite(placements);
for (Map.Entry<String, VariantState> entry : variantMapper.entrySet()) {
for (Map.Entry<String, BlockStateVariant> entry : variantMapper.entrySet()) {
String nbtString = entry.getKey();
CompoundTag tag = BlockNbtParser.deserialize(this, nbtString);
if (tag == null) {
throw new LocalizedResourceConfigException("warning.config.block.state.property.invalid_format", nbtString);
}
VariantState variantState = entry.getValue();
int vanillaStateRegistryId = appearances.getOrDefault(variantState.appearance(), -1);
BlockStateVariant blockStateVariant = entry.getValue();
int vanillaStateRegistryId = appearances.getOrDefault(blockStateVariant.appearance(), -1);
// This should never happen
if (vanillaStateRegistryId == -1) {
vanillaStateRegistryId = appearances.values().iterator().next();
@@ -72,9 +73,9 @@ public abstract class AbstractCustomBlock implements CustomBlock {
// Late init states
for (ImmutableBlockState state : this.getPossibleStates(tag)) {
state.setBehavior(this.behavior);
state.setSettings(variantState.settings());
state.setSettings(blockStateVariant.settings());
state.setVanillaBlockState(BlockRegistryMirror.stateByRegistryId(vanillaStateRegistryId));
state.setCustomBlockState(BlockRegistryMirror.stateByRegistryId(variantState.internalRegistryId()));
state.setCustomBlockState(BlockRegistryMirror.stateByRegistryId(blockStateVariant.internalRegistryId()));
}
}
// double check if there's any invalid state
@@ -189,10 +190,7 @@ public abstract class AbstractCustomBlock implements CustomBlock {
@Override
public ImmutableBlockState getStateForPlacement(BlockPlaceContext context) {
ImmutableBlockState state = this.placementFunction.apply(context, defaultState());
if (this.behavior instanceof AbstractBlockBehavior blockBehavior) {
state = blockBehavior.updateStateForPlacement(context, state);
}
return state;
return this.behavior.updateStateForPlacement(context, state);
}
@Override

View File

@@ -1,8 +0,0 @@
package net.momirealms.craftengine.core.block;
import net.momirealms.craftengine.core.util.ObjectHolder;
public interface BehaviorHolder {
ObjectHolder<BlockBehavior> getBehaviorHolder();
}

View File

@@ -12,6 +12,6 @@ public class BlockEntityState {
}
public CompoundTag nbt() {
return nbt;
return this.nbt;
}
}

View File

@@ -2,7 +2,9 @@ package net.momirealms.craftengine.core.block;
import net.momirealms.craftengine.core.util.Key;
public class BlockKeys {
public final class BlockKeys {
private BlockKeys() {}
public static final Key NOTE_BLOCK = Key.of("minecraft:note_block");
public static final Key TRIPWIRE = Key.of("minecraft:tripwire");
public static final Key CRAFTING_TABLE = Key.of("minecraft:crafting_table");

View File

@@ -1,21 +1,22 @@
package net.momirealms.craftengine.core.block;
public final class BlockRegistryMirror {
private static BlockStateWrapper[] customBlockStates;
private static BlockStateWrapper[] blockStates;
private static BlockStateWrapper stoneState;
public static void init(BlockStateWrapper[] states, BlockStateWrapper state) {
customBlockStates = states;
if (blockStates != null) throw new IllegalStateException("block states are already set");
blockStates = states;
stoneState = state;
}
public static BlockStateWrapper stateByRegistryId(int vanillaId) {
if (vanillaId < 0) return stoneState;
return customBlockStates[vanillaId];
return blockStates[vanillaId];
}
public static int size() {
return customBlockStates.length;
return blockStates.length;
}
public static BlockStateWrapper stoneState() {

View File

@@ -355,10 +355,12 @@ public class BlockSettings {
return this;
}
@FunctionalInterface
public interface Modifier {
void apply(BlockSettings settings);
@FunctionalInterface
interface Factory {
Modifier createModifier(Object value);

View File

@@ -2,7 +2,7 @@ package net.momirealms.craftengine.core.block;
public interface BlockShape {
Object getShape(Object thisObj, Object[] args) throws Exception;
Object getShape(Object thisObj, Object[] args);
Object getCollisionShape(Object thisObj, Object[] args);

View File

@@ -5,13 +5,15 @@ import net.momirealms.craftengine.core.util.Key;
import java.util.Map;
public class BlockSounds {
public final class BlockSounds {
/*
Fall 0.5 0.75
Place 1, 0.8
Step 0.15, 1
Place 1 0.8
Step 0.15 1
Hit 0.5 0.5
Break 1 0.8
Land 0.3 1
Destroy 1 1
*/
public static final SoundData EMPTY_SOUND = new SoundData(Key.of("minecraft:intentionally_empty"), SoundData.SoundValue.FIXED_1, SoundData.SoundValue.FIXED_1);
public static final BlockSounds EMPTY = new BlockSounds(EMPTY_SOUND, EMPTY_SOUND, EMPTY_SOUND, EMPTY_SOUND, EMPTY_SOUND, EMPTY_SOUND, EMPTY_SOUND);

View File

@@ -1,7 +0,0 @@
package net.momirealms.craftengine.core.block;
public class BlockStateArrangeException extends RuntimeException {
public BlockStateArrangeException(String message) {
super(message);
}
}

View File

@@ -1,108 +0,0 @@
package net.momirealms.craftengine.core.block;
import net.momirealms.craftengine.core.block.properties.Property;
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
import net.momirealms.craftengine.core.registry.Holder;
import net.momirealms.craftengine.core.util.Key;
import net.momirealms.craftengine.core.util.Pair;
import net.momirealms.craftengine.core.util.StringReader;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class BlockStateMatcher {
private final List<Pair<Property<?>, Comparable<?>>> properties;
private final Key id;
public BlockStateMatcher(Key id, List<Pair<Property<?>, Comparable<?>>> properties) {
this.properties = properties;
this.id = id;
}
public boolean matches(ImmutableBlockState state) {
if (!state.owner().value().id().equals(this.id)) {
return false;
}
for (Pair<Property<?>, Comparable<?>> pair : this.properties) {
if (!state.get(pair.left()).equals(pair.right())) {
return false;
}
}
return true;
}
@Override
public String toString() {
if (properties.isEmpty()) {
return id.toString();
}
return id + "[" + getPropertiesAsString() + "]";
}
public String getPropertiesAsString() {
return properties.stream()
.map(entry -> {
Property<?> property = entry.left();
return property.name() + "=" + Property.formatValue(property, entry.right());
})
.collect(Collectors.joining(","));
}
@SuppressWarnings("DuplicatedCode")
@Nullable
public static BlockStateMatcher deserialize(@NotNull String data) {
StringReader reader = StringReader.simple(data);
String blockIdString = reader.readUnquotedString();
if (reader.canRead() && reader.peek() == ':') {
reader.skip();
blockIdString = blockIdString + ":" + reader.readUnquotedString();
}
Optional<Holder.Reference<CustomBlock>> optional = BuiltInRegistries.BLOCK.get(Key.from(blockIdString));
if (optional.isEmpty()) {
return null;
}
Holder<CustomBlock> holder = optional.get();
List<Pair<Property<?>, Comparable<?>>> properties = new ArrayList<>();
if (reader.canRead() && reader.peek() == '[') {
reader.skip();
while (reader.canRead()) {
reader.skipWhitespace();
if (reader.peek() == ']') break;
String propertyName = reader.readUnquotedString();
reader.skipWhitespace();
if (!reader.canRead() || reader.peek() != '=') {
return null;
}
reader.skip();
reader.skipWhitespace();
String propertyValue = reader.readUnquotedString();
Property<?> property = holder.value().getProperty(propertyName);
if (property != null) {
Optional<?> optionalValue = property.optional(propertyValue);
if (optionalValue.isEmpty()) {
return null;
} else {
properties.add(Pair.of(property, (Comparable<?>) optionalValue.get()));
}
} else {
return null;
}
reader.skipWhitespace();
if (reader.canRead() && reader.peek() == ',') {
reader.skip();
}
}
reader.skipWhitespace();
if (reader.canRead() && reader.peek() == ']') {
reader.skip();
} else {
return null;
}
}
return new BlockStateMatcher(holder.value().id(), properties);
}
}

View File

@@ -1,11 +1,11 @@
package net.momirealms.craftengine.core.block;
public class VariantState {
public class BlockStateVariant {
private final String appearance;
private final BlockSettings settings;
private final int internalId;
public VariantState(String appearance, BlockSettings settings, int internalId) {
public BlockStateVariant(String appearance, BlockSettings settings, int internalId) {
this.appearance = appearance;
this.settings = settings;
this.internalId = internalId;

View File

@@ -18,7 +18,7 @@ import java.util.Map;
import java.util.function.Function;
import java.util.stream.Stream;
public class BlockStateVariantProvider {
public final class BlockStateVariantProvider {
private final ImmutableSortedMap<String, Property<?>> properties;
private final ImmutableList<ImmutableBlockState> states;
private final Holder<CustomBlock> owner;
@@ -65,7 +65,7 @@ public class BlockStateVariantProvider {
@NotNull
public ImmutableBlockState getDefaultState() {
ImmutableBlockState first = this.states.get(0);
ImmutableBlockState first = this.states.getFirst();
for (Property<?> property : this.properties.values()) {
first = ImmutableBlockState.with(first, property, property.defaultValue());
}
@@ -73,11 +73,11 @@ public class BlockStateVariantProvider {
}
public ImmutableList<ImmutableBlockState> states() {
return states;
return this.states;
}
public Holder<CustomBlock> owner() {
return owner;
return this.owner;
}
@Nullable

View File

@@ -59,6 +59,11 @@ public interface BlockStateWrapper {
super(handle, registryId);
}
@Override
public DelegatingBlockState handle() {
return (DelegatingBlockState) super.handle();
}
@Override
public boolean isVanillaBlock() {
return false;

View File

@@ -1,8 +0,0 @@
package net.momirealms.craftengine.core.block;
public interface ChainUpdateBlockIndicator {
boolean isNoteBlock();
boolean isTripwire();
}

View File

@@ -1,4 +0,0 @@
package net.momirealms.craftengine.core.block;
public interface CraftEngineNMSBlock extends ShapeHolder, BehaviorHolder, ChainUpdateBlockIndicator {
}

View File

@@ -53,7 +53,7 @@ public interface CustomBlock {
Builder settings(BlockSettings settings);
Builder variantMapper(Map<String, VariantState> variantMapper);
Builder variantMapper(Map<String, BlockStateVariant> variantMapper);
@NotNull CustomBlock build();
}

View File

@@ -1,8 +0,0 @@
package net.momirealms.craftengine.core.block;
public interface CustomBlockStateHolder {
ImmutableBlockState customBlockState();
void setCustomBlockState(ImmutableBlockState state);
}

View File

@@ -0,0 +1,40 @@
package net.momirealms.craftengine.core.block;
import net.momirealms.craftengine.core.util.ObjectHolder;
/**
* Interface representing a delegatable block that can dynamically modify its shape and behavior at runtime.
* Implementations of this interface serve as actual block instances injected into Minecraft's
* {@code BuiltInRegistries.BLOCK}, enabling real-time modifications to physical properties and geometry.
*
* <p>Utilizes the {@code ObjectHolder} pattern to delegate block characteristics, allowing runtime updates
* to collision boxes and interaction logic without reconstructing block instances.</p>
*/
public interface DelegatingBlock {
/**
* Gets the mutable holder for the block's shape delegate.
* Modifying the contained {@code BlockShape} will dynamically update
* collision bounding boxes and supporting shape.
*
* @return Non-null object holder containing current block shape
*/
ObjectHolder<BlockShape> shapeDelegate();
/**
* Gets the mutable holder for the block's behavior delegate.
* Modifying the contained {@code BlockBehavior} will dynamically adjust:
* - Physics properties
* - Interaction logic (e.g. click responses)
* - State update rules
*
* @return Non-null object holder containing current block behavior
*/
ObjectHolder<BlockBehavior> behaviorDelegate();
@Deprecated
boolean isNoteBlock();
@Deprecated
boolean isTripwire();
}

View File

@@ -0,0 +1,26 @@
package net.momirealms.craftengine.core.block;
import org.jetbrains.annotations.Nullable;
/**
* Interface representing a block state that can delegate its underlying immutable state.
*
* @see ImmutableBlockState The immutable state container being delegated
*/
public interface DelegatingBlockState {
/**
* Gets the current immutable block state being delegated.
*
* @return The current immutable block state instance
*/
@Nullable
ImmutableBlockState blockState();
/**
* Replaces the currently delegated block state with a new immutable state.
*
* @param state The new immutable state to delegate to
*/
void setBlockState(@Nullable ImmutableBlockState state);
}

View File

@@ -6,7 +6,7 @@ import net.momirealms.craftengine.core.util.Key;
import java.util.List;
import java.util.Map;
public class EmptyBlock extends AbstractCustomBlock {
public final class EmptyBlock extends AbstractCustomBlock {
public static EmptyBlock INSTANCE;
public static ImmutableBlockState STATE;

View File

@@ -1,10 +0,0 @@
package net.momirealms.craftengine.core.block;
public class EmptyBlockBehavior extends BlockBehavior {
public static final EmptyBlockBehavior INSTANCE = new EmptyBlockBehavior();
@Override
public CustomBlock block() {
return EmptyBlock.INSTANCE;
}
}

View File

@@ -17,7 +17,7 @@ import org.jetbrains.annotations.Nullable;
import java.util.List;
public class ImmutableBlockState extends BlockStateHolder {
public final class ImmutableBlockState extends BlockStateHolder {
private CompoundTag tag;
private BlockStateWrapper customBlockState;
private BlockStateWrapper vanillaBlockState;
@@ -26,7 +26,7 @@ public class ImmutableBlockState extends BlockStateHolder {
private Integer hashCode;
private BlockSettings settings;
protected ImmutableBlockState(
ImmutableBlockState(
Holder<CustomBlock> owner,
Reference2ObjectArrayMap<Property<?>, Comparable<?>> propertyMap
) {
@@ -54,7 +54,7 @@ public class ImmutableBlockState extends BlockStateHolder {
}
@Override
public final boolean equals(Object o) {
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ImmutableBlockState state)) return false;
return state.owner == this.owner && state.tag.equals(this.tag);
@@ -62,22 +62,10 @@ public class ImmutableBlockState extends BlockStateHolder {
@Override
public int hashCode() {
if (hashCode == null) {
hashCode = getNbtToSave().hashCode();
if (this.hashCode == null) {
this.hashCode = getNbtToSave().hashCode();
}
return hashCode;
}
public BlockSounds sounds() {
return settings.sounds;
}
public int luminance() {
return settings.luminance;
}
public PushReaction pushReaction() {
return settings.pushReaction;
return this.hashCode;
}
public BlockStateWrapper customBlockState() {

View File

@@ -9,7 +9,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class InactiveCustomBlock extends AbstractCustomBlock {
public final class InactiveCustomBlock extends AbstractCustomBlock {
private final Map<CompoundTag, ImmutableBlockState> cachedData = new HashMap<>();
public InactiveCustomBlock(Key id, Holder.Reference<CustomBlock> holder) {
@@ -23,7 +23,7 @@ public class InactiveCustomBlock extends AbstractCustomBlock {
@Override
public ImmutableBlockState getBlockState(CompoundTag nbt) {
return this.cachedData.computeIfAbsent(nbt, k -> {
ImmutableBlockState state = new ImmutableBlockState(holder, new Reference2ObjectArrayMap<>());
ImmutableBlockState state = new ImmutableBlockState(super.holder, new Reference2ObjectArrayMap<>());
state.setNbtToSave(state.toNbtToSave(nbt));
return state;
});

View File

@@ -2,11 +2,11 @@ package net.momirealms.craftengine.core.block;
import net.momirealms.craftengine.core.plugin.CraftEngine;
public class DelayedInitBlockState {
public final class LazyBlockState {
private final String state;
private BlockStateWrapper packedBlockState;
public DelayedInitBlockState(String state) {
public LazyBlockState(String state) {
this.state = state;
}

View File

@@ -1,8 +0,0 @@
package net.momirealms.craftengine.core.block;
import net.momirealms.craftengine.core.util.ObjectHolder;
public interface ShapeHolder {
ObjectHolder<BlockShape> getShapeHolder();
}

View File

@@ -14,7 +14,7 @@ import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class UnsafeBlockStateMatcher {
public final class UnsafeBlockStateMatcher {
private final List<Pair<String, String>> matchers;
private final Key id;

View File

@@ -1,6 +1,6 @@
package net.momirealms.craftengine.core.block;
public class UpdateOption {
public final class UpdateOption {
public static final UpdateOption UPDATE_ALL = new UpdateOption(3);
public static final UpdateOption UPDATE_NONE = new UpdateOption(4);
public static final UpdateOption UPDATE_ALL_IMMEDIATE = new UpdateOption(11);

View File

@@ -1,27 +0,0 @@
package net.momirealms.craftengine.core.block;
import net.momirealms.craftengine.core.util.Key;
public class VanillaBlockState {
private final Key type;
private final String properties;
private final int registryId;
public VanillaBlockState(Key type, String properties, int registryId) {
this.properties = properties;
this.registryId = registryId;
this.type = type;
}
public String properties() {
return properties;
}
public int registryId() {
return registryId;
}
public Key type() {
return type;
}
}

View File

@@ -2,7 +2,6 @@ package net.momirealms.craftengine.core.block.behavior;
import net.momirealms.craftengine.core.block.BlockBehavior;
import net.momirealms.craftengine.core.block.CustomBlock;
import net.momirealms.craftengine.core.block.EmptyBlockBehavior;
import net.momirealms.craftengine.core.plugin.locale.LocalizedResourceConfigException;
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
import net.momirealms.craftengine.core.registry.Holder;

View File

@@ -0,0 +1,14 @@
package net.momirealms.craftengine.core.block.behavior;
import net.momirealms.craftengine.core.block.BlockBehavior;
import net.momirealms.craftengine.core.block.CustomBlock;
import net.momirealms.craftengine.core.block.EmptyBlock;
public final class EmptyBlockBehavior extends BlockBehavior {
public static final EmptyBlockBehavior INSTANCE = new EmptyBlockBehavior();
@Override
public CustomBlock block() {
return EmptyBlock.INSTANCE;
}
}

View File

@@ -1,13 +1,13 @@
package net.momirealms.craftengine.core.block;
package net.momirealms.craftengine.core.block.parser;
import net.momirealms.craftengine.core.block.CustomBlock;
import net.momirealms.craftengine.core.block.properties.Property;
import net.momirealms.craftengine.core.util.StringReader;
import net.momirealms.sparrow.nbt.CompoundTag;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class BlockNbtParser {
public final class BlockNbtParser {
private BlockNbtParser() {}
@Nullable

View File

@@ -1,5 +1,7 @@
package net.momirealms.craftengine.core.block;
package net.momirealms.craftengine.core.block.parser;
import net.momirealms.craftengine.core.block.CustomBlock;
import net.momirealms.craftengine.core.block.ImmutableBlockState;
import net.momirealms.craftengine.core.block.properties.Property;
import net.momirealms.craftengine.core.registry.BuiltInRegistries;
import net.momirealms.craftengine.core.registry.Holder;
@@ -13,7 +15,7 @@ import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
public class BlockStateParser {
public final class BlockStateParser {
private static final char START = '[';
private static final char EQUAL = '=';
private static final char SEPARATOR = ',';

View File

@@ -1,6 +1,6 @@
package net.momirealms.craftengine.core.plugin.context.function;
import net.momirealms.craftengine.core.block.DelayedInitBlockState;
import net.momirealms.craftengine.core.block.LazyBlockState;
import net.momirealms.craftengine.core.item.DelayedInitItem;
import net.momirealms.craftengine.core.plugin.context.Condition;
import net.momirealms.craftengine.core.plugin.context.Context;
@@ -26,7 +26,7 @@ public class ParticleFunction<CTX extends Context> extends AbstractConditionalFu
static {
registerParticleData(map -> new BlockStateData(
new DelayedInitBlockState(ResourceConfigUtils.requireNonEmptyStringOrThrow(map.get("block-state"), "warning.config.function.particle.missing_block_state"))),
new LazyBlockState(ResourceConfigUtils.requireNonEmptyStringOrThrow(map.get("block-state"), "warning.config.function.particle.missing_block_state"))),
ParticleTypes.BLOCK, ParticleTypes.FALLING_DUST, ParticleTypes.DUST_PILLAR, ParticleTypes.BLOCK_CRUMBLE, ParticleTypes.BLOCK_MARKER);
registerParticleData(map -> new ColorData(
Color.fromString(ResourceConfigUtils.requireNonEmptyStringOrThrow(map.get("color"), "warning.config.function.particle.missing_color").split(","))),

View File

@@ -1,6 +1,6 @@
package net.momirealms.craftengine.core.plugin.context.function;
import net.momirealms.craftengine.core.block.DelayedInitBlockState;
import net.momirealms.craftengine.core.block.LazyBlockState;
import net.momirealms.craftengine.core.block.UpdateOption;
import net.momirealms.craftengine.core.plugin.context.Condition;
import net.momirealms.craftengine.core.plugin.context.Context;
@@ -18,15 +18,15 @@ import java.util.Map;
import java.util.Optional;
public class PlaceBlockFunction<CTX extends Context> extends AbstractConditionalFunction<CTX> {
private final DelayedInitBlockState delayedInitBlockState;
private final LazyBlockState lazyBlockState;
private final NumberProvider x;
private final NumberProvider y;
private final NumberProvider z;
private final NumberProvider updateFlags;
public PlaceBlockFunction(DelayedInitBlockState delayedInitBlockState, NumberProvider x, NumberProvider y, NumberProvider z, NumberProvider updateFlags, List<Condition<CTX>> predicates) {
public PlaceBlockFunction(LazyBlockState lazyBlockState, NumberProvider x, NumberProvider y, NumberProvider z, NumberProvider updateFlags, List<Condition<CTX>> predicates) {
super(predicates);
this.delayedInitBlockState = delayedInitBlockState;
this.lazyBlockState = lazyBlockState;
this.x = x;
this.y = y;
this.z = z;
@@ -38,7 +38,7 @@ public class PlaceBlockFunction<CTX extends Context> extends AbstractConditional
Optional<WorldPosition> optionalWorldPosition = ctx.getOptionalParameter(DirectContextParameters.POSITION);
if (optionalWorldPosition.isPresent()) {
World world = optionalWorldPosition.get().world();
world.setBlockAt(MCUtils.fastFloor(this.x.getDouble(ctx)), MCUtils.fastFloor(this.y.getDouble(ctx)), MCUtils.fastFloor(this.z.getDouble(ctx)), this.delayedInitBlockState.getState(), this.updateFlags.getInt(ctx));
world.setBlockAt(MCUtils.fastFloor(this.x.getDouble(ctx)), MCUtils.fastFloor(this.y.getDouble(ctx)), MCUtils.fastFloor(this.z.getDouble(ctx)), this.lazyBlockState.getState(), this.updateFlags.getInt(ctx));
}
}
@@ -56,12 +56,12 @@ public class PlaceBlockFunction<CTX extends Context> extends AbstractConditional
@Override
public Function<CTX> create(Map<String, Object> arguments) {
String state = ResourceConfigUtils.requireNonEmptyStringOrThrow(arguments.get("block-state"), "warning.config.function.place_block.missing_block_state");
DelayedInitBlockState delayedInitBlockState = new DelayedInitBlockState(state);
LazyBlockState lazyBlockState = new LazyBlockState(state);
NumberProvider x = NumberProviders.fromObject(arguments.getOrDefault("x", "<arg:position.x>"));
NumberProvider y = NumberProviders.fromObject(arguments.getOrDefault("y", "<arg:position.y>"));
NumberProvider z = NumberProviders.fromObject(arguments.getOrDefault("z", "<arg:position.z>"));
NumberProvider flags = Optional.ofNullable(arguments.get("update-flags")).map(NumberProviders::fromObject).orElse(NumberProviders.direct(UpdateOption.UPDATE_ALL.flags()));
return new PlaceBlockFunction<>(delayedInitBlockState, x, y, z, flags, getPredicates(arguments));
return new PlaceBlockFunction<>(lazyBlockState, x, y, z, flags, getPredicates(arguments));
}
}
}

View File

@@ -1,8 +1,8 @@
package net.momirealms.craftengine.core.plugin.locale;
import net.momirealms.craftengine.core.block.BlockStateParser;
import net.momirealms.craftengine.core.block.CustomBlock;
import net.momirealms.craftengine.core.block.ImmutableBlockState;
import net.momirealms.craftengine.core.block.parser.BlockStateParser;
import net.momirealms.craftengine.core.plugin.CraftEngine;
import net.momirealms.craftengine.core.util.Key;
import org.jetbrains.annotations.Nullable;

View File

@@ -1,12 +1,12 @@
package net.momirealms.craftengine.core.world.particle;
import net.momirealms.craftengine.core.block.BlockStateWrapper;
import net.momirealms.craftengine.core.block.DelayedInitBlockState;
import net.momirealms.craftengine.core.block.LazyBlockState;
public class BlockStateData implements ParticleData {
private final DelayedInitBlockState blockState;
private final LazyBlockState blockState;
public BlockStateData(DelayedInitBlockState blockState) {
public BlockStateData(LazyBlockState blockState) {
this.blockState = blockState;
}