use of org.spongepowered.api.data.manipulator.mutable.tileentity.SignData in project TotalEconomy by Erigitic.
the class TEJobManager 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 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 lineTwo = lineTwoText.toPlain().toLowerCase();
if (lineOne.equals("[TEJobs]")) {
if (jobExists(lineTwo)) {
if (setJob(player, lineTwo)) {
player.sendMessage(Text.of(TextColors.GRAY, "Job changed to: ", TextColors.GOLD, lineTwo));
} else {
player.sendMessage(Text.of(TextColors.RED, "[TE] Failed to set job. Contact your administrator."));
}
} else {
player.sendMessage(Text.of(TextColors.RED, "[TE] Sorry, this job does not exist"));
}
}
}
}
}
}
}
}
use of org.spongepowered.api.data.manipulator.mutable.tileentity.SignData in project modules-extra by CubeEngine.
the class MarketSignManager method updateSignText.
public void updateSignText(MarketSignData data, Location<World> loc) {
SignData sign = loc.get(SignData.class).orElse(null);
if (sign == null) {
return;
}
boolean isValid = isValidSign(data, null);
boolean inEditMode = isInEditMode(loc);
boolean isAdmin = data.isAdminOwner();
// First Line: SignType
TextColor color = inEditMode ? DARK_PURPLE : !isValid ? TextColors.DARK_RED : isAdmin ? TextColors.BLUE : TextColors.DARK_BLUE;
if (this.signInventories.containsKey(data.getID())) {
color = LIGHT_PURPLE;
}
String raw;
if (data.getSignType() != null) {
// Buy or Sell
raw = data.getSignType().getName();
} else if (// Edit
inEditMode) {
raw = "Edit";
} else {
// Invalid
raw = "Invalid";
}
if (// Append Admin?
isAdmin) {
raw = "Admin-" + raw;
} else if (// !isAdmin
!inEditMode && isValid) {
if (data.getSignType() == SignType.BUY && data.getStock() == 0) {
color = TextColors.RED;
// Replace with Sold out
raw = "Sold out";
} else if (data.getSignType() == SignType.SELL && data.isSatisfied()) {
color = TextColors.RED;
// Replace with Satisfied
raw = "Satisfied";
}
}
Text line1 = Text.of(color, TextStyles.BOLD, raw);
// Second Line: Item
Text line2 = getItemText(data, inEditMode);
// Third Line: Amount
color = inEditMode ? DARK_PURPLE : TextColors.DARK_RED;
raw = "No amount";
if (data.getAmount() != null) {
raw = data.getAmount().toString();
color = TextColors.BLACK;
}
String raw2 = "";
TextColor color2 = TextColors.RED;
if (data.getStock() != null && data.getAmount() != null) {
if (data.getSignType() == SignType.BUY) {
raw2 = " x" + data.getStock();
if (data.getStock() >= data.getAmount()) {
color2 = TextColors.DARK_BLUE;
}
} else // Sell
{
raw2 = " x?";
color2 = TextColors.AQUA;
if (data.getDemand() != null) {
raw2 = " x" + (data.getDemand() - data.getStock());
if (data.getStock() >= data.getDemand()) {
color2 = TextColors.DARK_RED;
}
}
}
}
Text line3 = Text.of(color, raw, color2, raw2);
// Fourth Line: Price
color = inEditMode ? DARK_PURPLE : TextColors.DARK_RED;
Text line4 = Text.of(color, "No Price");
if (data.getPrice() != null && data.getPrice() != 0) {
line4 = formatPrice(data).toBuilder().color(TextColors.BLACK).build();
}
sign.setElements(Arrays.asList(line1, line2, line3, line4));
loc.offer(sign);
}
use of org.spongepowered.api.data.manipulator.mutable.tileentity.SignData in project TotalEconomy by Erigitic.
the class TEJobManager 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().color(TextColors.GOLD).build();
String jobName = lineTwoPlain.toLowerCase();
if (jobExists(lineTwoPlain)) {
lineTwo = Text.of(jobName).toBuilder().color(TextColors.GRAY).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()));
}
}
use of org.spongepowered.api.data.manipulator.mutable.tileentity.SignData in project SpongeCommon by SpongePowered.
the class MixinNetHandlerPlayServer method callSignChangeEvent.
/**
* @author Zidane
*
* Invoke before {@code System.arraycopy(packetIn.getLines(), 0, tileentitysign.signText, 0, 4);} (line 1156 in source) to call SignChangeEvent.
* @param packetIn Injected packet param
* @param ci Info to provide mixin on how to handle the callback
* @param worldserver Injected world param
* @param blockpos Injected blockpos param
* @param tileentity Injected tilentity param
* @param tileentitysign Injected tileentitysign param
*/
@Inject(method = "processUpdateSign", at = @At(value = "INVOKE", target = UPDATE_SIGN), cancellable = true, locals = LocalCapture.CAPTURE_FAILHARD)
public void callSignChangeEvent(CPacketUpdateSign packetIn, CallbackInfo ci, WorldServer worldserver, BlockPos blockpos, IBlockState iblockstate, TileEntity tileentity, TileEntitySign tileentitysign) {
ci.cancel();
final Optional<SignData> existingSignData = ((Sign) tileentitysign).get(SignData.class);
if (!existingSignData.isPresent()) {
// TODO Unsure if this is the best to do here...
throw new RuntimeException("Critical error! Sign data not present on sign!");
}
final SignData changedSignData = existingSignData.get().copy();
final ListValue<Text> lines = changedSignData.lines();
for (int i = 0; i < packetIn.getLines().length; i++) {
lines.set(i, SpongeTexts.toText(new TextComponentString(packetIn.getLines()[i])));
}
changedSignData.set(lines);
// I pass changedSignData in here twice to emulate the fact that even-though the current sign data doesn't have the lines from the packet
// applied, this is what it "is" right now. If the data shown in the world is desired, it can be fetched from Sign.getData
Sponge.getCauseStackManager().pushCause(this.player);
final ChangeSignEvent event = SpongeEventFactory.createChangeSignEvent(Sponge.getCauseStackManager().getCurrentCause(), changedSignData.asImmutable(), changedSignData, (Sign) tileentitysign);
if (!SpongeImpl.postEvent(event)) {
((Sign) tileentitysign).offer(event.getText());
} else {
// If cancelled, I set the data back that was fetched from the sign. This means that if its a new sign, the sign will be empty else
// it will be the text of the sign that was showing in the world
((Sign) tileentitysign).offer(existingSignData.get());
}
Sponge.getCauseStackManager().popCause();
tileentitysign.markDirty();
worldserver.getPlayerChunkMap().markBlockForUpdate(blockpos);
}
use of org.spongepowered.api.data.manipulator.mutable.tileentity.SignData in project RedProtect by FabioZumbi12.
the class RegionBuilder method setErrorSign.
protected void setErrorSign(ChangeSignEvent e, String error) {
SignData sign = e.getText();
sign = sign.set(sign.getValue(Keys.SIGN_LINES).get().set(0, RPUtil.toText(RPLang.get("regionbuilder.signerror"))));
this.setError(e.getCause().first(Player.class).get(), error);
}
Aggregations