Search in sources :

Example 31 with BlockType

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;
}
Also used : Arrays(java.util.Arrays) Keys(org.spongepowered.api.data.key.Keys) Reflector(org.cubeengine.reflect.Reflector) ItemTypes(org.spongepowered.api.item.ItemTypes) HashMap(java.util.HashMap) LibCube(org.cubeengine.libcube.LibCube) Singleton(javax.inject.Singleton) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Key(org.spongepowered.api.data.key.Key) HashSet(java.util.HashSet) Inject(javax.inject.Inject) SimpleItemStackConverter(org.cubeengine.libcube.service.config.SimpleItemStackConverter) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ItemTypeConverter(org.cubeengine.libcube.service.config.ItemTypeConverter) ModuleManager(org.cubeengine.libcube.ModuleManager) Locale(java.util.Locale) Map(java.util.Map) PluginContainer(org.spongepowered.api.plugin.PluginContainer) ItemStackConverter(org.cubeengine.libcube.service.config.ItemStackConverter) Builder(org.spongepowered.api.block.BlockState.Builder) GameDictionary(org.spongepowered.api.GameDictionary) BlockTypeConverter(org.cubeengine.libcube.service.config.BlockTypeConverter) Collection(java.util.Collection) DataRegistrationNotFoundException(org.spongepowered.api.data.DataRegistrationNotFoundException) Set(java.util.Set) Sponge(org.spongepowered.api.Sponge) Collectors(java.util.stream.Collectors) SetMultimap(com.google.common.collect.SetMultimap) BlockState(org.spongepowered.api.block.BlockState) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) TreeMap(java.util.TreeMap) Entry(java.util.Map.Entry) BlockType(org.spongepowered.api.block.BlockType) Optional(java.util.Optional) Comparator(java.util.Comparator) Collections(java.util.Collections) ItemType(org.spongepowered.api.item.ItemType) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Entry(java.util.Map.Entry) BlockState(org.spongepowered.api.block.BlockState) BlockType(org.spongepowered.api.block.BlockType) ArrayList(java.util.ArrayList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 32 with BlockType

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);
}
Also used : BlockTypeBuilder(org.lanternpowered.server.block.BlockTypeBuilder) LanternBlockType(org.lanternpowered.server.block.LanternBlockType) LanternDirtType(org.lanternpowered.server.data.type.LanternDirtType) JukeboxInteractionBehavior(org.lanternpowered.server.block.behavior.vanilla.JukeboxInteractionBehavior) SnowyExtendedBlockStateProvider(org.lanternpowered.server.block.extended.SnowyExtendedBlockStateProvider) LanternBooleanTraits(org.lanternpowered.server.block.trait.LanternBooleanTraits) BoundingBoxes(org.lanternpowered.server.block.aabb.BoundingBoxes) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) ChestPlacementBehavior(org.lanternpowered.server.block.behavior.vanilla.ChestPlacementBehavior) LanternTreeType(org.lanternpowered.server.data.type.LanternTreeType) RegistrationDependency(org.spongepowered.api.registry.util.RegistrationDependency) LanternStoneType(org.lanternpowered.server.data.type.LanternStoneType) KeyRegistryModule(org.lanternpowered.server.game.registry.type.data.KeyRegistryModule) PropertyProviders.replaceable(org.lanternpowered.server.block.provider.property.PropertyProviders.replaceable) ChestInteractionBehavior(org.lanternpowered.server.block.behavior.vanilla.ChestInteractionBehavior) NoteBlockInteractionBehavior(org.lanternpowered.server.block.behavior.vanilla.NoteBlockInteractionBehavior) ShulkerBoxInteractionBehavior(org.lanternpowered.server.block.behavior.vanilla.ShulkerBoxInteractionBehavior) Object2ShortOpenHashMap(it.unimi.dsi.fastutil.objects.Object2ShortOpenHashMap) LanternIntegerTraits(org.lanternpowered.server.block.trait.LanternIntegerTraits) Short2ObjectOpenHashMap(it.unimi.dsi.fastutil.shorts.Short2ObjectOpenHashMap) LanternQuartzType(org.lanternpowered.server.data.type.LanternQuartzType) LanternDyeColor(org.lanternpowered.server.data.type.LanternDyeColor) BlockTypes(org.spongepowered.api.block.BlockTypes) INSTANT_BROKEN(org.lanternpowered.server.block.provider.property.PropertyProviderCollections.INSTANT_BROKEN) LanternPlantType(org.lanternpowered.server.data.type.LanternPlantType) HopperPlacementBehavior(org.lanternpowered.server.block.behavior.vanilla.HopperPlacementBehavior) SimpleBlockDropsProviderBehavior(org.lanternpowered.server.block.behavior.simple.SimpleBlockDropsProviderBehavior) Preconditions.checkState(com.google.common.base.Preconditions.checkState) SimpleBreakBehavior(org.lanternpowered.server.block.behavior.simple.SimpleBreakBehavior) LanternPortionType(org.lanternpowered.server.data.type.LanternPortionType) EquipmentTypeRegistryModule(org.lanternpowered.server.game.registry.type.item.inventory.equipment.EquipmentTypeRegistryModule) BlockState(org.spongepowered.api.block.BlockState) SlabType(org.spongepowered.api.data.type.SlabType) Direction(org.spongepowered.api.util.Direction) SimplePlacementBehavior(org.lanternpowered.server.block.behavior.simple.SimplePlacementBehavior) QuartzLinesRotationPlacementBehavior(org.lanternpowered.server.block.behavior.vanilla.QuartzLinesRotationPlacementBehavior) Lantern(org.lanternpowered.server.game.Lantern) BlockSnapshotProviderPlaceBehavior(org.lanternpowered.server.block.behavior.simple.BlockSnapshotProviderPlaceBehavior) RotationPlacementBehavior(org.lanternpowered.server.block.behavior.vanilla.RotationPlacementBehavior) InstrumentTypeRegistryModule(org.lanternpowered.server.game.registry.type.data.InstrumentTypeRegistryModule) BlockType(org.spongepowered.api.block.BlockType) Object2ShortMap(it.unimi.dsi.fastutil.objects.Object2ShortMap) Optional(java.util.Optional) CraftingTableInteractionBehavior(org.lanternpowered.server.block.behavior.vanilla.CraftingTableInteractionBehavior) LanternSlabType(org.lanternpowered.server.data.type.LanternSlabType) LanternBedPart(org.lanternpowered.server.data.type.LanternBedPart) InventorySnapshot(org.lanternpowered.server.inventory.InventorySnapshot) BlockTypeBuilderImpl(org.lanternpowered.server.block.BlockTypeBuilderImpl) LanternKeys(org.lanternpowered.server.data.key.LanternKeys) Keys(org.spongepowered.api.data.key.Keys) EnderChestInteractionBehavior(org.lanternpowered.server.block.behavior.vanilla.EnderChestInteractionBehavior) OppositeFaceDirectionalPlacementBehavior(org.lanternpowered.server.block.behavior.vanilla.OppositeFaceDirectionalPlacementBehavior) LanternRailDirection(org.lanternpowered.server.data.type.LanternRailDirection) EnumTrait(org.spongepowered.api.block.trait.EnumTrait) Byte2ObjectOpenHashMap(it.unimi.dsi.fastutil.bytes.Byte2ObjectOpenHashMap) TranslationProvider(org.lanternpowered.server.block.TranslationProvider) Supplier(java.util.function.Supplier) ItemRegistryModule(org.lanternpowered.server.game.registry.type.item.ItemRegistryModule) TreeType(org.spongepowered.api.data.type.TreeType) PropertyProviders.equipmentType(org.lanternpowered.server.item.PropertyProviders.equipmentType) PropertyProviders.lightEmission(org.lanternpowered.server.block.provider.property.PropertyProviders.lightEmission) HorizontalRotationPlacementBehavior(org.lanternpowered.server.block.behavior.vanilla.HorizontalRotationPlacementBehavior) TorchPlacementBehavior(org.lanternpowered.server.block.behavior.vanilla.TorchPlacementBehavior) Byte2ObjectMap(it.unimi.dsi.fastutil.bytes.Byte2ObjectMap) TranslationHelper.tr(org.lanternpowered.server.text.translation.TranslationHelper.tr) LogAxisRotationPlacementBehavior(org.lanternpowered.server.block.behavior.vanilla.LogAxisRotationPlacementBehavior) PASSABLE(org.lanternpowered.server.block.provider.property.PropertyProviderCollections.PASSABLE) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) PropertyProviders.hardness(org.lanternpowered.server.block.provider.property.PropertyProviders.hardness) PropertyProviderCollections(org.lanternpowered.server.block.provider.property.PropertyProviderCollections) LanternSandType(org.lanternpowered.server.data.type.LanternSandType) LanternShrubType(org.lanternpowered.server.data.type.LanternShrubType) LanternBlockState(org.lanternpowered.server.block.state.LanternBlockState) SpongeTranslationProvider(org.lanternpowered.server.block.translation.SpongeTranslationProvider) LanternSandstoneType(org.lanternpowered.server.data.type.LanternSandstoneType) SlabItemInteractionBehavior(org.lanternpowered.server.item.behavior.vanilla.SlabItemInteractionBehavior) PropertyProviders.blastResistance(org.lanternpowered.server.block.provider.property.PropertyProviders.blastResistance) Short2ObjectMap(it.unimi.dsi.fastutil.shorts.Short2ObjectMap) TileEntityTypes(org.spongepowered.api.block.tileentity.TileEntityTypes) PropertyProviders.flammableInfo(org.lanternpowered.server.block.provider.property.PropertyProviders.flammableInfo) EquipmentTypes(org.spongepowered.api.item.inventory.equipment.EquipmentTypes) LanternEnumTraits(org.lanternpowered.server.block.trait.LanternEnumTraits) AdditionalPluginCatalogRegistryModule(org.lanternpowered.server.game.registry.AdditionalPluginCatalogRegistryModule) OpenableContainerInteractionBehavior(org.lanternpowered.server.block.behavior.vanilla.OpenableContainerInteractionBehavior) LanternBlockType(org.lanternpowered.server.block.LanternBlockType)

