Search in sources :

Example 1 with BlockTrait

use of org.spongepowered.api.block.trait.BlockTrait in project SpongeCommon by SpongePowered.

the class BlockTypeRegistryModule method registerBlockTrait.

private void registerBlockTrait(String id, BlockType block) {
    Block nmsBlock = (Block) block;
    for (IBlockState state : nmsBlock.getBlockState().getValidStates()) {
        ((IMixinBlockState) state).generateId(nmsBlock);
        BlockStateRegistryModule.getInstance().registerBlockState((BlockState) state);
    }
    for (Map.Entry<BlockTrait<?>, ?> mapEntry : block.getDefaultState().getTraitMap().entrySet()) {
        BlockTrait<?> property = mapEntry.getKey();
        final String propertyId = BlockPropertyIdProvider.getIdAndTryRegistration((IProperty<?>) property, (Block) block, id);
        if (property instanceof IMixinPropertyHolder) {
            ((IMixinPropertyHolder) property).setId(propertyId);
        }
        if (property instanceof EnumTrait) {
            EnumTraitRegistryModule.getInstance().registerBlock(propertyId, block, (EnumTrait<?>) property);
        } else if (property instanceof IntegerTrait) {
            IntegerTraitRegistryModule.getInstance().registerBlock(propertyId, block, (IntegerTrait) property);
        } else if (property instanceof BooleanTrait) {
            BooleanTraitRegistryModule.getInstance().registerBlock(propertyId, block, (BooleanTrait) property);
        }
    }
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) BlockTrait(org.spongepowered.api.block.trait.BlockTrait) IMixinPropertyHolder(org.spongepowered.common.interfaces.block.IMixinPropertyHolder) Block(net.minecraft.block.Block) EnumTrait(org.spongepowered.api.block.trait.EnumTrait) BooleanTrait(org.spongepowered.api.block.trait.BooleanTrait) HashMap(java.util.HashMap) Map(java.util.Map) BiMap(com.google.common.collect.BiMap) HashBiMap(com.google.common.collect.HashBiMap) IMixinBlockState(org.spongepowered.common.interfaces.block.IMixinBlockState) IntegerTrait(org.spongepowered.api.block.trait.IntegerTrait)

Example 2 with BlockTrait

use of org.spongepowered.api.block.trait.BlockTrait in project TotalEconomy by Erigitic.

the class JobManager method onPlayerPlaceBlock.

/**
 * Used for the place option in jobs. Will check if the job has the place node and if it does it will check if the
 * block that was placed is present in the config of the player's job. If it is, it will grab the job exp reward as
 * well as the pay.
 *
 * @param event ChangeBlockEvent.Place
 */
