Alpha 1.0.0 RC01

Pretty sure this is done.
Fully configurable.
Simple.
Lightweight.
Effective.
Up to date.
This commit is contained in:
Paldiu 2022-05-09 22:40:35 -05:00
parent 8b69f92762
commit 724222553f
8 changed files with 131 additions and 96 deletions

View File

@ -3,7 +3,7 @@ plugins {
}
group = 'io.github.simplex'
version = '1.0.0'
version = 'v1.0-Alpha'
repositories {
mavenCentral()

View File

@ -0,0 +1,38 @@
package io.github.simplex.toolassist;
import io.github.simplex.toolassist.data.BlockIdentifier;
import io.github.simplex.toolassist.data.Config;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.inventory.ItemStack;
import java.util.List;
public class MineListener implements Listener {
private final BlockIdentifier identifier;
private final Config.Settings settings;
public MineListener(ToolAssist plugin) {
this.settings = plugin.getConfig().getSettings();
this.identifier = new BlockIdentifier(plugin);
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
@EventHandler
public void activate(BlockBreakEvent event) {
Player player = event.getPlayer();
ItemStack stack = player.getInventory().getItemInMainHand();
Block block = event.getBlock();
String permission = settings.permission();
List<Block> blocks = identifier.populateAndRetrieve(block, stack);
if (!player.hasPermission(permission)) return;
if (!player.isSneaking() && settings.useSneak()) return;
blocks.forEach(Block::breakNaturally);
}
}

View File

@ -11,12 +11,18 @@ public final class ToolAssist extends JavaPlugin {
@Override
public void onEnable() {
// Plugin startup logic
getLogger().info("Initializing configuration...");
this.config = new Config(this);
getLogger().info("Initialization complete! Registering listener...");
new MineListener(this);
getLogger().info("Initialization complete!");
}
@Override
public void onDisable() {
// Plugin shutdown logic
this.config.osave();
this.config = null;
getLogger().info("Goodbye!");
}
@Override

View File

@ -2,46 +2,76 @@ package io.github.simplex.toolassist.data;
import io.github.simplex.toolassist.ToolAssist;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
public class BlockIdentifier {
private final List<Block> blockList = new ArrayList<>();
private final List<Location> blockLocations = new ArrayList<>();
private final ToolAssist plugin;
boolean isValid = false;
public BlockIdentifier(ToolAssist plugin) {
this.plugin = plugin;
}
public List<Block> populateAndRetrieve(Block block) {
public List<Block> populateAndRetrieve(Block block, ItemStack requiredItem) {
Location start = block.getLocation().clone();
int radius = plugin.getConfig().getSettings().radius();
List<Block> surroundingBlocks = new ArrayList<>();
for (double x = start.getX() - radius; x <= start.getX() + radius; x++) {
for (double y = start.getY() - radius; y <= start.getY() + radius; y++) {
for (double z = start.getZ() - radius; z <= start.getZ() + radius; z++) {
Location location = new Location(start.getWorld(), x, y, z);
if (checkBlock(location.getBlock(), requiredItem)) {
surroundingBlocks.add(location.getBlock());
}
}
}
}
blockList.addAll(surroundingBlocks);
return blockList;
}
public boolean checkBlock(Block block, ItemStack targetItem) {
AtomicBoolean isValid = new AtomicBoolean(false);
if (plugin.getConfig().getSettings().useTags()) {
TagBox.oreTagList().forEach(tag -> {
if (tag.isTagged(block.getType())) isValid.set(true);
});
return isValid.get();
if (plugin.getConfig().getSettings().noConfig()) {
return block.isValidTool(targetItem);
}
Material m = block.getType();
Material item = targetItem.getType();
checkBlockConfig(block, targetItem);
return isValid;
}
// TODO: Tag or Material checks for the item, then the respective block type.
return isValid.get();
private void checkBlockConfig(Block block, ItemStack targetItem) {
String itemName = targetItem.getType().name();
Config.Settings settings = plugin.getConfig().getSettings();
if (itemName.endsWith("pickaxe")) {
if (settings.pickaxeMaterials().contains(block.getType())) isValid = true;
return;
}
if (!itemName.endsWith("pickaxe") && itemName.endsWith("axe")) {
if (settings.axeMaterials().contains(block.getType())) isValid = true;
return;
}
if (itemName.endsWith("shears")) {
if (settings.shearMaterials().contains(block.getType())) isValid = true;
return;
}
if (itemName.endsWith("shovel")) {
if (settings.shovelMaterials().contains(block.getType())) isValid = true;
return;
}
if (itemName.endsWith("hoe")) {
if (settings.hoeMaterials().contains(block.getType())) isValid = true;
return;
}
if (itemName.endsWith("sword")) {
if (settings.swordMaterials().contains(block.getType())) isValid = true;
return;
}
isValid = false;
}
}

View File

@ -8,11 +8,15 @@ import org.bukkit.configuration.file.YamlConfiguration;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
@SuppressWarnings("ResultOfMethodCallIgnored")
public class Config extends YamlConfiguration {
private final Settings settings;
private final File cf;
public Config(ToolAssist plugin) {
this.settings = new Settings(this);
@ -23,7 +27,7 @@ public class Config extends YamlConfiguration {
if (!dataFolder.exists()) dataFolder.mkdirs();
File cf = new File(dataFolder, fileName);
cf = new File(dataFolder, fileName);
InputStream stream = plugin.getResource(fileName);
assert stream != null;
@ -34,7 +38,7 @@ public class Config extends YamlConfiguration {
plugin.saveResource(fileName, true);
}
oload(cf);
oload();
reader.lines().filter(s -> s.contains(":"))
.map(s -> s.split(":")[0])
@ -42,11 +46,12 @@ public class Config extends YamlConfiguration {
.forEach(s -> {
plugin.getLogger().severe("Configuration is missing an entry, attempting to replace...");
Optional<String> stringStream = reader.lines().filter(c -> c.contains(s)).findFirst();
assert stringStream.isPresent();
if (stringStream.isEmpty())
throw new RuntimeException("Unable to fix your configuration file. Please delete the config.yml in the data folder and restart your server.");
String key = stringStream.get().split(":")[0].trim();
String value = stringStream.get().split(":")[1].trim();
super.addDefault(key, value);
osave(cf);
osave();
});
@ -54,10 +59,10 @@ public class Config extends YamlConfiguration {
plugin.getLogger().severe(ex.getMessage());
}
oload(cf);
oload();
}
public void osave(File cf) {
public void osave() {
try {
super.save(cf);
} catch (IOException e) {
@ -65,7 +70,7 @@ public class Config extends YamlConfiguration {
}
}
public void oload(File cf) {
public void oload() {
try {
super.load(cf);
} catch (IOException | InvalidConfigurationException e) {
@ -86,8 +91,12 @@ public class Config extends YamlConfiguration {
this.tool_settings = config.getConfigurationSection("tool_settings");
}
public final boolean useTags() {
return plugin_settings.getBoolean("use_tags_no_config", false);
public final String permission() {
return plugin_settings.getString("permission", "toolassist.activate");
}
public final boolean noConfig() {
return plugin_settings.getBoolean("no_config", false);
}
public final boolean useSneak() {
@ -99,57 +108,45 @@ public class Config extends YamlConfiguration {
}
public final Set<Material> pickaxeMaterials() {
Set<Material> materials = new HashSet<>();
tool_settings.getStringList("pickaxe")
return tool_settings.getStringList("pickaxe")
.stream()
.map(Material::matchMaterial)
.forEach(materials::add);
return materials;
.collect(Collectors.toSet());
}
public final Set<Material> axeMaterials() {
Set<Material> materials = new HashSet<>();
tool_settings.getStringList("axe")
return tool_settings.getStringList("axe")
.stream()
.map(Material::matchMaterial)
.forEach(materials::add);
return materials;
.collect(Collectors.toSet());
}
public final Set<Material> shovelMaterials() {
Set<Material> materials = new HashSet<>();
tool_settings.getStringList("shovel")
return tool_settings.getStringList("shovel")
.stream()
.map(Material::matchMaterial)
.forEach(materials::add);
return materials;
.collect(Collectors.toSet());
}
public final Set<Material> hoeMaterials() {
Set<Material> materials = new HashSet<>();
tool_settings.getStringList("hoe")
return tool_settings.getStringList("hoe")
.stream()
.map(Material::matchMaterial)
.forEach(materials::add);
return materials;
.collect(Collectors.toSet());
}
public final Set<Material> swordMaterials() {
Set<Material> materials = new HashSet<>();
tool_settings.getStringList("sword")
return tool_settings.getStringList("sword")
.stream()
.map(Material::matchMaterial)
.forEach(materials::add);
return materials;
.collect(Collectors.toSet());
}
public final Set<Material> shearMaterials() {
Set<Material> materials = new HashSet<>();
tool_settings.getStringList("shears")
return tool_settings.getStringList("shears")
.stream()
.map(Material::matchMaterial)
.forEach(materials::add);
return materials;
.collect(Collectors.toSet());
}
}
}

View File

@ -1,39 +0,0 @@
package io.github.simplex.toolassist.data;
import org.bukkit.Material;
import org.bukkit.Tag;
import java.util.List;
public class TagBox {
public static List<Tag<Material>> oreTagList() {
return List.of(Tag.COAL_ORES,
Tag.IRON_ORES,
Tag.COPPER_ORES,
Tag.GOLD_ORES,
Tag.DIAMOND_ORES,
Tag.REDSTONE_ORES,
Tag.EMERALD_ORES,
Tag.LAPIS_ORES);
}
public static Tag<Material> leaves() {
return Tag.LEAVES;
}
public static Tag<Material> sandType() {
return Tag.SAND;
}
public static Tag<Material> wart() {
return Tag.WART_BLOCKS;
}
public static Tag<Material> logs() {
return Tag.LOGS;
}
public static Tag<Material> vines() {
return Tag.CAVE_VINES;
}
}

View File

@ -1,14 +1,17 @@
# The overall settings for the plugin itself
plugin_settings:
# The permission required to use the assist feature.
permission: "toolassist.activate"
# How many blocks outwards should be scanned
search_radius: 15
# Whether a user must be sneaking to activate
sneak_activation: true
# Whether to use Tag<Material> to get the appropriate materials, or to use the groupings listed below.
use_tags_no_config: false
# Whether to use the respective tool to trigger activation, or to use the groupings listed below.
no_config: false
# The block material that can be targeted by the respective tool grouping
tool_settings:

View File

@ -3,4 +3,4 @@ version: '${version}'
main: io.github.simplex.toolassist.ToolAssist
api-version: 1.18
authors: [ SimplexDevelopment ]
description: A better vein miner plugin.
description: A lightweight vein-mining plugin.