Example 33 with BlockType

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);
}
Also used : DataView(org.spongepowered.api.data.DataView) BlockType(org.spongepowered.api.block.BlockType) LanternTexts(org.lanternpowered.server.text.LanternTexts) HashSet(java.util.HashSet)

Example 34 with BlockType

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);
}
Also used : ArrayList(java.util.ArrayList) BiomeType(org.spongepowered.api.world.biome.BiomeType) DataView(org.spongepowered.api.data.DataView) DataContainer(org.spongepowered.api.data.DataContainer) BlockType(org.spongepowered.api.block.BlockType) Nullable(javax.annotation.Nullable)

Example 35 with BlockType

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));
}
Also used : BlockType(org.spongepowered.api.block.BlockType)

Aggregations

BlockType (org.spongepowered.api.block.BlockType)91 World (org.spongepowered.api.world.World)34 BlockState (org.spongepowered.api.block.BlockState)23 Listener (org.spongepowered.api.event.Listener)21 Location (org.spongepowered.api.world.Location)17 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)16 ItemStack (org.spongepowered.api.item.inventory.ItemStack)14 Vector3i (com.flowpowered.math.vector.Vector3i)12 Optional (java.util.Optional)12 Player (org.spongepowered.api.entity.living.player.Player)12 List (java.util.List)10 Set (java.util.Set)10 Direction (org.spongepowered.api.util.Direction)10 Vector3d (com.flowpowered.math.vector.Vector3d)9 ArrayList (java.util.ArrayList)9 Keys (org.spongepowered.api.data.key.Keys)9 Sponge (org.spongepowered.api.Sponge)8 LanternBlockType (org.lanternpowered.server.block.LanternBlockType)7 Text (org.spongepowered.api.text.Text)7 HashMap (java.util.HashMap)6