Skip to content

Commit 8f430a0

Browse files
committed
Include a JSON message API based on PowerMessage
As of Commodus v2, PowerMessage will become officially deprecated/unsupported and users will be redirected to this library instead. PowerMessage can be found here: http://github.com/DSH105/PowerMessage
1 parent 48827a0 commit 8f430a0

23 files changed

Lines changed: 1784 additions & 10 deletions

pom.xml

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@
8181
<version>1.1-SNAPSHOT</version>
8282
</dependency>
8383

84+
<!-- Gson - shaded -->
85+
<dependency>
86+
<groupId>com.google.code.gson</groupId>
87+
<artifactId>gson</artifactId>
88+
<version>2.1</version>
89+
</dependency>
90+
8491
<!-- Reflection -->
8592
<dependency>
8693
<groupId>com.captainbern</groupId>
@@ -151,20 +158,36 @@
151158
</configuration>
152159
</plugin>
153160

154-
<!-- JavaDoc -->
161+
<!-- Shade -->
155162
<plugin>
156163
<groupId>org.apache.maven.plugins</groupId>
157-
<artifactId>maven-javadoc-plugin</artifactId>
158-
<version>2.9.1</version>
159-
<configuration>
160-
<failOnError>false</failOnError>
161-
</configuration>
164+
<artifactId>maven-shade-plugin</artifactId>
165+
<version>2.1</version>
162166
<executions>
163167
<execution>
164-
<id>attach-javadocs</id>
168+
<phase>package</phase>
165169
<goals>
166-
<goal>jar</goal>
170+
<goal>shade</goal>
167171
</goals>
172+
<configuration>
173+
<minimizeJar>true</minimizeJar>
174+
<createDependencyReducedPom>false</createDependencyReducedPom>
175+
176+
<!-- Artifacts to shade -->
177+
<artifactSet>
178+
<includes>
179+
<artifact>com.google.code.gson:gson</artifact>
180+
</includes>
181+
</artifactSet>
182+
183+
<!-- Relocate everything into the project package -->
184+
<relocations>
185+
<relocation>
186+
<pattern>com.google.gson</pattern>
187+
<shadedPattern>com.dsh105.commodus.libraries.gson</shadedPattern>
188+
</relocation>
189+
</relocations>
190+
</configuration>
168191
</execution>
169192
</executions>
170193
</plugin>

