Skip to content

Commit aae54c9

Browse files
committed
Add golem support
Golems can now no longer access protections, unless [Golem] or [Everyone] is on the sign.
1 parent 299df91 commit aae54c9

18 files changed

+297
-114
lines changed

pom.xml

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,19 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>nl.rutgerkok</groupId>
66
<artifactId>blocklocker</artifactId>
7-
<version>1.13</version>
7+
<version>1.14-SNAPSHOT</version>
88
<packaging>jar</packaging>
99
<name>BlockLocker</name>
1010
<url>https://github.com/rutgerkok/BlockLocker</url>
1111
<properties>
1212
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1313
<junit.jupiter.version>5.3.1</junit.jupiter.version>
14+
<maven.compiler.release>21</maven.compiler.release>
1415
</properties>
1516
<repositories>
1617
<repository>
17-
<id>spigot-repo</id>
18-
<url>https://hub.spigotmc.org/nexus/content/groups/public/</url>
18+
<id>papermc</id>
19+
<url>https://repo.papermc.io/repository/maven-public/</url>
1920
</repository>
2021
<repository>
2122
<id>rutger-repo</id>
@@ -28,17 +29,10 @@
2829
</repositories>
2930
<dependencies>
3031
<dependency>
31-
<groupId>org.spigotmc</groupId>
32-
<artifactId>spigot-api</artifactId>
33-
<version>1.20-R0.1-SNAPSHOT</version>
32+
<groupId>io.papermc.paper</groupId>
33+
<artifactId>paper-api</artifactId>
34+
<version>1.21.10-R0.1-SNAPSHOT</version>
3435
<scope>provided</scope>
35-
<exclusions>
36-
<exclusion>
37-
<!-- Don't pull in an outdated version of JUnit -->
38-
<groupId>junit</groupId>
39-
<artifactId>junit</artifactId>
40-
</exclusion>
41-
</exclusions>
4236
</dependency>
4337
<!-- Third party plugins -->
4438
<dependency>
@@ -110,14 +104,22 @@
110104
</includes>
111105
</resource>
112106
</resources>
107+
<!-- Make sure our plugin is not remapped -->
113108
<plugins>
114109
<plugin>
115110
<groupId>org.apache.maven.plugins</groupId>
116-
<artifactId>maven-compiler-plugin</artifactId>
117-
<version>3.7.0</version>
111+
<artifactId>maven-jar-plugin</artifactId>
112+
<version>3.4.2</version>
118113
<configuration>
119-
<source>17</source>
120-
<target>17</target>
114+
<archive>
115+
<index>true</index>
116+
<manifest>
117+
<addClasspath>true</addClasspath>
118+
</manifest>
119+
<manifestEntries>
120+
<paperweight-mappings-namespace>mojang</paperweight-mappings-namespace>
121+
</manifestEntries>
122+
</archive>
121123
</configuration>
122124
</plugin>
123125
</plugins>

