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: ...@@ -57,7 +57,9 @@ class MlgmXyysd_Genshin_Impact_Proxy:
"minor-api.mihoyo.com", "minor-api.mihoyo.com",
"public-data-api.mihoyo.com", "public-data-api.mihoyo.com",
"uspider.yuanshen.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: def request(self, flow: http.HTTPFlow) -> None:
......
package emu.grasscutter; 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 final class Config {
public String DatabaseUrl = "mongodb://localhost:27017"; public String DatabaseUrl = "mongodb://localhost:27017";
public String DatabaseCollection = "grasscutter"; public String DatabaseCollection = "grasscutter";
...@@ -12,11 +14,18 @@ public final class Config { ...@@ -12,11 +14,18 @@ public final class Config {
public String PACKETS_FOLDER = "./packets/"; public String PACKETS_FOLDER = "./packets/";
public String DUMPS_FOLDER = "./dumps/"; public String DUMPS_FOLDER = "./dumps/";
public String KEY_FOLDER = "./keys/"; 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 GameServerOptions GameServer = new GameServerOptions();
public DispatchServerOptions DispatchServer = new DispatchServerOptions(); public DispatchServerOptions DispatchServer = new DispatchServerOptions();
public Locale LocaleLanguage = Locale.getDefault();
public Locale DefaultLanguage = Locale.ENGLISH;
public Boolean OpenStamina = true;
public GameServerOptions getGameServerOptions() { public GameServerOptions getGameServerOptions() {
return GameServer; return GameServer;
} }
...@@ -27,11 +36,16 @@ public final class Config { ...@@ -27,11 +36,16 @@ public final class Config {
public String Ip = "0.0.0.0"; public String Ip = "0.0.0.0";
public String PublicIp = "127.0.0.1"; public String PublicIp = "127.0.0.1";
public int Port = 443; public int Port = 443;
public int PublicPort = 0;
public String KeystorePath = "./keystore.p12"; public String KeystorePath = "./keystore.p12";
public String KeystorePassword = ""; public String KeystorePassword = "123456";
public Boolean UseSSL = true; public Boolean UseSSL = true;
public Boolean FrontHTTPS = true;
public Boolean CORS = false;
public String[] CORSAllowedOrigins = new String[] { "*" };
public boolean AutomaticallyCreateAccounts = false; public boolean AutomaticallyCreateAccounts = false;
public String[] defaultPermissions = new String[] { "" };
public RegionInfo[] GameServers = {}; public RegionInfo[] GameServers = {};
...@@ -52,12 +66,11 @@ public final class Config { ...@@ -52,12 +66,11 @@ public final class Config {
public String Ip = "0.0.0.0"; public String Ip = "0.0.0.0";
public String PublicIp = "127.0.0.1"; public String PublicIp = "127.0.0.1";
public int Port = 22102; public int Port = 22102;
public int PublicPort = 0;
public String DispatchServerDatabaseUrl = "mongodb://localhost:27017"; public String DispatchServerDatabaseUrl = "mongodb://localhost:27017";
public String DispatchServerDatabaseCollection = "grasscutter"; public String DispatchServerDatabaseCollection = "grasscutter";
public boolean LOG_PACKETS = false;
public int InventoryLimitWeapon = 2000; public int InventoryLimitWeapon = 2000;
public int InventoryLimitRelic = 2000; public int InventoryLimitRelic = 2000;
public int InventoryLimitMaterial = 2000; public int InventoryLimitMaterial = 2000;
...@@ -67,8 +80,23 @@ public final class Config { ...@@ -67,8 +80,23 @@ public final class Config {
public int MaxAvatarsInTeamMultiplayer = 4; public int MaxAvatarsInTeamMultiplayer = 4;
public int MaxEntityLimit = 1000; // Max entity limit per world. // TODO: Enforce later. public int MaxEntityLimit = 1000; // Max entity limit per world. // TODO: Enforce later.
public boolean WatchGacha = false; 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 int[] WelcomeEmotes = {2007, 1002, 4010};
public String WelcomeMotd = "Welcome to Grasscutter emu"; 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(); public GameRates Game = new GameRates();
......
...@@ -5,7 +5,7 @@ import java.util.Arrays; ...@@ -5,7 +5,7 @@ import java.util.Arrays;
import emu.grasscutter.utils.Position; import emu.grasscutter.utils.Position;
import emu.grasscutter.utils.Utils; import emu.grasscutter.utils.Utils;
public final class GenshinConstants { public final class GameConstants {
public static String VERSION = "2.6.0"; public static String VERSION = "2.6.0";
public static final int MAX_TEAMS = 4; public static final int MAX_TEAMS = 4;
......
package emu.grasscutter; package emu.grasscutter;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.InputStreamReader; import java.io.IOError;
import java.net.InetSocketAddress; import java.util.Calendar;
import emu.grasscutter.command.CommandMap; 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 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.reflections.Reflections;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -18,21 +26,29 @@ import com.google.gson.GsonBuilder; ...@@ -18,21 +26,29 @@ import com.google.gson.GsonBuilder;
import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.Logger;
import emu.grasscutter.data.ResourceLoader; import emu.grasscutter.data.ResourceLoader;
import emu.grasscutter.database.DatabaseManager; import emu.grasscutter.database.DatabaseManager;
import emu.grasscutter.utils.Language;
import emu.grasscutter.server.dispatch.DispatchServer; import emu.grasscutter.server.dispatch.DispatchServer;
import emu.grasscutter.server.game.GameServer; import emu.grasscutter.server.game.GameServer;
import emu.grasscutter.tools.Tools; import emu.grasscutter.tools.Tools;
import emu.grasscutter.utils.Crypto; import emu.grasscutter.utils.Crypto;
import static emu.grasscutter.utils.Language.translate;
public final class Grasscutter { public final class Grasscutter {
private static final Logger log = (Logger) LoggerFactory.getLogger(Grasscutter.class); private static final Logger log = (Logger) LoggerFactory.getLogger(Grasscutter.class);
private static LineReader consoleLineReader = null;
private static Config config; private static Config config;
private static Language language;
private static final Gson gson = new GsonBuilder().setPrettyPrinting().create(); private static final Gson gson = new GsonBuilder().setPrettyPrinting().create();
private static final File configFile = new File("./config.json"); 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 DispatchServer dispatchServer;
private static GameServer gameServer; private static GameServer gameServer;
private static PluginManager pluginManager;
public static final Reflections reflector = new Reflections("emu.grasscutter"); public static final Reflections reflector = new Reflections("emu.grasscutter");
...@@ -43,61 +59,84 @@ public final class Grasscutter { ...@@ -43,61 +59,84 @@ public final class Grasscutter {
// Load server configuration. // Load server configuration.
Grasscutter.loadConfig(); Grasscutter.loadConfig();
// Load translation files.
Grasscutter.loadLanguage();
// Check server structure. // Check server structure.
Utils.startupCheck(); Utils.startupCheck();
} }
public static void main(String[] args) throws Exception { 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) { for (String arg : args) {
switch (arg.toLowerCase()) { switch (arg.toLowerCase()) {
case "-auth": case "-handbook" -> {
MODE = RunMode.AUTH; Tools.createGmHandbook(); exitEarly = true;
break; }
case "-game": case "-gachamap" -> {
MODE = RunMode.GAME; Tools.createGachaMapping(Grasscutter.getConfig().DATA_FOLDER + "/gacha_mappings.js"); exitEarly = true;
break; }
case "-handbook":
Tools.createGmHandbook();
return;
} }
} }
// Exit early if argument sets it.
if(exitEarly) System.exit(0);
// Initialize server. // Initialize server.
Grasscutter.getLogger().info("Starting Grasscutter..."); Grasscutter.getLogger().info(translate("messages.status.starting"));
// Load all resources. // Load all resources.
Grasscutter.updateDayOfWeek();
ResourceLoader.loadAll(); ResourceLoader.loadAll();
// Database ScriptLoader.init();
// Initialize database.
DatabaseManager.initialize(); DatabaseManager.initialize();
// Start servers. // Create server instances.
if(getConfig().RunMode.equalsIgnoreCase("HYBRID")) {
dispatchServer = new DispatchServer(); 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(); gameServer.start();
} else if(getConfig().RunMode.equalsIgnoreCase("DISPATCH_ONLY")) { } else if (getConfig().RunMode == ServerRunMode.DISPATCH_ONLY) {
dispatchServer = new DispatchServer();
dispatchServer.start(); dispatchServer.start();
} else if(getConfig().RunMode.equalsIgnoreCase("GAME_ONLY")) { } else if (getConfig().RunMode == ServerRunMode.GAME_ONLY) {
gameServer = new GameServer(new InetSocketAddress(getConfig().getGameServerOptions().Ip, getConfig().getGameServerOptions().Port));
gameServer.start(); gameServer.start();
} else { } else {
getLogger().error("Invalid server run mode. " + getConfig().RunMode); getLogger().error(translate("messages.status.run_mode_error", getConfig().RunMode));
getLogger().error("Server run mode must be 'HYBRID', 'DISPATCH_ONLY', or 'GAME_ONLY'. Unable to start Grasscutter..."); getLogger().error(translate("messages.status.run_mode_help"));
getLogger().error("Shutting down..."); getLogger().error(translate("messages.status.shutdown"));
System.exit(1); System.exit(1);
} }
// Enable all plugins.
pluginManager.enablePlugins();
// Hook into shutdown event.
Runtime.getRuntime().addShutdownHook(new Thread(Grasscutter::onShutdown));
// Open console. // Open console.
startConsole(); startConsole();
} }
/**
* Server shutdown event.
*/
private static void onShutdown() {
// Disable all plugins.
pluginManager.disablePlugins();
}
public static void loadConfig() { public static void loadConfig() {
try (FileReader file = new FileReader(configFile)) { try (FileReader file = new FileReader(configFile)) {
config = gson.fromJson(file, Config.class); config = gson.fromJson(file, Config.class);
...@@ -108,48 +147,96 @@ public final class Grasscutter { ...@@ -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() { public static void saveConfig() {
try (FileWriter file = new FileWriter(configFile)) { try (FileWriter file = new FileWriter(configFile)) {
file.write(gson.toJson(config)); file.write(gson.toJson(config));
} catch (Exception e) { } catch (Exception e) {
Grasscutter.getLogger().error("Config save error"); Grasscutter.getLogger().error("Unable to save config file.");
} }
} }
public static void startConsole() { public static void startConsole() {
String input; // Console should not start in dispatch only mode.
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) { if (getConfig().RunMode == ServerRunMode.DISPATCH_ONLY) {
while ((input = br.readLine()) != null) { getLogger().info(translate("messages.dispatch.no_commands_error"));
try {
if(getConfig().RunMode.equalsIgnoreCase("DISPATCH_ONLY")) {
getLogger().error("Commands are not supported in dispatch only mode");
return; return;
} }
CommandMap.getInstance().invoke(null, input);
} catch (Exception e) { getLogger().info(translate("messages.status.done"));
Grasscutter.getLogger().error("Command error: "); String input = null;
e.printStackTrace(); 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) { } 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() { public static Config getConfig() {
return config; return config;
} }
public static Language getLanguage() {
return language;
}
public static Logger getLogger() { public static Logger getLogger() {
return log; 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() { public static Gson getGsonFactory() {
return gson; return gson;
} }
...@@ -161,4 +248,25 @@ public final class Grasscutter { ...@@ -161,4 +248,25 @@ public final class Grasscutter {
public static GameServer getGameServer() { public static GameServer getGameServer() {
return gameServer; 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 { ...@@ -14,4 +14,8 @@ public @interface Command {
String[] aliases() default {}; String[] aliases() default {};
String permission() default ""; String permission() default "";
String permissionTargeted() default "";
boolean threading() default false;
} }
package emu.grasscutter.command; package emu.grasscutter.command;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.game.GenshinPlayer; import emu.grasscutter.game.player.Player;
import java.util.List; import java.util.List;
...@@ -12,7 +12,7 @@ public interface CommandHandler { ...@@ -12,7 +12,7 @@ public interface CommandHandler {
* @param player The player to send the message to, or null for the server console. * @param player The player to send the message to, or null for the server console.
* @param message The message to send. * @param message The message to send.
*/ */
static void sendMessage(GenshinPlayer player, String message) { static void sendMessage(Player player, String message) {
if (player == null) { if (player == null) {
Grasscutter.getLogger().info(message); Grasscutter.getLogger().info(message);
} else { } else {
...@@ -25,6 +25,6 @@ public interface CommandHandler { ...@@ -25,6 +25,6 @@ public interface CommandHandler {
* @param sender The player/console that invoked the command. * @param sender The player/console that invoked the command.
* @param args The arguments to 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; ...@@ -2,15 +2,20 @@ package emu.grasscutter.command;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.game.Account; import emu.grasscutter.game.Account;
import emu.grasscutter.game.GenshinPlayer; import emu.grasscutter.game.player.Player;
import org.reflections.Reflections; import org.reflections.Reflections;
import java.util.*; import java.util.*;
import static emu.grasscutter.utils.Language.translate;
@SuppressWarnings({"UnusedReturnValue", "unused"}) @SuppressWarnings({"UnusedReturnValue", "unused"})
public final class CommandMap { public final class CommandMap {
private final Map<String, CommandHandler> commands = new HashMap<>(); private final Map<String, CommandHandler> commands = new HashMap<>();
private final Map<String, Command> annotations = 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() { public CommandMap() {
this(false); this(false);
} }
...@@ -74,6 +79,12 @@ public final class CommandMap { ...@@ -74,6 +79,12 @@ public final class CommandMap {
return this; 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. * Returns a list of all registered commands.
* *
...@@ -103,40 +114,121 @@ public final class CommandMap { ...@@ -103,40 +114,121 @@ public final class CommandMap {
* @param player The player invoking the command or null for the server console. * @param player The player invoking the command or null for the server console.
* @param rawMessage The messaged used to invoke the command. * @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(); rawMessage = rawMessage.trim();
if(rawMessage.length() == 0) { if (rawMessage.length() == 0) {
CommandHandler.sendMessage(player, "No command specified."); return; 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. // Parse message.
String[] split = rawMessage.split(" "); String[] split = rawMessage.split(" ");
List<String> args = new LinkedList<>(Arrays.asList(split)); List<String> args = new LinkedList<>(Arrays.asList(split));
String label = args.remove(0); 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. // Get command handler.
CommandHandler handler = this.commands.get(label); CommandHandler handler = this.commands.get(label);
if (handler == null) { 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; 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. // Check for permission.
if (player != null) { if (player != null) {
String permissionNode = this.annotations.get(label).permission(); String permissionNode = this.annotations.get(label).permission();
String permissionNodeTargeted = this.annotations.get(label).permissionTargeted();
Account account = player.getAccount(); Account account = player.getAccount();
if(!permissionNode.isEmpty() && !account.hasPermission(permissionNode)) { if (player != targetPlayer) { // Additional permission required for targeting another player
CommandHandler.sendMessage(player, "You do not have permission to run this command."); 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; return;
} }
} }
// Invoke execute method for handler. // 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; ...@@ -3,23 +3,24 @@ package emu.grasscutter.command.commands;
import emu.grasscutter.command.Command; import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler; import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.database.DatabaseHelper; import emu.grasscutter.database.DatabaseHelper;
import emu.grasscutter.game.GenshinPlayer; import emu.grasscutter.game.player.Player;
import java.util.List; import java.util.List;
@Command(label = "account", usage = "account <create|delete> <username> [uid]", import static emu.grasscutter.utils.Language.translate;
description = "Modify user accounts")
@Command(label = "account", usage = "account <create|delete> <username> [uid]", description = "Modify user accounts")
public final class AccountCommand implements CommandHandler { public final class AccountCommand implements CommandHandler {
@Override @Override
public void execute(GenshinPlayer sender, List<String> args) { public void execute(Player sender, Player targetPlayer, List<String> args) {
if (sender != null) { 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; return;
} }
if (args.size() < 2) { if (args.size() < 2) {
CommandHandler.sendMessage(null, "Usage: account <create|delete> <username> [uid]"); CommandHandler.sendMessage(null, translate("commands.account.command_usage"));
return; return;
} }
...@@ -28,7 +29,7 @@ public final class AccountCommand implements CommandHandler { ...@@ -28,7 +29,7 @@ public final class AccountCommand implements CommandHandler {
switch (action) { switch (action) {
default: default:
CommandHandler.sendMessage(null, "Usage: account <create|delete> <username> [uid]"); CommandHandler.sendMessage(null, translate("commands.account.command_usage"));
return; return;
case "create": case "create":
int uid = 0; int uid = 0;
...@@ -36,26 +37,27 @@ public final class AccountCommand implements CommandHandler { ...@@ -36,26 +37,27 @@ public final class AccountCommand implements CommandHandler {
try { try {
uid = Integer.parseInt(args.get(2)); uid = Integer.parseInt(args.get(2));
} catch (NumberFormatException ignored) { } catch (NumberFormatException ignored) {
CommandHandler.sendMessage(null, "Invalid UID."); CommandHandler.sendMessage(null, translate("commands.account.invalid"));
return; return;
} }
} }
emu.grasscutter.game.Account account = DatabaseHelper.createAccountWithId(username, uid); emu.grasscutter.game.Account account = DatabaseHelper.createAccountWithId(username, uid);
if (account == null) { if (account == null) {
CommandHandler.sendMessage(null, "Account already exists."); CommandHandler.sendMessage(null, translate("commands.account.exists"));
return; return;
} else { } else {
CommandHandler.sendMessage(null, "Account created with UID " + account.getPlayerUid() + "."); account.addPermission("*");
account.addPermission("*"); // Grant the player superuser permissions.
account.save(); // Save account to database. account.save(); // Save account to database.
CommandHandler.sendMessage(null, translate("commands.account.create", Integer.toString(account.getPlayerUid())));
} }
return; return;
case "delete": case "delete":
if (DatabaseHelper.deleteAccount(username)) { if (DatabaseHelper.deleteAccount(username)) {
CommandHandler.sendMessage(null, "Account deleted."); CommandHandler.sendMessage(null, translate("commands.account.delete"));
} else { } else {
CommandHandler.sendMessage(null, "Account not found."); CommandHandler.sendMessage(null, translate("commands.account.no_account"));
} }
} }
} }
......
...@@ -3,27 +3,29 @@ package emu.grasscutter.command.commands; ...@@ -3,27 +3,29 @@ package emu.grasscutter.command.commands;
import emu.grasscutter.Grasscutter; import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command; import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler; import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.GenshinPlayer; import emu.grasscutter.game.player.Player;
import java.util.List; import java.util.List;
import static emu.grasscutter.utils.Language.translate;
@Command(label = "broadcast", usage = "broadcast <message>", @Command(label = "broadcast", usage = "broadcast <message>",
description = "Sends a message to all the players", aliases = {"b"}, permission = "server.broadcast") description = "Sends a message to all the players", aliases = {"b"}, permission = "server.broadcast")
public final class BroadcastCommand implements CommandHandler { public final class BroadcastCommand implements CommandHandler {
@Override @Override
public void execute(GenshinPlayer sender, List<String> args) { public void execute(Player sender, Player targetPlayer, List<String> args) {
if (args.size() < 1) { if (args.size() < 1) {
CommandHandler.sendMessage(sender, "Usage: broadcast <message>"); CommandHandler.sendMessage(sender, translate("commands.broadcast.command_usage"));
return; return;
} }
String message = String.join(" ", args.subList(0, args.size())); 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(p, message);
} }
CommandHandler.sendMessage(sender, "Message sent."); CommandHandler.sendMessage(sender, translate("commands.broadcast.message_sent"));
} }
} }
...@@ -2,41 +2,42 @@ package emu.grasscutter.command.commands; ...@@ -2,41 +2,42 @@ package emu.grasscutter.command.commands;
import emu.grasscutter.command.Command; import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler; import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.game.GenshinPlayer; import emu.grasscutter.game.player.Player;
import java.util.List; import java.util.List;
import static emu.grasscutter.utils.Language.translate;
@Command(label = "changescene", usage = "changescene <scene id>", @Command(label = "changescene", usage = "changescene <scene id>",
description = "Changes your scene", aliases = {"scene"}, permission = "player.changescene") description = "Changes your scene", aliases = {"scene"}, permission = "player.changescene")
public final class ChangeSceneCommand implements CommandHandler { public final class ChangeSceneCommand implements CommandHandler {
@Override @Override
public void execute(GenshinPlayer sender, List<String> args) { public void execute(Player sender, Player targetPlayer, List<String> args) {
if (sender == null) { if (targetPlayer == null) {
CommandHandler.sendMessage(null, "Run this command in-game."); CommandHandler.sendMessage(sender, translate("commands.execution.need_target"));
return; return;
} }
if (args.size() < 1) { if (args.size() != 1) {
CommandHandler.sendMessage(sender, "Usage: changescene <scene id>"); CommandHandler.sendMessage(sender, translate("commands.changescene.usage"));
return; return;
} }
try { try {
int sceneId = Integer.parseInt(args.get(0)); int sceneId = Integer.parseInt(args.get(0));
if (sceneId == targetPlayer.getSceneId()) {
if (sceneId == sender.getSceneId()) { CommandHandler.sendMessage(sender, translate("commands.changescene.already_in_scene"));
CommandHandler.sendMessage(sender, "You are already in that scene");
return; return;
} }
boolean result = sender.getWorld().transferPlayerToScene(sender, sceneId, sender.getPos()); boolean result = targetPlayer.getWorld().transferPlayerToScene(targetPlayer, sceneId, targetPlayer.getPos());
CommandHandler.sendMessage(sender, "Changed to scene " + sceneId); CommandHandler.sendMessage(sender, translate("commands.changescene.result", Integer.toString(sceneId)));
if (!result) { if (!result) {
CommandHandler.sendMessage(sender, "Scene does not exist"); CommandHandler.sendMessage(sender, translate("commands.changescene.exists_error"));
} }
} catch (Exception e) { } 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; package emu.grasscutter.command.commands;
import emu.grasscutter.Grasscutter;
import emu.grasscutter.command.Command; import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler; import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.data.GenshinData; import emu.grasscutter.data.GameData;
import emu.grasscutter.data.def.ItemData; import emu.grasscutter.data.def.ItemData;
import emu.grasscutter.game.GenshinPlayer;
import emu.grasscutter.game.entity.EntityItem; import emu.grasscutter.game.entity.EntityItem;
import emu.grasscutter.game.player.Player;
import emu.grasscutter.utils.Position; import emu.grasscutter.utils.Position;
import java.util.List; import java.util.List;
import static emu.grasscutter.utils.Language.translate;
@Command(label = "drop", usage = "drop <itemId|itemName> [amount]", @Command(label = "drop", usage = "drop <itemId|itemName> [amount]",
description = "Drops an item near you", aliases = {"d", "dropitem"}, permission = "server.drop") description = "Drops an item near you", aliases = {"d", "dropitem"}, permission = "server.drop")
public final class DropCommand implements CommandHandler { public final class DropCommand implements CommandHandler {
@Override @Override
public void execute(GenshinPlayer sender, List<String> args) { public void execute(Player sender, Player targetPlayer, List<String> args) {
if (sender == null) { if (targetPlayer == null) {
CommandHandler.sendMessage(null, "Run this command in-game."); CommandHandler.sendMessage(null, translate("commands.execution.need_target"));
return; return;
} }
if (args.size() < 1) { int item = 0;
CommandHandler.sendMessage(sender, "Usage: drop <itemId|itemName> [amount]"); int amount = 1;
return;
}
switch (args.size()) {
case 2:
try { try {
int item = Integer.parseInt(args.get(0)); amount = Integer.parseInt(args.get(1));
int amount = 1; } catch (NumberFormatException ignored) {
if (args.size() > 1) amount = Integer.parseInt(args.get(1)); 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) { if (itemData == null) {
CommandHandler.sendMessage(sender, "Invalid item id."); CommandHandler.sendMessage(sender, translate("commands.generic.invalid.itemId"));
return; return;
} }
if (itemData.isEquip()) { if (itemData.isEquip()) {
float range = (5f + (.1f * amount)); float range = (5f + (.1f * amount));
for (int i = 0; i < amount; i++) { 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)); 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(sender.getScene(), sender, itemData, pos, 1); EntityItem entity = new EntityItem(targetPlayer.getScene(), targetPlayer, itemData, pos, 1);
sender.getScene().addEntity(entity); targetPlayer.getScene().addEntity(entity);
} }
} else { } else {
EntityItem entity = new EntityItem(sender.getScene(), sender, itemData, sender.getPos().clone().addY(3f), amount); EntityItem entity = new EntityItem(targetPlayer.getScene(), targetPlayer, itemData, targetPlayer.getPos().clone().addY(3f), amount);
sender.getScene().addEntity(entity); targetPlayer.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.");
} }
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