Search in sources :

Example 6 with SignData

use of org.spongepowered.api.data.manipulator.mutable.tileentity.SignData in project modules-extra by CubeEngine.

the class Writer method editSignInSight.

/**
 * Edits the sign the user is looking at
 *
 * @param user  the user
 * @param line1 the 1st line
 * @param line2 the 2nd line
 * @param line3 the 3rd line
 * @param line4 the 4th line
 *
 * @return false if there is no sign to edit in sight
 */
public boolean editSignInSight(Player user, String line1, String line2, String line3, String line4) {
    Iterator<BlockRayHit<World>> it = BlockRay.from(user).distanceLimit(10).iterator();
    BlockRayHit<World> result = null;
    while (it.hasNext()) {
        BlockRayHit<World> next = it.next();
        BlockType type = next.getLocation().getBlockType();
        if (type == WALL_SIGN || type == STANDING_SIGN) {
            result = next;
            break;
        }
    }
    if (result == null) {
        return false;
    }
    Location<World> block = result.getLocation();
    BlockType type = block.getBlockType();
    if (type != WALL_SIGN && type != STANDING_SIGN) {
        return false;
    }
    SignData signData = block.get(SignData.class).get();
    ListValue<Text> lines = signData.lines();
    lines.set(0, line1 == null ? lines.get(0) : Text.of(line1));
    lines.set(1, line2 == null ? lines.get(1) : Text.of(line2));
    lines.set(2, line3 == null ? lines.get(2) : Text.of(line3));
    lines.set(3, line4 == null ? lines.get(3) : Text.of(line4));
    System.out.println(block.offer(signData).isSuccessful());
    i18n.send(user, POSITIVE, "The sign has been changed!");
    return true;
}
Also used : BlockType(org.spongepowered.api.block.BlockType) SignData(org.spongepowered.api.data.manipulator.mutable.tileentity.SignData) Text(org.spongepowered.api.text.Text) World(org.spongepowered.api.world.World) BlockRayHit(org.spongepowered.api.util.blockray.BlockRayHit)

Example 7 with SignData

use of org.spongepowered.api.data.manipulator.mutable.tileentity.SignData in project TotalEconomy by Erigitic.

the class JobManager method onSignInteract.

/**
 * Called when a player clicks a sign. If the clicked sign is a "Job Changing" sign then the player's job will
 * be changed on click.
 *
 * @param event InteractBlockEvent
 */
@Listener
public void onSignInteract(InteractBlockEvent.Secondary event) {
    if (event.getCause().first(Player.class).isPresent()) {
        Player player = event.getCause().first(Player.class).get();
        if (event.getTargetBlock().getLocation().isPresent()) {
            Optional<TileEntity> tileEntityOpt = event.getTargetBlock().getLocation().get().getTileEntity();
            if (tileEntityOpt.isPresent()) {
                TileEntity tileEntity = tileEntityOpt.get();
                if (tileEntity instanceof Sign) {
                    Sign sign = (Sign) tileEntity;
                    Optional<SignData> data = sign.getOrCreate(SignData.class);
                    if (data.isPresent()) {
                        SignData signData = data.get();
                        Text lineOneText = signData.lines().get(0);
                        Text lineTwoText = signData.lines().get(1);
                        String lineOne = lineOneText.toPlain();
                        String jobName = lineTwoText.toPlain().toLowerCase();
                        if (lineOne.equals("[TEJobs]")) {
                            if (jobExists(jobName)) {
                                if (setJob(player, jobName)) {
                                    Map<String, String> messageValues = new HashMap<>();
                                    messageValues.put("job", titleize(jobName));
                                    player.sendMessage(messageManager.getMessage("jobs.sign", messageValues));
                                } else {
                                    player.sendMessage(Text.of(TextColors.RED, "Failed to set job. Contact your administrator."));
                                }
                            } else {
                                player.sendMessage(Text.of(TextColors.RED, "Sorry, this job does not exist"));
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : TileEntity(org.spongepowered.api.block.tileentity.TileEntity) Player(org.spongepowered.api.entity.living.player.Player) SignData(org.spongepowered.api.data.manipulator.mutable.tileentity.SignData) Sign(org.spongepowered.api.block.tileentity.Sign) Text(org.spongepowered.api.text.Text) Listener(org.spongepowered.api.event.Listener)

Example 8 with SignData

use of org.spongepowered.api.data.manipulator.mutable.tileentity.SignData in project TotalEconomy by Erigitic.

the class JobManager method onJobSignCheck.

/**
 * Checks sign contents and converts it to a "Job Changing" sign if conditions are met
 *
 * @param event ChangeSignEvent
 */
@Listener
public void onJobSignCheck(ChangeSignEvent event) {
    SignData data = event.getText();
    Text lineOne = data.lines().get(0);
    Text lineTwo = data.lines().get(1);
    String lineOnePlain = lineOne.toPlain();
    String lineTwoPlain = lineTwo.toPlain();
    if (lineOnePlain.equals("[TEJobs]")) {
        lineOne = lineOne.toBuilder().style(TextStyles.BOLD).color(TextColors.DARK_BLUE).build();
        String jobName = titleize(lineTwoPlain);
        if (jobExists(lineTwoPlain)) {
            lineTwo = Text.of(jobName).toBuilder().color(TextColors.BLACK).build();
        } else {
            lineTwo = Text.of(jobName).toBuilder().color(TextColors.RED).build();
        }
        data.set(data.lines().set(0, lineOne));
        data.set(data.lines().set(1, lineTwo));
        data.set(data.lines().set(2, Text.of()));
        data.set(data.lines().set(3, Text.of()));
    }
}
Also used : SignData(org.spongepowered.api.data.manipulator.mutable.tileentity.SignData) Text(org.spongepowered.api.text.Text) Listener(org.spongepowered.api.event.Listener)

Aggregations

SignData (org.spongepowered.api.data.manipulator.mutable.tileentity.SignData)8 Text (org.spongepowered.api.text.Text)7 Listener (org.spongepowered.api.event.Listener)4 Sign (org.spongepowered.api.block.tileentity.Sign)3 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)2 Player (org.spongepowered.api.entity.living.player.Player)2 CPacketUpdateSign (net.minecraft.network.play.client.CPacketUpdateSign)1 TileEntitySign (net.minecraft.tileentity.TileEntitySign)1 TextComponentString (net.minecraft.util.text.TextComponentString)1 IMarketSignData (org.cubeengine.module.signmarket.data.IMarketSignData)1 ImmutableMarketSignData (org.cubeengine.module.signmarket.data.ImmutableMarketSignData)1 MarketSignData (org.cubeengine.module.signmarket.data.MarketSignData)1 BlockType (org.spongepowered.api.block.BlockType)1 ChangeSignEvent (org.spongepowered.api.event.block.tileentity.ChangeSignEvent)1 TextColor (org.spongepowered.api.text.format.TextColor)1 BlockRayHit (org.spongepowered.api.util.blockray.BlockRayHit)1 World (org.spongepowered.api.world.World)1 Inject (org.spongepowered.asm.mixin.injection.Inject)1