55import org .bukkit .command .Command ;
66import org .bukkit .command .CommandExecutor ;
77import org .bukkit .command .CommandSender ;
8+ import org .bukkit .configuration .ConfigurationSection ;
89import org .bukkit .configuration .file .FileConfiguration ;
10+ import org .bukkit .configuration .file .YamlConfiguration ;
911import org .bukkit .entity .Player ;
1012import org .bukkit .event .EventHandler ;
1113import org .bukkit .event .Listener ;
1214import org .bukkit .event .block .BlockBreakEvent ;
1315import org .bukkit .inventory .ItemStack ;
1416import org .bukkit .plugin .java .JavaPlugin ;
1517
18+ import java .io .File ;
19+ import java .io .IOException ;
20+ import java .util .HashMap ;
21+ import java .util .Map ;
22+ import java .util .UUID ;
23+
1624public class AutoTreeChop extends JavaPlugin implements Listener , CommandExecutor {
1725
18- private boolean autoTreeChopEnabled ;
26+ private Map < UUID , PlayerConfig > playerConfigs ;
1927 private String enabledMessage ;
2028 private String disabledMessage ;
2129 private String noPermissionMessage ;
2230
2331 @ Override
2432 public void onEnable () {
25-
26-
2733 getServer ().getPluginManager ().registerEvents (this , this );
2834 getCommand ("autotreechop" ).setExecutor (this );
2935
30-
3136 loadConfig ();
3237
33- autoTreeChopEnabled = false ;
38+ playerConfigs = new HashMap <>() ;
3439 }
3540
3641 private void loadConfig () {
37-
3842 getConfig ().addDefault ("messages.enabled" , "§a已開啟自動砍樹。" );
3943 getConfig ().addDefault ("messages.disabled" , "§c已關閉自動砍樹。" );
4044 getConfig ().addDefault ("messages.no-permission" , "§c你沒有權限使用此指令。" );
4145 getConfig ().options ().copyDefaults (true );
4246 saveConfig ();
4347
44-
4548 FileConfiguration config = getConfig ();
4649 enabledMessage = config .getString ("messages.enabled" );
4750 disabledMessage = config .getString ("messages.disabled" );
@@ -59,7 +62,11 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
5962 return true ;
6063 }
6164
62- autoTreeChopEnabled = !autoTreeChopEnabled ;
65+ UUID playerUUID = player .getUniqueId ();
66+ PlayerConfig playerConfig = getPlayerConfig (playerUUID );
67+ boolean autoTreeChopEnabled = !playerConfig .isAutoTreeChopEnabled ();
68+ playerConfig .setAutoTreeChopEnabled (autoTreeChopEnabled );
69+
6370 if (autoTreeChopEnabled ) {
6471 player .sendMessage (enabledMessage );
6572 } else {
@@ -75,20 +82,18 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String
7582
7683 @ EventHandler
7784 public void onBlockBreak (BlockBreakEvent event ) {
78- if (autoTreeChopEnabled ) {
79- Player player = event .getPlayer ();
85+ Player player = event .getPlayer ();
86+ UUID playerUUID = player .getUniqueId ();
87+ PlayerConfig playerConfig = getPlayerConfig (playerUUID );
88+
89+ if (playerConfig .isAutoTreeChopEnabled ()) {
8090 Block block = event .getBlock ();
8191 Material material = block .getType ();
8292
83-
8493 if (isLog (material )) {
85-
8694 event .setCancelled (true );
87-
88-
8995 chopTree (block );
9096
91-
9297 if (player .getInventory ().firstEmpty () == -1 ) {
9398 player .getWorld ().dropItem (player .getLocation (), new ItemStack (material ));
9499 } else {
@@ -98,18 +103,15 @@ public void onBlockBreak(BlockBreakEvent event) {
98103 }
99104 }
100105
101-
102106 private void chopTree (Block block ) {
103107 Block aboveBlock = block .getRelative (0 , 1 , 0 );
104108 if (isLog (aboveBlock .getType ())) {
105-
106109 aboveBlock .breakNaturally ();
107110 chopTree (aboveBlock );
108111 }
109112
110113 block .breakNaturally ();
111114
112-
113115 for (int xOffset = -1 ; xOffset <= 1 ; xOffset ++) {
114116 for (int zOffset = -1 ; zOffset <= 1 ; zOffset ++) {
115117 if (xOffset == 0 && zOffset == 0 ) {
@@ -123,7 +125,6 @@ private void chopTree(Block block) {
123125 }
124126 }
125127
126-
127128 private boolean isLog (Material material ) {
128129 return material == Material .OAK_LOG ||
129130 material == Material .SPRUCE_LOG ||
@@ -133,6 +134,59 @@ private boolean isLog(Material material) {
133134 material == Material .DARK_OAK_LOG ||
134135 material == Material .MANGROVE_LOG ||
135136 material == Material .CHERRY_LOG ;
137+ }
138+
139+ private PlayerConfig getPlayerConfig (UUID playerUUID ) {
140+ PlayerConfig playerConfig = playerConfigs .get (playerUUID );
141+ if (playerConfig == null ) {
142+ playerConfig = new PlayerConfig (playerUUID );
143+ playerConfigs .put (playerUUID , playerConfig );
144+ }
145+ return playerConfig ;
146+ }
147+
148+ private class PlayerConfig {
149+ private final UUID playerUUID ;
150+ private final File configFile ;
151+ private final FileConfiguration config ;
152+
153+ private boolean autoTreeChopEnabled ;
154+
155+ public PlayerConfig (UUID playerUUID ) {
156+ this .playerUUID = playerUUID ;
157+ this .configFile = new File (getDataFolder () + "/cache" , playerUUID .toString () + ".yml" );
158+ this .config = YamlConfiguration .loadConfiguration (configFile );
159+ this .autoTreeChopEnabled = false ;
160+ loadConfig ();
161+ this .autoTreeChopEnabled = false ;
162+ saveConfig ();
163+ }
164+
165+ private void loadConfig () {
166+ if (configFile .exists ()) {
167+ autoTreeChopEnabled = config .getBoolean ("autoTreeChopEnabled" );
168+ } else {
169+ config .set ("autoTreeChopEnabled" , autoTreeChopEnabled );
170+ saveConfig ();
171+ }
172+ }
173+
174+ private void saveConfig () {
175+ try {
176+ config .save (configFile );
177+ } catch (IOException e ) {
178+ e .printStackTrace ();
179+ }
180+ }
181+
182+ public boolean isAutoTreeChopEnabled () {
183+ return autoTreeChopEnabled ;
184+ }
136185
186+ public void setAutoTreeChopEnabled (boolean enabled ) {
187+ this .autoTreeChopEnabled = enabled ;
188+ config .set ("autoTreeChopEnabled" , enabled );
189+ saveConfig ();
190+ }
137191 }
138192}
0 commit comments