Skip to content

Commit

Permalink
Merge branch 'api-11' into update/1.20.6
Browse files Browse the repository at this point in the history
  • Loading branch information
ImMorpheus committed May 5, 2024
2 parents 280c2ed + 97b5c0a commit 30b7c59
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
package org.spongepowered.common.block;

import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.entity.BlockEntity;
Expand All @@ -39,6 +41,7 @@
import org.spongepowered.api.block.BlockSnapshot;
import org.spongepowered.api.block.BlockState;
import org.spongepowered.api.block.entity.BlockEntityArchetype;
import org.spongepowered.api.block.entity.BlockEntityType;
import org.spongepowered.api.data.DataHolder;
import org.spongepowered.api.data.DataManipulator;
import org.spongepowered.api.data.Key;
Expand All @@ -54,6 +57,7 @@
import org.spongepowered.api.world.server.ServerWorld;
import org.spongepowered.api.world.server.storage.ServerWorldProperties;
import org.spongepowered.common.SpongeCommon;
import org.spongepowered.common.block.entity.SpongeBlockEntityArchetypeBuilder;
import org.spongepowered.common.bridge.data.DataCompoundHolder;
import org.spongepowered.common.bridge.data.DataContainerHolder;
import org.spongepowered.common.data.holder.SpongeImmutableDataHolder;
Expand Down Expand Up @@ -257,7 +261,23 @@ public Optional<UUID> notifier() {

@Override
public Optional<BlockEntityArchetype> createArchetype() {
throw new UnsupportedOperationException("Not implemented yet, please fix when this is called");
if (this.compound == null) {
return Optional.empty();
}

final String blockEntityId = this.compound.getString(Constants.Item.BLOCK_ENTITY_ID);

final CompoundTag compound = this.compound.copy();
compound.remove(Constants.Sponge.BlockSnapshot.TILE_ENTITY_POSITION_X);
compound.remove(Constants.Sponge.BlockSnapshot.TILE_ENTITY_POSITION_Y);
compound.remove(Constants.Sponge.BlockSnapshot.TILE_ENTITY_POSITION_Z);
compound.remove(Constants.Item.BLOCK_ENTITY_ID);

return Optional.of(SpongeBlockEntityArchetypeBuilder.pooled()
.state(this.state())
.blockEntity((BlockEntityType) SpongeCommon.vanillaRegistry(Registries.BLOCK_ENTITY_TYPE).get(ResourceLocation.tryParse(blockEntityId)))
.blockEntityData(NBTTranslator.INSTANCE.translate(compound))
.build());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import com.google.common.collect.ImmutableList;
import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
Expand All @@ -34,11 +35,13 @@
import org.spongepowered.api.block.BlockState;
import org.spongepowered.api.block.entity.BlockEntity;
import org.spongepowered.api.block.entity.BlockEntityType;
import org.spongepowered.api.data.DataHolder;
import org.spongepowered.api.data.persistence.DataContainer;
import org.spongepowered.api.data.persistence.DataView;
import org.spongepowered.api.data.persistence.Queries;
import org.spongepowered.api.world.BlockChangeFlags;
import org.spongepowered.api.world.server.ServerLocation;
import org.spongepowered.common.SpongeCommon;
import org.spongepowered.common.block.SpongeBlockSnapshot;
import org.spongepowered.common.data.AbstractArchetype;
import org.spongepowered.common.data.SpongeDataManager;
Expand All @@ -50,6 +53,8 @@
import org.spongepowered.common.util.Constants;
import org.spongepowered.common.util.VecHelper;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.StringJoiner;
Expand All @@ -64,6 +69,8 @@ public final class SpongeBlockEntityArchetype extends AbstractArchetype<BlockEnt

private static final DataProviderLookup lookup = SpongeDataManager.getProviderRegistry().getProviderLookup(SpongeBlockEntityArchetype.class);

private net.minecraft.world.level.block.entity.@Nullable BlockEntity cachedBlockEntity;

SpongeBlockEntityArchetype(final SpongeBlockEntityArchetypeBuilder builder) {
super(builder.type, NBTTranslator.INSTANCE.translate(builder.data));
this.blockState = builder.blockState;
Expand Down Expand Up @@ -186,4 +193,13 @@ public String toString() {
.add("state=" + this.blockState)
.add("data=" + this.compound).toString();
}

public List<DataHolder> impl$delegateDataHolder() {
if (this.cachedBlockEntity == null) {
final CompoundTag compound = this.compound.copy();
compound.putString(Constants.Item.BLOCK_ENTITY_ID, SpongeCommon.vanillaRegistry(Registries.BLOCK_ENTITY_TYPE).getKey((net.minecraft.world.level.block.entity.BlockEntityType<?>) this.type).toString());
this.cachedBlockEntity = net.minecraft.world.level.block.entity.BlockEntity.loadStatic(new BlockPos(0, 0, 0), (net.minecraft.world.level.block.state.BlockState) this.blockState, compound);
}
return Arrays.asList(this, (DataHolder) this.cachedBlockEntity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@
package org.spongepowered.test.archetype;

import com.google.inject.Inject;
import net.kyori.adventure.text.Component;
import org.spongepowered.api.ResourceKey;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.Command;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.parameter.CommandContext;
import org.spongepowered.api.command.parameter.Parameter;
import org.spongepowered.api.data.Key;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.entity.EntityArchetype;
import org.spongepowered.api.entity.EntitySnapshot;
Expand All @@ -40,11 +44,13 @@
import org.spongepowered.api.event.cause.entity.SpawnTypes;
import org.spongepowered.api.event.entity.InteractEntityEvent;
import org.spongepowered.api.event.lifecycle.RegisterCommandEvent;
import org.spongepowered.api.world.server.ServerLocation;
import org.spongepowered.plugin.PluginContainer;
import org.spongepowered.plugin.builtin.jvm.Plugin;
import org.spongepowered.test.LoadableModule;

// TODO other Archetypes
@SuppressWarnings({"unchecked", "rawtypes"})
@Plugin("archetypetest")
public final class ArchetypeTest implements LoadableModule {

Expand All @@ -60,6 +66,10 @@ private void registerCommands(final RegisterCommandEvent<Command.Parameterized>
event.register(this.plugin, Command.builder()
.addChild(Command.builder().executor(this::testEntityArchetype).build(), "entity")
.build(), "testarchetypes");

event.register(this.plugin, Command.builder()
.addChild(this.queryBlockArchetype(), "block")
.build(), "queryArchetype");
}

private CommandResult testEntityArchetype(CommandContext context) {
Expand All @@ -70,6 +80,29 @@ private CommandResult testEntityArchetype(CommandContext context) {
return CommandResult.success();
}

private Command.Parameterized queryBlockArchetype() {
final Parameter.Value<ServerLocation> serverLocationParameter = Parameter.location().key("location").build();
final Parameter.Value<ResourceKey> resourceKeyValue = Parameter.resourceKey().key("resourceKey").build();

return Command.builder()
.addParameter(serverLocationParameter)
.addParameter(resourceKeyValue)
.executor(context -> {
final ServerLocation location = context.requireOne(serverLocationParameter);
final ResourceKey resourceKey = context.requireOne(resourceKeyValue);
location.createSnapshot().createArchetype().ifPresentOrElse(a ->
a.getKeys()
.stream()
.filter(k -> k.key().equals(resourceKey))
.findFirst()
.ifPresentOrElse(k -> context.sendMessage(Component.text("Value: " + a.get((Key) k).orElse(null))),
() -> context.sendMessage(Component.text("Not valid key"))),
() -> context.sendMessage(Component.text("No valid archetype could be created")));
return CommandResult.success();
})
.build();
}

@Override
public void enable(CommandContext ctx) {
Sponge.eventManager().registerListeners(this.plugin, new ArchetypeTestListener());
Expand Down

0 comments on commit 30b7c59

Please sign in to comment.