Fix Brews bugging in the distiller without glowstone

Fixes #127
This commit is contained in:
Sn0wStorm 2016-06-27 20:06:41 +02:00
parent dfe8e09638
commit a31247efca

View File

@ -30,11 +30,6 @@ import java.util.UUID;
* set of ingredients in the brewer can be distilled. * set of ingredients in the brewer can be distilled.
* Nothing here should interfere with vanilla brewing. * Nothing here should interfere with vanilla brewing.
* *
* Note in testing I did discover a few ways to "hack" brewing to distill your brews alongside
* potions; put fuel and at least one "valid" water bottle w/ a brewing component. You can distill
* two brews this way, just remove them before the "final" distillation or you will actually
* brew the potion as well.
*
* @author ProgrammerDan (1.9 distillation update only) * @author ProgrammerDan (1.9 distillation update only)
*/ */
public class InventoryListener implements Listener { public class InventoryListener implements Listener {
@ -101,7 +96,9 @@ public class InventoryListener implements Listener {
Integer curTask = trackedBrewers.get(brewery); Integer curTask = trackedBrewers.get(brewery);
if (curTask != null) { if (curTask != null) {
Bukkit.getScheduler().cancelTask(curTask); // cancel prior Bukkit.getScheduler().cancelTask(curTask); // cancel prior
brewer.getHolder().setBrewingTime(0); // Fixes brewing continuing without fuel for normal potions
} }
final int fuel = brewer.getHolder().getFuelLevel();
// Now check if we should bother to track it. // Now check if we should bother to track it.
trackedBrewers.put(brewery, new BukkitRunnable() { trackedBrewers.put(brewery, new BukkitRunnable() {
@ -112,11 +109,24 @@ public class InventoryListener implements Listener {
if (now instanceof BrewingStand) { if (now instanceof BrewingStand) {
BrewingStand stand = (BrewingStand) now; BrewingStand stand = (BrewingStand) now;
if (brewTime == DISTILLTIME) { // only check at the beginning (and end) for distillables if (brewTime == DISTILLTIME) { // only check at the beginning (and end) for distillables
if (!isCustom(stand.getInventory(), true)) { switch (hasCustom(stand.getInventory())) {
this.cancel(); case 1:
trackedBrewers.remove(brewery); // Custom potion but not for distilling. Stop any brewing and cancel this task
P.p.debugLog("nothing to distill"); if (stand.getBrewingTime() > 0) {
return; // Brewing time is sent and stored as short
// This sends a negative short value to the Client
// In the client the Brewer will look like it is not doing anything
stand.setBrewingTime(Short.MAX_VALUE << 1);
stand.setFuelLevel(fuel);
}
case 0:
// No custom potion, cancel and ignore
this.cancel();
trackedBrewers.remove(brewery);
P.p.debugLog("nothing to distill");
return;
default:
} }
} }
@ -124,9 +134,6 @@ public class InventoryListener implements Listener {
stand.setBrewingTime(brewTime); // arbitrary for now stand.setBrewingTime(brewTime); // arbitrary for now
if (brewTime <= 1) { // Done! if (brewTime <= 1) { // Done!
//BrewEvent doBrew = new BrewEvent(brewery, brewer);
//Bukkit.getServer().getPluginManager().callEvent(doBrew);
BrewerInventory brewer = stand.getInventory(); BrewerInventory brewer = stand.getInventory();
if (!runDistill(brewer)) { if (!runDistill(brewer)) {
this.cancel(); this.cancel();
@ -147,9 +154,10 @@ public class InventoryListener implements Listener {
}.runTaskTimer(P.p, 2L, 1L).getTaskId()); }.runTaskTimer(P.p, 2L, 1L).getTaskId());
} }
private boolean isCustom(BrewerInventory brewer, boolean distill) { private byte hasCustom(BrewerInventory brewer) {
ItemStack item = brewer.getItem(3); // ingredient ItemStack item = brewer.getItem(3); // ingredient
if (item == null || Material.GLOWSTONE_DUST != item.getType()) return false; // need dust in the top slot. boolean glowstone = (item != null && Material.GLOWSTONE_DUST == item.getType()); // need dust in the top slot.
byte customFound = 0;
for (int slot = 0; slot < 3; slot++) { for (int slot = 0; slot < 3; slot++) {
item = brewer.getItem(slot); item = brewer.getItem(slot);
if (item != null) { if (item != null) {
@ -157,20 +165,27 @@ public class InventoryListener implements Listener {
if (item.hasItemMeta()) { if (item.hasItemMeta()) {
int uid = Brew.getUID(item); int uid = Brew.getUID(item);
Brew pot = Brew.potions.get(uid); Brew pot = Brew.potions.get(uid);
if (pot != null && (!distill || pot.canDistill())) { // need at least one distillable potion. if (pot != null) {
return true; if (!glowstone) {
return 1;
}
if (pot.canDistill()) {
return 2;
} else {
customFound = 1;
}
} }
} }
} }
} }
} }
return false; return customFound;
} }
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onBrew(BrewEvent event) { public void onBrew(BrewEvent event) {
if (P.use1_9) { if (P.use1_9) {
if (isCustom(event.getContents(), false)) { if (hasCustom(event.getContents()) != 0) {
event.setCancelled(true); event.setCancelled(true);
} }
return; return;