mirror of
https://github.com/DieReicheErethons/Brewery.git
synced 2025-01-05 18:37:40 +01:00
Started MCBarrel
This commit is contained in:
parent
37f22f248f
commit
a95437cdf1
@ -78,6 +78,8 @@ public class Barrel implements InventoryHolder {
|
||||
}
|
||||
|
||||
if (woodsloc == null && stairsloc == null) {
|
||||
// If loading from old data, or block locations are missing, regenerate them
|
||||
// This will only be done in those extreme cases.
|
||||
Block broken = getBrokenBlock(true);
|
||||
if (broken != null) {
|
||||
remove(broken, null);
|
||||
|
124
src/com/dre/brewery/MCBarrel.java
Normal file
124
src/com/dre/brewery/MCBarrel.java
Normal file
@ -0,0 +1,124 @@
|
||||
package com.dre.brewery;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockState;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.InventoryHolder;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.ListIterator;
|
||||
|
||||
public class MCBarrel {
|
||||
|
||||
public static final byte OAK = 2;
|
||||
public static List<MCBarrel> barrels = new ArrayList<>();
|
||||
|
||||
private Block block;
|
||||
private float time;
|
||||
private byte brews = -1; // How many Brewery Brews are in this Barrel
|
||||
|
||||
|
||||
public MCBarrel(Block block, float time) {
|
||||
this.block = block;
|
||||
this.time = time;
|
||||
}
|
||||
|
||||
|
||||
// Now Opening this Barrel for a player
|
||||
public void open(Inventory inv, Player player) {
|
||||
brews = -1;
|
||||
if (time > 0) {
|
||||
// if nobody has the inventory opened
|
||||
if (inv.getViewers().isEmpty()) {
|
||||
brews = 0;
|
||||
// if inventory contains potions
|
||||
if (inv.contains(Material.POTION)) {
|
||||
long loadTime = System.nanoTime();
|
||||
for (ItemStack item : inv.getContents()) {
|
||||
if (item != null) {
|
||||
Brew brew = Brew.get(item);
|
||||
if (brew != null) {
|
||||
if (brews <= 6) {
|
||||
brew.age(item, time, OAK);
|
||||
}
|
||||
brews++;
|
||||
}
|
||||
}
|
||||
}
|
||||
loadTime = System.nanoTime() - loadTime;
|
||||
float ftime = (float) (loadTime / 1000000.0);
|
||||
P.p.debugLog("opening MC Barrel with potions (" + ftime + "ms)");
|
||||
}
|
||||
}
|
||||
}
|
||||
// reset barreltime, potions have new age
|
||||
time = 0;
|
||||
}
|
||||
|
||||
// Closing Inventory. Check if we need to track this Barrel
|
||||
// Returns true if there are Brews in the Inv
|
||||
public boolean close(Inventory inv, Player player) {
|
||||
if (inv.getViewers().size() == 1) {
|
||||
// This is the last viewer
|
||||
for (ItemStack item : inv.getContents()) {
|
||||
if (item != null) {
|
||||
Brew brew = Brew.get(item);
|
||||
if (brew != null) {
|
||||
// We found a brew, so we keep this Barrel
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
// No Brew found, remove this Barrel
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public Block getBlock() {
|
||||
return block;
|
||||
}
|
||||
|
||||
public float getTime() {
|
||||
return time;
|
||||
}
|
||||
|
||||
public Inventory getInventory() {
|
||||
BlockState state = block.getState();
|
||||
if (state instanceof InventoryHolder) {
|
||||
return ((InventoryHolder) state).getInventory();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void onUpdate() {
|
||||
if (barrels.isEmpty()) return;
|
||||
|
||||
// Check if stored MCBarrels still exist
|
||||
// Choose a random starting point for check
|
||||
int random = (int) Math.floor(Math.random() * barrels.size());
|
||||
random = Math.max(0, random - 5);
|
||||
ListIterator<MCBarrel> iter = barrels.listIterator(random);
|
||||
// Check at least 4 barrels, but if there are many, check about 1/64 of them all, so in about 1 hour we have checked all
|
||||
for (int i = Math.max(4, barrels.size() >> 6); i <= 0; i--) {
|
||||
if (!iter.hasNext()) break;
|
||||
|
||||
Block block = iter.next().block;
|
||||
if (Util.isChunkLoaded(block)) {
|
||||
// If the chunk is loaded we can check if the block is still a MC Barrel. If not we remove the stored entry.
|
||||
if (block.getType() != Material.BARREL) {
|
||||
iter.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (MCBarrel barrel : barrels) {
|
||||
// Minecraft day is 20 min, so add 1/20 to the time every minute
|
||||
barrel.time += (1.0 / 20.0);
|
||||
}
|
||||
}
|
||||
}
|
@ -903,6 +903,7 @@ public class P extends JavaPlugin {
|
||||
cauldron.onUpdate();// runs every min to update cooking time
|
||||
}
|
||||
Barrel.onUpdate();// runs every min to check and update ageing time
|
||||
MCBarrel.onUpdate();
|
||||
BPlayer.onUpdate();// updates players drunkeness
|
||||
|
||||
debugLog("Update");
|
||||
|
@ -3,15 +3,18 @@ package com.dre.brewery.listeners;
|
||||
import com.dre.brewery.*;
|
||||
import com.dre.brewery.integration.LogBlockBarrel;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.block.BlockState;
|
||||
import org.bukkit.block.BrewingStand;
|
||||
import org.bukkit.entity.HumanEntity;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.inventory.*;
|
||||
import org.bukkit.inventory.BlockInventoryHolder;
|
||||
import org.bukkit.inventory.BrewerInventory;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemFlag;
|
||||
@ -315,6 +318,25 @@ public class InventoryListener implements Listener {
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onInventoryOpen(InventoryOpenEvent event) {
|
||||
if (!P.use1_14) return;
|
||||
if (!(event.getPlayer() instanceof Player)) return;
|
||||
|
||||
if (event.getInventory().getType() == InventoryType.BARREL) {
|
||||
Inventory inv = event.getInventory();
|
||||
if (inv.getHolder() instanceof BlockInventoryHolder) {
|
||||
Location loc = ((BlockInventoryHolder) inv.getHolder()).getBlock().getLocation();
|
||||
for (MCBarrel barrel : MCBarrel.barrels) {
|
||||
if (barrel.getBlock().getLocation().equals(loc)) {
|
||||
barrel.open(inv, ((Player) event.getPlayer()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// block the pickup of items where getPickupDelay is > 1000 (puke)
|
||||
@EventHandler(ignoreCancelled = true)
|
||||
public void onInventoryPickupItem(InventoryPickupItemEvent event){
|
||||
|
Loading…
Reference in New Issue
Block a user