Created
September 2, 2023 14:55
-
-
Save whynotmax/57fbb57287aebab571edf20f39db8d76 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dev.mzcy.citybuild.utils.builder; | |
import dev.mzcy.citybuild.CityBuild; | |
import dev.mzcy.citybuild.utils.Utils; | |
import lombok.AccessLevel; | |
import lombok.experimental.FieldDefaults; | |
import net.kyori.adventure.text.Component; | |
import net.kyori.adventure.text.minimessage.MiniMessage; | |
import org.apache.commons.lang3.Validate; | |
import org.bukkit.entity.Player; | |
@FieldDefaults(level = AccessLevel.PRIVATE) | |
public class MessageBuilder { | |
boolean useComponents = false; | |
boolean withPrefix = false; | |
boolean useMiniMessage = false; | |
boolean smallLetters = false; | |
int repeat = 1; | |
String message = ""; | |
private MessageBuilder() { | |
} | |
public static MessageBuilder builder() { | |
return new MessageBuilder(); | |
} | |
public MessageBuilder repeat(int repeat) { | |
this.repeat = repeat; | |
return this; | |
} | |
public MessageBuilder withPrefix() { | |
this.withPrefix = true; | |
return this; | |
} | |
public MessageBuilder useComponents() { | |
this.useComponents = true; | |
return this; | |
} | |
public MessageBuilder useMiniMessage() { | |
this.useMiniMessage = true; | |
return this; | |
} | |
public MessageBuilder smallLetters() { | |
this.smallLetters = true; | |
return this; | |
} | |
public MessageBuilder message(String message) { | |
this.message = message; | |
return this; | |
} | |
public void send(Player player) { | |
Validate.notNull(player, "Player cannot be null"); | |
Validate.notNull(message, "Message cannot be null"); | |
boolean colorCode = false; | |
if (smallLetters) { | |
this.message = Utils.replaceLetters(this.message); | |
} | |
if (withPrefix) { | |
this.message = CityBuild.PREFIX + this.message; | |
} | |
if (useMiniMessage) { | |
while (repeat-- > 0) { | |
player.sendMessage(MiniMessage.miniMessage().deserialize(this.message)); | |
} | |
return; | |
} | |
if (useComponents) { | |
while (repeat-- > 0) { | |
player.sendMessage(Component.text(this.message)); | |
} | |
return; | |
} | |
while (repeat-- > 0) { | |
player.sendMessage(this.message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment