diff options
Diffstat (limited to 'src/main')
2 files changed, 97 insertions, 153 deletions
diff --git a/src/main/java/com/github/bartimaeusnek/bartworks/common/tileentities/classic/BW_TileEntity_ExperimentalFloodGate.java b/src/main/java/com/github/bartimaeusnek/bartworks/common/tileentities/classic/BW_TileEntity_ExperimentalFloodGate.java index 0497a1b5b6..8ab24a58f7 100644 --- a/src/main/java/com/github/bartimaeusnek/bartworks/common/tileentities/classic/BW_TileEntity_ExperimentalFloodGate.java +++ b/src/main/java/com/github/bartimaeusnek/bartworks/common/tileentities/classic/BW_TileEntity_ExperimentalFloodGate.java @@ -24,182 +24,105 @@ package com.github.bartimaeusnek.bartworks.common.tileentities.classic; import com.github.bartimaeusnek.bartworks.API.ITileAddsInformation; import com.github.bartimaeusnek.bartworks.util.Coords; -import net.minecraft.block.Block; -import net.minecraft.init.Blocks; -import net.minecraft.world.World; +import net.minecraft.nbt.NBTTagCompound; +import net.minecraftforge.common.util.ForgeDirection; +import net.minecraftforge.fluids.Fluid; +import net.minecraftforge.fluids.FluidStack; import net.minecraftforge.fluids.TileFluidHandler; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; +import java.util.Comparator; +import java.util.PriorityQueue; + +import static net.minecraftforge.common.util.ForgeDirection.*; public class BW_TileEntity_ExperimentalFloodGate extends TileFluidHandler implements ITileAddsInformation { - BW_TileEntity_ExperimentalFloodGate.recursiveBelowCheck check = new BW_TileEntity_ExperimentalFloodGate.recursiveBelowCheck(); - private long ticks; - private long noOfIts; - private Coords paused; + private final static ForgeDirection[] allowed_directions = new ForgeDirection[]{DOWN, WEST, EAST, SOUTH, NORTH}; + private PriorityQueue<Coords> breadthFirstQueue = new PriorityQueue<>(Comparator.comparingInt(x -> x.y)); + private boolean wasInited = false; public BW_TileEntity_ExperimentalFloodGate() { - + this.tank.setCapacity(64000); } @Override - public void updateEntity() { - if (this.paused == null) { - this.paused = new Coords(this.xCoord, this.yCoord, this.zCoord, this.worldObj.provider.dimensionId); - } - this.ticks++; - if (this.check.called != -1) { - if (this.ticks % 20 == 0) { - HashSet<Coords> toRem = new HashSet<>(); - for (Coords c : this.check.hashset) { - this.worldObj.setBlock(c.x, c.y, c.z, Blocks.water, 0, 4); - toRem.add(c); - } - this.check.hashset.removeAll(toRem); - } - } else { - this.noOfIts = 0; - this.setUpHashSet(); - this.paused = this.check.hashset.get(this.check.hashset.size() - 1); - } - if (this.ticks % 50 == 0) - this.ticks = 0; + public boolean canFill(ForgeDirection from, Fluid fluid) { + return fluid.canBePlacedInWorld(); } - private synchronized void setUpHashSet() { - this.check = new BW_TileEntity_ExperimentalFloodGate.recursiveBelowCheck(); - Thread t = new Thread(this.check); - t.start(); - while (t.isAlive()) { - try { - this.wait(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - this.check.hashset.remove(new Coords(this.xCoord, this.yCoord, this.zCoord, this.worldObj.provider.dimensionId)); + public void initEntity() { + if (wasInited) + return; + breadthFirstQueue.add(new Coords(this.xCoord, this.yCoord, this.zCoord)); + wasInited = true; } @Override - public String[] getInfoData() { - return new String[]{"Experimental Machine to fill Holes with Fluids"}; + public void updateEntity() { + initEntity(); + Coords current = breadthFirstQueue.poll(); + if (current == null) + return; + setFluidBlock(current); + for (ForgeDirection allowed_direction : allowed_directions) { + addBlockToQueue(current, allowed_direction); + } } - private class recursiveBelowCheck implements Runnable { - - private final List<Coords> hashset = new ArrayList<>(); - int called = -1; - - public int getCalled() { - return this.called; + @Override + public void writeToNBT(NBTTagCompound tag) { + super.writeToNBT(tag); + tag.setBoolean("init", wasInited); + + int[] x = new int[this.breadthFirstQueue.size()]; + int[] y = new int[this.breadthFirstQueue.size()]; + int[] z = new int[this.breadthFirstQueue.size()]; + Coords[] arr = this.breadthFirstQueue.toArray(new Coords[0]); + + for (int i = 0; i < this.breadthFirstQueue.size(); i++) { + x[i] = arr[i].x; + y[i] = arr[i].y; + z[i] = arr[i].z; } - public void setCalled(int called) { - this.called = called; - } + tag.setIntArray("queueX", x); + tag.setIntArray("queueY", y); + tag.setIntArray("queueZ", z); + } - public synchronized List<Coords> getHashset() { - return this.hashset; + @Override + public void readFromNBT(NBTTagCompound tag) { + super.readFromNBT(tag); + this.wasInited = tag.getBoolean("init"); + int[] x = tag.getIntArray("queueX"); + int[] y = tag.getIntArray("queueY"); + int[] z = tag.getIntArray("queueZ"); + for (int i = 0; i < x.length; i++) { + this.breadthFirstQueue.add(new Coords(x[i], y[i], z[i])); } + } - public byte check_sourroundings(World w, int x, int y, int z, Block b) { - byte ret = 0; - int wID = w.provider.dimensionId; - - if (this.hashset.contains(new Coords(x, y, z, wID))) - return ret; - - this.hashset.add(new Coords(x, y, z, wID)); - - if (w.getBlock(x, y + 1, z).equals(b)) - ret = (byte) (ret | 0b000001); - - if (w.getBlock(x, y - 1, z).equals(b)) - ret = (byte) (ret | 0b000010); - - if (w.getBlock(x + 1, y, z).equals(b)) - ret = (byte) (ret | 0b000100); - - if (w.getBlock(x - 1, y, z).equals(b)) - ret = (byte) (ret | 0b001000); - - if (w.getBlock(x, y, z + 1).equals(b)) - ret = (byte) (ret | 0b010000); - - if (w.getBlock(x, y, z - 1).equals(b)) - ret = (byte) (ret | 0b100000); + private void setFluidBlock(Coords current) { + if (!checkForAir(current)) + return; + if (this.tank.drain(1000, false) == null || this.tank.drain(1000, false).amount != 1000) + return; + FluidStack stack = this.tank.drain(1000, true); + worldObj.setBlock(current.x, current.y, current.z, stack.getFluid().getBlock(), 0, 2); + } - return ret; - } + private void addBlockToQueue(Coords current, ForgeDirection allowed_direction) { + Coords side = current.getCoordsFromSide(allowed_direction); + if (checkForAir(side)) + breadthFirstQueue.add(side); + } - public int get_connected(World w, int x, int y, int z, Block b, int iterations) { - - if (iterations >= 5000) - return -1; - int tail; - int ret = 0; - iterations++; - int wID = w.provider.dimensionId; - byte sides = this.check_sourroundings(w, x, y, z, b); - - if (((sides | 0b111110) == 0b111111) && !this.hashset.contains(new Coords(x, y + 1, z, wID)) && y + 1 <= BW_TileEntity_ExperimentalFloodGate.this.yCoord) { - tail = this.get_connected(w, x, y + 1, z, b, iterations); - if (tail == -1) - return -1; - ret++; - ret += tail; - } - - if (((sides | 0b111101) == 0b111111) && !this.hashset.contains(new Coords(x, y - 1, z, wID))) { - tail = this.get_connected(w, x, y - 1, z, b, iterations); - if (tail == -1) - return -1; - ret++; - ret += tail; - } - - if (((sides | 0b111011) == 0b111111) && !this.hashset.contains(new Coords(x + 1, y, z, wID))) { - tail = this.get_connected(w, x + 1, y, z, b, iterations); - if (tail == -1) - return -1; - ret++; - ret += tail; - } - - if (((sides | 0b110111) == 0b111111) && !this.hashset.contains(new Coords(x - 1, y, z, wID))) { - tail = this.get_connected(w, x - 1, y, z, b, iterations); - if (tail == -1) - return -1; - ret++; - ret += tail; - } - - if (((sides | 0b101111) == 0b111111) && !this.hashset.contains(new Coords(x, y, z + 1, wID))) { - tail = this.get_connected(w, x, y, z + 1, b, iterations); - if (tail == -1) - return -1; - ret++; - ret += tail; - - } - - if (((sides | 0b011111) == 0b111111) && !this.hashset.contains(new Coords(x, y, z - 1, wID))) { - tail = this.get_connected(w, x, y, z - 1, b, iterations); - if (tail == -1) - return -1; - ret++; - ret += tail; - } - - return ret; - } + private boolean checkForAir(Coords coords) { + return this.worldObj.isAirBlock(coords.x, coords.y, coords.z); + } - @Override - public synchronized void run() { - this.called = BW_TileEntity_ExperimentalFloodGate.this.check.get_connected(BW_TileEntity_ExperimentalFloodGate.this.worldObj, BW_TileEntity_ExperimentalFloodGate.this.paused.x, BW_TileEntity_ExperimentalFloodGate.this.paused.y, BW_TileEntity_ExperimentalFloodGate.this.paused.z, Blocks.air, 0); - this.notifyAll(); - } + @Override + public String[] getInfoData() { + return new String[]{"Experimental Machine to fill Holes with Fluids"}; } -} +}
\ No newline at end of file diff --git a/src/main/java/com/github/bartimaeusnek/bartworks/util/Coords.java b/src/main/java/com/github/bartimaeusnek/bartworks/util/Coords.java index 4a3ee25235..ef213ea16b 100644 --- a/src/main/java/com/github/bartimaeusnek/bartworks/util/Coords.java +++ b/src/main/java/com/github/bartimaeusnek/bartworks/util/Coords.java @@ -22,6 +22,8 @@ package com.github.bartimaeusnek.bartworks.util; +import net.minecraftforge.common.util.ForgeDirection; + public class Coords { public int x, z, wID; @@ -39,6 +41,25 @@ public class Coords { this.wID = 0; } + public Coords getCoordsFromSide(ForgeDirection direction) { + switch (direction) { + case UP: + return new Coords(this.x, this.y + 1, this.z, this.wID); + case DOWN: + return new Coords(this.x, this.y - 1, this.z, this.wID); + case WEST: + return new Coords(this.x - 1, this.y, this.z, this.wID); + case EAST: + return new Coords(this.x + 1, this.y, this.z, this.wID); + case NORTH: + return new Coords(this.x, this.y, this.z - 1, this.wID); + case SOUTH: + return new Coords(this.x, this.y, this.z + 1, this.wID); + default: + throw new UnsupportedOperationException("This is impossible."); + } + } + @Override public boolean equals(Object o) { if (this == o) |