@Listener
public void onPlayerPlaceBlock(ChangeBlockEvent.Place event) {
    if (event.getCause().first(Player.class).isPresent()) {
        Player player = event.getCause().first(Player.class).get();
        UUID playerUUID = player.getUniqueId();
        String playerJob = getPlayerJob(player);
        Optional<TEJob> optPlayerJob = getJob(playerJob, true);
        BlockState state = event.getTransactions().get(0).getFinal().getState();
        String blockName = state.getType().getName();
        // Enable admins to determine block information by displaying it to them - WHEN they have the flag enabled
        if (accountManager.getUserOption("totaleconomy:block-place-info", player).orElse("0").equals("1")) {
            List<BlockTrait<?>> traits = new ArrayList<>(state.getTraits());
            int count = traits.size();
            List<Text> traitTexts = new ArrayList<>(count);
            for (int i = 0; i < count; i++) {
                Object traitValue = state.getTraitValue(traits.get(i)).orElse(null);
                traitTexts.add(i, Text.of(traits.get(i).getName(), '=', traitValue != null ? traitValue.toString() : "null"));
            }
            Text t = Text.of(TextColors.GRAY, "TRAITS:\n    ", Text.joinWith(Text.of(",\n    "), traitTexts.toArray(new Text[traits.size()])));
            player.sendMessage(Text.of("Block-Name: ", blockName));
            player.sendMessage(t);
        }
        if (optPlayerJob.isPresent()) {
            Optional<TEActionReward> reward = Optional.empty();
            List<String> sets = optPlayerJob.get().getSets();
            for (String s : sets) {
                Optional<TEJobSet> optSet = getJobSet(s);
                if (!optSet.isPresent()) {
                    logger.warn("Job " + playerJob + " has the nonexistent set \"" + s + "\"");
                    continue;
                }
                Optional<TEAction> action = optSet.get().getActionFor("place", blockName);
                if (!action.isPresent()) {
                    continue;
                }
                Optional<TEActionReward> currentReward = action.get().evaluatePlace(logger, state);
                if (!reward.isPresent()) {
                    reward = currentReward;
                    continue;
                }
                if (!currentReward.isPresent()) {
                    continue;
                }
                // Use the one giving higher exp in case of duplicates
                if (currentReward.get().getExpReward() > reward.get().getExpReward()) {
                    reward = currentReward;
                }
            }
            if (reward.isPresent()) {
                TEAccount playerAccount = (TEAccount) accountManager.getOrCreateAccount(player.getUniqueId()).get();
                boolean notify = getNotificationState(playerUUID);
                int expAmount = reward.get().getExpReward();
                BigDecimal payAmount = new BigDecimal(reward.get().getMoneyReward());
                Currency currency = totalEconomy.getDefaultCurrency();
                if (reward.get().getCurrencyId() != null) {
                    Optional<Currency> currencyOpt = totalEconomy.getTECurrencyRegistryModule().getById("totaleconomy:" + reward.get().getCurrencyId());
                    if (currencyOpt.isPresent()) {
                        currency = currencyOpt.get();
                    }
                }
                if (notify) {
                    notifyPlayer(player, payAmount, currency);
                }
                addExp(player, expAmount);
                playerAccount.deposit(currency, payAmount, event.getCause());
                checkForLevel(player);
            }
        }
    }
}
Also used : BlockTrait(org.spongepowered.api.block.trait.BlockTrait) TEAccount(com.erigitic.config.TEAccount) Currency(org.spongepowered.api.service.economy.Currency) Player(org.spongepowered.api.entity.living.player.Player) Text(org.spongepowered.api.text.Text) BigDecimal(java.math.BigDecimal) BlockState(org.spongepowered.api.block.BlockState) Listener(org.spongepowered.api.event.Listener)

Example 3 with BlockTrait

use of org.spongepowered.api.block.trait.BlockTrait in project TotalEconomy by Erigitic.

the class JobManager method onPlayerBlockBreak.

/**
 * Used for the break option in jobs. Will check if the job has the break node and if it does it will check if the
 * block that was broken is present in the config of the player's job. If it is, it will grab the job exp reward as
 * well as the pay.
 *
 * @param event ChangeBlockEvent.Break
 */
