Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
target/
*.iml
dependency-reduced-pom.xml
.idea/
.idea/
.DS_Store
17 changes: 14 additions & 3 deletions src/main/java/com/sulphate/chatcolor2/main/ChatColor.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,22 @@ public void onEnable() {

@Override
public void onDisable() {
guiManager.closeOpenGuis();
playerDataStore.shutdown();
if (guiManager != null) {
guiManager.closeOpenGuis();
}

if (playerDataStore != null) {
playerDataStore.shutdown();
}

plugin = null;

console.sendMessage(M.PREFIX + M.SHUTDOWN.replace("[version]", getDescription().getVersion()));
if (M != null) {
console.sendMessage(M.PREFIX + M.SHUTDOWN.replace("[version]", getDescription().getVersion()));
}
else {
console.sendMessage(GeneralUtils.colourise("&b[ChatColor] &eChatColor 2 Version &b" + getDescription().getVersion() + " &ehas been &cdisabled&e."));
}
}

private void setupObjects() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
import org.bukkit.inventory.ItemStack;

import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// Utils for managing cross-api-version compatability.
public class CompatabilityUtils {

private static final Pattern VERSION_PATTERN = Pattern.compile("(\\d+(?:\\.\\d+)+)");

private static boolean isMaterialLegacy;
private static boolean isHexLegacy;
private static HashMap<String, Short> blockColourToDataMap;
Expand All @@ -19,14 +23,7 @@ private CompatabilityUtils() {
}

public static void init() {
// Parse minor version to check for hex compatability.
String version = Bukkit.getBukkitVersion();
version = version.substring(0, version.indexOf('-'));

int dotIndex = version.indexOf('.');
float minorVersion = Float.parseFloat(version.substring(dotIndex + 1));

isHexLegacy = minorVersion < 16;
isHexLegacy = isHexLegacyVersion(Bukkit.getBukkitVersion());
isMaterialLegacy = Material.getMaterial("INK_SAC") == null;

blockColourToDataMap = new HashMap<>();
Expand All @@ -43,6 +40,41 @@ public static void init() {
}
}

private static boolean isHexLegacyVersion(String version) {
int[] versionParts = parseVersionParts(version);

if (versionParts.length < 2) {
return false;
}

return versionParts[0] == 1 && versionParts[1] < 16;
}

private static int[] parseVersionParts(String version) {
if (version == null) {
return new int[0];
}

Matcher matcher = VERSION_PATTERN.matcher(version);
if (!matcher.find()) {
return new int[0];
}

String[] rawParts = matcher.group(1).split("\\.");
int[] versionParts = new int[rawParts.length];

for (int i = 0; i < rawParts.length; i++) {
try {
versionParts[i] = Integer.parseInt(rawParts[i]);
}
catch (NumberFormatException ex) {
return new int[0];
}
}

return versionParts;
}

public static ItemStack getColouredItem(String materialName) {
if (!isMaterialLegacy) {
return new ItemStack(Material.getMaterial(materialName), 1);
Expand Down