Skip to content

Commit fa59f6c

Browse files
author
BuildTools
committed
0.3.22.22 NPCs, Conig, Mojang and GUI.
1 parent 5f5d808 commit fa59f6c

33 files changed

+277
-30
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.exortions</groupId>
88
<artifactId>ExosPluginUtils</artifactId>
9-
<version>0.3.20.22</version>
9+
<version>0.3.22.22</version>
1010
<description>ExosPluginUtils, or PluginUtils for short, is a simple, easy-to-use
1111
library that saves many Minecraft Spigot developers a lot of time when creating
1212
plugins. It allows Spigot developers to create a lot of NMS

src/main/java/com/exortions/pluginutils/config/Configuration.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class Configuration {
1919

2020
@Getter
2121
private final FileConfiguration config;
22+
@Getter
2223
private final File file;
2324

2425
/**
@@ -32,19 +33,27 @@ public Configuration(JavaPlugin plugin, String path) {
3233
config = YamlConfiguration.loadConfiguration(file);
3334
}
3435

36+
public boolean createFile() throws IOException {
37+
return file.createNewFile();
38+
}
39+
3540
public Object get(String path) {
3641
return config.get(path);
3742
}
3843

39-
public void set(String path, Object o) {
44+
public void set(String path, Object o, boolean save) {
4045
try {
4146
config.set(path, o);
42-
config.save(file);
47+
if(save) config.save(file);
4348
} catch (IOException e){
4449
e.printStackTrace();
4550
}
4651
}
4752

53+
public void save() throws IOException {
54+
config.save(file);
55+
}
56+
4857
public String getString(String path) {
4958
return (String) get(path);
5059
}

src/main/java/com/exortions/pluginutils/example/ExamplePlugin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import com.exortions.pluginutils.command.CommandUtils;
44
import com.exortions.pluginutils.listener.ListenerUtils;
5-
import com.exortions.pluginutils.npc.NPC;
65
import com.exortions.pluginutils.startup.StartupUtils;
76
import lombok.Getter;
87
import lombok.Setter;
8+
import net.minecraft.server.v1_16_R3.EntityPlayer;
99
import org.bukkit.Bukkit;
1010
import org.bukkit.event.Listener;
1111
import org.bukkit.plugin.java.JavaPlugin;
@@ -22,7 +22,7 @@ public class ExamplePlugin extends JavaPlugin implements Listener {
2222
@Getter @Setter
2323
private HashMap<UUID, Integer> explosiveStickCooldowns;
2424
@Getter @Setter
25-
private List<NPC> npcs;
25+
private List<EntityPlayer> npcs;
2626

2727
@Override
2828
public void onEnable() {

src/main/java/com/exortions/pluginutils/example/commands/NPCCommand.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.exortions.pluginutils.command.PluginCommand;
66
import com.exortions.pluginutils.example.ExamplePlugin;
77
import com.exortions.pluginutils.npc.NPC;
8+
import net.minecraft.server.v1_16_R3.EntityPlayer;
89
import org.bukkit.ChatColor;
910
import org.bukkit.entity.Player;
1011

@@ -14,15 +15,15 @@
1415
public class NPCCommand extends PluginCommand {
1516
@Override
1617
public void execute(Player player, String[] args) {
17-
List<NPC> npcs = ExamplePlugin.getPlugin().getNpcs();
18+
List<EntityPlayer> npcs = ExamplePlugin.getPlugin().getNpcs();
1819
if(args.length == 1) { // Create npc with player's skin.
1920
NPC npc = new NPC(player.getLocation(), ChatUtils.colorize(args[0]), player.getDisplayName());
20-
npcs.add(npc);
21+
npcs.add(npc.getNpc());
2122
ExamplePlugin.getPlugin().setNpcs(npcs);
2223
npc.sendPacketToAllPlayers();
2324
} else if(args.length == 2) { // Create npc with specified skin.
2425
NPC npc = new NPC(player.getLocation(), ChatUtils.colorize(args[0]), args[1]);
25-
npcs.add(npc);
26+
npcs.add(npc.getNpc());
2627
ExamplePlugin.getPlugin().setNpcs(npcs);
2728
npc.sendPacketToAllPlayers();
2829
} else {

src/main/java/com/exortions/pluginutils/example/listeners/EventListener.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
import com.exortions.pluginutils.bossbar.BossbarUtils;
44
import com.exortions.pluginutils.example.ExamplePlugin;
5+
import com.exortions.pluginutils.npc.PacketReader;
56
import com.exortions.pluginutils.scoreboard.ScoreboardUtils;
7+
import com.exortions.pluginutils.scoreboard.TeamUtils;
68
import com.exortions.pluginutils.tablist.TablistUtils;
79
import org.bukkit.Bukkit;
810
import org.bukkit.ChatColor;
@@ -12,12 +14,17 @@
1214
import org.bukkit.event.EventHandler;
1315
import org.bukkit.event.Listener;
1416
import org.bukkit.event.player.PlayerJoinEvent;
17+
import org.bukkit.event.player.PlayerQuitEvent;
1518
import org.bukkit.scheduler.BukkitRunnable;
1619

1720
public class EventListener implements Listener {
1821

1922
@EventHandler
2023
public void onPlayerJoin(PlayerJoinEvent e) {
24+
// The next two lines are required for NPC Events to fire.
25+
PacketReader reader = new PacketReader();
26+
reader.inject(e.getPlayer(), ExamplePlugin.getPlugin().getNpcs());
27+
2128
ScoreboardUtils scoreboard = new ScoreboardUtils(Bukkit.getScoreboardManager(), e.getPlayer());
2229
scoreboard.setTitle("&e&lSTATS");
2330
scoreboard.addLines("&e" + Bukkit.getServer().getOnlinePlayers().size() + "/" + Bukkit.getServer().getMaxPlayers(),
@@ -56,6 +63,16 @@ public void run() {
5663
bossbar.addFlag(BarFlag.DARKEN_SKY);
5764
bossbar.setVisible(true);
5865
bossbar.addPlayer(e.getPlayer());
66+
67+
TeamUtils team = new TeamUtils("adminrank");
68+
team.setPrefix("&c[ADMIN] ");
69+
team.addPlayer(e.getPlayer());
70+
}
71+
72+
@EventHandler
73+
public void onPlayerQuit(PlayerQuitEvent e) {
74+
PacketReader reader = new PacketReader();
75+
reader.uninject(e.getPlayer());
5976
}
6077

6178
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.exortions.pluginutils.gui;
2+
3+
import org.bukkit.entity.HumanEntity;
4+
import org.bukkit.inventory.Inventory;
5+
6+
public abstract class GUIContainer {
7+
8+
public Inventory inv;
9+
10+
public abstract void init();
11+
12+
public void open(final HumanEntity humanEntity) {
13+
humanEntity.openInventory(inv);
14+
}
15+
16+
}

src/main/java/com/exortions/pluginutils/mojang/SkinUtils.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.exortions.pluginutils.mojang;
22

3+
import com.google.gson.JsonElement;
34
import com.google.gson.JsonObject;
45
import com.google.gson.JsonParser;
56
import org.bukkit.entity.Player;
@@ -8,8 +9,56 @@
89
import java.io.InputStreamReader;
910
import java.net.URL;
1011

12+
/**
13+
* @author Exortions
14+
* @since 0.3.18.22
15+
*/
1116
public class SkinUtils {
1217

18+
public static String getPropertyByPlayer(Player player, String property) throws IOException {
19+
URL url = new URL("https://api.mojang.com/users/profiles/minecraft/" + player.getDisplayName());
20+
InputStreamReader reader = new InputStreamReader(url.openStream());
21+
return new JsonParser().parse(reader).getAsJsonObject().get(property).getAsString();
22+
}
23+
24+
public static JsonElement getPropertyJsonObjectByPlayer(Player player, String property) throws IOException {
25+
URL url = new URL("https://api.mojang.com/users/profiles/minecraft/" + player.getDisplayName());
26+
InputStreamReader reader = new InputStreamReader(url.openStream());
27+
return new JsonParser().parse(reader).getAsJsonObject().get(property);
28+
}
29+
30+
public static Object getSessionServerInfoAsJsonObjectByPlayer(Player player, Property property) throws IOException {
31+
URL url = new URL("https://sessionserver.mojang.com/session/minecraft/profile/" + player.getUniqueId() + "?unasigned=false");
32+
InputStreamReader reader = new InputStreamReader(url.openStream());
33+
JsonObject jsonProperty = new JsonParser().parse(reader).getAsJsonObject().get("properties").getAsJsonArray().get(0).getAsJsonObject();
34+
switch (property) {
35+
case NAME:
36+
return getPropertyJsonObjectByPlayer(player, "name");
37+
case ID:
38+
return getPropertyJsonObjectByPlayer(player, "id");
39+
case PROPERTY:
40+
return jsonProperty;
41+
case PROPERTY_TEXTURE:
42+
return jsonProperty.get("value").getAsString();
43+
case PROPERTY_SIGNATURE:
44+
return jsonProperty.get("signature").getAsString();
45+
case PROPERTY_SIGNATURE_AND_TEXTURE:
46+
return new String[] {jsonProperty.get("value").getAsString(), jsonProperty.get("signature").getAsString()};
47+
}
48+
return null;
49+
}
50+
51+
public enum Property {
52+
53+
ID,
54+
NAME,
55+
PROPERTY,
56+
PROPERTY_TEXTURE,
57+
PROPERTY_SIGNATURE,
58+
PROPERTY_SIGNATURE_AND_TEXTURE
59+
60+
}
61+
1362
public static String[] getSkin(Player player) throws IOException {
1463
URL url = new URL("https://api.mojang.com/users/profiles/minecraft/" + player.getDisplayName());
1564
InputStreamReader reader = new InputStreamReader(url.openStream());

src/main/java/com/exortions/pluginutils/npc/NPC.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.exortions.pluginutils.mojang.SkinUtils;
44
import com.mojang.authlib.GameProfile;
55
import com.mojang.authlib.properties.Property;
6+
import lombok.Getter;
67
import net.minecraft.server.v1_16_R3.EntityPlayer;
78
import net.minecraft.server.v1_16_R3.MinecraftServer;
89
import net.minecraft.server.v1_16_R3.PlayerInteractManager;
@@ -32,6 +33,7 @@ public class NPC {
3233

3334
private final GameProfile profile;
3435

36+
@Getter
3537
private final EntityPlayer npc;
3638

3739
/**

src/main/java/com/exortions/pluginutils/npc/PacketReader.java

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
import java.util.UUID;
1818

1919
/**
20+
* An NPC Packet utility class.
21+
* Usage:
22+
*
23+
* <code>
24+
* PlayerJoinEvent:
25+
* PacketReader reader = new PacketReader();
26+
* reader.inject(e.getPlayer());
27+
* PlayerQuitEvent:
28+
* PacketReader reader = new PacketReader();
29+
* reader.uninject(e.getPlayer());
30+
* </code>
31+
*
2032
* @author Exortions
2133
* @since 0.3.20.22
2234
*/
@@ -26,18 +38,14 @@ public class PacketReader {
2638
Channel channel;
2739
public static Map<UUID, Channel> channels = new HashMap<>();
2840

29-
private HashMap<EntityPlayer, Integer> npcs;
41+
private List<EntityPlayer> npcs;
3042

3143
/**
32-
* OnJoin:
33-
* PacketReader reader = new PacketReader();
34-
* reader.inject(e.getPlayer());
35-
* OnQuit:
36-
* PacketReader reader = new PacketReader();
37-
* reader.uninject(e.getPlayer());
38-
* @param player
44+
* Inject packet reader into a player.
45+
* @param player Player to inject packet reader to.
46+
* @param npcs The list of current NPCs.
3947
*/
40-
public void inject(@NotNull Player player, @NotNull HashMap<EntityPlayer, Integer> npcs) {
48+
public void inject(@NotNull Player player, @NotNull List<EntityPlayer> npcs) {
4149
CraftPlayer craftPlayer = (CraftPlayer) player;
4250
channel = craftPlayer.getHandle().playerConnection.networkManager.channel;
4351
channels.put(player.getUniqueId(), channel);
@@ -48,13 +56,17 @@ public void inject(@NotNull Player player, @NotNull HashMap<EntityPlayer, Intege
4856

4957
channel.pipeline().addAfter("decoder", "PacketInjector", new MessageToMessageDecoder<PacketPlayInUseEntity>() {
5058
@Override
51-
protected void decode(ChannelHandlerContext channel, PacketPlayInUseEntity packet, List<Object> arg) throws Exception {
59+
protected void decode(ChannelHandlerContext channel, PacketPlayInUseEntity packet, List<Object> arg) {
5260
arg.add(packet);
5361
readPacket(player, packet);
5462
}
5563
});
5664
}
5765

66+
/**
67+
* Uninject the packet reader from the Player.
68+
* @param player The player to uninject the packet reader from.
69+
*/
5870
public void uninject(Player player) {
5971
channel = channels.get(player.getUniqueId());
6072
if(channel.pipeline().get("PacketInjector") != null) channel.pipeline().remove("PacketInjector");
@@ -63,23 +75,27 @@ public void uninject(Player player) {
6375
public void readPacket(Player player, PacketPlayInUseEntity packet) {
6476
if(packet.getClass().getSimpleName().equalsIgnoreCase("PacketPlayInUseEntity")) {
6577

78+
int id = (int) getValue(packet, "a");
79+
6680
if(getValue(packet, "action").toString().equalsIgnoreCase("ATTACK")) {
67-
// Left click
81+
for (EntityPlayer npc : npcs) {
82+
if(npc.getId() == id) {
83+
Bukkit.getPluginManager().callEvent(new PlayerLeftClickNPCEvent(player, npc));
84+
}
85+
}
6886
}
6987

7088
if(getValue(packet, "d").toString().equalsIgnoreCase("OFF_HAND")) {
71-
// Off hand
89+
return;
7290
}
7391

7492
if(getValue(packet, "action").toString().equalsIgnoreCase("INTERACT_AT")) {
75-
// Left click
93+
return;
7694
}
7795

78-
int id = (int) getValue(packet, "a");
79-
8096
if(getValue(packet, "action").toString().equalsIgnoreCase("INTERACT")) {
8197
// Right click
82-
for (EntityPlayer npc : npcs.keySet()) {
98+
for (EntityPlayer npc : npcs) {
8399
if(npc.getId() == id) {
84100
Bukkit.getPluginManager().callEvent(new PlayerRightClickNPCEvent(player, npc));
85101
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.exortions.pluginutils.npc;
2+
3+
import net.minecraft.server.v1_16_R3.EntityPlayer;
4+
import org.bukkit.entity.Player;
5+
import org.bukkit.event.Cancellable;
6+
import org.bukkit.event.Event;
7+
import org.bukkit.event.HandlerList;
8+
import org.jetbrains.annotations.NotNull;
9+
10+
/**
11+
* Fires when a player right clicks
12+
* and NPC and the player has a
13+
* PacketReader injected.
14+
* @author Exortions
15+
* @since 0.3.20.22
16+
*/
17+
@SuppressWarnings({"FieldCanBeLocal", "unused"})
18+
public class PlayerLeftClickNPCEvent extends Event implements Cancellable {
19+
20+
private final Player player;
21+
private final EntityPlayer npc;
22+
private boolean isCancelled;
23+
24+
private static final HandlerList HANDLERS = new HandlerList();
25+
26+
public PlayerLeftClickNPCEvent(Player player, EntityPlayer npc) {
27+
this.player = player;
28+
this.npc = npc;
29+
}
30+
31+
@Override
32+
public boolean isCancelled() {
33+
return isCancelled;
34+
}
35+
36+
@Override
37+
public void setCancelled(boolean b) {
38+
isCancelled = b;
39+
}
40+
41+
@NotNull
42+
@Override
43+
public HandlerList getHandlers() {
44+
return HANDLERS;
45+
}
46+
47+
@NotNull
48+
public static HandlerList getHandlerList() {
49+
return HANDLERS;
50+
}
51+
}

0 commit comments

Comments
 (0)