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);
}
}
}
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);
}
}
}
}
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);
}
}
}
}
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;
}
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();
}
Aggregations