src/main/java/com/dsh105/commodus/ServerBrand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static ServerBrand detectBrand() {
6363
try {
6464
Class.forName("org.bukkit.Bukkit");
6565
return initialiseBukkit(Bukkit.getVersion());
66-
} catch (ClassNotFoundException e) {
66+
} catch (ClassNotFoundException | NullPointerException e) {
6767
// ignore, obviously not running Bukkit
6868
}
6969

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* This file is part of Commodus.
3+
*
4+
* Commodus is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Commodus is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Commodus. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.dsh105.commodus.container;
19+
20+
import com.dsh105.commodus.reflection.Reflection;
21+
22+
public class AchievementContainer extends Container<AchievementContainer> {
23+
24+
// TODO: docs
25+
// TODO: sponge
26+
27+
private Object achievement;
28+
private String achievementName;
29+
30+
private AchievementContainer(Object achievement, String achievementName) {
31+
this.achievement = achievement;
32+
this.achievementName = achievementName;
33+
}
34+
35+
public static AchievementContainer from(org.bukkit.Achievement achievement) {
36+
Object nmsAchievement = Reflection.invokeStatic(Reflection.getMethod(Reflection.getOBCClass("CraftStatistic"), "getNMSAchievement", org.bukkit.Achievement.class), achievement);
37+
return new AchievementContainer(achievement, (String) Reflection.getFieldValue(Reflection.getNMSClass("Achievement"), nmsAchievement, "name"));
38+
}
39+
40+
public String getAchievementName() {
41+
return achievementName;
42+
}
43+
44+
public Object getAchievement() {
45+
return achievement;
46+
}
47+
48+
public org.bukkit.Achievement asBukkit() {
49+
if (achievement instanceof org.bukkit.Achievement) {
50+
return (org.bukkit.Achievement) achievement;
51+
}
52+
throw new IllegalStateException("Achievement contained is not a Bukkit Achievement.");
53+
}
54+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
* This file is part of Commodus.
3+
*
4+
* Commodus is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Commodus is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Commodus. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.dsh105.commodus.container;
19+
20+
import com.dsh105.commodus.ServerUtil;
21+
import com.dsh105.commodus.sponge.SpongeUtil;
22+
import com.google.common.base.Optional;
23+
import org.bukkit.Bukkit;
24+
import org.spongepowered.api.entity.living.Living;
25+
import org.spongepowered.api.world.World;
26+
27+
import java.util.UUID;
28+
29+
public class EntityContainer extends Container<EntityContainer> {
30+
31+
private UUID worldUID;
32+
private UUID entityUID;
33+
34+
private EntityContainer(UUID worldUID, UUID entityUID) {
35+
this.worldUID = worldUID;
36+
this.entityUID = entityUID;
37+
}
38+
39+
public static EntityContainer of(org.bukkit.entity.Entity entity) {
40+
return new EntityContainer(entity.getWorld().getUID(), entity.getUniqueId());
41+
}
42+
43+
public static EntityContainer of(org.spongepowered.api.entity.Entity entity) {
44+
return new EntityContainer(entity.getWorld().getUniqueID(), entity.getUniqueId());
45+
}
46+
47+
public org.bukkit.entity.Entity asBukkit() {
48+
for (org.bukkit.entity.Entity entity : Bukkit.getWorld(worldUID).getEntities()) {
49+
if (entity.getUniqueId().equals(entityUID)) {
50+
return entity;
51+
}
52+
}
53+
throw new IllegalStateException(String.format("Entity is no longer present (ID=%s)", entityUID));
54+
}
55+
56+
public org.spongepowered.api.entity.Entity asSponge() {
57+
Optional<World> world = SpongeUtil.getGame().getServer().get().getWorld(worldUID);
58+
if (!world.isPresent()) {
59+
throw new IllegalStateException(String.format("World is no longer present (ID=%s)", worldUID));
60+
}
61+
Optional<org.spongepowered.api.entity.Entity> entity = world.get().getEntityFromUUID(entityUID);
62+
if (!entity.isPresent()) {
63+
throw new IllegalStateException(String.format("Entity is no longer present (ID=%s)", entityUID));
64+
}
65+
return entity.get();
66+
}
67+
68+
public String getName() {
69+
switch (ServerUtil.getServerBrand().getCapsule()) {
70+
case BUKKIT:
71+
return asBukkit().getCustomName();
72+
case SPONGE:
73+
org.spongepowered.api.entity.Entity entity = asSponge();
74+
if (entity instanceof Living) {
75+
return ((Living) entity).getCustomName();
76+
}
77+
return "";
78+
}
79+
throw new IllegalStateException("Entity must be either a Bukkit or Sponge entity");
80+
}
81+
82+
public UUID getUID() {
83+
return entityUID;
84+
}
85+
86+
public String getTypeId() {
87+
switch (ServerUtil.getServerBrand().getCapsule()) {
88+
case BUKKIT:
89+
return asBukkit().getType().getName();
90+
case SPONGE:
91+
return asSponge().getType().getId();
92+
}
93+
throw new IllegalStateException("Entity must be either a Bukkit or Sponge entity");
94+
}
95+
}

src/main/java/com/dsh105/commodus/container/ItemStackContainer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ public org.spongepowered.api.item.inventory.ItemStack asSponge(String name, List
112112
GameRegistry gameRegistry = SpongeUtil.getGame().getRegistry();
113113
// TODO: name/loer
114114
// TODO: meta
115-
return gameRegistry.getItemBuilder().itemType(gameRegistry.getItem(id).get()).quantity(quantity).build();
115+
return null;
116+
//return gameRegistry.getItemBuilder().itemType(gameRegistry.getItem(id).get()).quantity(quantity).build();
116117
}
117118

118119
public String getId() {

src/main/java/com/dsh105/commodus/container/PlayerContainer.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.dsh105.commodus.ServerUtil;
2121
import com.dsh105.commodus.StringUtil;
2222
import com.dsh105.commodus.bukkit.BukkitIdentUtil;
23+
import com.dsh105.commodus.message.Message;
2324
import com.dsh105.commodus.sponge.SpongeIdentUtil;
2425

2526
import java.util.HashMap;
@@ -87,4 +88,14 @@ public org.bukkit.entity.Player asBukkit() {
8788
public org.spongepowered.api.entity.player.Player asSponge() {
8889
return SpongeIdentUtil.getPlayer(playerUID);
8990
}
91+
92+
public void sendMessage(Message... messages) {
93+
for (Message message : messages) {
94+
sendMessage(message);
95+
}
96+
}
97+
98+
public void sendMessage(Message message) {
99+
message.send(this);
100+
}
90101
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* This file is part of Commodus.
3+
*
4+
* Commodus is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Commodus is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Commodus. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.dsh105.commodus.container;
19+
20+
import com.dsh105.commodus.Affirm;
21+
import com.dsh105.commodus.reflection.Reflection;
22+
import org.bukkit.Material;
23+
import org.bukkit.Statistic;
24+
import org.bukkit.entity.Entity;
25+
import org.bukkit.entity.EntityType;
26+
27+
public class StatisticContainer extends Container<StatisticContainer> {
28+
29+
// TODO: docs
30+
// TODO: sponge
31+
32+
private Object statistic;
33+
private String statisticName;
34+
35+
private StatisticContainer(Object statistic, String statisticName) {
36+
this.statistic = statistic;
37+
this.statisticName = statisticName;
38+
}
39+
40+
public static StatisticContainer from(Statistic statistic) {
41+
Affirm.isTrue(statistic.getType() == Statistic.Type.UNTYPED, "That statistic requires an additional " + statistic.getType() + " parameter!");
42+
Object statisticHandle = Reflection.invokeStatic(Reflection.getMethod(Reflection.getOBCClass("CraftStatistic"), "getNMSStatistic", Statistic.class), statistic);
43+
return new StatisticContainer(statistic, (String) Reflection.getFieldValue(Reflection.getNMSClass("Statistic"), statisticHandle, "name"));
44+
}
45+
46+
public static StatisticContainer from(Statistic statistic, Material material) {
47+
if (statistic.getType() == Statistic.Type.UNTYPED) {
48+
return from(statistic);
49+
}
50+
Affirm.isTrue(!((statistic.getType() == Statistic.Type.BLOCK && statistic.isBlock()) || statistic.getType() == Statistic.Type.ENTITY), "Wrong parameter type for that statistic - needs " + statistic.getType() + "!");
51+
Object statisticHandle = Reflection.invokeStatic(Reflection.getMethod(Reflection.getOBCClass("CraftStatistic"), "getMaterialStatistic", Statistic.class, Material.class), statistic, material);
52+
return new StatisticContainer(statistic, (String) Reflection.getFieldValue(Reflection.getNMSClass("Statistic"), statisticHandle, "name"));
53+
}
54+
55+
public static StatisticContainer from(Statistic statistic, Entity entity) {
56+
if (statistic.getType() == Statistic.Type.UNTYPED) {
57+
return from(statistic);
58+
}
59+
Affirm.isTrue(statistic.getType() != Statistic.Type.ENTITY, "Wrong parameter type for that statistic - needs " + statistic.getType() + "!");
60+
Object statisticHandle = Reflection.invokeStatic(Reflection.getMethod(Reflection.getOBCClass("CraftStatistic"), "getEntityStatistic", Statistic.class, EntityType.class), statistic, entity);
61+
return new StatisticContainer(statistic, (String) Reflection.getFieldValue(Reflection.getNMSClass("Statistic"), statisticHandle, "name"));
62+
}
63+
64+
public String getStatisticName() {
65+
return statisticName;
66+
}
67+
68+
public Object getStatistic() {
69+
return statistic;
70+
}
71+
72+
public org.bukkit.Statistic asBukkit() {
73+
if (statistic instanceof org.bukkit.Statistic) {
74+
return (org.bukkit.Statistic) statistic;
75+
}
76+
throw new IllegalStateException("Statistic contained is not a Bukkit Statstic.");
77+
}
78+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* This file is part of Commodus.
3+
*
4+
* Commodus is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* Commodus is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with Commodus. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.dsh105.commodus.message;
19+
20+
import com.google.gson.stream.JsonWriter;
21+
import org.spongepowered.api.text.action.TextAction;
22+
23+
import java.io.IOException;
24+
25+
public interface Action<R> extends JsonWritable {
26+
27+
String getId();
28+
29+
R getResult();
30+
31+
String getResultString();
32+
33+
String getEventName();
34+
35+
TextAction<?> getSpongeAction();
36+
37+
abstract class AbstractAction<R> implements Action<R> {
38+
39+
@Override
40+
public String getResultString() {
41+
return getResult().toString();
42+
}
43+
44+
@Override
45+
public JsonWriter writeJson(JsonWriter writer) throws IOException {
46+
writer.name(getEventName() + "Event").beginObject().name("action").value(getId()).name("value").value(getResultString()).endObject();
47+
return writer;
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)