33 |
34 |
35 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | org.shanerx
8 | java-mojang-api
9 | 1.0-SNAPSHOT
10 | http://wiki.vg/Mojang_API
11 | Java Mojang API
12 |
13 |
14 | UTF-8
15 |
16 |
17 |
18 | install
19 |
20 |
21 | org.apache.maven.plugins
22 | maven-compiler-plugin
23 | 3.0
24 |
25 | 1.8
26 | 1.8
27 |
28 |
29 |
30 |
31 | org.apache.maven.plugins
32 | maven-shade-plugin
33 | 3.0.0
34 |
35 |
36 |
37 |
38 |
39 |
40 | central
41 | Maven Repository Switchboard
42 | https://repo1.maven.org/maven2/
43 |
44 |
45 |
46 |
47 |
48 | com.mashape.unirest
49 | unirest-java
50 | 1.4.9
51 |
52 |
53 |
54 | com.googlecode.json-simple
55 | json-simple
56 | 1.1.1
57 |
58 |
59 |
60 |
61 |
--------------------------------------------------------------------------------
/src/main/java/org/shanerx/mojang/SalesStats.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016-2017 SparklingComet @ http://shanerx.org
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.shanerx.mojang;
18 |
19 | /**
20 | * This class is used for retrieving information from the Mojang store.
21 | */
22 | @SuppressWarnings("unused")
23 | public class SalesStats {
24 |
25 | private int total;
26 | private int last24hrs;
27 | private int salesPerSec;
28 |
29 | protected SalesStats(int total, int last24hrs, int salesPerSec) {
30 | this.total = total;
31 | this.last24hrs = last24hrs;
32 | this.salesPerSec = salesPerSec;
33 | }
34 |
35 | /**
36 | * Gets the total amount of sales over time.
37 | *
38 | * @return all the sales made
39 | */
40 | public int getTotal() {
41 | return total;
42 | }
43 |
44 | /**
45 | * Gets the sales made within the last 24 hours.
46 | *
47 | * @return the sales made today
48 | */
49 | public int getLast24hrs() {
50 | return last24hrs;
51 | }
52 |
53 | /**
54 | * Gets the sale rate, aka the average amount of sales in a second.
55 | *
56 | * @return the mean sale rate
57 | */
58 | public int getSaleVelocityPerSeconds() {
59 | return salesPerSec;
60 | }
61 |
62 | /**
63 | * This enum represents the possible options for the shop-API query.
64 | */
65 | public enum Options {
66 | ITEM_SOLD_MINECRAFT,
67 | PREPAID_CARD_REDEEMED_MINECRAFT,
68 | ITEM_SOLD_COBALT,
69 | ITEM_SOLD_SCROLLS;
70 |
71 | /**
72 | * Returns the string version of this enum, to be used when querying the API.
73 | *
74 | * @return the string
75 | */
76 | @Override
77 | public String toString() {
78 | return name().toLowerCase();
79 | }
80 | }
81 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Java Mojang API
2 | This repository provides a full Java-wrapper for the Mojang Web-API.
3 |
4 | More information on the API may be found on the [Wiki page](http://wiki.vg/Mojang_API).
5 |
6 | ## Getting Started
7 | * Clone the repository and compile it with Maven and add it to your build path.
8 | ```
9 | $ git clone https://github.com/SparklingComet/java-mojang-api.git
10 | $ mvn clean install
11 | ```
12 |
13 | * Alternatively, you can use Maven to include the dependency in your project:
14 | ```xml
15 |
16 |
17 |
18 |
19 |
20 | jitpack.io
21 | https://jitpack.io
22 |
23 |
24 |
25 |
26 |
27 |
28 | com.github.SparklingComet
29 | java-mojang-api
30 | -SNAPSHOT
31 |
32 |
33 |
34 | ```
35 |
36 | * You can also use Gradle as a dependency manager:
37 | ```gradle
38 | allprojects {
39 | repositories {
40 | ...
41 | maven { url 'https://jitpack.io' }
42 | }
43 | }
44 |
45 | dependencies {
46 | compile 'com.github.SparklingComet:java-mojang-api:-SNAPSHOT'
47 | }
48 | ```
49 |
50 | * More information on using dependency managers with this repository may be found [on Jitpack](https://jitpack.io/#SparklingComet/java-mojang-api).
51 | * Precompiled binaries will be made available on our CI server soon.
52 |
53 | ## Usage
54 | Once you have imported the library, use the following to establish a connection with Mojang's servers.
55 | ```java
56 | Mojang api = new Mojang().connect();
57 | ```
58 |
59 | ## Documentation
60 | * Generated HTML-Javadoc files are available for download in HTML-format in the `docs/` directory.
61 | * You may also use the live version ([click](https://sparklingcomet.github.io/java-mojang-api/index.html))
62 |
63 |
64 | ## License
65 | The project is licensed under the Apache License v2.0 ([view](https://github.com/SparklingComet/java-mojang-api/blob/master/LICENSE)).
66 |
67 | To license your own project with it, add the file its root and prefix your source files with
68 | ```java
69 | /*
70 | * Copyright 2016-2017 SparklingComet @ http://shanerx.org
71 | *
72 | * Licensed under the Apache License, Version 2.0 (the "License");
73 | * you may not use this file except in compliance with the License.
74 | * You may obtain a copy of the License at
75 | *
76 | * http://www.apache.org/licenses/LICENSE-2.0
77 | *
78 | * Unless required by applicable law or agreed to in writing, software
79 | * distributed under the License is distributed on an "AS IS" BASIS,
80 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
81 | * See the License for the specific language governing permissions and
82 | * limitations under the License.
83 | */
84 | ```
85 |
--------------------------------------------------------------------------------
/docs/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Generated Documentation (Untitled)
7 |
60 |
61 |
72 |
73 |
--------------------------------------------------------------------------------
/docs/deprecated-list.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Deprecated List
7 |
8 |
9 |
10 |
11 |
12 |
22 |
25 |
26 |
118 |
119 |
120 |
121 |
--------------------------------------------------------------------------------
/src/main/java/org/shanerx/mojang/PlayerProfile.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016-2017 SparklingComet @ http://shanerx.org
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.shanerx.mojang;
18 |
19 | import java.net.URL;
20 | import java.util.Map;
21 | import java.util.Optional;
22 | import java.util.Set;
23 |
24 | /**
25 | * This class contains the fields that represent the metadata of a player account and the methods to interact with it.
26 | */
27 | public class PlayerProfile {
28 |
29 | private String uuid;
30 | private String username;
31 | private Set properties;
32 | private Optional textures;
33 |
34 | /**
35 | * Represents a property.
36 | */
37 | public static class Property {
38 | String name;
39 | String value;
40 | String signature;
41 |
42 | public String getName() {
43 | return name;
44 | }
45 |
46 | public String getValue() {
47 | return value;
48 | }
49 |
50 | public String getSignature() {
51 | return signature;
52 | }
53 | }
54 |
55 | public static class TexturesProperty extends Property {
56 | long timestamp;
57 | String profileId, profileName;
58 | boolean signatureRequired = false;
59 | Map textures;
60 |
61 | public long getTimestamp() {
62 | return timestamp;
63 | }
64 |
65 | public String getProfileId() {
66 | return profileId;
67 | }
68 |
69 | public String getProfileName() {
70 | return profileName;
71 | }
72 |
73 | public boolean isSignatureRequired() {
74 | return signatureRequired;
75 | }
76 |
77 | public Map getTextures() {
78 | return textures;
79 | }
80 |
81 | public Optional getSkin() {
82 | return Optional.ofNullable(textures.get("SKIN"));
83 | }
84 |
85 | public Optional getCape() {
86 | return Optional.ofNullable(textures.get("CAPE"));
87 | }
88 | }
89 |
90 | /**
91 | *
Constructor for the class.
92 | *
You may use {@code new Mojang().connect().getPlayerProfile(uuid)} to retrieve the instance as it will verify the validity of the parameters.
93 | *
94 | * @param uuid the UUID of the player this object should represent
95 | * @param username the username of said player (you may use {@code new Mojang().connect().getNameHistoryOfPlayer(uuid)} to retrieve it).
96 | * @param properties the properties for that player. Depends on what you wish to do with the object
97 | */
98 | public PlayerProfile(String uuid, String username, Set properties) {
99 | this.uuid = uuid;
100 | this.username = username;
101 | this.properties = properties;
102 | this.textures = properties.stream().filter(p -> p.getName().equals("textures")).map(p -> (TexturesProperty) p).findAny();
103 | }
104 |
105 | /**
106 | * Gets the UUID of the player.
107 | *
108 | * @return the uuid as a {@link java.lang.String String}
109 | */
110 | public String getUUID() {
111 | return uuid;
112 | }
113 |
114 | /**
115 | * Gets the username of the player.
116 | *
117 | * @return the username as a {@link java.lang.String String}
118 | */
119 | public String getUsername() {
120 | return username;
121 | }
122 |
123 | /**
124 | *
Returns the properties this object has.
125 | *
This method exists for transparency, as the properties set is used internally.
126 | *
127 | * @return the properties {@link java.util.Set Set}
128 | */
129 | public Set getProperties() {
130 | return properties;
131 | }
132 |
133 | public Optional getTextures() {
134 | return textures;
135 | }
136 | }
--------------------------------------------------------------------------------
/docs/constant-values.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 | Constant Field Values
7 |
8 |
9 |
10 |
11 |
12 |
22 |
25 |
26 |
This API (Application Programming Interface) document has pages corresponding to the items in the navigation bar, described as follows.
73 |
74 |
75 |
76 |
77 |
Package
78 |
Each package has a page that contains a list of its classes and interfaces, with a summary for each. This page can contain six categories:
79 |
80 |
Interfaces (italic)
81 |
Classes
82 |
Enums
83 |
Exceptions
84 |
Errors
85 |
Annotation Types
86 |
87 |
88 |
89 |
Class/Interface
90 |
Each class, interface, nested class and nested interface has its own separate page. Each of these pages has three sections consisting of a class/interface description, summary tables, and detailed member descriptions:
91 |
92 |
Class inheritance diagram
93 |
Direct Subclasses
94 |
All Known Subinterfaces
95 |
All Known Implementing Classes
96 |
Class/interface declaration
97 |
Class/interface description
98 |
99 |
100 |
Nested Class Summary
101 |
Field Summary
102 |
Constructor Summary
103 |
Method Summary
104 |
105 |
106 |
Field Detail
107 |
Constructor Detail
108 |
Method Detail
109 |
110 |
Each summary entry contains the first sentence from the detailed description for that item. The summary entries are alphabetical, while the detailed descriptions are in the order they appear in the source code. This preserves the logical groupings established by the programmer.
111 |
112 |
113 |
Annotation Type
114 |
Each annotation type has its own separate page with the following sections:
115 |
116 |
Annotation Type declaration
117 |
Annotation Type description
118 |
Required Element Summary
119 |
Optional Element Summary
120 |
Element Detail
121 |
122 |
123 |
124 |
Enum
125 |
Each enum has its own separate page with the following sections:
126 |
127 |
Enum declaration
128 |
Enum description
129 |
Enum Constant Summary
130 |
Enum Constant Detail
131 |
132 |
133 |
134 |
Tree (Class Hierarchy)
135 |
There is a Class Hierarchy page for all packages, plus a hierarchy for each package. Each hierarchy page contains a list of classes and a list of interfaces. The classes are organized by inheritance structure starting with java.lang.Object. The interfaces do not inherit from java.lang.Object.
136 |
137 |
When viewing the Overview page, clicking on "Tree" displays the hierarchy for all packages.
138 |
When viewing a particular package, class or interface page, clicking "Tree" displays the hierarchy for only that package.
139 |
140 |
141 |
142 |
Deprecated API
143 |
The Deprecated API page lists all of the API that have been deprecated. A deprecated API is not recommended for use, generally due to improvements, and a replacement API is usually given. Deprecated APIs may be removed in future implementations.
144 |
145 |
146 |
Index
147 |
The Index contains an alphabetic list of all classes, interfaces, constructors, methods, and fields.
148 |
149 |
150 |
Prev/Next
151 |
These links take you to the next or previous class, interface, package, or related page.
152 |
153 |
154 |
Frames/No Frames
155 |
These links show and hide the HTML frames. All pages are available with or without frames.
156 |
157 |
158 |
All Classes
159 |
The All Classes link shows all classes and interfaces except non-static nested types.
160 |
161 |
162 |
Serialized Form
163 |
Each serializable or externalizable class has a description of its serialization fields and methods. This information is of interest to re-implementors, not to developers using the API. While there is no link in the navigation bar, you can get to this information by going to any serialized class and clicking "Serialized Form" in the "See also" section of the class description.
299 |
300 |
301 |
302 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/src/main/java/org/shanerx/mojang/Mojang.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2016-2017 SparklingComet @ http://shanerx.org
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package org.shanerx.mojang;
18 |
19 | import java.net.MalformedURLException;
20 | import java.net.URL;
21 | import java.util.Arrays;
22 | import java.util.Collections;
23 | import java.util.HashMap;
24 | import java.util.List;
25 | import java.util.Map;
26 | import java.util.Map.Entry;
27 | import java.util.Set;
28 | import java.util.stream.Collectors;
29 | import java.util.stream.Stream;
30 |
31 | import org.apache.commons.codec.binary.Base64;
32 | import org.json.simple.JSONArray;
33 | import org.json.simple.JSONObject;
34 | import org.json.simple.parser.JSONParser;
35 | import org.json.simple.parser.ParseException;
36 |
37 | import com.mashape.unirest.http.Unirest;
38 | import com.mashape.unirest.http.exceptions.UnirestException;
39 |
40 | /**
41 | *
This class represents the connection with the Mojang API.
42 | *
All instances of other classes of this wrapper API should be retrieved through this class.
43 | *
Remember to call api.connect() after creating an instance of this class.
44 | */
45 | @SuppressWarnings({ "unchecked" })
46 | public class Mojang
47 | {
48 |
49 | /**
50 | * Constructor. Initializes member variables.
51 | */
52 | public Mojang() {}
53 |
54 | /**
55 | *
Opens the connection with the Mojang API.
56 | * Should always be called after creating the API object.
57 | *
58 | *
Example:
59 | * Mojang api = new Mojang().connect();
60 | *
61 | * @return the api itself. Useful for concatenation.
62 | */
63 | public Mojang connect()
64 | {
65 | return this;
66 | }
67 |
68 | /**
69 | * @deprecated This service was closed down by Mojang on October 8th 2021 due to incorrect status responses.
70 | * https://wiki.vg/Mojang_API#API_Status_-_Outdated
71 | *
72 | * https://bugs.mojang.com/browse/WEB-2303?focusedCommentId=1086543&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-1086543
73 | */
74 | public ServiceStatus getStatus(ServiceType service)
75 | {
76 | return ServiceStatus.UNKNOWN;
77 | }
78 |
79 | /**
80 | * Retrieves the current UUID linked to a username.
81 | *
82 | * @param username the username
83 | *
84 | * @return the UUID as a {@link java.lang.String String}
85 | */
86 | public String getUUIDOfUsername(String username)
87 | {
88 | return (String) getJSONObject("https://api.mojang.com/users/profiles/minecraft/" + username).get("id");
89 | }
90 |
91 | /**
92 | * Retrieves the UUID linked to a username at a certain moment in time.
93 | *
94 | * @param username the username
95 | * @param timestamp the Java Timestamp that represents the time
96 | *
97 | * @return the UUID as a {@link java.lang.String String}
98 | */
99 | public String getUUIDOfUsername(String username, String timestamp)
100 | {
101 | return (String) getJSONObject("https://api.mojang.com/users/profiles/minecraft/" + username + "?at=" + timestamp).get("id");
102 | }
103 |
104 | /**
105 | * Retrieves all the username a certain UUID has had in the past, including the current one.
106 | *
107 | * @param uuid the UUID
108 | *
109 | * @return a map with the username as key value and the Timestamp as a {@link java.lang.Long Long}
110 | */
111 | public Map getNameHistoryOfPlayer(String uuid)
112 | {
113 | JSONArray arr = getJSONArray("https://api.mojang.com/user/profiles/" + uuid + "/names");
114 | Map history = new HashMap<>();
115 | arr.forEach(o ->
116 | {
117 | JSONObject obj = (JSONObject) o;
118 | history.put((String) obj.get("name"), obj.get("changedToAt") == null ? 0 : Long.parseLong(obj.get("changedToAt").toString()));
119 | });
120 | return history;
121 | }
122 |
123 | /**
124 | * Returns the {@link org.shanerx.mojang.PlayerProfile PlayerProfile} object which holds and represents the metadata for a certain account.
125 | *
126 | * @param uuid the UUID of the player
127 | *
128 | * @return the {@link org.shanerx.mojang.PlayerProfile PlayerProfile} object}
129 | */
130 | public PlayerProfile getPlayerProfile(String uuid) {
131 | JSONObject obj = getJSONObject("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid);
132 | String name = (String) obj.get("name");
133 | Set properties = (Set) ((JSONArray) obj.get("properties")).stream().map(o -> {
134 | PlayerProfile.Property p;
135 | JSONObject prop = (JSONObject) o;
136 |
137 | String propName = (String) prop.get("name");
138 | String propValue = (String) prop.get("value");
139 | if (propName.equals("textures")) {
140 | JSONObject tex;
141 | try {
142 | tex = (JSONObject) new JSONParser().parse(new String(Base64.decodeBase64(propValue)));
143 | } catch (ParseException e2) {
144 | /* Don't blame me, I just follow the pattern from #getJSONObject */
145 | throw new RuntimeException(e2);
146 | }
147 | PlayerProfile.TexturesProperty q = new PlayerProfile.TexturesProperty();
148 | q.timestamp = (Long) tex.get("timestamp");
149 | q.profileId = (String) tex.get("profileId");
150 | q.profileName = (String) tex.get("profileName");
151 | q.signatureRequired = Boolean.parseBoolean((String) tex.get("signatureRequired"));
152 | q.textures = ((Stream>) ((JSONObject) tex.get("textures")).entrySet().stream()).collect(Collectors.toMap(
153 | e -> (String) e.getKey(),
154 | e -> {
155 | try {
156 | return new URL((String) ((JSONObject) e.getValue()).get("url"));
157 | } catch (MalformedURLException e1) {
158 | /* I want lambdas with exceptions in Java, *please* */
159 | throw new RuntimeException("Wrapper for checked exception for lambda", e1);
160 | }
161 | }));
162 | p = q;
163 | } else
164 | p = new PlayerProfile.Property();
165 | p.name = propName;
166 | p.signature = (String) prop.get("signature");
167 | p.value = propValue;
168 | return p;
169 | }).collect(Collectors.toSet());
170 | return new PlayerProfile(uuid, name, properties);
171 | }
172 |
173 | /**
174 | * Updates the skin of a player using a URI.
175 | * This means that the image file will not be uploaded to Mojang's servers, hence the API will need to query the given URI.
176 | *
177 | * @param uuid the UUID of said player
178 | * @param token the token used for API authentication
179 | * @param skinType the {@link org.shanerx.mojang.Mojang.SkinType type} of the skin
180 | * @param skinUrl a direct URL to the skin
181 | */
182 | public void updateSkin(String uuid, String token, SkinType skinType, String skinUrl)
183 | {
184 | try
185 | {
186 | Unirest.post("https://api.mojang.com/user/profile/" + uuid + "/skin").header("Authorization", "Bearer " + token).field("model", skinType.toString()).field("url", skinUrl).asString();
187 | }
188 | catch (UnirestException e)
189 | {
190 | e.printStackTrace();
191 | }
192 | }
193 |
194 | /**
195 | * Updates the skin of a player using a URI.
196 | * The raw skin data will be uploaded to Mojang's servers and stored there potentially forever.
197 | *
198 | * @param uuid the UUID of said player
199 | * @param token the token used for API authentication
200 | * @param skinType the {@link org.shanerx.mojang.Mojang.SkinType type} of the skin
201 | * @param file the raw image data
202 | */
203 | @Untested
204 | public void updateAndUpload(String uuid, String token, SkinType skinType, String file)
205 | {
206 | try
207 | {
208 | Unirest.put("https://api.mojang.com/user/profile/" + uuid + "/skin").header("Authorization", "Bearer " + token).field("model", skinType.toString().equals("") ? "alex" : skinType.toString()).field("file", file).asString();
209 | }
210 | catch (UnirestException e)
211 | {
212 | e.printStackTrace();
213 | }
214 | }
215 |
216 | /**
217 | * Resets the skin to the default.
218 | *
219 | * @param uuid the UUID of the player
220 | * @param token the token used for API authentication
221 | */
222 | public void resetSkin(String uuid, String token)
223 | {
224 | try
225 | {
226 | Unirest.delete("https://api.mojang.com/user/profile/" + uuid + "/skin").header("Authorization", "Bearer " + token).asString();
227 | }
228 | catch (UnirestException e)
229 | {
230 | e.printStackTrace();
231 | }
232 | }
233 |
234 | /**
235 | *
Returns a list of blacklisted hostnames, belonging to servers that were blocked due to Mojang's EULA infringement.
236 | *
N.B.: These may not be in human friendly form as they were hashed. You may want to use third-party services to obtain an (unofficial) list.
237 | *
238 | * @return a {@link java.util.List List} of all the blocked hostnames
239 | */
240 | public List getServerBlacklist()
241 | {
242 | try
243 | {
244 | return Arrays.asList(Unirest.get("https://sessionserver.mojang.com/blockedservers").asString().getBody().split("\n"));
245 | }
246 | catch (UnirestException e)
247 | {
248 | e.printStackTrace();
249 | return null;
250 | }
251 | }
252 |
253 | /**
254 | * Returns the official mojang's product sales statistics.
255 | *
256 | * @param options the query {@link org.shanerx.mojang.SalesStats.Options options}
257 | *
258 | * @return the stats
259 | */
260 | @Untested
261 | public SalesStats getSaleStatistics(SalesStats.Options... options)
262 | {
263 | JSONArray arr = new JSONArray();
264 | Collections.addAll(arr, options);
265 |
266 | SalesStats stats = null;
267 | try
268 | {
269 | JSONObject resp = (JSONObject) new JSONParser().parse(Unirest.post("https://api.mojang.com/orders/statistics").field("metricKeys", arr).asString().getBody());
270 | stats = new SalesStats(Integer.valueOf((String) resp.get("total")), Integer.valueOf((String) resp.get("last24h")), Integer.valueOf((String) resp.get("saleVelocityPerSeconds")));
271 | }
272 | catch (Exception e)
273 | {
274 | e.printStackTrace();
275 | }
276 | return stats;
277 | }
278 |
279 | /**
280 | * This enum represents the possible Mojang API servers availability statuses.
281 | */
282 | public enum ServiceStatus
283 | {
284 |
285 | RED,
286 | YELLOW,
287 | GREEN,
288 | UNKNOWN
289 | }
290 |
291 | /**
292 | * This enum represents the various portions of the Mojang API.
293 | */
294 | public enum ServiceType
295 | {
296 |
297 | MINECRAFT_NET,
298 | SESSION_MINECRAFT_NET,
299 | ACCOUNT_MOJANG_COM,
300 | AUTHSERVER_MOJANG_COM,
301 | SESSIONSERVER_MOJANG_COM,
302 | API_MOJANG_COM,
303 | TEXTURES_MINECRAFT_NET,
304 | MOJANG_COM;
305 |
306 | /**
307 | *
This method overrides {@code java.lang.Object.toString()} and returns the address of the mojang api portion a certain enum constant represents.
308 | *
Returns an array containing the constants of this enum type, in
264 | the order they are declared. This method may be used to iterate
265 | over the constants as follows:
266 |
267 | for (Mojang.ServiceStatus c : Mojang.ServiceStatus.values())
268 | System.out.println(c);
269 |
270 |
271 |
Returns:
272 |
an array containing the constants of this enum type, in the order they are declared
Returns the enum constant of this type with the specified name.
284 | The string must match exactly an identifier used to declare an
285 | enum constant in this type. (Extraneous whitespace characters are
286 | not permitted.)
287 |
288 |
Parameters:
289 |
name - the name of the enum constant to be returned.
290 |
Returns:
291 |
the enum constant with the specified name
292 |
Throws:
293 |
java.lang.IllegalArgumentException - if this enum type has no constant with the specified name
294 |
java.lang.NullPointerException - if the argument is null
Returns an array containing the constants of this enum type, in
252 | the order they are declared. This method may be used to iterate
253 | over the constants as follows:
254 |
255 | for (Mojang.SkinType c : Mojang.SkinType.values())
256 | System.out.println(c);
257 |
258 |
259 |
Returns:
260 |
an array containing the constants of this enum type, in the order they are declared
public static Mojang.SkinType valueOf(java.lang.String name)
271 |
Returns the enum constant of this type with the specified name.
272 | The string must match exactly an identifier used to declare an
273 | enum constant in this type. (Extraneous whitespace characters are
274 | not permitted.)
275 |
276 |
Parameters:
277 |
name - the name of the enum constant to be returned.
278 |
Returns:
279 |
the enum constant with the specified name
280 |
Throws:
281 |
java.lang.IllegalArgumentException - if this enum type has no constant with the specified name
282 |
java.lang.NullPointerException - if the argument is null