Skip to content

Commit

Permalink
v1.0.9 Update
Browse files Browse the repository at this point in the history
- Minor updates to Message.java class
- Minor updates to ReflectionAPI.java class
  • Loading branch information
AlbeMiglio committed Aug 22, 2020
1 parent 5b6e9ac commit 9751b17
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 10 deletions.
4 changes: 2 additions & 2 deletions Example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>PowerLibAPI</artifactId>
<groupId>it.mycraft</groupId>
<version>1.0.8</version>
<version>1.0.9</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>PowerLibExample</artifactId>
Expand Down Expand Up @@ -59,7 +59,7 @@
<dependency>
<groupId>it.mycraft</groupId>
<artifactId>PowerLib</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
2 changes: 1 addition & 1 deletion Java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>PowerLibAPI</artifactId>
<groupId>it.mycraft</groupId>
<version>1.0.8</version>
<version>1.0.9</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
51 changes: 51 additions & 0 deletions Java/src/main/java/it/mycraft/powerlib/chat/Message.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package it.mycraft.powerlib.chat;

import it.mycraft.powerlib.utils.ColorAPI;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.Nullable;

Expand All @@ -13,15 +14,23 @@ public class Message {
private String message;
private List<String> messages;

public Message() {
this.message = "";
this.messages = new ArrayList<>();
}

public Message(String message) {
this.message = ColorAPI.color(message);
this.messages = new ArrayList<>();
}

public Message(String... messages) {
this.message = "";
this.messages = new ArrayList<>(ColorAPI.color(Arrays.asList(messages)));
}

public Message(List<String> messages) {
this.message = "";
this.messages = new ArrayList<>(ColorAPI.color(messages));
}

Expand All @@ -36,6 +45,14 @@ public Message addPlaceHolder(String placeholder, Object value) {
return this;
}

public void set(String message) {
this.message = message;
}

public void set(List<String> messages) {
this.messages = messages;
}

@Nullable
public String getText(){
return message;
Expand All @@ -55,6 +72,40 @@ public void send(CommandSender commandSender) {
reset();
}

public void broadcast(String permission) {
if(!permission.equalsIgnoreCase("")) {
if (messages.isEmpty())
Bukkit.broadcastMessage(message);
else
messages.forEach(Bukkit::broadcastMessage);
}
else {
if (messages.isEmpty())
Bukkit.broadcast(message, permission);
else
messages.forEach((m) -> Bukkit.broadcast(m, permission));
}
}

public void color() {
if(messages.isEmpty()) {
this.message = ColorAPI.color(message);
}
else this.messages = ColorAPI.color(messages);
}

public void decolor() {
if(messages.isEmpty()) {
this.message = ColorAPI.decolor(message);
}
else this.messages = ColorAPI.decolor(messages);
}

public void broadcast() {
broadcast("");
}


private void reset() {
messages = new ArrayList<>();
message = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@
import it.mycraft.powerlib.PowerLib;
import lombok.Getter;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.lang.reflect.Constructor;
import java.util.logging.Level;

public class ReflectionAPI {

/**
* @return the package-name of the NMS version
* Returns The package-name of the NMS version
*/
@Getter
private static String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];

/**
* @return the server's numerical version
* Returns The server's numerical version
*/
@Getter
private static int numericalVersion = Integer.parseInt(version.split("_")[1]);

/**
* @param name The path of a 'net.minecraft.server.v1_X_RX.' class
* @return The NMS class object
* @return The NMS class object
*/
public static Class<?> getNMSClass(String name) {
try {
Expand All @@ -35,7 +37,7 @@ public static Class<?> getNMSClass(String name) {

/**
* @param name The path of a 'org.bukkit.craftbukkit.v1_X_RX.' class
* @return The OBC class object
* @return The OBC class object
*/
public static Class<?> getOBCClass(String name) {
try {
Expand All @@ -46,4 +48,56 @@ public static Class<?> getOBCClass(String name) {
}
}

/**
* Sends a custom Title and Subtitle to a player
* @param player The player to display the title to
* @param title The title being sent
* @param subtitle The subtitle being sent
* @param fadeIn The "fade-in" time
* @param stay The "stay" time
* @param fadeOut The "fade-out" time
*/
public static void sendTitle(Player player, String title, String subtitle, int fadeIn, int stay, int fadeOut) {
try {

Class<?> PacketPlayOutTitleClass = ReflectionAPI.getNMSClass("PacketPlayOutTitle");
Class<?> IChatBaseComponentClass = ReflectionAPI.getNMSClass("IChatBaseComponent");

Constructor<?> constructor = PacketPlayOutTitleClass.getConstructor(PacketPlayOutTitleClass
.getDeclaredClasses()[0], IChatBaseComponentClass, int.class, int.class, int.class);

Object titleComponent = IChatBaseComponentClass.getDeclaredClasses()[0].getMethod("a", String.class)
.invoke(null, "{\"text\":\"" + title + "\"}");
Object subTitleComponent = IChatBaseComponentClass.getDeclaredClasses()[0].getMethod("a", String.class)
.invoke(null, "{\"text\":\"" + subtitle + "\"}");

Object titlePacket = constructor.newInstance(PacketPlayOutTitleClass
.getDeclaredClasses()[0].getField("TITLE").get(null), titleComponent, fadeIn, stay, fadeOut);
Object subTitlePacket = constructor.newInstance(PacketPlayOutTitleClass
.getDeclaredClasses()[0].getField("SUBTITLE")
.get(null), subTitleComponent, fadeIn, stay, fadeOut);

sendPacket(player, titlePacket);
sendPacket(player, subTitlePacket);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Sends a packet to a certain player
* @param player The player to send the packet to
* @param packet The packet being sent
*/
public static void sendPacket(Player player, Object packet) {
try {
Object playerHandle = player.getClass().getMethod("getHandle", new Class[0]).invoke(player);
Object playerConnection = playerHandle.getClass().getField("playerConnection").get(playerHandle);
playerConnection.getClass().getMethod("sendPacket", new Class[]{ReflectionAPI.getNMSClass("Packet")})
.invoke(playerConnection, packet);
} catch (Exception e) {
e.printStackTrace();
}
}

}
2 changes: 1 addition & 1 deletion Java/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: PowerLib
authors: [AlbeMiglio, pompiere1]
main: it.mycraft.powerlib.PowerLib
version: 1.0.8
version: 1.0.9
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
<dependency>
<groupId>com.github.AlbeMiglio.PowerLib</groupId>
<artifactId>PowerLib</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
</dependency>
```
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<groupId>it.mycraft</groupId>
<artifactId>PowerLibAPI</artifactId>
<packaging>pom</packaging>
<version>1.0.8</version>
<version>1.0.9</version>
<modules>
<module>Example</module>
<module>Java</module>
Expand Down

0 comments on commit 9751b17

Please sign in to comment.