Skip to content

Commit

Permalink
Add preventAdjacent list to config and fix config warning
Browse files Browse the repository at this point in the history
  • Loading branch information
cjburkey01 committed Jan 4, 2022
1 parent 849c1cc commit ff1f631
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 26 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/cjburkey/claimchunk/Utils.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.cjburkey.claimchunk;

import lombok.Getter;

import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class ClaimChunkWorldProfile {
public SpreadProfile pistonExtend = new SpreadProfile("allow_piston");

/** A set of blocks for which to deny neighboring (same) block placement. */
public HashSet<Material> preventAdjacent =
public final HashSet<Material> preventAdjacent =
new HashSet<>(Arrays.asList(Material.CHEST, Material.TRAPPED_CHEST));

/** A set of commands that should be denied for un-owning players in claimed chunks. */
Expand Down Expand Up @@ -443,6 +443,9 @@ public void toCCConfig(@NotNull CCConfig config) {
// Piston protection configs
pistonExtend.toCCConfig(config);

// Change types of adjacent blocks to check, empty this list to stop checking
config.setList("_.preventAdjacent", preventAdjacent);

// Command blocking configs
config.setList("claimedChunks.other.blockedCmds", blockedCmdsInDiffClaimed);
config.setList("claimedChunks.owned.blockedCmds", blockedCmdsInOwnClaimed);
Expand Down Expand Up @@ -524,6 +527,23 @@ public void fromCCConfig(@NotNull CCConfig config) {
// Load piston protection properties
pistonExtend.fromCCConfig(config);

// Load list of adjacent block types to check
preventAdjacent.clear();
config.getStrList("_.preventAdjacent").stream()
.map(
blockType -> {
Material material = Material.getMaterial(blockType);
if (material == null) {
Utils.warn(
"Material type \"%s\" not found when loading from"
+ " preventAdjacent, this one will be removed!",
blockType);
}
return material;
})
.filter(Objects::nonNull)
.forEach(preventAdjacent::add);

// Load blocked commands for unowned claimed chunks
// (`getCommands()` will verify the commands actually exist)
blockedCmdsInDiffClaimed.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -549,8 +549,12 @@ public void onLiquidAndDragonEggSpread(BlockFromToEvent event) {
// Prevent dragon egg teleportation if interaction is disabled in this chunk
if (blockType == Material.DRAGON_EGG) {
// Get the profile and chunk information
ClaimChunkWorldProfile profile = claimChunk.getProfileManager().getProfile(event.getBlock().getWorld().getName());
boolean isClaimed = claimChunk.getChunkHandler().isClaimed(event.getBlock().getChunk());
ClaimChunkWorldProfile profile =
claimChunk
.getProfileManager()
.getProfile(event.getBlock().getWorld().getName());
boolean isClaimed =
claimChunk.getChunkHandler().isClaimed(event.getBlock().getChunk());

// Cancel this event if a non-access player wouldn't have access to this dragon egg.
// (We can't check if a player did it, I don't believe?)
Expand Down Expand Up @@ -719,7 +723,7 @@ private void onEntityEvent(
if (!profile.canAccessEntity(chunkOwner != null, isOwnerOrAccess, entity, accessType)
&& (chunkOwner == null
|| canOthersAccessPlysChunks(
chunkOwner, claimChunk.getServer(), profile))) {
chunkOwner, claimChunk.getServer(), profile))) {
// cancel event
cancel.run();

Expand Down Expand Up @@ -770,7 +774,7 @@ private void onBlockAdjacentCheck(
&& neighborOwner != chunkOwner
&& !isOwnerOrAccess
&& canOthersAccessPlysChunks(
chunkOwner, claimChunk.getServer(), profile)) {
chunkOwner, claimChunk.getServer(), profile)) {

// cancel event
cancel.run();
Expand Down Expand Up @@ -842,7 +846,7 @@ private boolean onBlockEvent(
if (profile.enabled
&& (chunkOwner == null
|| canOthersAccessPlysChunks(
chunkOwner, claimChunk.getServer(), profile))
chunkOwner, claimChunk.getServer(), profile))
&& !profile.canAccessBlock(
chunkOwner != null,
isOwnerOrAccess,
Expand Down Expand Up @@ -933,7 +937,7 @@ private void onExplosionEvent(@NotNull World world, @NotNull Collection<Block> b
UUID owner = chunkHandler.getOwner(c);
return (owner == null
|| canOthersAccessPlysChunks(
owner, claimChunk.getServer(), worldProfile))
owner, claimChunk.getServer(), worldProfile))
&& !worldProfile.getBlockAccess(
owner != null, worldName, block.getType())
.allowExplosion;
Expand Down Expand Up @@ -1032,7 +1036,7 @@ private void onPistonAction(
// If we find any protected chunks, we're gonna have to check further
if (targetChunkOwner != null
&& canOthersAccessPlysChunks(
targetChunkOwner, claimChunk.getServer(), profile)) {
targetChunkOwner, claimChunk.getServer(), profile)) {
onlineOfflineProtection = false;
break;
}
Expand Down
21 changes: 11 additions & 10 deletions src/main/java/com/cjburkey/claimchunk/lib/Metrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

import javax.net.ssl.HttpsURLConnection;

@SuppressWarnings("ALL")
public class Metrics {

private final Plugin plugin;
Expand Down Expand Up @@ -62,16 +63,16 @@ public Metrics(JavaPlugin plugin, int serviceId) {
config.addDefault("logResponseStatusText", false);
// Inform the server owners about bStats
config.options()
.header(
"bStats (https://bStats.org) collects some basic information for plugin"
+ " authors, like how\n"
+ "many people use their plugin and their total player count. It's"
+ " recommended to keep bStats\n"
+ "enabled, but if you're not comfortable with this, you can turn"
+ " this setting off. There is no\n"
+ "performance penalty associated with having metrics enabled, and"
+ " data sent to bStats is fully\n"
+ "anonymous.")
.setHeader(
Arrays.asList(
"bStats (https://bStats.org) collects some basic information"
+ " for plugin authors, like how many people use their"
+ " plugin and their total player count.",
"It's recommended to keep bStats enabled, but if you're not"
+ " comfortable with this, you can turn this setting off.",
"There is no performance penalty associated with having metrics"
+ " enabled or disabled, and data sent to bStats is fully"
+ " anonymous."))
.copyDefaults(true);
try {
config.save(configFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import org.bukkit.command.*;
import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.plugin.PluginManager;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Field;
Expand Down Expand Up @@ -58,10 +57,16 @@ public boolean execute(
return baseCommand.onCommand(sender, this, commandLabel, args);
}

private static Object getPrivateField(Object object, String field)throws SecurityException,
NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
private static Object getPrivateField(Object object, String field)
throws SecurityException, NoSuchFieldException, IllegalArgumentException,
IllegalAccessException {
Class<?> clazz = object.getClass();
Field objectField = field.equals("commandMap") ? clazz.getDeclaredField(field) : field.equals("knownCommands") ? clazz.getSuperclass().getDeclaredField(field) : null;
Field objectField =
field.equals("commandMap")
? clazz.getDeclaredField(field)
: field.equals("knownCommands")
? clazz.getSuperclass().getDeclaredField(field)
: null;
Objects.requireNonNull(objectField).setAccessible(true);
Object result = objectField.get(object);
objectField.setAccessible(false);
Expand All @@ -70,19 +75,23 @@ private static Object getPrivateField(Object object, String field)throws Securit

public void removeFromMap() {
try {
Object result = getPrivateField(claimChunk.getServer().getPluginManager(), "commandMap");
Object result =
getPrivateField(claimChunk.getServer().getPluginManager(), "commandMap");
SimpleCommandMap commandMap = (SimpleCommandMap) result;
Object map = getPrivateField(commandMap, "knownCommands");
@SuppressWarnings("unchecked")
HashMap<String, Command> knownCommands = (HashMap<String, Command>) map;
knownCommands.remove(this.getName());
for (String alias : this.getAliases()){
if(knownCommands.containsKey(alias) && knownCommands.get(alias).toString().contains(claimChunk.getName())){
for (String alias : this.getAliases()) {
if (knownCommands.containsKey(alias)
&& knownCommands.get(alias).toString().contains(claimChunk.getName())) {
knownCommands.remove(alias);
}
}
} catch (Exception e) {
Utils.err("Failed to unregister command! If you are reloading, updates to permissions won't appear until a server reboot.");
Utils.err(
"Failed to unregister command! If you are reloading, updates to permissions"
+ " won't appear until a server reboot.");
if (Utils.getDebugEnableOverride()) {
e.printStackTrace();
}
Expand Down

0 comments on commit ff1f631

Please sign in to comment.