Skip to content

Commit 2e66f0b

Browse files
Add lock expire function, pre-release 2.8.0
1 parent 8529335 commit 2e66f0b

File tree

10 files changed

+167
-20
lines changed

10 files changed

+167
-20
lines changed

plugin.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ name: LockettePro
22
main: me.crafter.mc.lockettepro.LockettePro
33
author: connection_lost
44
softdepend: [Vault, WorldGuard, Residence, Towny, ProtocolLib, Factions, ASkyBlock, PlotSquared]
5-
version: 2.7.0
5+
loadbefore: [SignShop]
6+
version: 2.8.0-pre1
67
description: >
78
LockettePro is a chest protection plugin for Bukkit. It is 100% compatible with original Lockette, but delivers a lot of performance enhancements and feature options.
89
commands:

src/me/crafter/mc/lockettepro/BlockDebugListener.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public void onDebugClick(PlayerInteractEvent event){
2828
p.sendMessage(" - isOwner/UserSingle: " + formatBoolean(LocketteProAPI.isOwnerSingleBlock(b, null, p)) + ChatColor.RESET + "/" + formatBoolean(LocketteProAPI.isUserSingleBlock(b, null, p)));
2929
p.sendMessage("isLockedUpDownLockedDoor: " + formatBoolean(LocketteProAPI.isUpDownLockedDoor(b)));
3030
p.sendMessage(" - isOwner/UserSingle: " + formatBoolean(LocketteProAPI.isOwnerUpDownLockedDoor(b, p)) + ChatColor.RESET + "/" + formatBoolean(LocketteProAPI.isOwnerUpDownLockedDoor(b, p)));
31+
if (LocketteProAPI.isLockSign(b)){
32+
p.sendMessage("isSignExpired: " + formatBoolean(LocketteProAPI.isSignExpired(b)));
33+
p.sendMessage(" - created: " + Utils.getCreatedFromLine(((Sign)b.getState()).getLine(0)));
34+
p.sendMessage(" - now : " + (int)(System.currentTimeMillis()/1000));
35+
}
3136

3237
// p.sendMessage("isLockSign: " + formatBoolean(LocketteProAPI.isLockSign(b)));
3338
// if (LocketteProAPI.isLockSign(b)){

src/me/crafter/mc/lockettepro/BlockEnvironmentListener.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import org.bukkit.block.Block;
66
import org.bukkit.block.BlockState;
7+
import org.bukkit.entity.Villager;
78
import org.bukkit.event.EventHandler;
89
import org.bukkit.event.EventPriority;
910
import org.bukkit.event.Listener;
@@ -12,6 +13,7 @@
1213
import org.bukkit.event.block.BlockPistonRetractEvent;
1314
import org.bukkit.event.block.BlockRedstoneEvent;
1415
import org.bukkit.event.entity.EntityExplodeEvent;
16+
import org.bukkit.event.entity.EntityInteractEvent;
1517
import org.bukkit.event.world.StructureGrowEvent;
1618

1719
public class BlockEnvironmentListener implements Listener{
@@ -82,5 +84,18 @@ public void onBlockRedstoneChange(BlockRedstoneEvent event){
8284
event.setNewCurrent(event.getOldCurrent());
8385
}
8486
}
87+
88+
// Prevent villager open door
89+
@EventHandler(priority = EventPriority.HIGH)
90+
public void onSomeMobOpenDoor(EntityInteractEvent event){
91+
if (Config.isProtectionExempted("villager")) return;
92+
// Explicitly to villager vs all doors
93+
if (event.getEntity() instanceof Villager &&
94+
(LocketteProAPI.isSingleDoorBlock(event.getBlock()) || LocketteProAPI.isDoubleDoorBlock(event.getBlock())) &&
95+
LocketteProAPI.isProtected(event.getBlock())){
96+
}
97+
}
98+
99+
// Prevent Enderman take block
85100

86101
}

