use of org.spongepowered.api.block.BlockType 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.block.BlockType in project LanternServer by LanternPowered.
the class BlockRegistryModule method register.
@Override
public void register(int internalId, BlockType blockType) {
LanternBlockType blockType0 = (LanternBlockType) checkNotNull(blockType, "blockType");
checkState(blockType0.getBlockStateBase().getBlockStates().stream().filter(s -> !((LanternBlockState) s).isExtended()).count() <= 1, "You cannot register a blockType with more then one state with this method.");
register0(internalId, blockType0, blockState -> (byte) 0);
}
use of org.spongepowered.api.block.BlockType in project LanternServer by LanternPowered.
the class ItemStackStore method deserializeValues.
@Override
public void deserializeValues(LanternItemStack object, SimpleValueContainer valueContainer, DataView dataView) {
final ItemTypeObjectSerializer serializer = this.itemTypeSerializers.get(object.getType());
if (serializer != null) {
serializer.deserializeValues(object, valueContainer, dataView);
}
final Optional<DataView> optDisplayView = dataView.getView(DISPLAY);
if (optDisplayView.isPresent()) {
final DataView displayView = optDisplayView.get();
if (!valueContainer.get(Keys.DISPLAY_NAME).isPresent()) {
Optional<String> name = displayView.getString(NAME);
if (name.isPresent()) {
valueContainer.set(Keys.DISPLAY_NAME, LanternTexts.fromLegacy(name.get()));
} else if ((name = displayView.getString(LOCALIZED_NAME)).isPresent()) {
valueContainer.set(Keys.DISPLAY_NAME, t(name.get()));
}
}
dataView.getStringList(LORE).ifPresent(lore -> {
if (!lore.isEmpty()) {
valueContainer.set(Keys.ITEM_LORE, lore.stream().map(LanternTexts::fromLegacy).collect(Collectors.toList()));
}
});
}
dataView.getStringList(CAN_DESTROY).ifPresent(types -> {
if (!types.isEmpty()) {
final Set<BlockType> blockTypes = new HashSet<>();
types.forEach(type -> BlockRegistryModule.get().getById(type).ifPresent(blockTypes::add));
valueContainer.set(Keys.BREAKABLE_BLOCK_TYPES, blockTypes);
}
});
deserializeEnchantments(dataView, ENCHANTMENTS, Keys.ITEM_ENCHANTMENTS, valueContainer);
deserializeEnchantments(dataView, STORED_ENCHANTMENTS, Keys.STORED_ENCHANTMENTS, valueContainer);
super.deserializeValues(object, valueContainer, dataView);
}
use of org.spongepowered.api.block.BlockType in project LanternServer by LanternPowered.
the class FlatGeneratorSettingsParser method fromString.
@Nullable
public static FlatGeneratorSettings fromString(@Nullable String value) {
if (value == null) {
return null;
}
// Split the value into parts
final List<String> parts = Lists.newArrayList(Splitter.on(';').split(value));
// Try to extract the version from the parts
int version = 0;
if (parts.size() > 1) {
version = Coerce.toInteger(parts.remove(0));
}
// Smaller then 0 is unknown? and 3 is the latest format version
if (version < 0 || version > 3) {
return null;
}
// The layers are stored in the first part
final String layersPart = parts.remove(0);
// The parsed layers
final List<FlatLayer> layers = new ArrayList<>();
// Can be empty if there are no layers
if (!layersPart.isEmpty()) {
// The separator that can be used to create a layer
// of x amount of blocks
final char depthSeparator = version >= 3 ? '*' : 'x';
Splitter.on(',').split(layersPart).forEach(s -> {
// The block type
BlockType blockType;
// The data value (optional)
int blockData = 0;
// The depth of the layer
int depth = 1;
// The depth separated by the depth separator followed by the block state
final List<String> parts1 = Lists.newArrayList(Splitter.on(depthSeparator).limit(2).split(s));
if (parts1.size() > 1) {
final Optional<Integer> optDepth = Coerce.asInteger(parts1.remove(0));
if (optDepth.isPresent()) {
depth = GenericMath.clamp(optDepth.get(), 0, 255);
if (depth <= 0) {
// Skip to the next layer
return;
}
}
}
String blockStatePart = parts1.get(0);
final int index = blockStatePart.lastIndexOf(':');
if (index > 0) {
Optional<Integer> optData = Coerce.asInteger(blockStatePart.substring(index + 1));
if (optData.isPresent()) {
blockData = GenericMath.clamp(optData.get(), 0, 15);
blockStatePart = blockStatePart.substring(0, index);
}
}
// Try to parse the block id as internal (int) id
final Optional<Integer> optId = Coerce.asInteger(blockStatePart);
if (optId.isPresent()) {
blockType = BlockRegistryModule.get().getStateByInternalId(optId.get()).orElse(BlockTypes.STONE.getDefaultState()).getType();
// Not an integer, try the catalog system
} else {
blockType = BlockRegistryModule.get().getById(blockStatePart).orElse(BlockTypes.STONE);
}
layers.add(new FlatLayer(BlockRegistryModule.get().getStateByTypeAndData(blockType, (byte) blockData).get(), depth));
});
}
// Try to parse the biome type if present
BiomeType biomeType = BiomeTypes.PLAINS;
if (!parts.isEmpty()) {
final String biomePart = parts.remove(0);
final Optional<Integer> optBiomeId = Coerce.asInteger(biomePart);
final Optional<BiomeType> optBiome;
if (optBiomeId.isPresent()) {
optBiome = BiomeRegistryModule.get().getByInternalId(optBiomeId.get());
} else {
optBiome = BiomeRegistryModule.get().getById(biomePart);
}
if (optBiome.isPresent()) {
biomeType = optBiome.get();
}
}
// Extra data (like structures)
final DataContainer extraData = DataContainer.createNew();
if (!parts.isEmpty()) {
final String extraPart = parts.remove(0);
if (!extraPart.isEmpty()) {
Splitter.on(',').split(extraPart).forEach(s -> {
String key = s;
// Check if there is extra data attached to the key
final int valuesIndex = s.indexOf('(');
if (valuesIndex != -1) {
// Separate the key from the values
key = s.substring(0, valuesIndex);
int endIndex = s.lastIndexOf(')');
if (endIndex == -1) {
endIndex = s.length();
}
// Get the values section from the string
s = s.substring(valuesIndex + 1, endIndex);
// Create the view to store the values
final DataView dataView = extraData.createView(DataQuery.of(key));
if (!s.isEmpty()) {
Splitter.on(' ').split(s).forEach(v -> {
final List<String> parts1 = Splitter.on('=').limit(2).splitToList(v);
// Must be greater then 1, otherwise it's invalid
if (parts1.size() > 1) {
// Currently, only integer values seem to be supported
dataView.set(DataQuery.of(parts1.get(0)), Coerce.toInteger(parts1.get(1)));
}
});
}
} else {
extraData.createView(DataQuery.of(key));
}
});
}
}
return new FlatGeneratorSettings(biomeType, layers, extraData);
}
use of org.spongepowered.api.block.BlockType in project LanternServer by LanternPowered.
the class LanternFurnace method shouldRefresh.
@Override
public boolean shouldRefresh(BlockState oldBlockState, BlockState newBlockState) {
final BlockType n = oldBlockState.getType();
final BlockType o = newBlockState.getType();
return !((n == BlockTypes.FURNACE || n == BlockTypes.LIT_FURNACE) && (o == BlockTypes.FURNACE || o == BlockTypes.LIT_FURNACE));
}
Aggregations