Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/development' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
KingRainbow44 committed May 7, 2022
2 parents 32b9e8f + 6ad0a1f commit fca5248
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 30 deletions.
19 changes: 13 additions & 6 deletions src/main/java/emu/grasscutter/Grasscutter.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,20 @@ public static void main(String[] args) throws Exception {
Crypto.loadKeys(); // Load keys from buffers.

// Parse arguments.
boolean exitEarly = false;
for (String arg : args) {
switch (arg.toLowerCase()) {
case "-handbook" -> Tools.createGmHandbook();
case "-gachamap" -> Tools.createGachaMapping("./gacha-mapping.js");
case "-handbook" -> {
Tools.createGmHandbook(); exitEarly = true;
}
case "-gachamap" -> {
Tools.createGachaMapping("./gacha-mapping.js"); exitEarly = true;
}
}
}
}

// Exit early if argument sets it.
if(exitEarly) System.exit(0);

// Initialize server.
Grasscutter.getLogger().info(translate("messages.status.starting"));
Expand All @@ -88,14 +96,13 @@ public static void main(String[] args) throws Exception {
// Database
DatabaseManager.initialize();

// Create plugin manager instance.
pluginManager = new PluginManager();

// Create server instances.
dispatchServer = new DispatchServer();
gameServer = new GameServer();
// Create a server hook instance with both servers.
new ServerHook(gameServer, dispatchServer);
// Create plugin manager instance.
pluginManager = new PluginManager();

// Start servers.
if (getConfig().RunMode == ServerRunMode.HYBRID) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public void run() {
if (Grasscutter.getConfig().OpenStamina) {
boolean moving = isPlayerMoving();
if (moving || (getCurrentStamina() < getMaximumStamina())) {
Grasscutter.getLogger().debug("Player moving: " + moving + ", stamina full: " + (getCurrentStamina() >= getMaximumStamina()) + ", recalculate stamina");
// Grasscutter.getLogger().debug("Player moving: " + moving + ", stamina full: " + (getCurrentStamina() >= getMaximumStamina()) + ", recalculate stamina");
Consumption consumption = Consumption.None;

// TODO: refactor these conditions.
Expand Down Expand Up @@ -306,14 +306,16 @@ public void run() {
} else if (MotionStatesCategorized.get("RUN").contains(currentState)) {
// RUN, DASH and WALK
// DASH
if (currentState == MotionState.MOTION_DASH) {
if (previousState == MotionState.MOTION_DASH) {
if (currentState == MotionState.MOTION_DASH_BEFORE_SHAKE) {
consumption = Consumption.DASH;
if (previousState == MotionState.MOTION_DASH_BEFORE_SHAKE) {
// only charge once
consumption = Consumption.SPRINT;
} else {
// cost more to start dashing
consumption = Consumption.DASH;
}
}
if (currentState == MotionState.MOTION_DASH) {
consumption = Consumption.SPRINT;
}
// RUN
if (currentState == MotionState.MOTION_RUN) {
consumption = Consumption.RUN;
Expand Down Expand Up @@ -347,14 +349,13 @@ public void run() {
staminaRecoverDelay = 0;
}
if (consumption.amount > 0) {
if (staminaRecoverDelay < 5) {
if (staminaRecoverDelay < 10) {
staminaRecoverDelay++;
consumption = Consumption.None;
}
}
int newStamina = updateStamina(cachedSession, consumption.amount);
cachedSession.send(new PacketPlayerPropNotify(player, PlayerProperty.PROP_CUR_PERSIST_STAMINA));

Grasscutter.getLogger().debug(player.getProperty(PlayerProperty.PROP_CUR_PERSIST_STAMINA) + "/" + player.getProperty(PlayerProperty.PROP_MAX_STAMINA) + "\t" + currentState + "\t" + "isMoving: " + isPlayerMoving() + "\t" + consumption + "(" + consumption.amount + ")");
}
}
Expand Down
34 changes: 21 additions & 13 deletions src/main/java/emu/grasscutter/server/game/GameServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
import java.time.OffsetDateTime;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import static emu.grasscutter.utils.Language.translate;

Expand Down Expand Up @@ -79,19 +82,6 @@ public GameServer(InetSocketAddress address) {
this.dropManager = new DropManager(this);
this.expeditionManager = new ExpeditionManager(this);
this.combineManger = new CombineManger(this);

// Schedule game loop.
Timer gameLoop = new Timer();
gameLoop.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
onTick();
} catch (Exception e) {
Grasscutter.getLogger().error(translate("messages.game.game_update_error"), e);
}
}
}, new Date(), 1000L);

// Hook into shutdown event.
Runtime.getRuntime().addShutdownHook(new Thread(this::onServerShutdown));
Expand Down Expand Up @@ -229,6 +219,24 @@ public void deregisterWorld(World world) {

}

@Override
public synchronized void start() {
// Schedule game loop.
Timer gameLoop = new Timer();
gameLoop.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
try {
onTick();
} catch (Exception e) {
Grasscutter.getLogger().error(translate("messages.game.game_update_error"), e);
}
}
}, new Date(), 1000L);

super.start();
}

@Override
public void onStartFinish() {
Grasscutter.getLogger().info(translate("messages.status.free_software"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;

@SuppressWarnings("unchecked")
public class GameServerPacketHandler {
private final Int2ObjectMap<PacketHandler> handlers;

Expand Down
2 changes: 0 additions & 2 deletions src/main/java/emu/grasscutter/tools/Tools.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ public static void createGmHandbook() throws Exception {
}

Grasscutter.getLogger().info("GM Handbook generated!");
System.exit(0);
}

@SuppressWarnings("deprecation")
Expand Down Expand Up @@ -183,6 +182,5 @@ public static void createGachaMapping(String location) throws Exception {
}

Grasscutter.getLogger().info("Mappings generated!");
System.exit(0);
}
}
2 changes: 1 addition & 1 deletion src/main/java/emu/grasscutter/utils/Language.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private Language(String fileName) {
@Nullable JsonObject languageData = null;

try {
InputStream file = Grasscutter.class.getResourceAsStream("/lang/" + fileName);
InputStream file = Grasscutter.class.getResourceAsStream("/languages/" + fileName);
languageData = Grasscutter.getGsonFactory().fromJson(Utils.readFromInputStream(file), JsonObject.class);
} catch (Exception exception) {
Grasscutter.getLogger().error("Failed to load language file: " + fileName, exception);
Expand Down
39 changes: 39 additions & 0 deletions src/main/java/emu/grasscutter/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.nio.file.StandardCopyOption;
import java.time.*;
import java.time.temporal.TemporalAdjusters;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import emu.grasscutter.Config;
Expand Down Expand Up @@ -260,4 +262,41 @@ public static String readFromInputStream(InputStream stream) {
Grasscutter.getLogger().warn("Failed to read from input stream.");
} return stringBuilder.toString();
}

/**
* Switch properties from upper case to lower case?
*/
public static Map<String, Object> switchPropertiesUpperLowerCase(Map<String, Object> objMap, Class<?> cls) {
Map<String, Object> map = new HashMap<>(objMap.size());
for (String key : objMap.keySet()) {
try {
char c = key.charAt(0);
if (c >= 'a' && c <= 'z') {
try {
cls.getDeclaredField(key);
map.put(key, objMap.get(key));
} catch (NoSuchFieldException e) {
String s1 = String.valueOf(c).toUpperCase();
String after = key.length() > 1 ? s1 + key.substring(1) : s1;
cls.getDeclaredField(after);
map.put(after, objMap.get(key));
}
} else if (c >= 'A' && c <= 'Z') {
try {
cls.getDeclaredField(key);
map.put(key, objMap.get(key));
} catch (NoSuchFieldException e) {
String s1 = String.valueOf(c).toLowerCase();
String after = key.length() > 1 ? s1 + key.substring(1) : s1;
cls.getDeclaredField(after);
map.put(after, objMap.get(key));
}
}
} catch (NoSuchFieldException e) {
map.put(key, objMap.get(key));
}
}

return map;
}
}

0 comments on commit fca5248

Please sign in to comment.