Skip to content

Commit

Permalink
Merge pull request #7 from Th3Shadowbroker/feature/material-restricti…
Browse files Browse the repository at this point in the history
…on-list

Feature/material restriction list
  • Loading branch information
InventivetalentDev authored Sep 21, 2019
2 parents 5d98aaa + 1255661 commit a02be73
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

<groupId>org.inventivetalent</groupId>
<artifactId>bookshelves</artifactId>
<version>1.4.0-SNAPSHOT</version>
<version>1.5.0-SNAPSHOT</version>


<build>
Expand Down
19 changes: 18 additions & 1 deletion resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,21 @@ inventory:
disabledWorlds: []

# Whether to allow only books in shelves
onlyBooks: true
onlyBooks: true

# Material-restrictions
# IMPORTANT: Restrictions only work when onlyBooks is set to false
restrictions:

# When true restrictions will be enabled
enabled: false

# When true the material-list will be used as whitelist
whitelist: true

# Define the restricted/whitelisted materials below
materials:
- BOOK
- WRITABLE_BOOK
- WRITTEN_BOOK
- ENCHANTED_BOOK
14 changes: 13 additions & 1 deletion src/org/inventivetalent/bookshelves/Bookshelves.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class Bookshelves extends JavaPlugin {
Set<String> disabledWorlds = new HashSet<>();
boolean onlyBooks = true;
boolean worldGuardSupport = false;
boolean checkRestrictions = false;
RestrictionManager restrictionManager = null;

Set<Location> shelves = new HashSet<>();
File shelfFile = new File(getDataFolder(), "shelves.json");
Expand All @@ -50,6 +52,7 @@ public void onEnable() {
instance = this;
Bukkit.getPluginManager().registerEvents(new ShelfListener(this), this);

// Save configuration & load values
saveDefaultConfig();
INVENTORY_SIZE = getConfig().getInt("inventory.size");
if (INVENTORY_SIZE % 9 != 0) {
Expand All @@ -59,7 +62,14 @@ public void onEnable() {
INVENTORY_TITLE = ChatColor.translateAlternateColorCodes('&', getConfig().getString("inventory.title")) +/* Unique title */"§B§S";
if (getConfig().contains("disabledWorlds")) { disabledWorlds.addAll(getConfig().getStringList("disabledWorlds")); }
onlyBooks = getConfig().getBoolean("onlyBooks", true);
checkRestrictions = getConfig().getBoolean("restrictions.enabled");

// Initialize restrictions
if (checkRestrictions) {
restrictionManager = new RestrictionManager();
}

// GriefPrevention compatibility
if (Bukkit.getPluginManager().isPluginEnabled("GriefPrevention")) {
getLogger().info("Found GriefPrevention plugin");
try {
Expand Down Expand Up @@ -87,6 +97,7 @@ public void onEnable() {
}
}

// ShelfFile creation
if (!shelfFile.exists()) {
try {
shelfFile.createNewFile();
Expand All @@ -95,6 +106,7 @@ public void onEnable() {
}
}

// Schedule bookshelf loading
Bukkit.getScheduler().runTaskLater(this, new Runnable() {
@Override
public void run() {
Expand Down Expand Up @@ -192,7 +204,7 @@ public void onDisable() {
}

boolean isValidBook(ItemStack itemStack) {
if (!onlyBooks) { return true; }
if (!onlyBooks) { return checkRestrictions ? restrictionManager.isRestricted(itemStack.getType()) : true; }

if (itemStack == null) { return false; }
if (itemStack.getType() == Material.BOOK) { return true; }
Expand Down
37 changes: 37 additions & 0 deletions src/org/inventivetalent/bookshelves/RestrictionManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.inventivetalent.bookshelves;

import org.bukkit.Material;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class RestrictionManager {

private final boolean whitelist;

private final List<Material> materials = new ArrayList<>();

public RestrictionManager() {
this.whitelist = Bookshelves.instance.getConfig().getBoolean("restrictions.whitelist");

Bookshelves.instance.getConfig().getStringList("restrictions.materials").forEach(
materialName -> {
Optional<Material> parsedMaterial = Optional.ofNullable(Material.getMaterial(materialName));

if (parsedMaterial.isPresent()) {
materials.add(parsedMaterial.get());
} else {
Bookshelves.instance.getLogger().warning(String.format("Unable to parse material \"%s\". Material will be ignored.", materialName));
}
}
);

Bookshelves.instance.getLogger().info(String.format("Loaded %s material-restrictions.", materials.size()));
}

public boolean isRestricted(Material material) {
return whitelist ? materials.contains(material) : !materials.contains(material);
}

}

0 comments on commit a02be73

Please sign in to comment.