@Listener
public void onPlayerBlockBreak(ChangeBlockEvent.Break event) {
    if (event.getCause().first(Player.class).isPresent()) {
        Player player = event.getCause().first(Player.class).get();
        UUID playerUUID = player.getUniqueId();
        String playerJob = getPlayerJob(player);
        Optional<TEJob> optPlayerJob = getJob(playerJob, true);
        BlockState state = event.getTransactions().get(0).getOriginal().getState();
        String blockName = state.getType().getName();
        Optional<UUID> blockCreator = event.getTransactions().get(0).getOriginal().getCreator();
        // Enable admins to determine block information by displaying it to them - WHEN they have the flag enabled
        if (accountManager.getUserOption("totaleconomy:block-break-info", player).orElse("0").equals("1")) {
            List<BlockTrait<?>> traits = new ArrayList<>(state.getTraits());
            int count = traits.size();
            List<Text> traitTexts = new ArrayList<>(count);
            for (int i = 0; i < count; i++) {
                Object traitValue = state.getTraitValue(traits.get(i)).orElse(null);
                traitTexts.add(i, Text.of(traits.get(i).getName(), '=', traitValue != null ? traitValue.toString() : "null"));
            }
            Text t = Text.of(TextColors.GRAY, "TRAITS:\n    ", Text.joinWith(Text.of(",\n    "), traitTexts.toArray(new Text[traits.size()])));
            player.sendMessage(Text.of("Block-Name: ", blockName));
            player.sendMessage(t);
        }
        if (optPlayerJob.isPresent()) {
            Optional<TEActionReward> reward = Optional.empty();
            List<String> sets = optPlayerJob.get().getSets();
            for (String s : sets) {
                Optional<TEJobSet> optSet = getJobSet(s);
                if (!optSet.isPresent()) {
                    logger.warn("Job " + playerJob + " has the nonexistent set \"" + s + "\"");
                    continue;
                }
                Optional<TEAction> action = optSet.get().getActionFor("break", blockName);
                if (!action.isPresent()) {
                    continue;
                }
                Optional<TEActionReward> currentReward = action.get().evaluateBreak(logger, state, blockCreator.orElse(null));
                if (!reward.isPresent()) {
                    reward = currentReward;
                    continue;
                }
                if (!currentReward.isPresent()) {
                    continue;
                }
                // Use the one giving higher exp in case of duplicates
                if (currentReward.get().getExpReward() > reward.get().getExpReward()) {
                    reward = currentReward;
                }
            }
            if (reward.isPresent()) {
                TEAccount playerAccount = (TEAccount) accountManager.getOrCreateAccount(player.getUniqueId()).get();
                boolean notify = getNotificationState(playerUUID);
                int expAmount = reward.get().getExpReward();
                BigDecimal payAmount = new BigDecimal(reward.get().getMoneyReward());
                Currency currency = totalEconomy.getDefaultCurrency();
                if (reward.get().getCurrencyId() != null) {
                    Optional<Currency> currencyOpt = totalEconomy.getTECurrencyRegistryModule().getById("totaleconomy:" + reward.get().getCurrencyId());
                    if (currencyOpt.isPresent()) {
                        currency = currencyOpt.get();
                    }
                }
                if (notify) {
                    notifyPlayer(player, payAmount, currency);
                }
                addExp(player, expAmount);
                playerAccount.deposit(currency, payAmount, event.getCause());
                checkForLevel(player);
            }
        }
    }
}
Also used : BlockTrait(org.spongepowered.api.block.trait.BlockTrait) TEAccount(com.erigitic.config.TEAccount) Currency(org.spongepowered.api.service.economy.Currency) Player(org.spongepowered.api.entity.living.player.Player) Text(org.spongepowered.api.text.Text) BigDecimal(java.math.BigDecimal) BlockState(org.spongepowered.api.block.BlockState) Listener(org.spongepowered.api.event.Listener)

Example 4 with BlockTrait

use of org.spongepowered.api.block.trait.BlockTrait in project LanternServer by LanternPowered.

the class LanternBlockState method toContainer.