src/main/java/nl/rutgerkok/blocklocker/AttackType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public enum AttackType {
1313
ENDERMAN,
1414
BLOCK_EXPLOSION,
1515
FIRE,
16+
GOLEM,
1617
GHAST,
1718
PISTON,
1819
SAPLING,

src/main/java/nl/rutgerkok/blocklocker/BlockLockerPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public interface BlockLockerPlugin {
4545
* Gets the cache that stores whether a block is protected or not.
4646
* @return The cache.
4747
*/
48-
HopperCache getHopperCache();
48+
ProtectionCache getProtectionCache();
4949

5050
/**
5151
* Gets the location checkers, which are used to prevent players from placing chests in the wilderness.

src/main/java/nl/rutgerkok/blocklocker/HopperCache.java

Lines changed: 0 additions & 46 deletions
This file was deleted.

src/main/java/nl/rutgerkok/blocklocker/ProfileFactory.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,11 @@ public interface ProfileFactory {
4646
* @return The profile.
4747
*/
4848
Profile fromRedstone();
49+
50+
/**
51+
* Gets the profile for golems.
52+
*
53+
* @return The profile.
54+
*/
55+
Profile fromGolem();
4956
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package nl.rutgerkok.blocklocker;
2+
3+
import org.bukkit.block.Block;
4+
5+
/**
6+
* A simple cache that stores whether a block is protected. Entries expire after
7+
* a few seconds. Used for hoppers and copper golems, which do <em>a lot</em> of successive
8+
* checks.
9+
*/
10+
public interface ProtectionCache {
11+
12+
enum CacheFlag {
13+
/**
14+
* Block is certainly protected.
15+
*/
16+
NOT_ALLOWED,
17+
/**
18+
* Block is not protected.
19+
*/
20+
ALLOWED,
21+
/**
22+
* Not cached; we don't know whether the block is protected or not.
23+
*/
24+
MISS_CACHE
25+
}
26+
27+
enum CacheType {
28+
/**
29+
* Checks whether redstone is allowed.
30+
*/
31+
REDSTONE,
32+
/**
33+
* Checks whether golems are allowed.
34+
*/
35+
GOLEM
36+
}
37+
38+
/**
39+
* Gets whether redstone/golems are allowed at the given block.
40+
*
41+
* @param cacheType Golems or redstone.
42+
* @param block The block.
43+
* @return Whether the given block is locked.
44+
*/
45+
CacheFlag getAllowed(Block block, CacheType cacheType);
46+
47+
/**
48+
* Sets whether the given block is locked for redstone.
49+
*
50+
* @param cacheType Golems or redstone.
51+
* @param block The block.
52+
* @param allowed True if redstone/golems are allowed. Always true if there's no protection at the location.
53+
*/
54+
void setAllowed(Block block, CacheType cacheType, boolean allowed);
55+
56+
57+
}

src/main/java/nl/rutgerkok/blocklocker/SearchMode.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
* </ul>
1313
*/
1414
public enum SearchMode {
15+
/**
16+
* Ignores both {@link #NO_SIGNS signs} and {@link #NO_SUPPORTING_BLOCKS supporting blocks}.
17+
*/
18+
MAIN_BLOCKS_ONLY,
1519
/**
1620
* Ignores blocks that are solely part of the protection because they are
1721
* supporting another block.
@@ -36,7 +40,7 @@ public enum SearchMode {
3640
* otherwise.
3741
*/
3842
public boolean searchForSupportingBlocks() {
39-
return this != NO_SUPPORTING_BLOCKS;
43+
return this != NO_SUPPORTING_BLOCKS && this != MAIN_BLOCKS_ONLY;
4044
}
4145

4246
/**
@@ -47,6 +51,6 @@ public boolean searchForSupportingBlocks() {
4751
* otherwise.
4852
*/
4953
public boolean searchForSigns() {
50-
return this != NO_SIGNS;
54+
return this != NO_SIGNS && this != MAIN_BLOCKS_ONLY;
5155
}
5256
}

src/main/java/nl/rutgerkok/blocklocker/Translator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public enum Translation {
3838
TAG_MORE_USERS,
3939
TAG_PRIVATE,
4040
TAG_REDSTONE,
41+
TAG_GOLEM,
4142
TAG_TIMER,
4243
UPDATER_MORE_INFORMATION,
4344
UPDATER_UNSUPPORTED_SERVER,

src/main/java/nl/rutgerkok/blocklocker/impl/BlockLockerPluginImpl.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.function.Consumer;
1212
import java.util.logging.Level;
1313

14+
import nl.rutgerkok.blocklocker.impl.event.*;
1415
import org.bukkit.Bukkit;
1516
import org.bukkit.block.Block;
1617
import org.bukkit.configuration.Configuration;
@@ -25,7 +26,7 @@
2526

2627
import nl.rutgerkok.blocklocker.BlockLockerPlugin;
2728
import nl.rutgerkok.blocklocker.ChestSettings;
28-
import nl.rutgerkok.blocklocker.HopperCache;
29+
import nl.rutgerkok.blocklocker.ProtectionCache;
2930
import nl.rutgerkok.blocklocker.ProfileFactory;
3031
import nl.rutgerkok.blocklocker.ProtectableBlocksSettings;
3132
import nl.rutgerkok.blocklocker.ProtectionFinder;
@@ -35,11 +36,6 @@
3536
import nl.rutgerkok.blocklocker.group.CombinedGroupSystem;
3637
import nl.rutgerkok.blocklocker.group.GroupSystem;
3738
import nl.rutgerkok.blocklocker.impl.blockfinder.BlockFinder;
38-
import nl.rutgerkok.blocklocker.impl.event.BlockDestroyListener;
39-
import nl.rutgerkok.blocklocker.impl.event.BlockLockerCommand;
40-
import nl.rutgerkok.blocklocker.impl.event.BlockPlaceListener;
41-
import nl.rutgerkok.blocklocker.impl.event.InteractListener;
42-
import nl.rutgerkok.blocklocker.impl.event.SignChangeListener;
4339
import nl.rutgerkok.blocklocker.impl.group.FactionsGroupSystem;
4440
import nl.rutgerkok.blocklocker.impl.group.GuildsGroupSystem;
4541
import nl.rutgerkok.blocklocker.impl.group.PermissionsGroupSystem;
@@ -65,7 +61,7 @@ public class BlockLockerPluginImpl extends JavaPlugin implements BlockLockerPlug
6561
private Translator translator;
6662
private CombinedLocationChecker combinedLocationChecker;
6763
private SchedulerSupport schedulerSupport;
68-
private HopperCache redstoneProtectCache;
64+
private ProtectionCache protectionCache;
6965

7066
@Override
7167
public <E extends Event> E callEvent(E event) {
@@ -85,8 +81,8 @@ public CombinedGroupSystem getGroupSystems() {
8581
}
8682

8783
@Override
88-
public HopperCache getHopperCache() {
89-
return redstoneProtectCache;
84+
public ProtectionCache getProtectionCache() {
85+
return protectionCache;
9086
}
9187

9288
/**
@@ -204,7 +200,7 @@ private void loadServices() {
204200
BlockFinder blockFinder = BlockFinder.create(signParser, config.getConnectContainers());
205201
protectionFinder = new ProtectionFinderImpl(blockFinder, chestSettings);
206202
protectionUpdater = new ProtectionUpdaterImpl(getServer(), signParser, profileFactory);
207-
redstoneProtectCache = new HopperCacheImpl(this);
203+
protectionCache = new HopperCacheImpl();
208204
}
209205

210206
private Translator loadTranslations(String fileName) {
@@ -250,6 +246,7 @@ private void registerEvents() {
250246
plugins.registerEvents(new BlockDestroyListener(this), this);
251247
plugins.registerEvents(new BlockPlaceListener(this), this);
252248
plugins.registerEvents(new InteractListener(this), this);
249+
plugins.registerEvents(new GolemListener(this), this);
253250
plugins.registerEvents(new SignChangeListener(this), this);
254251
getCommand(getName().toLowerCase(Locale.ROOT)).setExecutor(new BlockLockerCommand(this));
255252
}

src/main/java/nl/rutgerkok/blocklocker/impl/HopperCacheImpl.java

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,52 @@
33
import java.util.concurrent.TimeUnit;
44

55
import org.bukkit.block.Block;
6-
import org.bukkit.plugin.Plugin;
76

87
import com.google.common.cache.Cache;
98
import com.google.common.cache.CacheBuilder;
109

11-
import nl.rutgerkok.blocklocker.HopperCache;
10+
import nl.rutgerkok.blocklocker.ProtectionCache;
1211

13-
final class HopperCacheImpl implements HopperCache {
12+
final class HopperCacheImpl implements ProtectionCache {
1413

1514
private static final long EXPIRE_TIME_SECONDS = 10;
16-
private Cache<Block, Boolean> accessCaching;
15+
private final Cache<Block, Boolean> redstoneCache;
16+
private final Cache<Block, Boolean> golemCache;
1717

18-
HopperCacheImpl(Plugin plugin) {
19-
accessCaching = CacheBuilder.newBuilder().initialCapacity(1000)
18+
HopperCacheImpl() {
19+
redstoneCache = CacheBuilder.newBuilder().initialCapacity(1000)
2020
.maximumSize(5000)
2121
.expireAfterWrite(EXPIRE_TIME_SECONDS, TimeUnit.SECONDS)
2222
.build();
23+
golemCache = CacheBuilder.newBuilder().initialCapacity(1000)
24+
.maximumSize(5000)
25+
.expireAfterWrite(EXPIRE_TIME_SECONDS, TimeUnit.SECONDS)
26+
.build();
27+
}
28+
29+
private Cache<Block, Boolean> getCache(CacheType cacheType) {
30+
return switch (cacheType) {
31+
case REDSTONE -> this.redstoneCache;
32+
case GOLEM -> this.golemCache;
33+
};
2334
}
2435

2536
@Override
26-
public CacheFlag getIsRedstoneAllowed(Block block) {
27-
Boolean isLocked = accessCaching.getIfPresent(block);
37+
public CacheFlag getAllowed(Block block, CacheType cacheType) {
38+
Boolean isAllowed = getCache(cacheType).getIfPresent(block);
2839

29-
if (isLocked == null) {
40+
if (isAllowed == null) {
3041
return CacheFlag.MISS_CACHE;
3142
}
32-
if (isLocked == true) {
33-
return CacheFlag.PROTECTED;
43+
if (isAllowed) {
44+
return CacheFlag.ALLOWED;
3445
} else {
35-
return CacheFlag.NOT_PROTECTED;
46+
return CacheFlag.NOT_ALLOWED;
3647
}
3748
}
3849

3950
@Override
40-
public void setIsRedstoneAllowed(Block block, boolean locked) {
41-
accessCaching.put(block, locked);
51+
public void setAllowed(Block block, CacheType cacheType, boolean allowed) {
52+
getCache(cacheType).put(block, allowed);
4253
}
4354
}

0 commit comments

Comments
 (0)