-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from Th3Shadowbroker/feature/hopper-support-2
Feature/hopper support concept
- Loading branch information
Showing
5 changed files
with
128 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
src/org/inventivetalent/bookshelves/ScheduledItemTransfer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |