Skip to content

Commit

Permalink
Merge pull request #17 from Th3Shadowbroker/feature/hopper-support-2
Browse files Browse the repository at this point in the history
Feature/hopper support concept
  • Loading branch information
InventivetalentDev authored Dec 12, 2019
2 parents 06a46b3 + 82e6e29 commit 0e3b449
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

<groupId>org.inventivetalent</groupId>
<artifactId>bookshelves</artifactId>
<version>1.5.2-SNAPSHOT</version>
<version>1.6.0-SNAPSHOT</version>


<build>
Expand Down
3 changes: 3 additions & 0 deletions resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ disabledWorlds: []
# Whether to allow only books in shelves
onlyBooks: true

# Hopper support
hoppers: false

# Material-restrictions
# IMPORTANT: Restrictions only work when onlyBooks is set to false
restrictions:
Expand Down
8 changes: 7 additions & 1 deletion src/org/inventivetalent/bookshelves/Bookshelves.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public class Bookshelves extends JavaPlugin {
Set<String> disabledWorlds = new HashSet<>();
boolean onlyBooks = true;
boolean worldGuardSupport = false;
boolean checkRestrictions = false;
boolean checkRestrictions = false;
boolean hopperSupport = false;
RestrictionManager restrictionManager = null;

Set<Location> shelves = new HashSet<>();
Expand Down Expand Up @@ -64,6 +65,7 @@ public void onEnable() {
if (getConfig().contains("disabledWorlds")) { disabledWorlds.addAll(getConfig().getStringList("disabledWorlds")); }
onlyBooks = getConfig().getBoolean("onlyBooks", true);
checkRestrictions = getConfig().getBoolean("restrictions.enabled");
hopperSupport = getConfig().getBoolean("hoppers");

// Initialize restrictions
if (checkRestrictions) {
Expand Down Expand Up @@ -156,6 +158,8 @@ public void run() {
}
}
}

if (hopperSupport) HopperUtils.initializeHopperSupport();
} catch (Exception e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -222,6 +226,8 @@ public Inventory initShelf(Block block) {
MetaHelper.setMetaValue(block, "BOOKSHELF_INVENTORY", inventory);

shelves.add(block.getLocation());
if (hopperSupport) HopperUtils.pull(block);

return inventory;
} else {
inventory = getShelf(block);
Expand Down
77 changes: 77 additions & 0 deletions src/org/inventivetalent/bookshelves/HopperUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.inventivetalent.bookshelves;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Hopper;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class HopperUtils {

public static void pull(Block block) {
Inventory shelf = Bookshelves.instance.getShelf(block);
List<Hopper> hoppers = getHoppers(block, BlockFace.UP, BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST);
int seconds = 1;

if (shelf == null) return;

if (shelf.firstEmpty() > -1) {
for (Hopper hopper : hoppers) {
for (ItemStack itemStack : hopper.getInventory().getContents()) {
if (!Bookshelves.instance.isValidBook(itemStack)) continue;
new ScheduledItemTransfer(hopper.getInventory(), shelf, itemStack, seconds);
seconds++;
}
}
}
scheduleNextCheck(block, seconds + 1);
push(block);
}

public static void push(Block block) {
List<Hopper> hoppers = getHoppers(block, BlockFace.DOWN).stream().filter(hopper -> !hopper.isLocked() && hopper.getInventory().firstEmpty() > -1).collect(Collectors.toCollection(ArrayList::new));

if (!hoppers.isEmpty()) {
int seconds = 1;
Hopper hopper = hoppers.get(0);
Inventory shelf = Bookshelves.instance.getShelf(block);

if (shelf == null) return;

for (ItemStack itemStack : shelf) {
if (!Bookshelves.instance.isValidBook(itemStack)) continue;
new ScheduledItemTransfer(shelf, hopper.getInventory(), itemStack, seconds);
seconds++;
}
}
}

public static List<Hopper> getHoppers(Block block, BlockFace... faces) {
List<Hopper> hoppers = new ArrayList<>();
for (BlockFace face : faces) {
Block relative = block.getRelative(face);
if (relative.getType() == Material.HOPPER) {
if (face == BlockFace.DOWN || relative.getRelative(((org.bukkit.block.data.type.Hopper) relative.getBlockData()).getFacing()).equals(block)) {
hoppers.add((Hopper) relative.getState());
}
}
}
return hoppers;
}

public static void scheduleNextCheck(Block block, int seconds) {
Bukkit.getScheduler().runTaskLater(Bookshelves.instance, () -> pull(block), 20 * seconds);
}

public static void initializeHopperSupport() {
Bookshelves.instance.getLogger().info("Hopper support enabled!");
Bookshelves.instance.shelves.forEach(shelfLocation -> pull(shelfLocation.getBlock()));
}

}
40 changes: 40 additions & 0 deletions src/org/inventivetalent/bookshelves/ScheduledItemTransfer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.inventivetalent.bookshelves;

import org.bukkit.Bukkit;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

public class ScheduledItemTransfer implements Runnable {

private final Inventory source;

private final Inventory destination;

private final ItemStack itemStack;

public ScheduledItemTransfer(Inventory source, Inventory destination, ItemStack itemStack, int seconds) {
this.source = source;
this.destination = destination;
this.itemStack = itemStack;

Bukkit.getScheduler().runTaskLater(Bookshelves.instance, this, 20 * seconds);
}

@Override
public void run() {
if (source.contains(itemStack)) {
if (destination.firstEmpty() > -1) {
if (itemStack.getAmount() <= 2) {
source.remove(itemStack);
destination.addItem(itemStack);
} else {
ItemStack partialStack = itemStack.clone();
partialStack.setAmount(2);
itemStack.setAmount(itemStack.getAmount() - 2);
destination.addItem(partialStack);
}
}
}
}

}

0 comments on commit 0e3b449

Please sign in to comment.