use of org.spongepowered.api.data.value.Value in project SpongeCommon by SpongePowered.
the class SpongeKeyBuilder method build0.
@Override
public Key<V> build0() {
Objects.requireNonNull(this.valueType, "The value type must be set");
Objects.requireNonNull(this.elementType, "The element type must be set");
BiPredicate<? super E, ? super E> includesTester = this.includesTester;
if (includesTester == null) {
includesTester = (e, e2) -> false;
}
Comparator<? super E> comparator = this.comparator;
if (comparator == null) {
if (Comparable.class.isAssignableFrom(GenericTypeReflector.erase(this.elementType))) {
// noinspection unchecked
comparator = Comparator.comparing(o -> ((Comparable) o));
} else {
comparator = (o1, o2) -> {
if (o1.equals(o2))
return 0;
// There could be collisions, but yeah, what can you do about that..
if (o1.hashCode() > o2.hashCode())
return 1;
return -1;
};
}
}
Supplier<E> defaultValueSupplier = () -> null;
final Class<?> rawType = GenericTypeReflector.erase(this.valueType);
if (ListValue.class.isAssignableFrom(rawType)) {
defaultValueSupplier = () -> (E) new ArrayList();
} else if (SetValue.class.isAssignableFrom(rawType)) {
defaultValueSupplier = () -> (E) new HashSet();
} else if (WeightedCollectionValue.class.isAssignableFrom(rawType)) {
defaultValueSupplier = () -> (E) new WeightedTable();
} else if (MapValue.class.isAssignableFrom(rawType)) {
defaultValueSupplier = () -> (E) new HashMap<>();
}
final SpongeKey<Value<E>, E> key = new SpongeKey<>(this.key, this.valueType, this.elementType, comparator, includesTester, defaultValueSupplier);
KeyProvider.INSTANCE.register(this.key, (Key<Value<?>>) (Object) key);
return (Key<V>) key;
}
use of org.spongepowered.api.data.value.Value 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