Skip to content

Commit

Permalink
Moved some test code, optimized Distiller
Browse files Browse the repository at this point in the history
  • Loading branch information
Sn0wStorm committed Nov 25, 2019
1 parent a5f73ce commit c287b63
Show file tree
Hide file tree
Showing 8 changed files with 477 additions and 446 deletions.
3 changes: 3 additions & 0 deletions resources/config/v13/de/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ updateCheck: true
# Autosave Intervall in Minuten [3]
autosave: 3

# Debug Nachrichten im Log anzeigen [false]
debug: false

# Config Version
version: '1.8'

Expand Down
3 changes: 3 additions & 0 deletions resources/config/v13/en/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ updateCheck: true
# Autosave interval in minutes [3]
autosave: 3

# Debug Nachrichten im Log anzeigen [false]
debug: false

# Config Version
version: '1.8'

Expand Down
42 changes: 29 additions & 13 deletions src/com/dre/brewery/BDistiller.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,23 @@ public static Brew[] getDistillContents(BrewerInventory inv) {
return contents;
}

public static byte hasBrew(BrewerInventory brewer) {
public static void checkContents(BrewerInventory inv, Brew[] contents) {
ItemStack item;
for (int slot = 0; slot < 3; slot++) {
if (contents[slot] != null) {
item = inv.getItem(slot);
if (item == null || !Brew.isBrew(item)) {
contents[slot] = null;
}
}
}
}

public static byte hasBrew(BrewerInventory brewer, Brew[] contents) {
ItemStack item = brewer.getItem(3); // ingredient
boolean glowstone = (item != null && Material.GLOWSTONE_DUST == item.getType()); // need dust in the top slot.
byte customFound = 0;
for (Brew brew : getDistillContents(brewer)) {
for (Brew brew : contents) {
if (brew != null) {
if (!glowstone) {
return 1;
Expand All @@ -103,9 +115,8 @@ public static byte hasBrew(BrewerInventory brewer) {
return customFound;
}

public static boolean runDistill(BrewerInventory inv) {
public static boolean runDistill(BrewerInventory inv, Brew[] contents) {
boolean custom = false;
Brew[] contents = getDistillContents(inv);
for (int slot = 0; slot < 3; slot++) {
if (contents[slot] == null) continue;
if (contents[slot].canDistill()) {
Expand All @@ -122,10 +133,9 @@ public static boolean runDistill(BrewerInventory inv) {
return false;
}

public static int getLongestDistillTime(BrewerInventory inv) {
public static int getLongestDistillTime(Brew[] contents) {
int bestTime = 0;
int time;
Brew[] contents = getDistillContents(inv);
for (int slot = 0; slot < 3; slot++) {
if (contents[slot] == null) continue;
time = contents[slot].getDistillTimeNextRun();
Expand All @@ -143,8 +153,7 @@ public static int getLongestDistillTime(BrewerInventory inv) {
return 800;
}

public static void showAlc(BrewerInventory inv) {
Brew[] contents = getDistillContents(inv);
public static void showAlc(BrewerInventory inv, Brew[] contents) {
for (int slot = 0; slot < 3; slot++) {
if (contents[slot] != null) {
// Show Alc in lore
Expand All @@ -159,14 +168,21 @@ public static void showAlc(BrewerInventory inv) {
}

public class DistillRunnable extends BukkitRunnable {
Brew[] contents = null;

@Override
public void run() {
BlockState now = standBlock.getState();
if (now instanceof BrewingStand) {
BrewingStand stand = (BrewingStand) now;
if (brewTime == -1) { // only check at the beginning (and end) for distillables
switch (hasBrew(stand.getInventory())) {
BrewerInventory inventory = stand.getInventory();
if (contents == null) {
contents = getDistillContents(inventory);
} else {
checkContents(inventory, contents);
}
switch (hasBrew(inventory, contents)) {
case 1:
// Custom potion but not for distilling. Stop any brewing and cancel this task
if (stand.getBrewingTime() > 0) {
Expand All @@ -187,11 +203,11 @@ public void run() {
// No custom potion, cancel and ignore
this.cancel();
trackedDistillers.remove(standBlock);
showAlc(stand.getInventory());
showAlc(inventory, contents);
P.p.debugLog("nothing to distill");
return;
default:
runTime = getLongestDistillTime(stand.getInventory());
runTime = getLongestDistillTime(contents);
brewTime = runTime;
P.p.debugLog("using brewtime: " + runTime);

Expand All @@ -202,10 +218,10 @@ public void run() {
stand.setBrewingTime((int) ((float) brewTime / ((float) runTime / (float) DISTILLTIME)) + 1);

if (brewTime <= 1) { // Done!
checkContents(stand.getInventory(), contents);
stand.setBrewingTime(0);
stand.update();
BrewerInventory brewer = stand.getInventory();
if (!runDistill(brewer)) {
if (!runDistill(stand.getInventory(), contents)) {
this.cancel();
trackedDistillers.remove(standBlock);
P.p.debugLog("All done distilling");
Expand Down
Loading

0 comments on commit c287b63

Please sign in to comment.