Skip to content

Commit a5e794b

Browse files
committed
Merge remote-tracking branch 'origin/api-14' into api-15
2 parents af1f0b0 + c4b7a1a commit a5e794b

File tree

9 files changed

+131
-8
lines changed

9 files changed

+131
-8
lines changed

src/main/java/org/spongepowered/common/bridge/world/inventory/InventoryMenuBridge.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
*/
2525
package org.spongepowered.common.bridge.world.inventory;
2626

27-
public interface InventoryMenuBridge {
27+
import org.spongepowered.common.bridge.world.inventory.container.ContainerBridge;
28+
29+
public interface InventoryMenuBridge extends ContainerBridge {
2830

2931
void bridge$markClean();
3032
}

src/main/java/org/spongepowered/common/data/SpongeDataManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.spongepowered.api.block.BlockSnapshot;
3636
import org.spongepowered.api.block.BlockState;
3737
import org.spongepowered.api.block.entity.BlockEntityArchetype;
38+
import org.spongepowered.api.block.entity.BlockEntityType;
3839
import org.spongepowered.api.data.DataHolder;
3940
import org.spongepowered.api.data.DataHolderBuilder;
4041
import org.spongepowered.api.data.DataManager;
@@ -434,6 +435,7 @@ public <T> Optional<RegistryType<T>> findRegistryTypeFor(Class type) {
434435
this.registryTypeMap.put(ParticleType.class, RegistryTypes.PARTICLE_TYPE);
435436
this.registryTypeMap.put(ParticleOption.class, RegistryTypes.PARTICLE_OPTION);
436437
this.registryTypeMap.put(PotionEffectType.class, RegistryTypes.POTION_EFFECT_TYPE);
438+
this.registryTypeMap.put(BlockEntityType.class, RegistryTypes.BLOCK_ENTITY_TYPE);
437439
// TODO add all RegistryTypes that we have global registries for
438440
// there needs to be a better way to do this
439441
}

src/mixins/java/org/spongepowered/common/mixin/api/data/DataHolderMixin_API.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,11 @@
2626

2727
import net.minecraft.world.entity.Entity;
2828
import net.minecraft.world.item.ItemStack;
29-
import net.minecraft.world.level.block.entity.BlockEntity;
3029
import org.spongepowered.api.data.DataHolder;
3130
import org.spongepowered.asm.mixin.Mixin;
3231
import org.spongepowered.common.data.holder.SpongeMutableDataHolder;
3332

34-
@Mixin(value = {BlockEntity.class, Entity.class, ItemStack.class}, priority = 899)
33+
@Mixin(value = {Entity.class, ItemStack.class}, priority = 899)
3534
public abstract class DataHolderMixin_API implements DataHolder, SpongeMutableDataHolder {
3635

3736
}

src/mixins/java/org/spongepowered/common/mixin/api/minecraft/world/level/LevelMixin_API.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ public <E extends org.spongepowered.api.entity.Entity> E createEntityNaturally(f
311311

312312
@Override
313313
public Optional<Entity> createEntity(final DataContainer container) {
314-
return ((LevelBridge) this).bridge$createEntity(container, null, null);
314+
return Optional.ofNullable(((LevelBridge) this).bridge$createEntity(container, null, null));
315315
}
316316

317317
@Override

src/mixins/java/org/spongepowered/common/mixin/core/world/level/LevelMixin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public abstract class LevelMixin implements LevelBridge, LevelAccessor {
160160
scale = null;
161161
}
162162

163-
final Entity createdEntity = this.bridge$createEntity(type, position, false);
163+
final Entity createdEntity = this.bridge$createEntity(type, proposedPosition, false);
164164
dataContainer.getView(Constants.Sponge.UNSAFE_NBT)
165165
.map(NBTTranslator.INSTANCE::translate)
166166
.ifPresent(x -> {

src/mixins/java/org/spongepowered/common/mixin/core/world/level/block/entity/BannerBlockEntityMixin.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.spongepowered.api.data.type.DyeColor;
3232
import org.spongepowered.asm.mixin.Final;
3333
import org.spongepowered.asm.mixin.Mixin;
34-
import org.spongepowered.asm.mixin.Mutable;
3534
import org.spongepowered.asm.mixin.Shadow;
3635
import org.spongepowered.asm.mixin.Unique;
3736
import org.spongepowered.common.bridge.CustomNameableBridge;
@@ -44,7 +43,7 @@
4443
@Mixin(BannerBlockEntity.class)
4544
public abstract class BannerBlockEntityMixin extends BlockEntityMixin implements BannerBlockEntityBridge, CustomNameableBridge {
4645

47-
@Shadow @Final @Mutable private net.minecraft.world.item.DyeColor baseColor;
46+
@Shadow @Final @org.spongepowered.asm.mixin.Mutable private net.minecraft.world.item.DyeColor baseColor;
4847
@Shadow @Nullable private Component name;
4948

5049
@Unique

src/mixins/java/org/spongepowered/common/mixin/core/world/level/block/entity/BlockEntityMixin.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
import net.minecraft.world.level.block.state.BlockState;
3333
import org.checkerframework.checker.nullness.qual.Nullable;
3434
import org.spongepowered.api.ResourceKey;
35+
import org.spongepowered.api.block.entity.BlockEntity;
36+
import org.spongepowered.api.data.DataHolder;
3537
import org.spongepowered.api.world.server.ServerWorld;
3638
import org.spongepowered.asm.mixin.Final;
3739
import org.spongepowered.asm.mixin.Mixin;
@@ -42,13 +44,16 @@
4244
import org.spongepowered.common.bridge.data.DataCompoundHolder;
4345
import org.spongepowered.common.bridge.world.level.block.entity.BlockEntityBridge;
4446
import org.spongepowered.common.data.DataUtil;
47+
import org.spongepowered.common.data.holder.SpongeMutableDataHolder;
4548
import org.spongepowered.common.data.provider.nbt.NBTDataType;
4649
import org.spongepowered.common.data.provider.nbt.NBTDataTypes;
4750

51+
import java.util.Arrays;
52+
import java.util.List;
4853
import java.util.StringJoiner;
4954

5055
@Mixin(net.minecraft.world.level.block.entity.BlockEntity.class)
51-
public abstract class BlockEntityMixin implements BlockEntityBridge, DataCompoundHolder {
56+
public abstract class BlockEntityMixin implements BlockEntityBridge, DataCompoundHolder, SpongeMutableDataHolder {
5257

5358
//@formatter:off
5459
@Shadow @Final private BlockEntityType<?> type;
@@ -119,4 +124,10 @@ protected StringJoiner getPrettyPrinterStringHelper() {
119124
public String bridge$getPrettyPrinterString() {
120125
return this.getPrettyPrinterStringHelper().toString();
121126
}
127+
128+
@Override
129+
public List<DataHolder> impl$delegateDataHolder() {
130+
final org.spongepowered.api.block.BlockState state = ((BlockEntity) this).block();
131+
return Arrays.asList(this, state, state.type());
132+
}
122133
}

src/mixins/java/org/spongepowered/common/mixin/inventory/impl/world/inventory/InventoryMenuMixin_Bridge_Inventory.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
*/
2525
package org.spongepowered.common.mixin.inventory.impl.world.inventory;
2626

27+
import net.minecraft.server.level.ServerPlayer;
2728
import net.minecraft.world.entity.player.Player;
2829
import net.minecraft.world.inventory.InventoryMenu;
30+
import org.checkerframework.checker.nullness.qual.Nullable;
2931
import org.spongepowered.asm.mixin.Final;
3032
import org.spongepowered.asm.mixin.Mixin;
3133
import org.spongepowered.asm.mixin.Shadow;
@@ -41,4 +43,9 @@ public abstract class InventoryMenuMixin_Bridge_Inventory implements InventoryMe
4143
public void bridge$markClean() {
4244
((PlayerInventoryBridge) this.owner.getInventory()).bridge$markClean();
4345
}
46+
47+
@Override
48+
public @Nullable ServerPlayer bridge$getViewer() {
49+
return this.owner instanceof final ServerPlayer player ? player : null;
50+
}
4451
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* This file is part of Sponge, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.common.data;
26+
27+
import org.junit.jupiter.api.Assertions;
28+
import org.junit.jupiter.params.ParameterizedTest;
29+
import org.junit.jupiter.params.provider.Arguments;
30+
import org.junit.jupiter.params.provider.MethodSource;
31+
import org.spongepowered.api.ResourceKey;
32+
import org.spongepowered.api.Sponge;
33+
import org.spongepowered.api.block.BlockTypes;
34+
import org.spongepowered.api.block.entity.BlockEntity;
35+
import org.spongepowered.api.block.entity.BlockEntityArchetype;
36+
import org.spongepowered.api.block.entity.BlockEntityTypes;
37+
import org.spongepowered.api.data.DataRegistration;
38+
import org.spongepowered.api.data.DataTransactionResult;
39+
import org.spongepowered.api.data.Key;
40+
import org.spongepowered.api.data.SerializableDataHolder;
41+
import org.spongepowered.api.data.persistence.DataContainer;
42+
import org.spongepowered.api.data.value.Value;
43+
import org.spongepowered.api.entity.Entity;
44+
import org.spongepowered.api.entity.EntityTypes;
45+
import org.spongepowered.api.item.ItemTypes;
46+
import org.spongepowered.api.item.inventory.ItemStack;
47+
import org.spongepowered.api.world.DefaultWorldKeys;
48+
import org.spongepowered.api.world.chunk.WorldChunk;
49+
import org.spongepowered.api.world.server.ServerWorld;
50+
import org.spongepowered.api.world.server.storage.ServerWorldProperties;
51+
import org.spongepowered.common.util.Constants;
52+
import org.spongepowered.math.vector.Vector3d;
53+
54+
import java.util.Optional;
55+
import java.util.function.Function;
56+
import java.util.function.Supplier;
57+
import java.util.stream.Stream;
58+
59+
public class DataHolderTest {
60+
61+
private static final Key<Value<String>> STRING_KEY = Key.from(ResourceKey.of("test", "string"), String.class);
62+
63+
static {
64+
SpongeDataManager.INSTANCE.registerCustomDataRegistration((SpongeDataRegistration) DataRegistration.of(DataHolderTest.STRING_KEY, ItemStack.class, BlockEntity.class, Entity.class, WorldChunk.class, ServerWorldProperties.class));
65+
}
66+
67+
public static Stream<Arguments> testEntity() {
68+
final ServerWorld world = Sponge.server().worldManager().world(DefaultWorldKeys.DEFAULT).get();
69+
70+
return Stream.of(
71+
DataHolderTest.testEntityArguments(() -> ItemStack.of(ItemTypes.DIAMOND), c -> ItemStack.builder().fromContainer(c).build()),
72+
DataHolderTest.testEntityArguments(() -> BlockEntityArchetype.builder().blockEntity(BlockEntityTypes.CHEST).state(BlockTypes.CHEST.get().defaultState()).build(), c -> BlockEntityArchetype.builder().build(c).orElseThrow()),
73+
DataHolderTest.testEntityArguments(() -> world.createEntity(EntityTypes.CAT, Vector3d.ZERO), c -> world.createEntity(c).orElseThrow())
74+
);
75+
}
76+
77+
private static <T extends SerializableDataHolder.Mutable> Arguments testEntityArguments(final Supplier<T> supplier, final Function<DataContainer, T> function) {
78+
return Arguments.of(supplier, function);
79+
}
80+
81+
@ParameterizedTest
82+
@MethodSource
83+
public <T extends SerializableDataHolder.Mutable> void testEntity(final Supplier<T> supplier, final Function<DataContainer, T> function) {
84+
final var input = "test" + System.currentTimeMillis();
85+
86+
final T holder = supplier.get();
87+
final var offerResult = holder.offer(DataHolderTest.STRING_KEY, input);
88+
89+
Assertions.assertEquals(DataTransactionResult.successResult(Value.immutableOf(DataHolderTest.STRING_KEY, input)), offerResult);
90+
Assertions.assertEquals(Optional.of(input), holder.get(DataHolderTest.STRING_KEY));
91+
92+
final DataContainer originalContainer = holder.toContainer();
93+
94+
final T copy = function.apply(originalContainer);
95+
96+
final DataContainer copyContainer = copy.toContainer();
97+
98+
originalContainer.remove(Constants.Sponge.UNSAFE_NBT.then(Constants.Entity.ENTITY_UUID));
99+
copyContainer.remove(Constants.Sponge.UNSAFE_NBT.then(Constants.Entity.ENTITY_UUID));
100+
101+
Assertions.assertEquals(originalContainer, copyContainer);
102+
}
103+
}

0 commit comments

Comments
 (0)