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);
});
}
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;
}
}
Aggregations