@Override
public DataContainer toContainer() {
    final DataContainer dataContainer = DataContainer.createNew();
    dataContainer.set(DataQuery.of("BlockType"), this.baseState.getBlockType().getId());
    for (Map.Entry<BlockTrait<?>, Comparable<?>> entry : this.traitValues.entrySet()) {
        final Object value = entry.getValue();
        dataContainer.set(((LanternBlockTrait) entry.getKey()).getKey().getQuery(), value);
    }
    return dataContainer;
}
Also used : DataContainer(org.spongepowered.api.data.DataContainer) BlockTrait(org.spongepowered.api.block.trait.BlockTrait) LanternBlockTrait(org.lanternpowered.server.block.trait.LanternBlockTrait) LanternBlockTrait(org.lanternpowered.server.block.trait.LanternBlockTrait) ImmutableBiMap(com.google.common.collect.ImmutableBiMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 5 with BlockTrait

use of org.spongepowered.api.block.trait.BlockTrait in project Nucleus by NucleusPowered.

the class BlockInfoCommand method executeCommand.

@Override
public CommandResult executeCommand(Player player, CommandContext args) throws Exception {
    BlockRay<World> bl = BlockRay.from(player).distanceLimit(10).stopFilter(BlockRay.continueAfterFilter(BlockRay.onlyAirFilter(), 1)).build();
    Optional<BlockRayHit<World>> ob = bl.end();
    // If the last block is not air...
    if (ob.isPresent() && ob.get().getLocation().getBlockType() != BlockTypes.AIR) {
        BlockRayHit<World> brh = ob.get();
        // get the information.
        BlockState b = brh.getLocation().getBlock();
        BlockType it = b.getType();
        List<Text> lt = new ArrayList<>();
        lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.blockinfo.id", it.getId(), it.getTranslation().get()));
        lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.extendedid", b.getId()));
        if (args.hasAny("e") || args.hasAny("extended")) {
            Collection<Property<?, ?>> cp = b.getApplicableProperties();
            if (!cp.isEmpty()) {
                cp.forEach(x -> {
                    if (x.getValue() != null) {
                        DataScanner.getText(player, "command.blockinfo.property.item", x.getKey().toString(), x.getValue()).ifPresent(lt::add);
                    }
                });
            }
            Collection<BlockTrait<?>> cb = b.getTraits();
            if (!cb.isEmpty()) {
                cb.forEach(x -> b.getTraitValue(x).ifPresent(v -> DataScanner.getText(player, "command.blockinfo.traits.item", x.getName(), v).ifPresent(lt::add)));
            }
        }
        Sponge.getServiceManager().provideUnchecked(PaginationService.class).builder().contents(lt).padding(Text.of(TextColors.GREEN, "-")).title(plugin.getMessageProvider().getTextMessageWithFormat("command.blockinfo.list.header", String.valueOf(brh.getBlockX()), String.valueOf(brh.getBlockY()), String.valueOf(brh.getBlockZ()))).sendTo(player);
        return CommandResult.success();
    }
    player.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.blockinfo.none"));
    return CommandResult.empty();
}
Also used : RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) DataScanner(io.github.nucleuspowered.nucleus.internal.DataScanner) PermissionInformation(io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) HashMap(java.util.HashMap) GenericArguments(org.spongepowered.api.command.args.GenericArguments) BlockRay(org.spongepowered.api.util.blockray.BlockRay) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) ArrayList(java.util.ArrayList) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Map(java.util.Map) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) BlockTrait(org.spongepowered.api.block.trait.BlockTrait) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) TextColors(org.spongepowered.api.text.format.TextColors) BlockRayHit(org.spongepowered.api.util.blockray.BlockRayHit) CommandResult(org.spongepowered.api.command.CommandResult) BlockTypes(org.spongepowered.api.block.BlockTypes) Collection(java.util.Collection) Sponge(org.spongepowered.api.Sponge) PaginationService(org.spongepowered.api.service.pagination.PaginationService) CommandElement(org.spongepowered.api.command.args.CommandElement) BlockState(org.spongepowered.api.block.BlockState) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) World(org.spongepowered.api.world.World) BlockType(org.spongepowered.api.block.BlockType) Property(org.spongepowered.api.data.Property) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) BlockTrait(org.spongepowered.api.block.trait.BlockTrait) ArrayList(java.util.ArrayList) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) CommandResult(org.spongepowered.api.command.CommandResult) Text(org.spongepowered.api.text.Text) World(org.spongepowered.api.world.World) BlockState(org.spongepowered.api.block.BlockState) BlockType(org.spongepowered.api.block.BlockType) PaginationService(org.spongepowered.api.service.pagination.PaginationService) Property(org.spongepowered.api.data.Property) BlockRayHit(org.spongepowered.api.util.blockray.BlockRayHit)

Aggregations

BlockTrait (org.spongepowered.api.block.trait.BlockTrait)5 Map (java.util.Map)3 BlockState (org.spongepowered.api.block.BlockState)3 Player (org.spongepowered.api.entity.living.player.Player)3 Text (org.spongepowered.api.text.Text)3 TEAccount (com.erigitic.config.TEAccount)2 BigDecimal (java.math.BigDecimal)2 HashMap (java.util.HashMap)2 Listener (org.spongepowered.api.event.Listener)2 Currency (org.spongepowered.api.service.economy.Currency)2 BiMap (com.google.common.collect.BiMap)1 HashBiMap (com.google.common.collect.HashBiMap)1 ImmutableBiMap (com.google.common.collect.ImmutableBiMap)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 DataScanner (io.github.nucleuspowered.nucleus.internal.DataScanner)1 RunAsync (io.github.nucleuspowered.nucleus.internal.annotations.RunAsync)1 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)1 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)1 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)1 PermissionInformation (io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation)1