Skip to content

Commit

Permalink
Remove old NMS code
Browse files Browse the repository at this point in the history
  • Loading branch information
rutgerkok committed Apr 1, 2023
1 parent 9d4026d commit aed37ef
Show file tree
Hide file tree
Showing 9 changed files with 3 additions and 1,142 deletions.
Expand Up @@ -45,8 +45,6 @@
import nl.rutgerkok.blocklocker.impl.group.TownyGroupSystem;
import nl.rutgerkok.blocklocker.impl.group.mcMMOGroupSystem;
import nl.rutgerkok.blocklocker.impl.location.TownyLocationChecker;
import nl.rutgerkok.blocklocker.impl.nms.NMSAccessorProvider;
import nl.rutgerkok.blocklocker.impl.nms.ServerSpecific;
import nl.rutgerkok.blocklocker.impl.profile.ProfileFactoryImpl;
import nl.rutgerkok.blocklocker.impl.updater.Updater;
import nl.rutgerkok.blocklocker.location.CombinedLocationChecker;
Expand All @@ -56,7 +54,6 @@ public class BlockLockerPluginImpl extends JavaPlugin implements BlockLockerPlug
private ChestSettings chestSettings;
private CombinedGroupSystem combinedGroupSystem;
private Config config;
private ServerSpecific nms;
private ProfileFactoryImpl profileFactory;
private ProtectionFinderImpl protectionFinder;
private ProtectionUpdater protectionUpdater;
Expand Down Expand Up @@ -195,7 +192,7 @@ private void loadServices() {
// Parsers and finders
profileFactory = new ProfileFactoryImpl(combinedGroupSystem, translator);
chestSettings = new ChestSettingsImpl(translator, config);
signParser = new SignParserImpl(chestSettings, nms, profileFactory);
signParser = new SignParserImpl(chestSettings, profileFactory);
BlockFinder blockFinder = BlockFinder.create(signParser, config.getConnectContainers());
protectionFinder = new ProtectionFinderImpl(blockFinder, chestSettings);
protectionUpdater = new ProtectionUpdaterImpl(getServer(), signParser, profileFactory);
Expand Down Expand Up @@ -228,16 +225,6 @@ private Translator loadTranslations(String fileName) {

@Override
public void onEnable() {
// NMS checks
try {
this.nms = NMSAccessorProvider.create();
} catch (Throwable t) {
getLogger()
.log(Level.SEVERE, "This Minecraft version is not supported. Find another version of the plugin, if available.", t);
Bukkit.getPluginManager().disablePlugin(this);
return;
}

loadServices();

// Events
Expand Down
73 changes: 2 additions & 71 deletions src/main/java/nl/rutgerkok/blocklocker/impl/SignParserImpl.java
Expand Up @@ -16,11 +16,8 @@

import nl.rutgerkok.blocklocker.ChestSettings;
import nl.rutgerkok.blocklocker.ProtectionSign;
import nl.rutgerkok.blocklocker.SecretSignEntry;
import nl.rutgerkok.blocklocker.SignParser;
import nl.rutgerkok.blocklocker.SignType;
import nl.rutgerkok.blocklocker.impl.nms.ServerSpecific;
import nl.rutgerkok.blocklocker.impl.nms.ServerSpecific.JsonSign;
import nl.rutgerkok.blocklocker.impl.profile.ProfileFactoryImpl;
import nl.rutgerkok.blocklocker.profile.Profile;

Expand All @@ -36,26 +33,13 @@ class SignParserImpl implements SignParser {
NbtSecretSignEntry.key("profile_2"), NbtSecretSignEntry.key("profile_3") };

private final ChestSettings chestSettings;
private final ServerSpecific nms;
private final ProfileFactoryImpl profileFactory;

SignParserImpl(ChestSettings chestSettings, ServerSpecific nms,
ProfileFactoryImpl profileFactory) {
this.nms = nms;
SignParserImpl(ChestSettings chestSettings, ProfileFactoryImpl profileFactory) {
this.profileFactory = profileFactory;
this.chestSettings = chestSettings;
}

private int countProfileLines(String[] linesOnSign) {
int count = 0;
for (int i = 1; i < linesOnSign.length; i++) {
if (linesOnSign[i].length() > 0) {
count++;
}
}
return count;
}

@Override
public String[] getDisplayLines(ProtectionSign sign) {
List<Profile> profiles = sign.getProfiles();
Expand Down Expand Up @@ -140,53 +124,6 @@ private Optional<ProtectionSign> parseAdvancedSign(Sign sign) {
.of(new ProtectionSignImpl(sign.getLocation(), type, profiles, signHadDataMismatch || headerMismatch));
}

/**
* Used for signs where the hidden text was found.
*
* @param location
* The location of the sign.
* @param jsonSign
* The hidden JSON stored on the sign.
* @param linesOnSign
* The displayed lines stored on the sign.
* @return The parsed sign, if the sign is actually a protection sign.
*/
private Optional<ProtectionSign> parseOldAdvancedSign(Location location, JsonSign jsonSign, String[] linesOnSign) {
SignType signType = getSignTypeOrNull(jsonSign.getFirstLine());
if (signType == null) {
return Optional.empty();
}

List<Profile> profiles = new ArrayList<>();
int lineNumber = 1; // Starts as one, as line 0 contains the sign header
for (SecretSignEntry object : jsonSign) {
Optional<Profile> oProfile = profileFactory.fromSavedObject(object);
if (oProfile.isPresent()) {
Profile profile = oProfile.get();

String lineOnSign = linesOnSign[lineNumber];
if (!profile.getDisplayName().equals(lineOnSign)) {
// JSON data doesn't match sign contents, so it must be corrupt or outdated
// Therefore, ignore the data
return parseSimpleSign(location, linesOnSign);
}

profiles.add(profile);
}
lineNumber++;
}

if (countProfileLines(linesOnSign) > profiles.size()) {
// JSON data is incomplete, therefore corrupt or outdated
// Therefore, ignore the data
return parseSimpleSign(location, linesOnSign);
}

// Last argument == true: we want to save this sign in our new format
ProtectionSignImpl protectionSignImpl = new ProtectionSignImpl(location, signType, profiles, true);
return Optional.<ProtectionSign>of(protectionSignImpl);
}

@Override
public Optional<ProtectionSign> parseSign(Block sign) {
Sign signState = (Sign) sign.getState();
Expand All @@ -197,14 +134,8 @@ public Optional<ProtectionSign> parseSign(Block sign) {
return parsedSign;
}

// Try old, hacky method
JsonSign foundTextData = nms.getJsonData(sign.getWorld(), sign.getX(), sign.getY(), sign.getZ());
String[] lines = signState.getLines();
if (foundTextData.hasData()) {
return parseOldAdvancedSign(sign.getLocation(), foundTextData, lines);
}

// Try plain sign, written by the user
String[] lines = signState.getLines();
return parseSimpleSign(sign.getLocation(), lines);
}

Expand Down

0 comments on commit aed37ef

Please sign in to comment.