Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option for automatic collecting with the hopper #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/main/java/com/songoda/epicfarming/settings/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public class Settings {
"The enabled language file.",
"More language files (if available) can be found in the plugins data folder.");

public static final ConfigSetting COLLECT_WITH_HOPPER = new ConfigSetting(config, "Main.Collect With Hopper", true,
"Should you be able to collect items with an hopper under the farming item?");

/**
* In order to set dynamic economy comment correctly, this needs to be
* called after EconomyManager load
Expand All @@ -84,4 +87,4 @@ public static void setupConfig() {

config.saveChanges();
}
}
}
49 changes: 26 additions & 23 deletions src/main/java/com/songoda/epicfarming/tasks/HopperTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.songoda.epicfarming.EpicFarming;
import com.songoda.epicfarming.farming.Farm;
import com.songoda.epicfarming.farming.FarmManager;
import com.songoda.epicfarming.settings.Settings;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
Expand Down Expand Up @@ -34,38 +35,40 @@ public static HopperTask startTask(EpicFarming plugin) {

@Override
public void run() {
for (Farm farm : manager.getFarms().values()) {
Location farmLocation = farm.getLocation();
if (farmLocation == null || farmLocation.getWorld() == null) {
manager.removeFarm(farm.getLocation());
continue;
}
if(Settings.COLLECT_WITH_HOPPER.getBoolean()) {
for (Farm farm : manager.getFarms().values()) {
Location farmLocation = farm.getLocation();
if (farmLocation == null || farmLocation.getWorld() == null) {
manager.removeFarm(farm.getLocation());
continue;
}

int x = farmLocation.getBlockX() >> 4;
int z = farmLocation.getBlockZ() >> 4;
int x = farmLocation.getBlockX() >> 4;
int z = farmLocation.getBlockZ() >> 4;

if (!farmLocation.getWorld().isChunkLoaded(x, z)) {
continue;
}
if (!farmLocation.getWorld().isChunkLoaded(x, z)) {
continue;
}

Block block = farmLocation.getBlock().getRelative(BlockFace.DOWN);
Block block = farmLocation.getBlock().getRelative(BlockFace.DOWN);

if (block.getType() != Material.HOPPER)
continue;
if (block.getType() != Material.HOPPER)
continue;

Inventory hopperInventory = ((Hopper) block.getState()).getInventory();
Inventory hopperInventory = ((Hopper) block.getState()).getInventory();

for (ItemStack item : farm.getItems().toArray(new ItemStack[0])) {
if (item.getType() == CompatibleMaterial.BONE_MEAL.getMaterial()) continue;
for (ItemStack item : farm.getItems().toArray(new ItemStack[0])) {
if (item.getType() == CompatibleMaterial.BONE_MEAL.getMaterial()) continue;

ItemStack toMove = item.clone();
toMove.setAmount(1);
ItemStack toMove = item.clone();
toMove.setAmount(1);

if (canHop(hopperInventory, toMove)) {
farm.removeMaterial(toMove.getType(), 1);
hopperInventory.addItem(toMove);
if (canHop(hopperInventory, toMove)) {
farm.removeMaterial(toMove.getType(), 1);
hopperInventory.addItem(toMove);
}
break;
}
break;
}
}
}
Expand Down