use of org.spongepowered.api.data.key.Key in project SpongeCommon by SpongePowered.
the class SpongeItemStackBuilder method build.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public ItemStack build() throws IllegalStateException {
checkState(this.type != null, "Item type has not been set");
if (this.type == ItemTypes.NONE || this.quantity <= 0) {
// If either type is none(air) or quantity is 0 return the vanilla EMPTY item
return ((ItemStack) net.minecraft.item.ItemStack.EMPTY);
}
final ItemStack stack = (ItemStack) new net.minecraft.item.ItemStack((Item) this.type, this.quantity, this.damageValue);
if (this.compound != null) {
((net.minecraft.item.ItemStack) stack).setTagCompound(this.compound.copy());
}
if (this.itemDataSet != null) {
this.itemDataSet.forEach(stack::offer);
}
if (this.keyValues != null) {
this.keyValues.entrySet().forEach(entry -> stack.offer((Key) entry.getKey(), entry.getValue()));
}
return stack;
}
use of org.spongepowered.api.data.key.Key in project core by CubeEngine.
the class MaterialMatcher method buildVariantMap.
private Map<BlockType, Map<String, BlockState>> buildVariantMap() {
Map<BlockType, Map<String, BlockState>> blockStateItemsByType = new HashMap<>();
for (Entry<String, BlockState> entry : blockStateItems.entrySet()) {
BlockType itemType = entry.getValue().getType();
Map<String, BlockState> itemTypes = blockStateItemsByType.computeIfAbsent(itemType, k -> new HashMap<>());
itemTypes.put(entry.getKey(), entry.getValue());
}
Map<BlockType, Map<String, BlockState>> variants = new HashMap<>();
blockStateItemsByType.entrySet().stream().filter(e -> e.getValue().size() != 1).forEach(e -> {
Map<String, Set<String>> variantNames = new HashMap<>();
Map<List<String>, BlockState> fullVariant = new HashMap<>();
for (Entry<String, BlockState> entry : e.getValue().entrySet()) {
String variant = entry.getKey();
variant = variant.substring(variant.indexOf("[") + 1, variant.indexOf("]"));
// multiple variants
String[] split = variant.split(",");
fullVariant.put(Arrays.asList(split), entry.getValue());
for (String variantEntry : split) {
String[] variantEntryPart = variantEntry.split("=");
Set<String> variantValues = variantNames.computeIfAbsent(variantEntryPart[0], k -> new HashSet<>());
variantValues.add(variantEntryPart[1]);
}
}
for (Entry<String, Set<String>> entry : variantNames.entrySet()) {
if (entry.getKey().equals("axis") || entry.getKey().equals("facing") || entry.getKey().equals("half") || entry.getKey().equals("shape") || entry.getKey().equals("open") || entry.getKey().equals("powered") || entry.getKey().equals("stage") || entry.getKey().equals("decayable")) {
Map<List<String>, BlockState> filtered = new HashMap<>();
for (Entry<List<String>, BlockState> offender : fullVariant.entrySet()) {
List<String> key = new ArrayList<>(offender.getKey());
for (String fv : entry.getValue()) {
key.remove(entry.getKey() + "=" + fv);
}
if (!key.isEmpty()) {
filtered.put(key, offender.getValue());
}
}
fullVariant = filtered;
}
if (entry.getValue().size() == 1) {
String singleVariant = entry.getKey() + "=" + entry.getValue().iterator().next();
fullVariant = fullVariant.entrySet().stream().collect(Collectors.toMap(fv -> {
List<String> split = new ArrayList<>(fv.getKey());
split.remove(singleVariant);
return split;
}, Entry::getValue));
}
}
for (Entry<List<String>, BlockState> variant : fullVariant.entrySet()) {
if (variant.getKey().size() > 1) {
System.out.print(e.getKey().getName() + " Has multiple Variants:");
for (String s : variant.getKey()) {
System.out.print(" " + s);
}
System.out.print("\n");
}
}
variants.put(e.getKey(), fullVariant.entrySet().stream().collect(Collectors.toMap(en -> String.join(" ", en.getKey().stream().map(s -> s.split("=")[1]).collect(toList())), Entry::getValue)));
});
/*
for (Entry<ItemType, Map<String, ItemStack>> variant : variants.entrySet())
{
System.out.print(variant.getKey().getName() + ":\n");
for (Entry<String, ItemStack> entry : variant.getValue().entrySet())
{
System.out.print(" " + entry.getKey() + ": " + entry.getValue().getTranslation().get() + "\n");
}
}
*/
return variants;
}
use of org.spongepowered.api.data.key.Key in project LanternServer by LanternPowered.
the class CompositeValueStoreHelper method offerFast.
protected static <H extends ValueContainer<?>> boolean offerFast(ICompositeValueStore<?, H> store, Iterable<H> valueContainers, MergeFunction function) {
// Leave this, the compiler complains
final CompositeValueStore store1 = store;
if (store1 instanceof DataHolder) {
final Set<Key<?>> keys = new HashSet<>();
for (H valueContainer : valueContainers) {
keys.addAll(valueContainer.getKeys());
}
final boolean hasListeners = hasListeners(store, keys);
if (hasListeners) {
return offer(store, valueContainers, function, () -> true).isSuccessful();
}
}
return store.offerFastNoEvents(valueContainers, function);
}
use of org.spongepowered.api.data.key.Key in project LanternServer by LanternPowered.
the class IValueContainer method matchContents.
/**
* Matches the contents of the two {@link ValueContainer}s.
*
* @param valueContainerA The first value container
* @param valueContainerB The second value container
* @return Whether the contents match
*/
static boolean matchContents(IValueContainer<?> valueContainerA, IValueContainer<?> valueContainerB) {
final boolean additional = valueContainerA instanceof IAdditionalCompositeValueStore;
if (additional != valueContainerB instanceof IAdditionalCompositeValueStore) {
return false;
}
final ValueCollection valueCollectionA = valueContainerA.getValueCollection();
final ValueCollection valueCollectionB = valueContainerB.getValueCollection();
final Collection<Key<?>> keysA = valueCollectionA.getKeys();
final Collection<Key<?>> keysB = valueCollectionB.getKeys();
// The same keys have to be present in both of the containers
if (keysA.size() != keysB.size() || !keysA.containsAll(keysB)) {
return false;
}
for (KeyRegistration<?, ?> registration1 : valueCollectionA.getAll()) {
final KeyRegistration registration2 = (KeyRegistration) valueCollectionB.get((Key) registration1.getKey()).get();
// Get the values from both of the containers and match them
final Object value1 = ((Processor) registration1).getFrom(valueContainerA).orElse(null);
final Object value2 = ((Processor) registration2).getFrom(valueContainerB).orElse(null);
if (!Objects.equals(value1, value2)) {
return false;
}
}
// Match additional containers
if (additional) {
final Map<Class<?>, ValueContainer<?>> mapA = ((IAdditionalCompositeValueStore) valueContainerA).getAdditionalContainers().getMap();
final Map<Class<?>, ValueContainer<?>> mapB = ((IAdditionalCompositeValueStore) valueContainerB).getAdditionalContainers().getMap();
if (mapA.size() != mapB.size() || !mapA.keySet().containsAll(mapB.keySet())) {
return false;
}
for (Map.Entry<Class<?>, ValueContainer<?>> entry : mapA.entrySet()) {
final ValueContainer<?> containerA = entry.getValue();
final ValueContainer<?> containerB = mapB.get(entry.getKey());
if (!Objects.equals(containerA, containerB)) {
return false;
}
}
}
return true;
}
use of org.spongepowered.api.data.key.Key in project LanternServer by LanternPowered.
the class IDataHolder method supports.
@Override
default boolean supports(Class<? extends DataManipulator<?, ?>> containerClass) {
checkNotNull(containerClass, "containerClass");
// Offer all the default key values as long if they are supported
final Optional<DataManipulatorRegistration> optRegistration = DataManipulatorRegistry.get().getBy(containerClass);
if (optRegistration.isPresent()) {
final DataManipulatorRegistration registration = optRegistration.get();
for (Key key : (Set<Key>) registration.getRequiredKeys()) {
if (!supports(key)) {
return false;
}
}
return true;
}
// Support all the additional manipulators
return this instanceof AdditionalContainerHolder;
}
Aggregations