Skip to content

Commit

Permalink
MessageEvent changes
Browse files Browse the repository at this point in the history
Signed-off-by: Walker Crouse <[email protected]>
  • Loading branch information
windy1 committed Feb 29, 2016
1 parent fcbeab5 commit 6b09b2b
Show file tree
Hide file tree
Showing 11 changed files with 363 additions and 83 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ buildscript {
dependencies {
classpath 'nl.javadude.gradle.plugins:license-gradle-plugin:0.11.0'
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.3'
classpath 'org.spongepowered:event-impl-gen:1.0.0'
classpath 'org.spongepowered:event-impl-gen:1.1.0'
classpath 'org.spongepowered:spongegradle:0.3.1-SNAPSHOT'
}
}
Expand Down Expand Up @@ -60,7 +60,7 @@ dependencies {

// Event generation
compile 'org.ow2.asm:asm:5.0.3'
compile 'org.spongepowered:event-gen-core:1.0.0'
compile 'org.spongepowered:event-gen-core:1.1.0'
}

// JAR manifest configuration
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/spongepowered/api/event/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* <p>This is a marker interface, which must be implemented
* by any event used with the Sponge event bus.</p>
*/
@ImplementedBy(AbstractEvent.class)
@ImplementedBy(value = AbstractEvent.class, priority = Integer.MIN_VALUE)
public interface Event {
/**
* Get the cause for the event.
Expand Down
120 changes: 60 additions & 60 deletions src/main/java/org/spongepowered/api/event/SpongeEventFactory.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
package org.spongepowered.api.event.achievement;

import org.spongepowered.api.event.Cancellable;
import org.spongepowered.api.event.message.MessageChannelEvent;
import org.spongepowered.api.event.entity.living.humanoid.player.TargetPlayerEvent;
import org.spongepowered.api.event.message.MessageChannelEvent;
import org.spongepowered.api.statistic.achievement.Achievement;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@

import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.entity.living.Living;
import org.spongepowered.api.event.message.MessageChannelEvent;
import org.spongepowered.api.event.entity.living.TargetLivingEvent;
import org.spongepowered.api.event.message.MessageChannelEvent;

/**
* An event where the {@link Entity} is being either removed usually due to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@
*/
public interface KickPlayerEvent extends TargetPlayerEvent, MessageChannelEvent {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.api.event.impl;

import org.spongepowered.api.event.message.MessageEvent;
import org.spongepowered.api.eventgencore.annotation.UseField;
import org.spongepowered.api.text.Text;

/**
* Abstract implementation of {@link MessageEvent}. Contains a
* {@link MessageFormatter} instance to hold the message data.
*/
@SuppressWarnings("NullableProblems")
public abstract class AbstractMessageEvent extends AbstractEvent implements MessageEvent {

@UseField protected MessageFormatter formatter;
@UseField protected Text originalMessage;

@Override
protected final void init() {
this.originalMessage = this.formatter.format();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,6 @@ interface Chat extends MessageChannelEvent, Cancellable {
* @return The raw message
*/
Text getRawMessage();

}
}
249 changes: 237 additions & 12 deletions src/main/java/org/spongepowered/api/event/message/MessageEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,262 @@
*/
package org.spongepowered.api.event.message;

import static com.google.common.base.Preconditions.checkNotNull;
import static org.spongepowered.api.text.TextTemplate.arg;
import static org.spongepowered.api.text.TextTemplate.of;

import org.spongepowered.api.event.Event;
import org.spongepowered.api.event.impl.AbstractMessageEvent;
import org.spongepowered.api.eventgencore.annotation.ImplementedBy;
import org.spongepowered.api.eventgencore.annotation.PropertySettings;
import org.spongepowered.api.text.Text;

import java.util.Optional;

import javax.annotation.Nullable;
import org.spongepowered.api.text.TextRepresentable;
import org.spongepowered.api.text.transform.FixedPartitionedTextFormatter;
import org.spongepowered.api.text.transform.SimpleTextFormatter;
import org.spongepowered.api.text.transform.SimpleTextTemplateApplier;
import org.spongepowered.api.text.transform.TextTemplateApplier;

/**
* Describes events when a involving a {@link Text} message.
*/
@ImplementedBy(value = AbstractMessageEvent.class)
public interface MessageEvent extends Event {

/**
* Parameter for header section in header partition.
*/
String PARAM_MESSAGE_HEADER = "header";

/**
* Parameter for body section in body partition.
*/
String PARAM_MESSAGE_BODY = "body";

/**
* Parameter for footer section in footer partition.
*/
String PARAM_MESSAGE_FOOTER = "footer";

/**
* Gets the original {@link Text} message.
*
* @return The message if present, otherwise {@link Optional#empty()}
* @return The message
*/
@PropertySettings(requiredParameter = false)
Text getOriginalMessage();

/**
* Returns true if the {@link Text} returned by
* {@link #getMessage()} should not be sent.
*
* @return True if message should not be sent
*/
boolean isMessageCancelled();

/**
* Sets whether the {@link Text} returned by
* {@link #getMessage()} should be sent.
*
* @param cancelled True if should not be sent
*/
void setMessageCancelled(boolean cancelled);

/**
* Returns the {@link MessageFormatter} used to mutate the event's message.
*
* @return MessageFormatter of event
*/
MessageFormatter getFormatter();

/**
* Returns the formatted message.
*
* @return Message
*/
@PropertySettings(requiredParameter = false, generateMethods = false)
default Text getMessage() {
return getFormatter().format();
}

/**
* Clears the currently set message and returns the empty formatter.
*
* @return Empty {@link MessageFormatter}
*/
default MessageFormatter clearMessage() {
MessageFormatter formatter = getFormatter();
formatter.clear();
return formatter;
}

/**
* Clears any existing components and sets the header, body, and footer
* respectively.
*
* @param header Header value
* @param body Body value
* @param footer Footer value
*/
Optional<Text> getOriginalMessage();
default void setMessage(TextRepresentable header, TextRepresentable body, TextRepresentable footer) {
checkNotNull(header, "header");
checkNotNull(body, "body");
checkNotNull(footer, "footer");
MessageFormatter formatter = clearMessage();
formatter.setHeader(header);
formatter.setBody(body);
formatter.setFooter(footer);
}

/**
* Gets the new {@link Text} message.
* Clears any existing components and sets the header and body
* respectively.
*
* @return The new message if present, otherwise {@link Optional#empty()}
* @param header Header value
* @param body Body value
*/
Optional<Text> getMessage();
default void setMessage(TextRepresentable header, TextRepresentable body) {
checkNotNull(header, "header");
checkNotNull(body, "body");
MessageFormatter formatter = clearMessage();
formatter.setHeader(header);
formatter.setBody(body);
}

/**
* Sets the new {@link Text} message.
* Clears any existing components and sets the body.
*
* @param message The new message
* @param body Body value
*/
default void setMessage(TextRepresentable body) {
checkNotNull(body, "body");
MessageFormatter formatter = clearMessage();
formatter.setBody(body);
}

/**
* Formatter used for formatting messages within this event. This formatter
* is partitioned into three sections: header, body, and footer. This is in
* an effort to make formatting messages much more flexible and modular for
* developers allowing for appending/inserting prefixes, adding suffixes,
* etc.
*/
class MessageFormatter extends FixedPartitionedTextFormatter {

public MessageFormatter() {
super(3);
}

public MessageFormatter(Text header, Text body) {
this();
checkNotNull(header, "header");
checkNotNull(body, "body");
getHeader().add(new DefaultHeaderApplier(header));
getBody().add(new DefaultBodyApplier(body));
}

public MessageFormatter(Text body) {
this();
checkNotNull(body, "body");
getBody().add(new DefaultBodyApplier(body));
}

/**
* Returns the header partition within this formatter.
*
* @return Header partition
*/
public SimpleTextFormatter getHeader() {
return get(0);
}

/**
* Clears any existing components within the header partition and adds
* a new {@link TextTemplateApplier} to the partition with the
* specified value.
*
* @param header Header value
*/
public void setHeader(TextRepresentable header) {
set(0, PARAM_MESSAGE_HEADER, header);
}

/**
* Returns the body partition within this formatter.
*
* @return Body partition
*/
public SimpleTextFormatter getBody() {
return get(1);
}

/**
* Clears any existing components within the body partition and adds
* a new {@link TextTemplateApplier} to the partition with the
* specified value.
*
* @param body Body value
*/
public void setBody(TextRepresentable body) {
set(1, PARAM_MESSAGE_BODY, body);
}

/**
* Returns the footer partition within this formatter.
*
* @return Footer partition
*/
public SimpleTextFormatter getFooter() {
return get(2);
}

/**
* Clears any existing components within the footer partition and adds
* a new {@link TextTemplateApplier} to the partition with the
* specified value.
*
* @param footer Footer value
*/
public void setFooter(TextRepresentable footer) {
set(2, PARAM_MESSAGE_FOOTER, footer);
}

private void set(int i, String key, TextRepresentable value) {
checkNotNull(value, "value");
SimpleTextFormatter partition = get(i);
partition.clear();
SimpleTextTemplateApplier applier = new SimpleTextTemplateApplier(of(arg(key)));
applier.setParameter(key, value);
partition.add(applier);
}

}

/**
* Represents the initial header set by the implementation before the event
* is posted.
*/
void setMessage(@Nullable Text message);
final class DefaultHeaderApplier extends SimpleTextTemplateApplier {

public DefaultHeaderApplier(TextRepresentable value) {
super(of('<', arg(PARAM_MESSAGE_HEADER), "> "));
checkNotNull(value, "value");
setParameter(PARAM_MESSAGE_HEADER, value);
}

}

/**
* Represents the initial body set by the implementation before the event
* is posted.
*/
final class DefaultBodyApplier extends SimpleTextTemplateApplier {

public DefaultBodyApplier(TextRepresentable value) {
super(of(arg(PARAM_MESSAGE_BODY)));
checkNotNull(value, "value");
setParameter(PARAM_MESSAGE_BODY, value);
}

}

}
Loading

0 comments on commit 6b09b2b

Please sign in to comment.