Skip to content

Commit 7fcf880

Browse files
authored
Add files via upload
1 parent 9dc0ac6 commit 7fcf880

File tree

4 files changed

+221
-0
lines changed

4 files changed

+221
-0
lines changed

pom.xml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>me.abhimaanst</groupId>
8+
<artifactId>FastCane</artifactId>
9+
<version>1.0</version>
10+
<packaging>jar</packaging>
11+
12+
<name>FastCane</name>
13+
<description>Optimized sugar cane growth plugin</description>
14+
15+
<properties>
16+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
17+
<maven.compiler.source>1.8</maven.compiler.source>
18+
<maven.compiler.target>1.8</maven.compiler.target>
19+
<bukkit.version>1.8.8-R0.1-SNAPSHOT</bukkit.version>
20+
</properties>
21+
22+
<repositories>
23+
<repository>
24+
<id>spigot-repo</id>
25+
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
26+
</repository>
27+
</repositories>
28+
29+
<dependencies>
30+
<dependency>
31+
<groupId>org.spigotmc</groupId>
32+
<artifactId>spigot-api</artifactId>
33+
<version>${bukkit.version}</version>
34+
<scope>provided</scope>
35+
</dependency>
36+
</dependencies>
37+
38+
<build>
39+
<finalName>${project.artifactId}-${project.version}</finalName>
40+
<plugins>
41+
<plugin>
42+
<groupId>org.apache.maven.plugins</groupId>
43+
<artifactId>maven-compiler-plugin</artifactId>
44+
<version>3.8.1</version>
45+
<configuration>
46+
<source>1.8</source>
47+
<target>1.8</target>
48+
</configuration>
49+
</plugin>
50+
<plugin>
51+
<groupId>org.apache.maven.plugins</groupId>
52+
<artifactId>maven-shade-plugin</artifactId>
53+
<version>3.2.4</version>
54+
<executions>
55+
<execution>
56+
<phase>package</phase>
57+
<goals>
58+
<goal>shade</goal>
59+
</goals>
60+
</execution>
61+
</executions>
62+
</plugin>
63+
</plugins>
64+
</build>
65+
</project>
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package me.abhimaanst.fastcane;
2+
3+
import org.bukkit.*;
4+
import org.bukkit.block.*;
5+
import org.bukkit.configuration.file.FileConfiguration;
6+
import org.bukkit.entity.Player;
7+
import org.bukkit.plugin.java.JavaPlugin;
8+
import org.bukkit.scheduler.BukkitRunnable;
9+
10+
import java.util.*;
11+
import java.util.concurrent.ConcurrentHashMap;
12+
13+
public class FastGrowPlugin extends JavaPlugin {
14+
15+
private double growthInterval;
16+
private int maxCaneHeight;
17+
private int checkRadius;
18+
private int growthAmount;
19+
private final Map<Location, Long> lastProcessed = new ConcurrentHashMap<>();
20+
private final int PROCESS_COOLDOWN = 500;
21+
22+
@Override
23+
public void onEnable() {
24+
saveDefaultConfig();
25+
reloadConfigValues();
26+
getLogger().info(String.format(
27+
"FastCane enabled! Settings: interval=%.1fs, amount=%d, max-height=%d, radius=%d",
28+
growthInterval, growthAmount, maxCaneHeight, checkRadius
29+
));
30+
startGrowthScheduler();
31+
}
32+
33+
private void reloadConfigValues() {
34+
FileConfiguration config = getConfig();
35+
growthInterval = Math.max(0.05, config.getDouble("growth-interval", 0.5));
36+
checkRadius = Math.min(256, Math.max(16, config.getInt("check-radius", 64)));
37+
maxCaneHeight = Math.min(255, Math.max(1, config.getInt("max-height", 3)));
38+
growthAmount = Math.min(maxCaneHeight, Math.max(1, config.getInt("growth-amount", 1)));
39+
}
40+
41+
private void startGrowthScheduler() {
42+
new BukkitRunnable() {
43+
@Override
44+
public void run() {
45+
Bukkit.getScheduler().runTaskAsynchronously(FastGrowPlugin.this, () -> {
46+
long currentTime = System.currentTimeMillis();
47+
Set<Block> blocksToGrow = new HashSet<>();
48+
49+
for (Player player : Bukkit.getOnlinePlayers()) {
50+
Location loc = player.getLocation();
51+
World world = loc.getWorld();
52+
53+
54+
int minX = loc.getBlockX() - checkRadius;
55+
int maxX = loc.getBlockX() + checkRadius;
56+
int minZ = loc.getBlockZ() - checkRadius;
57+
int maxZ = loc.getBlockZ() + checkRadius;
58+
59+
// Check blocks in cuboid
60+
for (int x = minX; x <= maxX; x++) {
61+
for (int z = minZ; z <= maxZ; z++) {
62+
63+
for (int y = world.getMaxHeight(); y >= 0; y--) {
64+
Block block = world.getBlockAt(x, y, z);
65+
66+
if (block.getType() == Material.SUGAR_CANE_BLOCK) {
67+
Block base = findBaseBlock(block);
68+
Location baseLoc = base.getLocation();
69+
70+
71+
Long lastProcessedTime = lastProcessed.get(baseLoc);
72+
if (lastProcessedTime == null ||
73+
currentTime - lastProcessedTime > PROCESS_COOLDOWN) {
74+
75+
int currentHeight = getCaneHeight(base);
76+
if (currentHeight < maxCaneHeight) {
77+
Block top = getTopBlock(base);
78+
Block above = top.getRelative(BlockFace.UP);
79+
80+
if (above.getType() == Material.AIR && canGrow(top)) {
81+
blocksToGrow.add(above);
82+
lastProcessed.put(baseLoc, currentTime);
83+
}
84+
}
85+
}
86+
}
87+
}
88+
}
89+
}
90+
}
91+
92+
if (!blocksToGrow.isEmpty()) {
93+
Bukkit.getScheduler().runTask(FastGrowPlugin.this, () -> {
94+
for (Block block : blocksToGrow) {
95+
int remainingGrowth = Math.min(growthAmount,
96+
maxCaneHeight - getCaneHeight(block.getRelative(BlockFace.DOWN)));
97+
98+
for (int i = 0; i < remainingGrowth; i++) {
99+
Block current = block.getRelative(0, i, 0);
100+
if (current.getType() == Material.AIR &&
101+
canGrow(current.getRelative(BlockFace.DOWN))) {
102+
current.setType(Material.SUGAR_CANE_BLOCK);
103+
}
104+
}
105+
}
106+
});
107+
}
108+
});
109+
}
110+
}.runTaskTimer(this, 20L, (long) (growthInterval * 20L));
111+
}
112+
113+
private Block findBaseBlock(Block block) {
114+
while (block.getRelative(BlockFace.DOWN).getType() == Material.SUGAR_CANE_BLOCK) {
115+
block = block.getRelative(BlockFace.DOWN);
116+
}
117+
return block;
118+
}
119+
120+
private Block getTopBlock(Block base) {
121+
Block top = base;
122+
while (top.getRelative(BlockFace.UP).getType() == Material.SUGAR_CANE_BLOCK) {
123+
top = top.getRelative(BlockFace.UP);
124+
}
125+
return top;
126+
}
127+
128+
private int getCaneHeight(Block base) {
129+
int height = 1;
130+
Block current = base;
131+
while ((current = current.getRelative(BlockFace.UP)).getType() == Material.SUGAR_CANE_BLOCK) {
132+
height++;
133+
}
134+
return height;
135+
}
136+
137+
private boolean canGrow(Block sugarCane) {
138+
Block below = sugarCane.getRelative(BlockFace.DOWN);
139+
Material belowType = below.getType();
140+
return belowType == Material.SUGAR_CANE_BLOCK ||
141+
belowType == Material.GRASS ||
142+
belowType == Material.DIRT ||
143+
belowType == Material.SAND;
144+
}
145+
}

src/main/resources/config.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# FastCane Configuration
2+
growth-interval: 0.5 # Cane growth timer in seconds
3+
max-height: 3 # Maximum height of the sugarcane counting the base (1-255)
4+
check-radius: 64 # Blocks to check from a player in a cuboid
5+
growth-amount: 1 # Blocks to grow each interval

src/main/resources/plugin.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: FastGrowPlugin
2+
version: 1.0
3+
main: me.abhimaanst.fastcane.FastGrowPlugin
4+
api-version: 1.16
5+
description: Makes sugar cane grow faster based on a configurable interval
6+
author: Abhimaanst

0 commit comments

Comments
 (0)