Skip to content
This repository has been archived by the owner on Dec 28, 2019. It is now read-only.

Commit

Permalink
Fix Java 8 support (#489)
Browse files Browse the repository at this point in the history
  • Loading branch information
kolakcc authored and ronancpl committed Jul 16, 2019
1 parent 2df59b2 commit 68fae41
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 28 deletions.
1 change: 0 additions & 1 deletion configuration.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ HOST=127.0.0.1
URL=jdbc:mysql://localhost:3306/heavenms
DB_USER=root
DB_PASS=
JAVA8=FALSE
SHUTDOWNHOOK=true
28 changes: 23 additions & 5 deletions src/constants/ServerConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,8 @@ public class ServerConstants {
public static boolean LOCALSERVER;

//Other Configuration
public static boolean JAVA_8;
public static final boolean JAVA_8 = getJavaVersion() >= 8;
public static boolean SHUTDOWNHOOK;
// JAVA_8: every static function in AbstractPlayerInteraction are to be made non-static, and code comment sections uncommented after enabling this functionality.


//Server Flags
public static final boolean USE_CUSTOM_KEYSET = true; //Enables auto-setup of the HeavenMS's custom keybindings when creating characters.
Expand Down Expand Up @@ -320,8 +318,7 @@ public class ServerConstants {
ServerConstants.DB_USER = p.getProperty("DB_USER");
ServerConstants.DB_PASS = p.getProperty("DB_PASS");

//java8 And Shutdownhook
ServerConstants.JAVA_8 = p.getProperty("JAVA8").equalsIgnoreCase("TRUE");
// shutdownhook
ServerConstants.SHUTDOWNHOOK = p.getProperty("SHUTDOWNHOOK").equalsIgnoreCase("true");

} catch (Exception e) {
Expand All @@ -330,4 +327,25 @@ public class ServerConstants {
System.exit(0);
}
}
// https://github.com/openstreetmap/josm/blob/a3a6e8a6b657cf4c5b4c64ea14d6e87be6280d65/src/org/openstreetmap/josm/tools/Utils.java#L1566-L1585
/**
* Returns the Java version as an int value.
* @return the Java version as an int value (8, 9, etc.)
* @since 12130
*/
public static int getJavaVersion() {
String version = System.getProperty("java.version");
if (version.startsWith("1.")) {
version = version.substring(2);
}
// Allow these formats:
// 1.8.0_72-ea
// 9-ea
// 9
// 9.0.1
int dotPos = version.indexOf('.');
int dashPos = version.indexOf('-');
return Integer.parseInt(version.substring(0,
dotPos > -1 ? dotPos : dashPos > -1 ? dashPos : 1));
}
}
18 changes: 9 additions & 9 deletions src/scripting/AbstractPlayerInteraction.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ public MapleMap getMap() {
return c.getPlayer().getMap();
}

public static int getHourOfDay() {
public int getHourOfDay() {
return Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
}

public int getMarketPortalId(int mapId) {
return getMarketPortalId(getWarpMap(mapId));
}

private static int getMarketPortalId(MapleMap map) {
private int getMarketPortalId(MapleMap map) {
return (map.findMarketPortal() != null) ? map.findMarketPortal().getId() : map.getRandomPlayerSpawnpoint().getId();
}

Expand Down Expand Up @@ -236,7 +236,7 @@ public boolean canHold(int itemid, int quantity, int removeItemid, int removeQua
return canHoldAllAfterRemoving(Collections.singletonList(itemid), Collections.singletonList(quantity), Collections.singletonList(removeItemid), Collections.singletonList(removeQuantity));
}

private static List<Integer> convertToIntegerArray(List<Double> list) {
private List<Integer> convertToIntegerArray(List<Double> list) {
List<Integer> intList = new LinkedList<>();
for(Double d: list) {
intList.add(d.intValue());
Expand Down Expand Up @@ -270,7 +270,7 @@ private boolean canHoldAll(List<Integer> itemids, List<Integer> quantity, boolea
return MapleInventory.checkSpots(c.getPlayer(), addedItems, false);
}

private static List<Pair<Item, MapleInventoryType>> prepareProofInventoryItems(List<Pair<Integer, Integer>> items) {
private List<Pair<Item, MapleInventoryType>> prepareProofInventoryItems(List<Pair<Integer, Integer>> items) {
List<Pair<Item, MapleInventoryType>> addedItems = new LinkedList<>();
for(Pair<Integer, Integer> p : items) {
Item it = new Item(p.getLeft(), (short) 0, p.getRight().shortValue());
Expand All @@ -280,7 +280,7 @@ private static List<Pair<Item, MapleInventoryType>> prepareProofInventoryItems(L
return addedItems;
}

private static List<List<Pair<Integer, Integer>>> prepareInventoryItemList(List<Integer> itemids, List<Integer> quantity) {
private List<List<Pair<Integer, Integer>>> prepareInventoryItemList(List<Integer> itemids, List<Integer> quantity) {
int size = Math.min(itemids.size(), quantity.size());

List<List<Pair<Integer, Integer>>> invList = new ArrayList<>(6);
Expand Down Expand Up @@ -948,7 +948,7 @@ public void gainAndEquip(int itemid, short slot) {
c.announce(MaplePacketCreator.modifyInventory(false, Collections.singletonList(new ModifyInventory(0, newItem))));
}

public static void spawnNpc(int npcId, Point pos, MapleMap map) {
public void spawnNpc(int npcId, Point pos, MapleMap map) {
MapleNPC npc = MapleLifeFactory.getNPC(npcId);
if (npc != null) {
npc.setPosition(pos);
Expand All @@ -967,11 +967,11 @@ public void spawnMonster(int id, int x, int y) {
getPlayer().getMap().spawnMonster(monster);
}

public static MapleMonster getMonsterLifeFactory(int mid) {
public MapleMonster getMonsterLifeFactory(int mid) {
return MapleLifeFactory.getMonster(mid);
}

public static MobSkill getMobSkill(int skill, int level) {
public MobSkill getMobSkill(int skill, int level) {
return MobSkillFactory.getMobSkill(skill, level);
}

Expand Down Expand Up @@ -1165,7 +1165,7 @@ public boolean canGetFirstJob(int jobType) {
}
}

public static String getFirstJobStatRequirement(int jobType) {
public String getFirstJobStatRequirement(int jobType) {
switch(jobType) {
case 1:
return "STR " + 35;
Expand Down
18 changes: 8 additions & 10 deletions src/scripting/event/EventManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
package scripting.event;

import jdk.nashorn.api.scripting.ScriptObjectMirror;
import jdk.nashorn.api.scripting.ScriptUtils;
import tools.exceptions.EventInstanceInProgressException;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -165,14 +167,14 @@ private void emptyLocks() {
startLock = startLock.dispose();
}

private static List<Integer> convertToIntegerArray(List<Double> list) {
private List<Integer> convertToIntegerArray(List<Double> list) {
List<Integer> intList = new ArrayList<>();
for(Double d: list) intList.add(d.intValue());

return intList;
}

public static long getLobbyDelay() {
public long getLobbyDelay() {
return ServerConstants.EVENT_LOBBY_DELAY;
}

Expand All @@ -181,17 +183,14 @@ private List<Integer> getLobbyRange() {
if (!ServerConstants.JAVA_8) {
return convertToIntegerArray((List<Double>)iv.invokeFunction("setLobbyRange", (Object) null));
} else { // java 8 support here thanks to MedicOP
/*
ScriptObjectMirror object = (ScriptObjectMirror) iv.invokeFunction("setLobbyRange", (Object) null);
int[] to = object.to(int[].class);
List<Integer> list = new ArrayList<>();
for (int i : to) {
list.add(i);
}
return list;
*/

throw new NoSuchMethodException();

}
} catch (ScriptException | NoSuchMethodException ex) { // they didn't define a lobby range
List<Integer> defaultRange = new ArrayList<>();
Expand Down Expand Up @@ -750,13 +749,12 @@ public List<MaplePartyCharacter> getEligibleParty(MapleParty party) {
if(p != null) {
List<MaplePartyCharacter> lmpc;

/*if(ServerConstants.JAVA_8) {
if(ServerConstants.JAVA_8) {
lmpc = new ArrayList<>(((Map<String, MaplePartyCharacter>)(ScriptUtils.convert(p, Map.class))).values());
} else {
lmpc = new ArrayList<>((List<MaplePartyCharacter>) p);
}*/

lmpc = new ArrayList<>((List<MaplePartyCharacter>) p);
}

party.setEligibleMembers(lmpc);
return lmpc;
}
Expand Down
4 changes: 2 additions & 2 deletions src/scripting/npc/NPCConversationManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ public class NPCConversationManager extends AbstractPlayerInteraction {
private boolean itemScript;
private List<MaplePartyCharacter> otherParty;

private static Map<Integer, String> npcDefaultTalks = new HashMap<>();
private Map<Integer, String> npcDefaultTalks = new HashMap<>();

private static String getDefaultTalk(int npcid) {
private String getDefaultTalk(int npcid) {
String talk = npcDefaultTalks.get(npcid);
if (talk == null) {
talk = MapleLifeFactory.getNPCDefaultTalk(npcid);
Expand Down
2 changes: 1 addition & 1 deletion src/scripting/reactor/ReactorActionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private List<ReactorDropEntry> getDropChances() {
return ReactorScriptManager.getInstance().getDrops(reactor.getId());
}

private static List<ReactorDropEntry> generateDropList(List<ReactorDropEntry> drops, int dropRate, boolean meso, int mesoChance, int minItems) {
private List<ReactorDropEntry> generateDropList(List<ReactorDropEntry> drops, int dropRate, boolean meso, int mesoChance, int minItems) {
MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();

List<ReactorDropEntry> items = new ArrayList<>();
Expand Down

0 comments on commit 68fae41

Please sign in to comment.