Search in sources :

Example 1 with MapItemSavedDataBridge

use of org.spongepowered.common.bridge.world.storage.MapItemSavedDataBridge in project SpongeCommon by SpongePowered.

the class MapInfoItemStackData method register.

// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
    registrator.asMutable(ItemStack.class).create(Keys.MAP_INFO).supports(item -> item.getItem() instanceof MapItem).get(itemStack -> {
        if (itemStack.getTag() == null) {
            return null;
        }
        return (MapInfo) ((Level) Sponge.server().worldManager().defaultWorld()).getMapData(Constants.Map.MAP_PREFIX + itemStack.getTag().getInt(Constants.Map.MAP_ID));
    }).set((itemStack, mapInfo) -> {
        @Nullable CompoundTag nbt = itemStack.getTag();
        if (nbt == null) {
            nbt = new CompoundTag();
        }
        nbt.putInt(Constants.Map.MAP_ID, ((MapItemSavedDataBridge) mapInfo).bridge$getMapId());
        itemStack.setTag(nbt);
    });
}
Also used : Keys(org.spongepowered.api.data.Keys) CompoundTag(net.minecraft.nbt.CompoundTag) MapInfo(org.spongepowered.api.map.MapInfo) Constants(org.spongepowered.common.util.Constants) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) MapItem(net.minecraft.world.item.MapItem) Sponge(org.spongepowered.api.Sponge) ItemStack(net.minecraft.world.item.ItemStack) MapItemSavedDataBridge(org.spongepowered.common.bridge.world.storage.MapItemSavedDataBridge) Level(net.minecraft.world.level.Level) Nullable(org.checkerframework.checker.nullness.qual.Nullable) Level(net.minecraft.world.level.Level) MapItem(net.minecraft.world.item.MapItem) Nullable(org.checkerframework.checker.nullness.qual.Nullable) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 2 with MapItemSavedDataBridge

use of org.spongepowered.common.bridge.world.storage.MapItemSavedDataBridge in project SpongeCommon by SpongePowered.

the class EmptyMapItemMixin method impl$createMapWithSpongeData.

@Redirect(method = "use", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/item/MapItem;create(Lnet/minecraft/world/level/Level;IIBZZ)Lnet/minecraft/world/item/ItemStack;"))
private ItemStack impl$createMapWithSpongeData(final Level level, final int x, final int y, final byte scale, final boolean trackingPosition, final boolean unlimitedTracking, final Level level2, final net.minecraft.world.entity.player.Player playerIn, final InteractionHand usedHand) {
    if (level.isClientSide()) {
        // ignore on client
        return MapItem.create(level, x, y, scale, trackingPosition, unlimitedTracking);
    }
    final Player player = (Player) playerIn;
    try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
        frame.addContext(EventContextKeys.PLAYER, player);
        final HandType handType = (HandType) (Object) usedHand;
        frame.addContext(EventContextKeys.USED_HAND, handType);
        frame.addContext(EventContextKeys.USED_ITEM, player.itemInHand(handType).createSnapshot());
        final Set<Value<?>> mapValues = Sets.newHashSet(Value.immutableOf(Keys.MAP_LOCATION, Vector2i.from((int) playerIn.getX(), (int) playerIn.getZ())), Value.immutableOf(Keys.MAP_WORLD, ((ServerWorld) level).key()), Value.immutableOf(Keys.MAP_TRACKS_PLAYERS, trackingPosition), Value.immutableOf(Keys.MAP_UNLIMITED_TRACKING, unlimitedTracking), Value.immutableOf(Keys.MAP_SCALE, (int) scale));
        final Optional<MapInfo> optMapInfo = SpongeCommonEventFactory.fireCreateMapEvent(frame.currentCause(), mapValues);
        if (!optMapInfo.isPresent()) {
            return ItemStack.EMPTY;
        }
        final int id = ((MapItemSavedDataBridge) optMapInfo.get()).bridge$getMapId();
        final ItemStack newMap = new ItemStack(Items.FILLED_MAP, 1);
        final CompoundTag nbt = newMap.getOrCreateTag();
        nbt.putInt(Constants.Map.MAP_ID, id);
        return newMap;
    }
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) HandType(org.spongepowered.api.data.type.HandType) ServerWorld(org.spongepowered.api.world.server.ServerWorld) CauseStackManager(org.spongepowered.api.event.CauseStackManager) Value(org.spongepowered.api.data.value.Value) MapInfo(org.spongepowered.api.map.MapInfo) ItemStack(net.minecraft.world.item.ItemStack) MapItemSavedDataBridge(org.spongepowered.common.bridge.world.storage.MapItemSavedDataBridge) CompoundTag(net.minecraft.nbt.CompoundTag) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Aggregations

CompoundTag (net.minecraft.nbt.CompoundTag)2 ItemStack (net.minecraft.world.item.ItemStack)2 MapInfo (org.spongepowered.api.map.MapInfo)2 MapItemSavedDataBridge (org.spongepowered.common.bridge.world.storage.MapItemSavedDataBridge)2 MapItem (net.minecraft.world.item.MapItem)1 Level (net.minecraft.world.level.Level)1 Nullable (org.checkerframework.checker.nullness.qual.Nullable)1 Sponge (org.spongepowered.api.Sponge)1 Keys (org.spongepowered.api.data.Keys)1 HandType (org.spongepowered.api.data.type.HandType)1 Value (org.spongepowered.api.data.value.Value)1 Player (org.spongepowered.api.entity.living.player.Player)1 CauseStackManager (org.spongepowered.api.event.CauseStackManager)1 ServerWorld (org.spongepowered.api.world.server.ServerWorld)1 Redirect (org.spongepowered.asm.mixin.injection.Redirect)1 DataProviderRegistrator (org.spongepowered.common.data.provider.DataProviderRegistrator)1 Constants (org.spongepowered.common.util.Constants)1