Skip to content

Commit

Permalink
Merge pull request #162 from eirikh1996/Repair-fuel-furnace
Browse files Browse the repository at this point in the history
Repair fuel furnace
  • Loading branch information
Dan authored Jul 14, 2019
2 parents b535865 + 5cb4026 commit 1171c9a
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
import com.sk89q.jnbt.Tag;
import com.sk89q.worldedit.blocks.BaseBlock;
import net.countercraft.movecraft.MovecraftLocation;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Dispenser;
import org.bukkit.block.Sign;
import org.bukkit.block.*;
import org.bukkit.inventory.FurnaceInventory;
import org.bukkit.inventory.ItemStack;

public class WorldEditUpdateCommand extends UpdateCommand {
Expand Down Expand Up @@ -115,5 +114,61 @@ public void doUpdate() {
s.update(false, false);
}
}
if (type == Material.FURNACE){
ListTag list = worldEditBaseBlock.getNbtData().getListTag("Items");
FurnaceInventory fInv = ((Furnace) block.getState()).getInventory();
if (list != null){
for (Tag t : list.getValue()){
if (!(t instanceof CompoundTag)){
continue;
}
CompoundTag ct = (CompoundTag) t;
byte slot = ct.getByte("Slot");
if (slot == 2){//Ignore the result slot
continue;
}
String id = ct.getString("id");
ImmutablePair<Material, Byte> content;
if (id.equals("minecraft:coal")){
byte data = (byte) ct.getShort("Damage");
byte count = ct.getByte("Count");
//Smelting slot

if (slot == 0) {
if (fInv.getSmelting() != null && fInv.getSmelting().getData().getData() == data){
fInv.getSmelting().setAmount(count);
} else {
fInv.setSmelting(new ItemStack(Material.COAL, count, (short) 0, data));
}
} else if (slot == 1) {//Fuel slot
if (fInv.getFuel() != null && fInv.getFuel().getData().getData() == data){
fInv.getFuel().setAmount(count);
} else {
fInv.setFuel(new ItemStack(Material.COAL, count, (short) 0, data));
}
}

}
if (id.equals("minecraft:coal_block")){
byte count = ct.getByte("Count");
//Smelting slot
//Fuel slot
if (slot == 0) {
if (fInv.getSmelting() != null){
fInv.getSmelting().setAmount(count);
} else {
fInv.setSmelting(new ItemStack(Material.COAL_BLOCK, count));
}
} else if (slot == 1) {//Fuel slot
if (fInv.getFuel() != null){
fInv.getFuel().setAmount(count);
} else {
fInv.setFuel(new ItemStack(Material.COAL_BLOCK, count));
}
}
}
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,26 +140,30 @@ private void signRightClick(PlayerInteractEvent event){
secondClick = true;
}
}
HashMap<Material, Double> numMissingItems = movecraftRepair.getMissingBlocks(repairName);
HashMap<ImmutablePair<Material, Byte>, Double> numMissingItems = movecraftRepair.getMissingBlocks(repairName);
ArrayDeque<ImmutablePair<Vector, Vector>> locMissingBlocks = movecraftRepair.getMissingBlockLocations(repairName);
int totalSize = locMissingBlocks.size() + pCraft.getHitBox().size();
if (secondClick){
// check all the chests for materials for the repair
HashMap<Material, ArrayList<InventoryHolder>> chestsToTakeFrom = new HashMap<>(); // typeid, list of chest inventories
boolean enoughMaterial = true;
for (Material type : numMissingItems.keySet()) {
for (ImmutablePair<Material, Byte> type : numMissingItems.keySet()) {
long longRemQty = Math.round(numMissingItems.get(type));
int remainingQty = (int) longRemQty;
ArrayList<InventoryHolder> chests = new ArrayList<>();
for (MovecraftLocation loc : pCraft.getHitBox()) {
Block b = pCraft.getW().getBlockAt(loc.getX(), loc.getY(), loc.getZ());
if ((b.getType() == Material.CHEST) || (b.getType() == Material.TRAPPED_CHEST)) {
InventoryHolder inventoryHolder = (InventoryHolder) b.getState();
if (inventoryHolder.getInventory().contains(type) && remainingQty > 0) {
HashMap<Integer, ? extends ItemStack> foundItems = inventoryHolder.getInventory().all(type);
if (inventoryHolder.getInventory().contains(type.getLeft()) && remainingQty > 0) {
HashMap<Integer, ? extends ItemStack> foundItems = inventoryHolder.getInventory().all(type.getLeft());
// count how many were in the chest
int numfound = 0;
for (ItemStack istack : foundItems.values()) {
//Check data value if it is coal
if (istack.getType().equals(Material.COAL) && istack.getData().getData() != type.getRight()){
continue;
}
numfound += istack.getAmount();
}
remainingQty -= numfound;
Expand All @@ -168,10 +172,15 @@ private void signRightClick(PlayerInteractEvent event){
}
}
if (remainingQty > 0) {
event.getPlayer().sendMessage(String.format(I18nSupport.getInternationalisedString("Need more of material") + ": %s - %d", type.name().toLowerCase().replace("_", " "), remainingQty));
if (type.getLeft().equals(Material.COAL)) {
event.getPlayer().sendMessage(String.format(I18nSupport.getInternationalisedString("Need more of material") + ": %s - %d", type.getRight() == 1 ? "charcoal" : "coal", remainingQty));
} else {
event.getPlayer().sendMessage(String.format(I18nSupport.getInternationalisedString("Need more of material") + ": %s - %d", type.getLeft().name().toLowerCase().replace("_", " "), remainingQty));
}

enoughMaterial = false;
} else {
chestsToTakeFrom.put(type, chests);
chestsToTakeFrom.put(type.getLeft(), chests);
}
}
if (Movecraft.getInstance().getEconomy() != null && enoughMaterial) {
Expand All @@ -185,11 +194,14 @@ private void signRightClick(PlayerInteractEvent event){
}
if (enoughMaterial) {
// we know we have enough materials to make the repairs, so remove the materials from the chests
for (Material type : numMissingItems.keySet()) {
for (ImmutablePair<Material, Byte> type : numMissingItems.keySet()) {
int remainingQty = (int) Math.round(numMissingItems.get(type));
for (InventoryHolder inventoryHolder : chestsToTakeFrom.get(type)) {
HashMap<Integer, ? extends ItemStack> foundItems = inventoryHolder.getInventory().all(type);
for (InventoryHolder inventoryHolder : chestsToTakeFrom.get(type.getLeft())) {
HashMap<Integer, ? extends ItemStack> foundItems = inventoryHolder.getInventory().all(type.getLeft());
for (ItemStack istack : foundItems.values()) {
if (istack.getType().equals(Material.COAL) && istack.getData().getData() != type.getRight()){
continue;
}
if (istack.getAmount() <= remainingQty) {
remainingQty -= istack.getAmount();
inventoryHolder.getInventory().removeItem(istack);
Expand Down Expand Up @@ -239,8 +251,12 @@ private void signRightClick(PlayerInteractEvent event){
}
if (numDifferentBlocks != 0) {
event.getPlayer().sendMessage(I18nSupport.getInternationalisedString("SUPPLIES NEEDED"));
for (Material blockType : numMissingItems.keySet()) {
event.getPlayer().sendMessage(String.format("%s : %d", blockType.name().toLowerCase().replace("_", " "), Math.round(numMissingItems.get(blockType))));
for (ImmutablePair<Material, Byte> blockType : numMissingItems.keySet()) {
if (blockType.getLeft().equals(Material.COAL)) {
event.getPlayer().sendMessage(String.format("%s : %d", blockType.getRight() == 1 ? "charcoal" : "coal" , Math.round(numMissingItems.get(blockType))));
} else {
event.getPlayer().sendMessage(String.format("%s : %d", blockType.getLeft().name().toLowerCase().replace("_", " "), Math.round(numMissingItems.get(blockType))));
}
}
long durationInSeconds = numDifferentBlocks * Settings.RepairTicksPerBlock / 20;
event.getPlayer().sendMessage(String.format(I18nSupport.getInternationalisedString("Seconds to complete repair") + ": %d", durationInSeconds));
Expand Down
1 change: 1 addition & 0 deletions modules/Movecraft/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ FadeWrecksAfter: 600
BlockQueueChunkSize: 200000
RepairTicksPerBlock: 10
RepairMoneyPerBlock: 10
RepairMaxPercent: 50
SiegeTaskSeconds: 600 # Seconds per siege message, defaults to 600 (10 minutes)
AllowCrewSigns: true
MaxRemoteSigns: -1 # Maximum number of remote signs that can be triggered at once. Setting to -1 allows unlimited.
Expand Down
Loading

0 comments on commit 1171c9a

Please sign in to comment.