Skip to content

Commit

Permalink
Instantly disable LockettePro when enabling to ensure safety.
Browse files Browse the repository at this point in the history
Update main signs first, so additional signs don't overwrite it.
Don't update display while updating a legacy lock.
Fix API-Version on older paper builds
Fix Sign selection error
don't allow dewaxing unupdated additional signs
update german lang file with the old lockette tranlslations. Needs more work
  • Loading branch information
FireInstall committed Nov 12, 2023
1 parent 923fbe5 commit 77c9059
Show file tree
Hide file tree
Showing 15 changed files with 209 additions and 73 deletions.
3 changes: 2 additions & 1 deletion TodoList
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ todo wiki
todo dokument whole plugin
todo permission group support
todo all langfiles except english are out of date.
todo make multi door opening/closing together configurable per sign
todo make multi door opening/closing together configurable per sign
todo load additional legacy private/everyone/timer names to update legacy signs better
10 changes: 10 additions & 0 deletions src/main/java/de/greensurvivors/padlock/Padlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import de.greensurvivors.padlock.listener.BlockEnvironmentListener;
import de.greensurvivors.padlock.listener.BlockInventoryMoveListener;
import de.greensurvivors.padlock.listener.BlockPlayerListener;
import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

Expand All @@ -36,6 +38,14 @@ public static Padlock getPlugin() {
public void onEnable() {
plugin = this;

// don't allow
Plugin lockettePro = Bukkit.getPluginManager().getPlugin("LockettePro");
if (lockettePro != null) {
Bukkit.getPluginManager().disablePlugin(lockettePro);
} else {
plugin.getLogger().info("no lockette found.");
}

// Read config
messageManager = new MessageManager(this);
configManager = new ConfigManager(this);
Expand Down
44 changes: 28 additions & 16 deletions src/main/java/de/greensurvivors/padlock/PadlockAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@
import de.greensurvivors.padlock.impl.MiscUtils;
import de.greensurvivors.padlock.impl.openabledata.DoubleBlockParts;
import de.greensurvivors.padlock.impl.openabledata.Openables;
import de.greensurvivors.padlock.impl.signdata.EveryoneSign;
import de.greensurvivors.padlock.impl.signdata.SignExpiration;
import de.greensurvivors.padlock.impl.signdata.SignLock;
import de.greensurvivors.padlock.impl.signdata.SignTimer;
import de.greensurvivors.padlock.impl.signdata.*;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.Tag;
Expand Down Expand Up @@ -59,14 +56,19 @@ public static void setInvalid(@NotNull Sign sign) {
Sign lockSign = getNearLockDoubleBlock(attachedDoor);

if (lockSign != null) {
SignLock.updateLegacyLock(lockSign);
SignExpiration.updateLegacyTime(lockSign);
SignTimer.updateLegacyTimer(lockSign);
EveryoneSign.updateLegacy(lockSign);
SignDisplay.updateDisplay(lockSign);

for (Sign additional : getAdditionalSignsDoor(attachedDoor)) {
SignLock.updateSignFromAdditional(lockSign, additional);
SignExpiration.updateLegacyTimeFromAdditional(lockSign, additional);
SignTimer.updateLegacyTimerFromAdditional(lockSign, additional);
EveryoneSign.updateLegacyFromAdditional(lockSign, additional);
}

SignLock.updateLegacyUUIDs(lockSign);
SignExpiration.updateLegacyTime(lockSign);
SignTimer.updateLegacyTimer(lockSign);
EveryoneSign.updateLegacy(lockSign);
return lockSign;
} else {
Padlock.getPlugin().getLogger().warning("Couldn't find a lock sign to update, but the double block at " + attachedTo.getLocation() + " is locked.");
Expand All @@ -77,14 +79,19 @@ public static void setInvalid(@NotNull Sign sign) {
} else if (data instanceof Chest) {
Sign lockSign = getLockChest(attachedTo);
if (lockSign != null) {
SignLock.updateLegacyLock(lockSign);
SignExpiration.updateLegacyTime(lockSign);
SignTimer.updateLegacyTimer(lockSign);
EveryoneSign.updateLegacy(lockSign);
SignDisplay.updateDisplay(lockSign);

for (Sign additional : getAdditionalSignsChest(attachedTo)) {
SignLock.updateSignFromAdditional(lockSign, additional);
SignExpiration.updateLegacyTimeFromAdditional(lockSign, additional);
SignTimer.updateLegacyTimerFromAdditional(lockSign, additional);
EveryoneSign.updateLegacyFromAdditional(lockSign, additional);
}

SignLock.updateLegacyUUIDs(lockSign);
SignExpiration.updateLegacyTime(lockSign);
SignTimer.updateLegacyTimer(lockSign);
EveryoneSign.updateLegacy(lockSign);
return lockSign;
} else {
Padlock.getPlugin().getLogger().warning("Couldn't find a lock sign to update, but the door at " + attachedTo.getLocation() + "is locked.");
Expand All @@ -93,14 +100,19 @@ public static void setInvalid(@NotNull Sign sign) {
Sign lockSign = getLockSignSingleBlock(attachedTo, null);

if (lockSign != null) {
SignLock.updateLegacyLock(lockSign);
SignExpiration.updateLegacyTime(lockSign);
SignTimer.updateLegacyTimer(lockSign);
EveryoneSign.updateLegacy(lockSign);
SignDisplay.updateDisplay(lockSign);

for (Sign additional : getAdditionalSignsSingleBlock(attachedTo, null)) {
SignLock.updateSignFromAdditional(lockSign, additional);
SignExpiration.updateLegacyTimeFromAdditional(lockSign, additional);
SignTimer.updateLegacyTimerFromAdditional(lockSign, additional);
EveryoneSign.updateLegacyFromAdditional(lockSign, additional);
}

SignLock.updateLegacyUUIDs(lockSign);
SignExpiration.updateLegacyTime(lockSign);
SignTimer.updateLegacyTimer(lockSign);
EveryoneSign.updateLegacy(lockSign);
return lockSign;
} else {
Padlock.getPlugin().getLogger().warning("Couldn't find a lock sign to update, but the block at " + attachedTo.getLocation() + "is locked.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ protected boolean onCommand(@NotNull CommandSender sender, @NotNull String[] arg
Long millisEpoch = getEpochMillisFromString(args[1]);

if (millisEpoch != null) {
SignExpiration.updateWithTime(sign, millisEpoch);
SignExpiration.updateWithTime(sign, millisEpoch, true);

plugin.getMessageManager().sendLang(sender, MessageManager.LangPath.SET_CREATED_SUCCESS);
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ protected boolean onCommand(@NotNull CommandSender sender, @NotNull String[] arg

if (setting != null) {
// success!
EveryoneSign.setEveryone(sign, setting);
EveryoneSign.setEveryone(sign, setting, true);
plugin.getMessageManager().sendLang(sender, MessageManager.LangPath.SET_EVERYONE_SUCCESS,
Placeholder.component(MessageManager.PlaceHolder.ARGUMENT.getPlaceholder(), Component.text(setting)));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ protected boolean onCommand(@NotNull CommandSender sender, @NotNull String[] arg

if (timerDuration != 0) {
// success
SignTimer.setTimer(sign, timerDuration);
SignTimer.setTimer(sign, timerDuration, true);

if (timerDuration > 0) {
plugin.getMessageManager().sendLang(sender, MessageManager.LangPath.SET_TIMER_SUCCESS_ON,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class SignSelection {
public static @Nullable Sign getSelectedSign(@NotNull Player player) {
Block signBlock = selectedSign.getIfPresent(player.getUniqueId());

if (signBlock == null || player.getWorld().getName().equals(signBlock.getWorld().getName())) {
if (signBlock != null && player.getWorld().getName().equals(signBlock.getWorld().getName())) {
if (signBlock.getState() instanceof Sign sign) {
return sign;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ public class EveryoneSign {

/**
* Set if everyone should have member access or not.
* @param shouldUpdateDisplay if the display should get updated. is important to set to false for updating a lock sign from legacy
*/
public static void setEveryone(@NotNull Sign sign, boolean everyoneHasAccess) {
public static void setEveryone(@NotNull Sign sign, boolean everyoneHasAccess, boolean shouldUpdateDisplay) {
sign.getPersistentDataContainer().set(everyoneKey, PersistentDataType.BOOLEAN, everyoneHasAccess);
sign.update();

SignDisplay.updateDisplay(sign);
if (shouldUpdateDisplay) {
SignDisplay.updateDisplay(sign);
}
}

/**
Expand All @@ -42,11 +45,12 @@ public static boolean getAccessEveryone(@NotNull Sign sign) {
}

/**
* update a legacy lockette sign with potential an everyone line on it.
* update a legacy lockette lock sign with potential an everyone line on it.
* Will not update the Display of the sign afterwarts to not overwrite other unimported data like timers
*/
@Deprecated(forRemoval = true)
public static void updateLegacy(@NotNull Sign sign) {
setEveryone(sign, getLegacySetting(sign));
setEveryone(sign, getLegacySetting(sign), false);
}

/**
Expand All @@ -73,4 +77,11 @@ private static boolean getLegacySetting(@NotNull Sign sign) {

return false;
}

/**
* update a legacy lockette additional sign with potential an everyone line on it.
*/
public static void updateLegacyFromAdditional(Sign lockSign, Sign additional) {
setEveryone(lockSign, getLegacySetting(additional), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,15 @@ public class SignExpiration {
*
* @param sign to update
* @param epochMilli milliseconds since Epoch
* @param shouldUpdateDisplay if the display should get updated. is important to set to false for updating a lock sign from legacy
*/
public static void updateWithTime(Sign sign, long epochMilli) {
public static void updateWithTime(Sign sign, long epochMilli, boolean shouldUpdateDisplay) {
sign.getPersistentDataContainer().set(createdKey, PersistentDataType.LONG, epochMilli);
sign.update();

if (shouldUpdateDisplay) {
SignDisplay.updateDisplay(sign);
}
}

/**
Expand Down Expand Up @@ -90,7 +95,8 @@ public static boolean isSignExpired(Sign sign) {
}

/**
* update a sign from lockette times to modern padlock data storage
* update a lock sign from lockette times to modern padlock data storage
* Will not update the Display of the sign afterwarts to not overwrite other unimported data like everyone
*/
@Deprecated(forRemoval = true)
public static void updateLegacyTime(@NotNull Sign sign) {
Expand All @@ -100,7 +106,21 @@ public static void updateLegacyTime(@NotNull Sign sign) {
if (matcher.matches()) {
// note that we already test if it could be a number by matching against our pattern.
// While the number still could be out of bounds, I'm willing to risk it here. Nothing should break anyway.
updateWithTime(sign, Long.parseLong(matcher.group(2)));
updateWithTime(sign, Long.parseLong(matcher.group(2)), false);
}
}

/**
* update a additional sign from lockette times to modern padlock data storage
*/
public static void updateLegacyTimeFromAdditional(Sign lockSign, Sign additional) {
String text = PlainTextComponentSerializer.plainText().serialize(lockSign.getSide(Side.FRONT).line(0));
Matcher matcher = legacyPattern.matcher(text);

if (matcher.matches()) {
// note that we already test if it could be a number by matching against our pattern.
// While the number still could be out of bounds, I'm willing to risk it here. Nothing should break anyway.
updateWithTime(additional, Long.parseLong(matcher.group(2)), true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,25 @@ public static boolean isMember(@NotNull final Sign sign, UUID uuid) {
*/
@Deprecated(forRemoval = true)
public static void updateSignFromAdditional(@NotNull Sign main, @NotNull Sign additional) {
Padlock.getPlugin().getLogger().info("updating additional sign at " + additional.getLocation());
PlainTextComponentSerializer plainedText = PlainTextComponentSerializer.plainText();
for (int i = 1; i <= 3; i++) {
String line = additional.getSide(Side.FRONT).getLine(i);
String line = plainedText.serialize(additional.getSide(Side.FRONT).line(i));

Padlock.getPlugin().getLogger().info("line: " + i + ", txt " + line);

if (line.contains("#")) {
String[] splitted = line.split("#", 2);

if (splitted[1].length() == 36) { // uuid valid check
Padlock.getPlugin().getLogger().info("member uuid");
addPlayer(main, false, Bukkit.getOfflinePlayer(UUID.fromString(splitted[1])));
}
} else {
OfflinePlayer maybePlayer = tryGetPlayerJustFromNameComp(additional.getSide(Side.FRONT).line(i));

if (maybePlayer != null) {
Padlock.getPlugin().getLogger().info("member");
addPlayer(main, false, maybePlayer);
}
}
Expand All @@ -150,7 +156,7 @@ public static void updateSignFromAdditional(@NotNull Sign main, @NotNull Sign ad
* Will not update the Display of the sign afterwarts to not overwrite other unimported data like timers
*/
@Deprecated(forRemoval = true)
public static void updateLegacyUUIDs(@NotNull Sign sign) {
public static void updateLegacyLock(@NotNull Sign sign) {
PersistentDataContainer container = sign.getPersistentDataContainer();

ListOrderedSet<String> owners = container.get(storedOwnersUUIDKey, uuidSetDataType);
Expand All @@ -162,8 +168,10 @@ public static void updateLegacyUUIDs(@NotNull Sign sign) {
members = new ListOrderedSet<>();
}

PlainTextComponentSerializer plainedText = PlainTextComponentSerializer.plainText();

for (int i = 1; i <= 3; i++) {
String line = sign.getSide(Side.FRONT).getLine(i);
String line = plainedText.serialize(sign.getSide(Side.FRONT).line(i));

if (line.contains("#")) {
String[] splitted = line.split("#", 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,15 @@ public class SignTimer {
* set the timer to toggle
*
* @param timerDuration time duration in milliseconds
* @param shouldUpdateDisplay if the display should get updated. is important to set to false for updating a lock sign from legacy
*/
public static void setTimer(@NotNull Sign sign, long timerDuration) {
public static void setTimer(@NotNull Sign sign, long timerDuration, boolean shouldUpdateDisplay) {
sign.getPersistentDataContainer().set(timerKey, PersistentDataType.LONG, timerDuration);
sign.update();

SignDisplay.updateDisplay(sign);
if (shouldUpdateDisplay) {
SignDisplay.updateDisplay(sign);
}
}

/**
Expand Down Expand Up @@ -108,13 +111,14 @@ public static void setTimer(@NotNull Sign sign, long timerDuration) {

/**
* update from a legacy timer, purely written on the sign to one in the getPersistentDataContainer.
* Will not update the Display of the sign afterwarts to not overwrite other unimported data like timers
*/
@Deprecated(forRemoval = true)
public static void updateLegacyTimer(@NotNull Sign sign) {
Long legacyTimer = getLegacyTimer(sign);

if (legacyTimer != null) {
setTimer(sign, legacyTimer);
setTimer(sign, legacyTimer, false);
}
}

Expand All @@ -135,4 +139,15 @@ public static void updateLegacyTimer(@NotNull Sign sign) {

return null;
}

/**
* update from a legacy timer, purely written on the lock sign to one in the getPersistentDataContainer.
*/
public static void updateLegacyTimerFromAdditional(Sign lockSign, Sign additional) {
Long legacyTimer = getLegacyTimer(additional);

if (legacyTimer != null) {
setTimer(lockSign, legacyTimer, true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ private void onManualLock(@NotNull SignChangeEvent event) {
} else if (strLine.isBlank() && player.hasPermission(PermissionManager.ADMIN_BREAK.getPerm())) { // failsafe: only allow setting a sign without owner if you could break it!
SignLock.addPlayer(sign, true, null);
} else {
plugin.getMessageManager().sendLang(player, MessageManager.LangPath.UNKNOWN_PLAYER);
plugin.getMessageManager().sendLang(player, MessageManager.LangPath.UNKNOWN_PLAYER,
Placeholder.unparsed(MessageManager.PlaceHolder.PLAYER.getPlaceholder(), strLine));
event.line(0, plugin.getMessageManager().getLang(MessageManager.LangPath.ERROR_SIGN));
return;
}
Expand All @@ -203,9 +204,9 @@ private void onManualLock(@NotNull SignChangeEvent event) {
Long timer = SignTimer.getTimerFromComp(line);

if (timer != null) {
SignTimer.setTimer(sign, timer);
SignTimer.setTimer(sign, timer, true);
} else if (plugin.getMessageManager().isSignComp(line, MessageManager.LangPath.EVERYONE_SIGN)) {
EveryoneSign.setEveryone(sign, true);
EveryoneSign.setEveryone(sign, true, true);
} else if (MiscUtils.isUserName(strLine)) {
SignLock.addPlayer(sign, false, Bukkit.getOfflinePlayer(strLine));
} // else invalid line
Expand Down Expand Up @@ -294,7 +295,8 @@ private void onAttemptBreakBlock(@NotNull BlockBreakEvent event) {
@EventHandler(ignoreCancelled = true, priority = EventPriority.LOW)
private void onAttemptChangeLockSign(@NotNull SignChangeEvent event) {
Block block = event.getBlock();
if (block.getState() instanceof Sign sign && (PadlockAPI.isLockSign(sign))) {
if (block.getState() instanceof Sign sign &&
(PadlockAPI.isLockSign(sign) || PadlockAPI.isAdditionalSign(sign))) {
plugin.getMessageManager().sendLang(event.getPlayer(), MessageManager.LangPath.ACTION_PREVENTED_USE_CMDS);
sign.setWaxed(true);
sign.update();
Expand Down
Loading

0 comments on commit 77c9059

Please sign in to comment.