Skip to content

Commit

Permalink
add conveyor belts
Browse files Browse the repository at this point in the history
  • Loading branch information
Quarri6343 committed May 22, 2023
1 parent b37da35 commit a91538a
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 3 deletions.
29 changes: 29 additions & 0 deletions src/main/java/quarri6343/overcrafted/impl/block/BlockConveyor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package quarri6343.overcrafted.impl.block;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.scheduler.BukkitTask;
import quarri6343.overcrafted.Overcrafted;
import quarri6343.overcrafted.impl.task.ConveyorRunnable;

import java.util.HashMap;
import java.util.Map;

public class BlockConveyor extends BlockTable {

/**
* コンベヤとそのタスクのマップ
*/
private static final Map<Block, BukkitTask> progressionMap = new HashMap<>();

public BlockConveyor() {
super(Material.MAGENTA_GLAZED_TERRACOTTA);
onPlace.add(block -> progressionMap.put(block, new ConveyorRunnable(this, block).runTaskTimer(Overcrafted.getInstance(), 20, 20)));
onPickUp.add((block, player) -> {
if(progressionMap.get(block) != null){
progressionMap.get(block).cancel();
progressionMap.remove(block);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public class BlockTable extends OCBlock implements IRightClickEventHandler {
/**
* アイテムが拾われた時に発火されるイベント
*/
protected List<BiConsumer<Block, Player>> onPickUp = new ArrayList<>();
public List<BiConsumer<Block, Player>> onPickUp = new ArrayList<>();

/**
* アイテムが置かれた時に発火されるイベント
*/
protected List<Consumer<Block>> onPlace = new ArrayList<>();
public List<Consumer<Block>> onPlace = new ArrayList<>();

public BlockTable(Material material) {
super(material);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ public enum OCBlocks {
TRASHCAN(new BlockTrashCan()),
SUPPLIER(new BlockSupplier()),
TABLE(new BlockTable(Material.DARK_OAK_PLANKS)),
TABLE_2(new BlockTable(Material.DARK_OAK_STAIRS)),
TABLE_3(new BlockTable(Material.DARK_OAK_SLAB)),

CRAFTING(new BlockManualProcessor(Material.CRAFTING_TABLE, "crafting_progression", OCSoundData.craftingSound, OCParticleData.craftParticle)),
SMELTING(new BlockAutomaticProcessor(Material.FURNACE, "smelting_progression", OCSoundData.smeltingSound, OCParticleData.smeltingParticle)),
WASHING(new BlockManualProcessor(Material.WATER_CAULDRON, "washing_progression", OCSoundData.washingSound, OCParticleData.washingParticle)),
FORGING(new BlockManualProcessor(Material.ANVIL, "forging_progression", OCSoundData.forgingSound, OCParticleData.forgingParticle)),
ENCHANTING(new BlockAutomaticProcessor(Material.ENCHANTING_TABLE, "enchanting_progression", OCSoundData.enchantingSound, OCParticleData.enchantingParticle)),
COUNTER(new BlockCounter(Material.END_PORTAL_FRAME));
COUNTER(new BlockCounter(Material.END_PORTAL_FRAME)),
CONVEYOR(new BlockConveyor());

private final OCBlock ocBlock;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package quarri6343.overcrafted.impl.task;

import org.bukkit.block.Block;
import org.bukkit.block.data.Directional;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import quarri6343.overcrafted.core.handler.PlaceItemHandler;
import quarri6343.overcrafted.impl.block.BlockConveyor;

public class ConveyorRunnable extends BukkitRunnable {

BlockConveyor blockConveyor;
Block conveyorBlock;

public ConveyorRunnable(BlockConveyor blockConveyor, Block conveyorBlock){
this.blockConveyor = blockConveyor;
this.conveyorBlock = conveyorBlock;
}

@Override
public void run() {
ItemStack itemOnConveyor = PlaceItemHandler.getItem(conveyorBlock);
Block nextBlock = conveyorBlock.getRelative(((Directional)conveyorBlock.getBlockData()).getFacing().getOppositeFace());
if(nextBlock.getType() != conveyorBlock.getType())
return;

if(PlaceItemHandler.placeItem(nextBlock,itemOnConveyor)){
blockConveyor.onPlace.forEach(blockConsumer -> blockConsumer.accept(nextBlock));
PlaceItemHandler.pickUpItem(conveyorBlock);
blockConveyor.onPickUp.forEach(blockConsumer -> blockConsumer.accept(conveyorBlock, null));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ private void snapDroppedItemsOnTable(){
}

if(PlaceItemHandler.placeItem(droppedItem.getLocation().getBlock().getRelative(BlockFace.DOWN), ((Item)droppedItem).getItemStack())){
((BlockTable)blockBeneathItem).onPlace.forEach(blockConsumer -> blockConsumer.accept(droppedItem.getLocation().getBlock().getRelative(BlockFace.DOWN)));
droppedItem.remove();

if(blockBeneathItem instanceof IBlockProcessor){
Expand Down

0 comments on commit a91538a

Please sign in to comment.