Commit 01b190bc authored by Magix's avatar Magix Committed by GitHub
Browse files

UPGRADE TO 1.1.0 POG

Merge `development` into `stable`
parents 6b81b888 1beddf16
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400&display=swap">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/css/bootstrap.min.css">
<style>
body {
background-color: #f0f0f0;
}
p {
font-weight:300;
}
a,a:hover {
text-decoration:none !important;
color:#626976;
}
.content {
padding:3rem 0;
}
.container {
color:#626976;
position: relative;
}
h2 {
font-size:20px;
}
.custom-table {
min-width:900px;
}
.custom-table thead tr,.custom-table thead th {
padding-bottom:30px;
color:#000;
}
.custom-table tbody th,.custom-table tbody td {
color:#777;
font-weight:400;
padding-bottom:20px;
padding-top:20px;
font-weight:300;
border:none;
}
.yellow {
color: rgb(255, 162, 0);
}
.blue {
color: rgb(75, 107, 251);
}
.purple {
color: rgb(242, 40, 242);
}
</style>
<title>Gacha Records</title>
<script>
// Debug entry
// record = [
// {"time": 10000341, "item": 1041},
// {"time": 10000342, "item": 1032},
// {"time": 10000343, "item": 1035},
// ];
// maxPage = 5;
// in production environment
record = {{REPLACE_RECORD}};
maxPage = {{REPLACE_MAXPAGE}};
// TODO: implement this mapper by yourself
// I don't want to put real items' name here to avoid being DMCA'd
mappings = {
'en-us': {
200: "Standard",
301: "Event Avatar",
302: "Event Weapon",
1041 : ["M0n4", "blue"],
1032 : ["B4nn477", "purple"],
1035 : ["77", "yellow"]
},
'zh-cn': {
// encoding issues here, maybe we should consider load mappings remotely
// will display as "锟斤铐锟斤铐锟斤铐", lmao
// 200: "常驻",
// 301: "角色UP-1",
// 302: "武器UP"
200: "Standard",
301: "Event Avatar",
302: "Event Weapon",
}
};
</script>
<!-- This file could be generated automatically using `java -jar grasscutter.jar -gachamap` -->
<!-- You can also modify the file manually to customize it -->
<!-- Otherwise you may onle see number IDs in the gacha record -->
<script type="text/javascript" src="/gacha/mappings"></script>
<script>
mappings['default'] = mappings['en-us']; // make en-us as default/fallback option
</script>
</head>
<body>
<div class="content">
<div class="container">
<h2 class="mb-5">Gacha Records</h2>
<table id="container" class="table table-striped custom-table">
<thead>
<tr>
<th scope="col">Date</th>
<th scope="col">Item</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
<div class="navbar">
<a href="" id="prev">&lt;&lt;&lt;</a>
<span id="curpage">1</span>
<a href="" id="next">&gt;&gt;&gt;</a>
</div>
</div>
</div>
<footer>
<div class="copyright">
<div class="container">
<div class="row">
<div class="col-md-6">
<span>
Template by BecodReyes. All rights reserved.
</span>
</div>
<div class="col-md-6">
<ul style="float:right">
<li class="list-inline-item">
<a href="https://github.com/Grasscutters/Grasscutter">Github</a>
</li>
<li class="list-inline-item">·</li>
<li class="list-inline-item">
<a href="https://github.com/Grasscutters/Grasscutter/blob/stable/LICENSE">License</a>
</li>
</ul>
</div>
</div>
</div>
</div>
</footer>
<script>
var lang = new window.URLSearchParams(window.location.search).get("lang");
function itemMapper(itemID) {
if (mappings[lang] != null && mappings[lang][itemID] != null) {
var entry = mappings[lang][itemID];
if (entry){
return "<span class='" + entry[1] + "'>" + entry[0] + "</span>";
}
} else {
if (mappings['default'][itemID] != null) {
var entry = mappings['default'][itemID];
if (entry){
return "<span class='" + entry[1] + "'>" + entry[0] + "</span>";
}
}
}
return "<span class='blue'>" + itemID + "</span>";
}
function dateFormatter(timeStamp) {
var date = new Date(timeStamp);
if (lang == "en-us" || lang == null) { // MM/DD/YYYY hh:mm:ss.SSS
return String(date.getMonth()+1).padStart(2, "0") +
"/"+String(date.getDate()).padStart(2, "0")+
"/"+date.getFullYear()+
" "+String(date.getHours()).padStart(2, "0")+
":"+String(date.getMinutes()).padStart(2, "0")+
":"+String(date.getSeconds()).padStart(2, "0")+
"."+String(date.getMilliseconds()).padStart(3, "0");
} else if (lang == "zh-cn") { // YYYY/MM/DD hh:mm:ss.SSS
return date.getFullYear()+
"/" + String(date.getMonth()+1).padStart(2, "0") +
"/"+String(date.getDate()).padStart(2, "0")+
" "+String(date.getHours()).padStart(2, "0")+
":"+String(date.getMinutes()).padStart(2, "0")+
":"+String(date.getSeconds()).padStart(2, "0")+
"."+String(date.getMilliseconds()).padStart(3, "0");
}
}
(function (){
var container = document.getElementById("container");
record.forEach(element => {
var e = document.createElement("tr");
e.innerHTML= "<td>" + dateFormatter(element.time) + "</td><td>" + itemMapper(element.item) + "</td>";
container.appendChild(e);
});
// setup pagenation buttons
var page = parseInt(new window.URLSearchParams(window.location.search).get("p"));
if (!page){
page = 0;
}
document.getElementById("curpage").innerText = page + 1;
var href = new URL(window.location);
href.searchParams.set("p", page - 1);
document.getElementById("prev").href = href.toString();
href.searchParams.set("p", page + 1);
document.getElementById("next").href = href.toString();
if (page <= 0) {
document.getElementById("prev").style.display = "none";
}
if (page >= maxPage - 1) {
document.getElementById("next").style.display = "none";
}
// setup gacha type info
var gachaType = new window.URLSearchParams(window.location.search).get("gachaType");
var gachaString;
if (mappings[lang] != null && mappings[lang][gachaType] != null) {
gachaString = mappings[lang][gachaType];
}else{
gachaString = mappings['default'][gachaType];
if (gachaString == null) {
gachaString = gachaType;
}
}
document.getElementById("gacha-type").innerText = gachaString;
})();
</script>
</body>
</html>
File mode changed from 100644 to 100755
No preview for this file type
......@@ -57,7 +57,9 @@ class MlgmXyysd_Genshin_Impact_Proxy:
"minor-api.mihoyo.com",
"public-data-api.mihoyo.com",
"uspider.yuanshen.com",
"sdk-static.mihoyo.com"
"sdk-static.mihoyo.com",
"abtest-api-data-sg.hoyoverse.com",
"log-upload-os.hoyoverse.com"
]
def request(self, flow: http.HTTPFlow) -> None:
......
package emu.grasscutter;
import java.util.ArrayList;
import java.util.Locale;
import emu.grasscutter.Grasscutter.ServerDebugMode;
import emu.grasscutter.Grasscutter.ServerRunMode;
import emu.grasscutter.game.mail.Mail;
public final class Config {
public String DatabaseUrl = "mongodb://localhost:27017";
public String DatabaseCollection = "grasscutter";
......@@ -12,11 +14,18 @@ public final class Config {
public String PACKETS_FOLDER = "./packets/";
public String DUMPS_FOLDER = "./dumps/";
public String KEY_FOLDER = "./keys/";
public String SCRIPTS_FOLDER = "./resources/Scripts/";
public String PLUGINS_FOLDER = "./plugins/";
public String LANGUAGE_FOLDER = "./languages/";
public String RunMode = "HYBRID"; // HYBRID, DISPATCH_ONLY, GAME_ONLY
public ServerDebugMode DebugMode = ServerDebugMode.NONE; // ALL, MISSING, NONE
public ServerRunMode RunMode = ServerRunMode.HYBRID; // HYBRID, DISPATCH_ONLY, GAME_ONLY
public GameServerOptions GameServer = new GameServerOptions();
public DispatchServerOptions DispatchServer = new DispatchServerOptions();
public Locale LocaleLanguage = Locale.getDefault();
public Locale DefaultLanguage = Locale.ENGLISH;
public Boolean OpenStamina = true;
public GameServerOptions getGameServerOptions() {
return GameServer;
}
......@@ -27,11 +36,16 @@ public final class Config {
public String Ip = "0.0.0.0";
public String PublicIp = "127.0.0.1";
public int Port = 443;
public int PublicPort = 0;
public String KeystorePath = "./keystore.p12";
public String KeystorePassword = "";
public String KeystorePassword = "123456";
public Boolean UseSSL = true;
public Boolean FrontHTTPS = true;
public Boolean CORS = false;
public String[] CORSAllowedOrigins = new String[] { "*" };
public boolean AutomaticallyCreateAccounts = false;
public String[] defaultPermissions = new String[] { "" };
public RegionInfo[] GameServers = {};
......@@ -52,12 +66,11 @@ public final class Config {
public String Ip = "0.0.0.0";
public String PublicIp = "127.0.0.1";
public int Port = 22102;
public int PublicPort = 0;
public String DispatchServerDatabaseUrl = "mongodb://localhost:27017";
public String DispatchServerDatabaseCollection = "grasscutter";
public boolean LOG_PACKETS = false;
public int InventoryLimitWeapon = 2000;
public int InventoryLimitRelic = 2000;
public int InventoryLimitMaterial = 2000;
......@@ -67,8 +80,23 @@ public final class Config {
public int MaxAvatarsInTeamMultiplayer = 4;
public int MaxEntityLimit = 1000; // Max entity limit per world. // TODO: Enforce later.
public boolean WatchGacha = false;
public String ServerNickname = "Server";
public int ServerAvatarId = 10000007;
public int ServerNameCardId = 210001;
public int ServerLevel = 1;
public int ServerWorldLevel = 1;
public String ServerSignature = "Server Signature";
public int[] WelcomeEmotes = {2007, 1002, 4010};
public String WelcomeMotd = "Welcome to Grasscutter emu";
public String WelcomeMailTitle = "Welcome to Grasscutter!";
public String WelcomeMailSender = "Lawnmower";
public String WelcomeMailContent = "Hi there!\r\nFirst of all, welcome to Grasscutter. If you have any issues, please let us know so that Lawnmower can help you! \r\n\r\nCheck out our:\r\n<type=\"browser\" text=\"Discord\" href=\"https://discord.gg/T5vZU6UyeG\"/>";
public Mail.MailItem[] WelcomeMailItems = {
new Mail.MailItem(13509, 1, 1),
new Mail.MailItem(201, 10000, 1),
};
public boolean EnableOfficialShop = true;
public GameRates Game = new GameRates();
......
......@@ -5,7 +5,7 @@ import java.util.Arrays;
import emu.grasscutter.utils.Position;
import emu.grasscutter.utils.Utils;
public final class GenshinConstants {
public final class GameConstants {
public static String VERSION = "2.6.0";
public static final int MAX_TEAMS = 4;
......
package emu.grasscutter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.io.IOError;
import java.util.Calendar;
import emu.grasscutter.command.CommandMap;
import emu.grasscutter.plugin.PluginManager;
import emu.grasscutter.plugin.api.ServerHook;
import emu.grasscutter.scripts.ScriptLoader;
import emu.grasscutter.utils.Utils;
import org.jline.reader.EndOfFileException;
import org.jline.reader.LineReader;
import org.jline.reader.LineReaderBuilder;
import org.jline.reader.UserInterruptException;
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import org.reflections.Reflections;
import org.slf4j.LoggerFactory;
......@@ -18,21 +26,29 @@ import com.google.gson.GsonBuilder;
import ch.qos.logback.classic.Logger;
import emu.grasscutter.data.ResourceLoader;
import emu.grasscutter.database.DatabaseManager;
import emu.grasscutter.utils.Language;
import emu.grasscutter.server.dispatch.DispatchServer;
import emu.grasscutter.server.game.GameServer;
import emu.grasscutter.tools.Tools;
import emu.grasscutter.utils.Crypto;
import static emu.grasscutter.utils.Language.translate;
public final class Grasscutter {
private static final Logger log = (Logger) LoggerFactory.getLogger(Grasscutter.class);
private static LineReader consoleLineReader = null;
private static Config config;
private static Language language;
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
private static final File configFile = new File("./config.json");
public static RunMode MODE = RunMode.BOTH;
private static int day; // Current day of week.
private static DispatchServer dispatchServer;
private static GameServer gameServer;
private static PluginManager pluginManager;
public static final Reflections reflector = new Reflections("emu.grasscutter");
......@@ -43,61 +59,84 @@ public final class Grasscutter {
// Load server configuration.
Grasscutter.loadConfig();
// Load translation files.
Grasscutter.loadLanguage();
// Check server structure.
Utils.startupCheck();
}
public static void main(String[] args) throws Exception {
Crypto.loadKeys();
Crypto.loadKeys(); // Load keys from buffers.
// Parse arguments.
boolean exitEarly = false;
for (String arg : args) {
switch (arg.toLowerCase()) {
case "-auth":
MODE = RunMode.AUTH;
break;
case "-game":
MODE = RunMode.GAME;
break;
case "-handbook":
Tools.createGmHandbook();
return;
case "-handbook" -> {
Tools.createGmHandbook(); exitEarly = true;
}
case "-gachamap" -> {
Tools.createGachaMapping(Grasscutter.getConfig().DATA_FOLDER + "/gacha_mappings.js"); exitEarly = true;
}
}
}
// Exit early if argument sets it.
if(exitEarly) System.exit(0);
// Initialize server.
Grasscutter.getLogger().info("Starting Grasscutter...");
Grasscutter.getLogger().info(translate("messages.status.starting"));
// Load all resources.
Grasscutter.updateDayOfWeek();
ResourceLoader.loadAll();
// Database
ScriptLoader.init();
// Initialize database.
DatabaseManager.initialize();
// Start servers.
if(getConfig().RunMode.equalsIgnoreCase("HYBRID")) {
// Create server instances.
dispatchServer = new DispatchServer();
dispatchServer.start();
gameServer = new GameServer();
// Create a server hook instance with both servers.
new ServerHook(gameServer, dispatchServer);
// Create plugin manager instance.
pluginManager = new PluginManager();
gameServer = new GameServer(new InetSocketAddress(getConfig().getGameServerOptions().Ip, getConfig().getGameServerOptions().Port));
// Start servers.
if (getConfig().RunMode == ServerRunMode.HYBRID) {
dispatchServer.start();
gameServer.start();
} else if(getConfig().RunMode.equalsIgnoreCase("DISPATCH_ONLY")) {
dispatchServer = new DispatchServer();
} else if (getConfig().RunMode == ServerRunMode.DISPATCH_ONLY) {
dispatchServer.start();
} else if(getConfig().RunMode.equalsIgnoreCase("GAME_ONLY")) {
gameServer = new GameServer(new InetSocketAddress(getConfig().getGameServerOptions().Ip, getConfig().getGameServerOptions().Port));
} else if (getConfig().RunMode == ServerRunMode.GAME_ONLY) {
gameServer.start();
} else {
getLogger().error("Invalid server run mode. " + getConfig().RunMode);
getLogger().error("Server run mode must be 'HYBRID', 'DISPATCH_ONLY', or 'GAME_ONLY'. Unable to start Grasscutter...");
getLogger().error("Shutting down...");
getLogger().error(translate("messages.status.run_mode_error", getConfig().RunMode));
getLogger().error(translate("messages.status.run_mode_help"));
getLogger().error(translate("messages.status.shutdown"));
System.exit(1);
}
// Enable all plugins.
pluginManager.enablePlugins();
// Hook into shutdown event.
Runtime.getRuntime().addShutdownHook(new Thread(Grasscutter::onShutdown));
// Open console.
startConsole();
}
/**
* Server shutdown event.
*/
private static void onShutdown() {
// Disable all plugins.
pluginManager.disablePlugins();
}
public static void loadConfig() {
try (FileReader file = new FileReader(configFile)) {
config = gson.fromJson(file, Config.class);
......@@ -108,48 +147,96 @@ public final class Grasscutter {
}
}
public static void loadLanguage() {
var locale = config.LocaleLanguage;
var languageTag = locale.toLanguageTag();
if (languageTag.equals("und")) {
Grasscutter.getLogger().error("Illegal locale language, using 'en-US' instead.");
language = Language.getLanguage("en-US");
} else {
language = Language.getLanguage(languageTag);
}
}
public static void saveConfig() {
try (FileWriter file = new FileWriter(configFile)) {
file.write(gson.toJson(config));
} catch (Exception e) {
Grasscutter.getLogger().error("Config save error");
Grasscutter.getLogger().error("Unable to save config file.");
}
}
public static void startConsole() {
String input;
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
while ((input = br.readLine()) != null) {
try {
if(getConfig().RunMode.equalsIgnoreCase("DISPATCH_ONLY")) {
getLogger().error("Commands are not supported in dispatch only mode");
// Console should not start in dispatch only mode.
if (getConfig().RunMode == ServerRunMode.DISPATCH_ONLY) {
getLogger().info(translate("messages.dispatch.no_commands_error"));
return;
}
CommandMap.getInstance().invoke(null, input);
} catch (Exception e) {
Grasscutter.getLogger().error("Command error: ");
e.printStackTrace();
getLogger().info(translate("messages.status.done"));
String input = null;
boolean isLastInterrupted = false;
while (true) {
try {
input = consoleLineReader.readLine("> ");
} catch (UserInterruptException e) {
if (!isLastInterrupted) {
isLastInterrupted = true;
Grasscutter.getLogger().info("Press Ctrl-C again to shutdown.");
continue;
} else {
Runtime.getRuntime().exit(0);
}
} catch (EndOfFileException e) {
Grasscutter.getLogger().info("EOF detected.");
continue;
} catch (IOError e) {
Grasscutter.getLogger().error("An IO error occurred.", e);
continue;
}
isLastInterrupted = false;
try {
CommandMap.getInstance().invoke(null, null, input);
} catch (Exception e) {
Grasscutter.getLogger().error("An error occurred.", e);
Grasscutter.getLogger().error(translate("messages.game.command_error"), e);
}
}
public enum RunMode {
BOTH,
AUTH,
GAME
}
public static Config getConfig() {
return config;
}
public static Language getLanguage() {
return language;
}
public static Logger getLogger() {
return log;
}
public static LineReader getConsole() {
if (consoleLineReader == null) {
Terminal terminal = null;
try {
terminal = TerminalBuilder.builder().jna(true).build();
} catch (Exception e) {
try {
// Fallback to a dumb jline terminal.
terminal = TerminalBuilder.builder().dumb(true).build();
} catch (Exception ignored) {
// When dumb is true, build() never throws.
}
}
consoleLineReader = LineReaderBuilder.builder()
.terminal(terminal)
.build();
}
return consoleLineReader;
}
public static Gson getGsonFactory() {
return gson;
}
......@@ -161,4 +248,25 @@ public final class Grasscutter {
public static GameServer getGameServer() {
return gameServer;
}
public static PluginManager getPluginManager() {
return pluginManager;
}
public static void updateDayOfWeek() {
Calendar calendar = Calendar.getInstance();
day = calendar.get(Calendar.DAY_OF_WEEK);
}
public static int getCurrentDayOfWeek() {
return day;
}
public enum ServerRunMode {
HYBRID, DISPATCH_ONLY, GAME_ONLY
}
public enum ServerDebugMode {
ALL, MISSING, NONE
}
}
......@@ -14,4 +14,8 @@ public @interface Command {
String[] aliases() default {};
String permission() default "";
String permissionTargeted() default "";
boolean threading() default false;
}
package emu.grasscutter.command;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.player.Player;
import java.util.List;
......@@ -12,7 +12,7 @@ public interface CommandHandler {
* @param player The player to send the message to, or null for the server console.
* @param message The message to send.
*/
static void sendMessage(GenshinPlayer player, String message) {
static void sendMessage(Player player, String message) {
if (player == null) {
Grasscutter.getLogger().info(message);
} else {
......@@ -25,6 +25,6 @@ public interface CommandHandler {
* @param sender The player/console that invoked the command.
* @param args The arguments to the command.
*/
default void execute(GenshinPlayer sender, List<String> args) {
default void execute(Player sender, Player targetPlayer, List<String> args) {
}
}
......@@ -2,15 +2,20 @@ package emu.grasscutter.command;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.game.Account;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.player.Player;
import org.reflections.Reflections;
import java.util.*;
import static emu.grasscutter.utils.Language.translate;
@SuppressWarnings({"UnusedReturnValue", "unused"})
public final class CommandMap {
private final Map<String, CommandHandler> commands = new HashMap<>();
private final Map<String, Command> annotations = new HashMap<>();
private final Map<String, Integer> targetPlayerIds = new HashMap<>();
private static final String consoleId = "console";
public CommandMap() {
this(false);
}
......@@ -74,6 +79,12 @@ public final class CommandMap {
return this;
}
public List<Command> getAnnotationsAsList() { return new LinkedList<>(this.annotations.values()); }
public HashMap<String, Command> getAnnotations() {
return new LinkedHashMap<>(this.annotations);
}
/**
* Returns a list of all registered commands.
*
......@@ -103,40 +114,121 @@ public final class CommandMap {
* @param player The player invoking the command or null for the server console.
* @param rawMessage The messaged used to invoke the command.
*/
public void invoke(GenshinPlayer player, String rawMessage) {
public void invoke(Player player, Player targetPlayer, String rawMessage) {
rawMessage = rawMessage.trim();
if(rawMessage.length() == 0) {
CommandHandler.sendMessage(player, "No command specified."); return;
if (rawMessage.length() == 0) {
CommandHandler.sendMessage(player, translate("commands.generic.not_specified"));
return;
}
// Remove prefix if present.
if (!Character.isLetter(rawMessage.charAt(0)))
rawMessage = rawMessage.substring(1);
// Parse message.
String[] split = rawMessage.split(" ");
List<String> args = new LinkedList<>(Arrays.asList(split));
String label = args.remove(0);
String playerId = (player == null) ? consoleId : player.getAccount().getId();
// Check for special cases - currently only target command.
String targetUidStr = null;
if (label.startsWith("@")) { // @[UID]
targetUidStr = label.substring(1);
} else if (label.equalsIgnoreCase("target")) { // target [[@]UID]
if (args.size() > 0) {
targetUidStr = args.get(0);
if (targetUidStr.startsWith("@")) {
targetUidStr = targetUidStr.substring(1);
}
} else {
targetUidStr = "";
}
}
if (targetUidStr != null) {
if (targetUidStr.equals("")) { // Clears the default targetPlayer.
targetPlayerIds.remove(playerId);
CommandHandler.sendMessage(player, translate("commands.execution.clear_target"));
} else { // Sets default targetPlayer to the UID provided.
try {
int uid = Integer.parseInt(targetUidStr);
targetPlayer = Grasscutter.getGameServer().getPlayerByUid(uid);
if (targetPlayer == null) {
CommandHandler.sendMessage(player, translate("commands.generic.execution.player_exist_offline_error"));
} else {
targetPlayerIds.put(playerId, uid);
CommandHandler.sendMessage(player, translate("commands.execution.set_target", targetUidStr));
}
} catch (NumberFormatException e) {
CommandHandler.sendMessage(player, translate("commands.execution.uid_error"));
}
}
return;
}
// Get command handler.
CommandHandler handler = this.commands.get(label);
if (handler == null) {
CommandHandler.sendMessage(player, "Unknown command: " + label);
CommandHandler.sendMessage(player, translate("commands.generic.unknown_command", label));
return;
}
// If any @UID argument is present, override targetPlayer with it.
for (int i = 0; i < args.size(); i++) {
String arg = args.get(i);
if (arg.startsWith("@")) {
arg = args.remove(i).substring(1);
try {
int uid = Integer.parseInt(arg);
targetPlayer = Grasscutter.getGameServer().getPlayerByUid(uid);
if (targetPlayer == null) {
CommandHandler.sendMessage(player, translate("commands.generic.execution.player_exist_offline_error"));
return;
}
break;
} catch (NumberFormatException e) {
CommandHandler.sendMessage(player, translate("commands.execution.uid_error"));
return;
}
}
}
// If there's still no targetPlayer at this point, use previously-set target
if (targetPlayer == null) {
if (targetPlayerIds.containsKey(playerId)) {
targetPlayer = Grasscutter.getGameServer().getPlayerByUid(targetPlayerIds.get(playerId)); // We check every time in case the target goes offline after being targeted
if (targetPlayer == null) {
CommandHandler.sendMessage(player, translate("commands.generic.execution.player_exist_offline_error"));
return;
}
} else {
// If there's still no targetPlayer at this point, use executor.
targetPlayer = player;
}
}
// Check for permission.
if (player != null) {
String permissionNode = this.annotations.get(label).permission();
String permissionNodeTargeted = this.annotations.get(label).permissionTargeted();
Account account = player.getAccount();
if(!permissionNode.isEmpty() && !account.hasPermission(permissionNode)) {
CommandHandler.sendMessage(player, "You do not have permission to run this command.");
if (player != targetPlayer) { // Additional permission required for targeting another player
if (!permissionNodeTargeted.isEmpty() && !account.hasPermission(permissionNodeTargeted)) {
CommandHandler.sendMessage(player, translate("commands.generic.permission_error"));
return;
}
}
if (!permissionNode.isEmpty() && !account.hasPermission(permissionNode)) {
CommandHandler.sendMessage(player, translate("commands.generic.permission_error"));
return;
}
}
// Invoke execute method for handler.
handler.execute(player, args);
boolean threading = this.annotations.get(label).threading();
final Player targetPlayerF = targetPlayer; // Is there a better way to do this?
Runnable runnable = () -> handler.execute(player, targetPlayerF, args);
if(threading) {
new Thread(runnable).start();
} else {
runnable.run();
}
}
/**
......
......@@ -3,23 +3,24 @@ package emu.grasscutter.command.commands;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.player.Player;
import java.util.List;
@Command(label = "account", usage = "account <create|delete> <username> [uid]",
description = "Modify user accounts")
import static emu.grasscutter.utils.Language.translate;
@Command(label = "account", usage = "account <create|delete> <username> [uid]", description = "Modify user accounts")
public final class AccountCommand implements CommandHandler {
@Override
public void execute(GenshinPlayer sender, List<String> args) {
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (sender != null) {
CommandHandler.sendMessage(sender, "This command can only be run from the console.");
CommandHandler.sendMessage(sender, translate("commands.generic.console_execute_error"));
return;
}
if (args.size() < 2) {
CommandHandler.sendMessage(null, "Usage: account <create|delete> <username> [uid]");
CommandHandler.sendMessage(null, translate("commands.account.command_usage"));
return;
}
......@@ -28,7 +29,7 @@ public final class AccountCommand implements CommandHandler {
switch (action) {
default:
CommandHandler.sendMessage(null, "Usage: account <create|delete> <username> [uid]");
CommandHandler.sendMessage(null, translate("commands.account.command_usage"));
return;
case "create":
int uid = 0;
......@@ -36,26 +37,27 @@ public final class AccountCommand implements CommandHandler {
try {
uid = Integer.parseInt(args.get(2));
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(null, "Invalid UID.");
CommandHandler.sendMessage(null, translate("commands.account.invalid"));
return;
}
}
emu.grasscutter.game.Account account = DatabaseHelper.createAccountWithId(username, uid);
if (account == null) {
CommandHandler.sendMessage(null, "Account already exists.");
CommandHandler.sendMessage(null, translate("commands.account.exists"));
return;
} else {
CommandHandler.sendMessage(null, "Account created with UID " + account.getPlayerUid() + ".");
account.addPermission("*"); // Grant the player superuser permissions.
account.addPermission("*");
account.save(); // Save account to database.
CommandHandler.sendMessage(null, translate("commands.account.create", Integer.toString(account.getPlayerUid())));
}
return;
case "delete":
if (DatabaseHelper.deleteAccount(username)) {
CommandHandler.sendMessage(null, "Account deleted.");
CommandHandler.sendMessage(null, translate("commands.account.delete"));
} else {
CommandHandler.sendMessage(null, "Account not found.");
CommandHandler.sendMessage(null, translate("commands.account.no_account"));
}
}
}
......
......@@ -3,27 +3,29 @@ package emu.grasscutter.command.commands;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.player.Player;
import java.util.List;
import static emu.grasscutter.utils.Language.translate;
@Command(label = "broadcast", usage = "broadcast <message>",
description = "Sends a message to all the players", aliases = {"b"}, permission = "server.broadcast")
public final class BroadcastCommand implements CommandHandler {
@Override
public void execute(GenshinPlayer sender, List<String> args) {
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (args.size() < 1) {
CommandHandler.sendMessage(sender, "Usage: broadcast <message>");
CommandHandler.sendMessage(sender, translate("commands.broadcast.command_usage"));
return;
}
String message = String.join(" ", args.subList(0, args.size()));
for (GenshinPlayer p : Grasscutter.getGameServer().getPlayers().values()) {
for (Player p : Grasscutter.getGameServer().getPlayers().values()) {
CommandHandler.sendMessage(p, message);
}
CommandHandler.sendMessage(sender, "Message sent.");
CommandHandler.sendMessage(sender, translate("commands.broadcast.message_sent"));
}
}
......@@ -2,41 +2,42 @@ package emu.grasscutter.command.commands;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.player.Player;
import java.util.List;
import static emu.grasscutter.utils.Language.translate;
@Command(label = "changescene", usage = "changescene <scene id>",
description = "Changes your scene", aliases = {"scene"}, permission = "player.changescene")
public final class ChangeSceneCommand implements CommandHandler {
@Override
public void execute(GenshinPlayer sender, List<String> args) {
if (sender == null) {
CommandHandler.sendMessage(null, "Run this command in-game.");
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, translate("commands.execution.need_target"));
return;
}
if (args.size() < 1) {
CommandHandler.sendMessage(sender, "Usage: changescene <scene id>");
if (args.size() != 1) {
CommandHandler.sendMessage(sender, translate("commands.changescene.usage"));
return;
}
try {
int sceneId = Integer.parseInt(args.get(0));
if (sceneId == sender.getSceneId()) {
CommandHandler.sendMessage(sender, "You are already in that scene");
if (sceneId == targetPlayer.getSceneId()) {
CommandHandler.sendMessage(sender, translate("commands.changescene.already_in_scene"));
return;
}
boolean result = sender.getWorld().transferPlayerToScene(sender, sceneId, sender.getPos());
CommandHandler.sendMessage(sender, "Changed to scene " + sceneId);
boolean result = targetPlayer.getWorld().transferPlayerToScene(targetPlayer, sceneId, targetPlayer.getPos());
CommandHandler.sendMessage(sender, translate("commands.changescene.result", Integer.toString(sceneId)));
if (!result) {
CommandHandler.sendMessage(sender, "Scene does not exist");
CommandHandler.sendMessage(sender, translate("commands.changescene.exists_error"));
}
} catch (Exception e) {
CommandHandler.sendMessage(sender, "Usage: changescene <scene id>");
CommandHandler.sendMessage(sender, translate("commands.execution.argument_error"));
}
}
}
package emu.grasscutter.command.commands;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.inventory.Inventory;
import emu.grasscutter.game.inventory.ItemType;
import java.util.List;
@Command(label = "clearartifacts", usage = "clearartifacts",
description = "Deletes all unequipped and unlocked level 0 artifacts, including yellow rarity ones from your inventory",
aliases = {"clearart"}, permission = "player.clearartifacts")
public final class ClearArtifactsCommand implements CommandHandler {
@Override
public void execute(GenshinPlayer sender, List<String> args) {
if (sender == null) {
CommandHandler.sendMessage(null, "Run this command in-game.");
return; // TODO: clear player's artifacts from console or other players
}
Inventory playerInventory = sender.getInventory();
playerInventory.getItems().values().stream()
.filter(item -> item.getItemType() == ItemType.ITEM_RELIQUARY)
.filter(item -> item.getLevel() == 1 && item.getExp() == 0)
.filter(item -> !item.isLocked() && !item.isEquipped())
.forEach(item -> playerInventory.removeItem(item, item.getCount()));
}
}
package emu.grasscutter.command.commands;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.inventory.GameItem;
import emu.grasscutter.game.inventory.Inventory;
import emu.grasscutter.game.inventory.ItemType;
import emu.grasscutter.game.player.Player;
import java.util.List;
import static emu.grasscutter.utils.Language.translate;
@Command(label = "clear", usage = "clear <all|wp|art|mat>", //Merged /clearartifacts and /clearweapons to /clear <args> [uid]
description = "Deletes unequipped unlocked items, including yellow rarity ones from your inventory",
aliases = {"clear"}, permission = "player.clearinv")
public final class ClearCommand implements CommandHandler {
@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, translate("commands.execution.need_target"));
return;
}
if (args.size() < 1) {
CommandHandler.sendMessage(sender, translate("commands.clear.command_usage"));
return;
}
Inventory playerInventory = targetPlayer.getInventory();
List<GameItem> toDelete = null;
switch (args.get(0)) {
case "wp" -> {
toDelete = playerInventory.getItems().values().stream()
.filter(item -> item.getItemType() == ItemType.ITEM_WEAPON)
.filter(item -> !item.isLocked() && !item.isEquipped())
.toList();
CommandHandler.sendMessage(sender, translate("commands.clear.weapons", targetPlayer.getNickname()));
}
case "art" -> {
toDelete = playerInventory.getItems().values().stream()
.filter(item -> item.getItemType() == ItemType.ITEM_RELIQUARY)
.filter(item -> item.getLevel() == 1 && item.getExp() == 0)
.filter(item -> !item.isLocked() && !item.isEquipped())
.toList();
CommandHandler.sendMessage(sender, translate("commands.clear.artifacts", targetPlayer.getNickname()));
}
case "mat" -> {
toDelete = playerInventory.getItems().values().stream()
.filter(item -> item.getItemType() == ItemType.ITEM_MATERIAL)
.filter(item -> item.getLevel() == 1 && item.getExp() == 0)
.filter(item -> !item.isLocked() && !item.isEquipped())
.toList();
CommandHandler.sendMessage(sender, translate("commands.clear.materials", targetPlayer.getNickname()));
}
case "all" -> {
toDelete = playerInventory.getItems().values().stream()
.filter(item1 -> item1.getItemType() == ItemType.ITEM_RELIQUARY)
.filter(item1 -> item1.getLevel() == 1 && item1.getExp() == 0)
.filter(item1 -> !item1.isLocked() && !item1.isEquipped())
.toList();
CommandHandler.sendMessage(sender, translate("commands.clear.artifacts", targetPlayer.getNickname()));
playerInventory.removeItems(toDelete);
toDelete = playerInventory.getItems().values().stream()
.filter(item2 -> item2.getItemType() == ItemType.ITEM_MATERIAL)
.filter(item2 -> !item2.isLocked() && !item2.isEquipped())
.toList();
playerInventory.removeItems(toDelete);
CommandHandler.sendMessage(sender, translate("commands.clear.materials", targetPlayer.getNickname()));
toDelete = playerInventory.getItems().values().stream()
.filter(item3 -> item3.getItemType() == ItemType.ITEM_WEAPON)
.filter(item3 -> item3.getLevel() == 1 && item3.getExp() == 0)
.filter(item3 -> !item3.isLocked() && !item3.isEquipped())
.toList();
playerInventory.removeItems(toDelete);
CommandHandler.sendMessage(sender, translate("commands.clear.weapons", targetPlayer.getNickname()));
toDelete = playerInventory.getItems().values().stream()
.filter(item4 -> item4.getItemType() == ItemType.ITEM_FURNITURE)
.filter(item4 -> !item4.isLocked() && !item4.isEquipped())
.toList();
playerInventory.removeItems(toDelete);
CommandHandler.sendMessage(sender, translate("commands.clear.furniture", targetPlayer.getNickname()));
toDelete = playerInventory.getItems().values().stream()
.filter(item5 -> item5.getItemType() == ItemType.ITEM_DISPLAY)
.filter(item5 -> !item5.isLocked() && !item5.isEquipped())
.toList();
playerInventory.removeItems(toDelete);
CommandHandler.sendMessage(sender, translate("commands.clear.displays", targetPlayer.getNickname()));
toDelete = playerInventory.getItems().values().stream()
.filter(item6 -> item6.getItemType() == ItemType.ITEM_VIRTUAL)
.filter(item6 -> !item6.isLocked() && !item6.isEquipped())
.toList();
CommandHandler.sendMessage(sender, translate("commands.clear.virtuals", targetPlayer.getNickname()));
CommandHandler.sendMessage(sender, translate("commands.clear.everything", targetPlayer.getNickname()));
}
}
if (toDelete != null) {
playerInventory.removeItems(toDelete);
}
}
}
package emu.grasscutter.command.commands;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.player.Player;
import java.util.List;
import static emu.grasscutter.utils.Language.translate;
@Command(label = "coop", usage = "coop [host UID]",
description = "Forces someone to join the world of others", permission = "server.coop")
public final class CoopCommand implements CommandHandler {
@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (targetPlayer == null) {
CommandHandler.sendMessage(sender, translate("commands.execution.need_target"));
return;
}
Player host = sender;
switch (args.size()) {
case 0: // Summon target to self
CommandHandler.sendMessage(sender, translate("commands.coop.usage"));
if (sender == null) // Console doesn't have a self to summon to
return;
break;
case 1: // Summon target to argument
try {
int hostId = Integer.parseInt(args.get(0));
host = Grasscutter.getGameServer().getPlayerByUid(hostId);
if (host == null) {
CommandHandler.sendMessage(sender, translate("commands.execution.player_offline_error"));
return;
}
break;
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(sender, translate("commands.execution.uid_error"));
return;
}
default:
CommandHandler.sendMessage(sender, translate("commands.coop.usage"));
return;
}
// There's no target==host check but this just places them in multiplayer in their own world which seems fine.
if (targetPlayer.isInMultiplayer()) {
targetPlayer.getServer().getMultiplayerManager().leaveCoop(targetPlayer);
}
host.getServer().getMultiplayerManager().applyEnterMp(targetPlayer, host.getUid());
targetPlayer.getServer().getMultiplayerManager().applyEnterMpReply(host, targetPlayer.getUid(), true);
CommandHandler.sendMessage(sender, translate("commands.coop.success", targetPlayer.getNickname(), host.getNickname()));
}
}
package emu.grasscutter.command.commands;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.data.GenshinData;
import emu.grasscutter.data.GameData;
import emu.grasscutter.data.def.ItemData;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.entity.EntityItem;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.utils.Position;
import java.util.List;
import static emu.grasscutter.utils.Language.translate;
@Command(label = "drop", usage = "drop <itemId|itemName> [amount]",
description = "Drops an item near you", aliases = {"d", "dropitem"}, permission = "server.drop")
public final class DropCommand implements CommandHandler {
@Override
public void execute(GenshinPlayer sender, List<String> args) {
if (sender == null) {
CommandHandler.sendMessage(null, "Run this command in-game.");
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (targetPlayer == null) {
CommandHandler.sendMessage(null, translate("commands.execution.need_target"));
return;
}
if (args.size() < 1) {
CommandHandler.sendMessage(sender, "Usage: drop <itemId|itemName> [amount]");
return;
}
int item = 0;
int amount = 1;
switch (args.size()) {
case 2:
try {
int item = Integer.parseInt(args.get(0));
int amount = 1;
if (args.size() > 1) amount = Integer.parseInt(args.get(1));
amount = Integer.parseInt(args.get(1));
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(sender, translate("commands.generic.invalid.amount"));
return;
} // Slightly cheeky here: no break, so it falls through to initialize the first argument too
case 1:
try {
item = Integer.parseInt(args.get(0));
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(sender, translate("commands.generic.invalid.itemId"));
return;
}
break;
default:
CommandHandler.sendMessage(sender, translate("commands.drop.command_usage"));
return;
}
ItemData itemData = GenshinData.getItemDataMap().get(item);
ItemData itemData = GameData.getItemDataMap().get(item);
if (itemData == null) {
CommandHandler.sendMessage(sender, "Invalid item id.");
CommandHandler.sendMessage(sender, translate("commands.generic.invalid.itemId"));
return;
}
if (itemData.isEquip()) {
float range = (5f + (.1f * amount));
for (int i = 0; i < amount; i++) {
Position pos = sender.getPos().clone().addX((float) (Math.random() * range) - (range / 2)).addY(3f).addZ((float) (Math.random() * range) - (range / 2));
EntityItem entity = new EntityItem(sender.getScene(), sender, itemData, pos, 1);
sender.getScene().addEntity(entity);
Position pos = targetPlayer.getPos().clone().addX((float) (Math.random() * range) - (range / 2)).addY(3f).addZ((float) (Math.random() * range) - (range / 2));
EntityItem entity = new EntityItem(targetPlayer.getScene(), targetPlayer, itemData, pos, 1);
targetPlayer.getScene().addEntity(entity);
}
} else {
EntityItem entity = new EntityItem(sender.getScene(), sender, itemData, sender.getPos().clone().addY(3f), amount);
sender.getScene().addEntity(entity);
}
CommandHandler.sendMessage(sender, String.format("Dropped %s of %s.", amount, item));
} catch (NumberFormatException ignored) {
CommandHandler.sendMessage(sender, "Invalid item or player ID.");
EntityItem entity = new EntityItem(targetPlayer.getScene(), targetPlayer, itemData, targetPlayer.getPos().clone().addY(3f), amount);
targetPlayer.getScene().addEntity(entity);
}
CommandHandler.sendMessage(sender, translate("commands.drop.success", Integer.toString(amount), Integer.toString(item)));
}
}
\ No newline at end of file
package emu.grasscutter.command.commands;
import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.player.Player;
import java.util.List;
import static emu.grasscutter.utils.Language.translate;
@Command(label = "enterdungeon", usage = "enterdungeon <dungeon id>",
description = "Enter a dungeon", aliases = {"dungeon"}, permission = "player.enterdungeon")
public final class EnterDungeonCommand implements CommandHandler {
@Override
public void execute(Player sender, Player targetPlayer, List<String> args) {
if (targetPlayer == null) {
CommandHandler.sendMessage(null, translate("commands.execution.need_target"));
return;
}
if (args.size() < 1) {
CommandHandler.sendMessage(sender, translate("commands.enter_dungeon.usage"));
return;
}
try {
int dungeonId = Integer.parseInt(args.get(0));
if (dungeonId == targetPlayer.getSceneId()) {
CommandHandler.sendMessage(sender, translate("commands.enter_dungeon.in_dungeon_error"));
return;
}
boolean result = targetPlayer.getServer().getDungeonManager().enterDungeon(targetPlayer.getSession().getPlayer(), 0, dungeonId);
CommandHandler.sendMessage(sender, translate("commands.enter_dungeon.changed", dungeonId));
if (!result) {
CommandHandler.sendMessage(sender, translate("commands.enter_dungeon.not_found_error"));
}
} catch (Exception e) {
CommandHandler.sendMessage(sender, translate("commands.enter_dungeon.usage"));
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment