Skip to content

Commit 7242225

Browse files
committed
Alpha 1.0.0 RC01
Pretty sure this is done. Fully configurable. Simple. Lightweight. Effective. Up to date.
1 parent 8b69f92 commit 7242225

File tree

8 files changed

+131
-96
lines changed

8 files changed

+131
-96
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins {
33
}
44

55
group = 'io.github.simplex'
6-
version = '1.0.0'
6+
version = 'v1.0-Alpha'
77

88
repositories {
99
mavenCentral()
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.github.simplex.toolassist;
2+
3+
import io.github.simplex.toolassist.data.BlockIdentifier;
4+
import io.github.simplex.toolassist.data.Config;
5+
import org.bukkit.block.Block;
6+
import org.bukkit.entity.Player;
7+
import org.bukkit.event.EventHandler;
8+
import org.bukkit.event.Listener;
9+
import org.bukkit.event.block.BlockBreakEvent;
10+
import org.bukkit.inventory.ItemStack;
11+
12+
import java.util.List;
13+
14+
public class MineListener implements Listener {
15+
private final BlockIdentifier identifier;
16+
private final Config.Settings settings;
17+
18+
public MineListener(ToolAssist plugin) {
19+
this.settings = plugin.getConfig().getSettings();
20+
this.identifier = new BlockIdentifier(plugin);
21+
plugin.getServer().getPluginManager().registerEvents(this, plugin);
22+
}
23+
24+
@EventHandler
25+
public void activate(BlockBreakEvent event) {
26+
Player player = event.getPlayer();
27+
ItemStack stack = player.getInventory().getItemInMainHand();
28+
Block block = event.getBlock();
29+
String permission = settings.permission();
30+
List<Block> blocks = identifier.populateAndRetrieve(block, stack);
31+
32+
if (!player.hasPermission(permission)) return;
33+
34+
if (!player.isSneaking() && settings.useSneak()) return;
35+
36+
blocks.forEach(Block::breakNaturally);
37+
}
38+
}

src/main/java/io/github/simplex/toolassist/ToolAssist.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@ public final class ToolAssist extends JavaPlugin {
1111
@Override
1212
public void onEnable() {
1313
// Plugin startup logic
14+
getLogger().info("Initializing configuration...");
1415
this.config = new Config(this);
16+
getLogger().info("Initialization complete! Registering listener...");
17+
new MineListener(this);
18+
getLogger().info("Initialization complete!");
1519
}
1620

1721
@Override
1822
public void onDisable() {
19-
// Plugin shutdown logic
23+
this.config.osave();
24+
this.config = null;
25+
getLogger().info("Goodbye!");
2026
}
2127

2228
@Override

src/main/java/io/github/simplex/toolassist/data/BlockIdentifier.java

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,76 @@
22

33
import io.github.simplex.toolassist.ToolAssist;
44
import org.bukkit.Location;
5-
import org.bukkit.Material;
65
import org.bukkit.block.Block;
7-
import org.bukkit.block.BlockFace;
86
import org.bukkit.inventory.ItemStack;
97

108
import java.util.ArrayList;
119
import java.util.List;
12-
import java.util.concurrent.atomic.AtomicBoolean;
1310

1411
public class BlockIdentifier {
1512
private final List<Block> blockList = new ArrayList<>();
16-
private final List<Location> blockLocations = new ArrayList<>();
1713
private final ToolAssist plugin;
14+
boolean isValid = false;
1815

1916
public BlockIdentifier(ToolAssist plugin) {
2017
this.plugin = plugin;
2118
}
2219

23-
public List<Block> populateAndRetrieve(Block block) {
20+
public List<Block> populateAndRetrieve(Block block, ItemStack requiredItem) {
2421
Location start = block.getLocation().clone();
25-
22+
int radius = plugin.getConfig().getSettings().radius();
23+
List<Block> surroundingBlocks = new ArrayList<>();
24+
for (double x = start.getX() - radius; x <= start.getX() + radius; x++) {
25+
for (double y = start.getY() - radius; y <= start.getY() + radius; y++) {
26+
for (double z = start.getZ() - radius; z <= start.getZ() + radius; z++) {
27+
Location location = new Location(start.getWorld(), x, y, z);
28+
if (checkBlock(location.getBlock(), requiredItem)) {
29+
surroundingBlocks.add(location.getBlock());
30+
}
31+
}
32+
}
33+
}
34+
blockList.addAll(surroundingBlocks);
2635

2736
return blockList;
2837
}
2938

3039
public boolean checkBlock(Block block, ItemStack targetItem) {
31-
AtomicBoolean isValid = new AtomicBoolean(false);
32-
33-
if (plugin.getConfig().getSettings().useTags()) {
34-
TagBox.oreTagList().forEach(tag -> {
35-
if (tag.isTagged(block.getType())) isValid.set(true);
36-
});
37-
return isValid.get();
40+
if (plugin.getConfig().getSettings().noConfig()) {
41+
return block.isValidTool(targetItem);
3842
}
3943

40-
Material m = block.getType();
41-
Material item = targetItem.getType();
42-
43-
// TODO: Tag or Material checks for the item, then the respective block type.
44+
checkBlockConfig(block, targetItem);
45+
return isValid;
46+
}
4447

45-
return isValid.get();
48+
private void checkBlockConfig(Block block, ItemStack targetItem) {
49+
String itemName = targetItem.getType().name();
50+
Config.Settings settings = plugin.getConfig().getSettings();
51+
if (itemName.endsWith("pickaxe")) {
52+
if (settings.pickaxeMaterials().contains(block.getType())) isValid = true;
53+
return;
54+
}
55+
if (!itemName.endsWith("pickaxe") && itemName.endsWith("axe")) {
56+
if (settings.axeMaterials().contains(block.getType())) isValid = true;
57+
return;
58+
}
59+
if (itemName.endsWith("shears")) {
60+
if (settings.shearMaterials().contains(block.getType())) isValid = true;
61+
return;
62+
}
63+
if (itemName.endsWith("shovel")) {
64+
if (settings.shovelMaterials().contains(block.getType())) isValid = true;
65+
return;
66+
}
67+
if (itemName.endsWith("hoe")) {
68+
if (settings.hoeMaterials().contains(block.getType())) isValid = true;
69+
return;
70+
}
71+
if (itemName.endsWith("sword")) {
72+
if (settings.swordMaterials().contains(block.getType())) isValid = true;
73+
return;
74+
}
75+
isValid = false;
4676
}
4777
}

src/main/java/io/github/simplex/toolassist/data/Config.java

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88

99
import java.io.*;
1010
import java.nio.charset.StandardCharsets;
11-
import java.util.*;
11+
import java.util.Optional;
12+
import java.util.Set;
13+
import java.util.stream.Collectors;
1214

15+
@SuppressWarnings("ResultOfMethodCallIgnored")
1316
public class Config extends YamlConfiguration {
1417

1518
private final Settings settings;
19+
private final File cf;
1620

1721
public Config(ToolAssist plugin) {
1822
this.settings = new Settings(this);
@@ -23,7 +27,7 @@ public Config(ToolAssist plugin) {
2327

2428
if (!dataFolder.exists()) dataFolder.mkdirs();
2529

26-
File cf = new File(dataFolder, fileName);
30+
cf = new File(dataFolder, fileName);
2731

2832
InputStream stream = plugin.getResource(fileName);
2933
assert stream != null;
@@ -34,38 +38,39 @@ public Config(ToolAssist plugin) {
3438
plugin.saveResource(fileName, true);
3539
}
3640

37-
oload(cf);
41+
oload();
3842

3943
reader.lines().filter(s -> s.contains(":"))
4044
.map(s -> s.split(":")[0])
4145
.filter(s -> !super.getValues(true).containsKey(s))
4246
.forEach(s -> {
4347
plugin.getLogger().severe("Configuration is missing an entry, attempting to replace...");
4448
Optional<String> stringStream = reader.lines().filter(c -> c.contains(s)).findFirst();
45-
assert stringStream.isPresent();
49+
if (stringStream.isEmpty())
50+
throw new RuntimeException("Unable to fix your configuration file. Please delete the config.yml in the data folder and restart your server.");
4651
String key = stringStream.get().split(":")[0].trim();
4752
String value = stringStream.get().split(":")[1].trim();
4853
super.addDefault(key, value);
49-
osave(cf);
54+
osave();
5055
});
5156

5257

5358
} catch (IOException ex) {
5459
plugin.getLogger().severe(ex.getMessage());
5560
}
5661

57-
oload(cf);
62+
oload();
5863
}
5964

60-
public void osave(File cf) {
65+
public void osave() {
6166
try {
6267
super.save(cf);
6368
} catch (IOException e) {
6469
e.printStackTrace();
6570
}
6671
}
6772

68-
public void oload(File cf) {
73+
public void oload() {
6974
try {
7075
super.load(cf);
7176
} catch (IOException | InvalidConfigurationException e) {
@@ -86,8 +91,12 @@ public Settings(Config config) {
8691
this.tool_settings = config.getConfigurationSection("tool_settings");
8792
}
8893

89-
public final boolean useTags() {
90-
return plugin_settings.getBoolean("use_tags_no_config", false);
94+
public final String permission() {
95+
return plugin_settings.getString("permission", "toolassist.activate");
96+
}
97+
98+
public final boolean noConfig() {
99+
return plugin_settings.getBoolean("no_config", false);
91100
}
92101

93102
public final boolean useSneak() {
@@ -99,57 +108,45 @@ public final int radius() {
99108
}
100109

101110
public final Set<Material> pickaxeMaterials() {
102-
Set<Material> materials = new HashSet<>();
103-
tool_settings.getStringList("pickaxe")
111+
return tool_settings.getStringList("pickaxe")
104112
.stream()
105113
.map(Material::matchMaterial)
106-
.forEach(materials::add);
107-
return materials;
114+
.collect(Collectors.toSet());
108115
}
109116

110117
public final Set<Material> axeMaterials() {
111-
Set<Material> materials = new HashSet<>();
112-
tool_settings.getStringList("axe")
118+
return tool_settings.getStringList("axe")
113119
.stream()
114120
.map(Material::matchMaterial)
115-
.forEach(materials::add);
116-
return materials;
121+
.collect(Collectors.toSet());
117122
}
118123

119124
public final Set<Material> shovelMaterials() {
120-
Set<Material> materials = new HashSet<>();
121-
tool_settings.getStringList("shovel")
125+
return tool_settings.getStringList("shovel")
122126
.stream()
123127
.map(Material::matchMaterial)
124-
.forEach(materials::add);
125-
return materials;
128+
.collect(Collectors.toSet());
126129
}
127130

128131
public final Set<Material> hoeMaterials() {
129-
Set<Material> materials = new HashSet<>();
130-
tool_settings.getStringList("hoe")
132+
return tool_settings.getStringList("hoe")
131133
.stream()
132134
.map(Material::matchMaterial)
133-
.forEach(materials::add);
134-
return materials;
135+
.collect(Collectors.toSet());
135136
}
136137

137138
public final Set<Material> swordMaterials() {
138-
Set<Material> materials = new HashSet<>();
139-
tool_settings.getStringList("sword")
139+
return tool_settings.getStringList("sword")
140140
.stream()
141141
.map(Material::matchMaterial)
142-
.forEach(materials::add);
143-
return materials;
142+
.collect(Collectors.toSet());
144143
}
145144

146145
public final Set<Material> shearMaterials() {
147-
Set<Material> materials = new HashSet<>();
148-
tool_settings.getStringList("shears")
146+
return tool_settings.getStringList("shears")
149147
.stream()
150148
.map(Material::matchMaterial)
151-
.forEach(materials::add);
152-
return materials;
149+
.collect(Collectors.toSet());
153150
}
154151
}
155152
}

src/main/java/io/github/simplex/toolassist/data/TagBox.java

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

src/main/resources/config.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
# The overall settings for the plugin itself
22
plugin_settings:
33

4+
# The permission required to use the assist feature.
5+
permission: "toolassist.activate"
6+
47
# How many blocks outwards should be scanned
58
search_radius: 15
69

710
# Whether a user must be sneaking to activate
811
sneak_activation: true
912

10-
# Whether to use Tag<Material> to get the appropriate materials, or to use the groupings listed below.
11-
use_tags_no_config: false
13+
# Whether to use the respective tool to trigger activation, or to use the groupings listed below.
14+
no_config: false
1215

1316
# The block material that can be targeted by the respective tool grouping
1417
tool_settings:

src/main/resources/plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ version: '${version}'
33
main: io.github.simplex.toolassist.ToolAssist
44
api-version: 1.18
55
authors: [ SimplexDevelopment ]
6-
description: A better vein miner plugin.
6+
description: A lightweight vein-mining plugin.

0 commit comments

Comments
 (0)