src/me/crafter/mc/lockettepro/BlockPlayerListener.java

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,66 @@ public class BlockPlayerListener implements Listener {
2626
@EventHandler(priority = EventPriority.NORMAL)
2727
public void onPlayerQuickLockChest(PlayerInteractEvent event){
2828
if (event.isCancelled()) return;
29+
// Check quick lock enabled
2930
if (Config.getQuickProtectAction() == (byte)0) return;
31+
// Get player and action info
3032
Action action = event.getAction();
3133
Player player = event.getPlayer();
34+
// Check action correctness
3235
if (action == Action.RIGHT_CLICK_BLOCK && player.getItemInHand().getType() == Material.SIGN){
36+
// Check quick lock action correctness
3337
if (!((event.getPlayer().isSneaking() && Config.getQuickProtectAction() == (byte)2) ||
3438
(!event.getPlayer().isSneaking() && Config.getQuickProtectAction() == (byte)1))) return;
39+
// Check permission
3540
if (!player.hasPermission("lockettepro.lock")) return;
41+
// Get target block to lock
3642
BlockFace blockface = event.getBlockFace();
3743
if (blockface == BlockFace.NORTH || blockface == BlockFace.WEST || blockface == BlockFace.EAST || blockface == BlockFace.SOUTH){
3844
Block block = event.getClickedBlock();
39-
if (Dependency.isProtectedFrom(block, player)) return; // External check here
40-
if (Dependency.isProtectedFrom(block.getRelative(event.getBlockFace()), player)) return; // External check again for sign here
41-
if (block.getRelative(blockface).getType() != Material.AIR) return; // This location is obstructed
45+
// Check permission with external plugin
46+
if (Dependency.isProtectedFrom(block, player)) return; // blockwise
47+
if (Dependency.isProtectedFrom(block.getRelative(event.getBlockFace()), player)) return; // signwise
48+
// Check whether locking location is obstructed
49+
if (block.getRelative(blockface).getType() != Material.AIR) return;
50+
// Check whether this block is lockable
4251
if (LocketteProAPI.isLockable(block)){
52+
// Is this block already locked?
4353
boolean locked = LocketteProAPI.isLocked(block);
54+
// Cancel event here
4455
event.setCancelled(true);
56+
// Check lock info info
4557
if (!locked && !LocketteProAPI.isUpDownLockedDoor(block)){
58+
// Not locked, not a locked door nearby
4659
Utils.removeASign(player);
60+
// Send message
4761
Utils.sendMessages(player, Config.getLang("locked-quick"));
48-
Utils.putSignOn(block, blockface, Config.getDefaultPrivateString(), player.getName());
62+
// Put sign on
63+
Block newsign = Utils.putSignOn(block, blockface, Config.getDefaultPrivateString(), player.getName());
4964
Utils.resetCache(block);
65+
// Cleanups - UUID
5066
if (Config.isUuidEnabled()){
51-
Utils.updateLineByPlayer(block.getRelative(blockface), 1, player);
67+
Utils.updateLineByPlayer(newsign, 1, player);
5268
}
69+
// Cleanups - Expiracy
70+
// if (Config.isLockExpire()){
71+
// if (player.hasPermission("lockettepro.noexpire")){
72+
// Utils.updateLineWithTime(newsign, true); // set created to -1 (no expire)
73+
// } else {
74+
Utils.updateLineWithTime(newsign, false); // set created to now
75+
// }
76+
// }
5377
} else if (!locked && LocketteProAPI.isOwnerUpDownLockedDoor(block, player)){
78+
// Not locked, (is locked door nearby), is owner of locked door nearby
5479
Utils.removeASign(player);
5580
Utils.sendMessages(player, Config.getLang("additional-sign-added-quick"));
5681
Utils.putSignOn(block, blockface, Config.getDefaultAdditionalString(), "");
5782
} else if (LocketteProAPI.isOwner(block, player)){
83+
// Locked, (not locked door nearby), is owner of locked block
5884
Utils.removeASign(player);
5985
Utils.putSignOn(block, blockface, Config.getDefaultAdditionalString(), "");
6086
Utils.sendMessages(player, Config.getLang("additional-sign-added-quick"));
6187
} else {
88+
// Cannot lock this block
6289
Utils.sendMessages(player, Config.getLang("cannot-lock-quick"));
6390
}
6491
}
@@ -159,7 +186,11 @@ public void onAttemptBreakSign(BlockBreakEvent event){
159186
Utils.playAccessDenyEffect(player, block);
160187
}
161188
} else if (LocketteProAPI.isAdditionalSign(block)){
162-
if (LocketteProAPI.isOwnerOfSign(block, player)){
189+
// TODO the next line is spaghetti
190+
if (!LocketteProAPI.isLocked(LocketteProAPI.getAttachedBlock(block))){
191+
// phew, the locked block is expired!
192+
// nothing
193+
} else if (LocketteProAPI.isOwnerOfSign(block, player)){
163194
Utils.sendMessages(player, Config.getLang("break-own-additional-sign"));
164195
} else if (!LocketteProAPI.isProtected(LocketteProAPI.getAttachedBlock(block))){
165196
Utils.sendMessages(player, Config.getLang("break-redundant-additional-sign"));

src/me/crafter/mc/lockettepro/Config.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public class Config {
3434
private static int cachetime = 0;
3535
private static boolean cacheenabled = false;
3636
private static byte blockhopperminecart = 0;
37+
private static boolean lockexpire = false;
38+
private static double lockexpiredays = 60D;
39+
private static long lockdefaultcreatetime = -1L;
40+
private static String lockexpirestring = "";
3741
private static Set<String> protectionexempt = new HashSet<String>();
3842

3943
public Config(Plugin _plugin){
@@ -108,6 +112,13 @@ public static void reload(){
108112
blockhopperminecart = 2;
109113
break;
110114
}
115+
116+
lockexpire = config.getBoolean("lock-expire", false);
117+
lockexpiredays = config.getDouble("lock-expire-days", 0D);
118+
lockdefaultcreatetime = config.getLong("lock-default-create-time-unix", -1L);
119+
if (lockdefaultcreatetime < -1L) lockdefaultcreatetime = -1L;
120+
lockexpirestring = ChatColor.translateAlternateColorCodes('&',
121+
config.getString("lock-expire-string", "&3[Expired]"));
111122
List<String> unprocesseditems = config.getStringList("lockables");
112123
lockables = new HashSet<Material>();
113124
for (String unprocesseditem : unprocesseditems){
@@ -174,9 +185,13 @@ public static void initDefaultConfig(){
174185
config.addDefault("lockables", lockables);
175186
String[] protection_exempt = {"nothing"};
176187
config.addDefault("protection-exempt", protection_exempt);
188+
189+
config.addDefault("lock-expire", false);
190+
config.addDefault("lock-expire-days", 30D);
191+
config.addDefault("lock-default-create-time-unix", -1L);
192+
config.addDefault("lock-expire-string", ChatColor.RED + "[Expired]");
177193

178194
config.options().copyDefaults(true);
179-
180195
try {
181196
config.save(new File(plugin.getDataFolder(), "config.yml"));
182197
} catch (IOException e) {
@@ -185,7 +200,7 @@ public static void initDefaultConfig(){
185200
}
186201

187202
public static void initAdditionalFiles(){
188-
String[] availablefiles = {"lang.yml", "lang_zh-cn.yml", "lang_es.yml"};
203+
String[] availablefiles = {"lang.yml", "lang_zh-cn.yml", "lang_es.yml", "lang_it.yml"};
189204
for (String filename : availablefiles){
190205
File langfile = new File(plugin.getDataFolder(), filename);
191206
if (!langfile.exists()){
@@ -200,6 +215,11 @@ public static void initAdditionalFiles(){
200215
public static boolean isItemTransferOutBlocked() {return blockitemtransferout;}
201216
public static byte getHopperMinecartAction() {return blockhopperminecart;}
202217

218+
public static boolean isLockExpire() {return lockexpire;}
219+
public static Double getLockExpireDays() {return lockexpiredays;}
220+
public static long getLockDefaultCreateTimeUnix() {return lockdefaultcreatetime;}
221+
public static String getLockExpireString() {return lockexpirestring;}
222+
203223
public static String getLang(String path){
204224
return ChatColor.translateAlternateColorCodes('&', lang.getString(path, ""));
205225
}

src/me/crafter/mc/lockettepro/LockettePro.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
public class LockettePro extends JavaPlugin {
1414

1515
private static Plugin plugin;
16-
private boolean debug = false;
16+
private boolean debug = true;
1717
private static Version version = Version.UNKNOWN;
1818
private static boolean needcheckhand = false;
1919

@@ -41,18 +41,19 @@ public void onEnable(){
4141
case v1_9_R2:
4242
case v1_10_R1:
4343
case v1_11_R1:
44+
case v1_12_R1:
45+
case UNKNOWN: // 1.13...+
4446
needcheckhand = true;
4547
break;
4648
case v1_8_R1:
4749
case v1_8_R2:
4850
case v1_8_R3:
49-
case UNKNOWN:
5051
default:
5152
needcheckhand = false;
5253
break;
5354
}
5455
// If UUID is not enabled, UUID listener won't register
55-
if (Config.isUuidEnabled()){
56+
if (Config.isUuidEnabled() || Config.isLockExpire()){
5657
if (Bukkit.getPluginManager().getPlugin("ProtocolLib") != null){
5758
DependencyProtocolLib.setUpProtocolLib(this);
5859
getServer().getPluginManager().registerEvents(new SignSendListener(), this);
@@ -135,15 +136,17 @@ public boolean onCommand(CommandSender sender, Command cmd, String commandLabel,
135136
message += args[i];
136137
}
137138
message = ChatColor.translateAlternateColorCodes('&', message);
138-
if (message.length() > 16) {
139+
if (!player.hasPermission("lockettepro.admin.edit") && !debug && message.length() > 18) {
139140
Utils.sendMessages(player, Config.getLang("line-is-too-long"));
140141
return true;
141142
}
142143
if (LocketteProAPI.isLockSign(block)){
143144
switch (args[0]){
144145
case "1":
145-
Utils.sendMessages(player, Config.getLang("cannot-change-this-line"));
146-
break;
146+
if (!debug || !player.hasPermission("lockettepro.admin.edit")){
147+
Utils.sendMessages(player, Config.getLang("cannot-change-this-line"));
148+
break;
149+
}
147150
case "2":
148151
if (!player.hasPermission("lockettepro.admin.edit")){
149152
Utils.sendMessages(player, Config.getLang("cannot-change-this-line"));
@@ -161,8 +164,10 @@ public boolean onCommand(CommandSender sender, Command cmd, String commandLabel,
161164
} else if (LocketteProAPI.isAdditionalSign(block)){
162165
switch (args[0]){
163166
case "1":
164-
Utils.sendMessages(player, Config.getLang("cannot-change-this-line"));
165-
break;
167+
if (!debug || !player.hasPermission("lockettepro.admin.edit")){
168+
Utils.sendMessages(player, Config.getLang("cannot-change-this-line"));
169+
break;
170+
}
166171
case "2":
167172
case "3":
168173
case "4":
@@ -191,6 +196,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String commandLabel,
191196
player.sendMessage("Bukkit: " + "v" + Bukkit.getServer().getClass().getPackage().getName().split("v")[1] + " / LockettePro: " + version);
192197
// Config
193198
player.sendMessage("UUID: " + Config.isUuidEnabled());
199+
player.sendMessage("Expire: " + Config.isLockExpire() + " " + (Config.isLockExpire() ? Config.getLockExpireDays() : ""));
194200
// ProtocolLib
195201
player.sendMessage("ProtocolLib info:");
196202
if (Bukkit.getPluginManager().getPlugin("ProtocolLib") == null){

src/me/crafter/mc/lockettepro/LocketteProAPI.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,12 @@ public static boolean isLockedSingleBlock(Block block, BlockFace exempt){
158158
for (BlockFace blockface : newsfaces){
159159
if (blockface == exempt) continue;
160160
Block relativeblock = block.getRelative(blockface);
161+
// Find [Private] sign?
161162
if (isLockSign(relativeblock) && (((org.bukkit.material.Sign)relativeblock.getState().getData()).getFacing() == blockface)){
163+
// Found [Private] sign, is expire turned on and expired? (relativeblock is now sign)
164+
if (Config.isLockExpire() && LocketteProAPI.isSignExpired(relativeblock)) {
165+
continue; // Private sign but expired... But impossible to have 2 [Private] signs anyway?
166+
}
162167
return true;
163168
}
164169
}
@@ -397,6 +402,18 @@ public static boolean isUserOnSign(Block block, Player player){ // Requires (isL
397402
return false;
398403
}
399404

405+
public static boolean isSignExpired(Block block){
406+
if (!isSign(block) || !isLockSign(block)) return false;
407+
return isLineExpired(((Sign)block.getState()).getLine(0));
408+
}
409+
410+
public static boolean isLineExpired(String line){
411+
long createdtime = Utils.getCreatedFromLine(line);
412+
if (createdtime == -1L) return false; // No expire
413+
long currenttime = (int)(System.currentTimeMillis()/1000);
414+
return createdtime + Config.getLockExpireDays() * 86400L < currenttime;
415+
}
416+
400417
public static boolean isUpDownLockedDoor(Block block){
401418
Block blockup = block.getRelative(BlockFace.UP);
402419
if (blockup != null && isUpDownAlsoLockableBlock(blockup) && isLocked(blockup)) return true;
@@ -422,10 +439,12 @@ public static boolean isUserUpDownLockedDoor(Block block, Player player){
422439
}
423440

424441
public static boolean isLockString(String line){
442+
if (line.contains("#")) line = line.split("#", 2)[0];
425443
return Config.isPrivateSignString(line);
426444
}
427445

428446
public static boolean isAdditionalString(String line){
447+
if (line.contains("#")) line = line.split("#", 2)[0];
429448
return Config.isAdditionalSignString(line);
430449
}
431450

src/me/crafter/mc/lockettepro/SignSendListener.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,20 @@ public class SignSendListener implements Listener {
1010
@EventHandler
1111
public void onSignSend(SignSendEvent event){
1212
if (LocketteProAPI.isLockStringOrAdditionalString(Utils.getSignLineFromUnknown(event.getLine(0)))){
13+
// Private line
14+
String line0 = Utils.getSignLineFromUnknown(event.getLine(0));
15+
if (LocketteProAPI.isLineExpired(line0)){
16+
event.setLine(0, WrappedChatComponent.fromText(Config.getLockExpireString()).getJson());
17+
} else {
18+
event.setLine(0, WrappedChatComponent.fromText(Utils.StripSharpSign(line0)).getJson());
19+
}
20+
// Other line
1321
for (int i = 1; i < 4; i ++){
1422
String line = Utils.getSignLineFromUnknown(event.getLine(i));
1523
if (Utils.isUsernameUuidLine(line)){
1624
event.setLine(i, WrappedChatComponent.fromText(Utils.getUsernameFromLine(line)).getJson());
25+
} else if (Utils.isPrivateTimeLine(line)){
26+
event.setLine(i, WrappedChatComponent.fromText(Utils.getUsernameFromLine(line)).getJson());
1727
}
1828
}
1929
}

0 commit comments

Comments
 (0)