WIP: infusion

This commit is contained in:
Aleksey 2020-11-08 22:30:58 +03:00
parent a1d2e0d9bf
commit beab6ce10a
6 changed files with 191 additions and 20 deletions

View file

@ -1,42 +1,114 @@
package ru.betterend.rituals;
import net.minecraft.block.BlockState;
import java.awt.Point;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;
import ru.betterend.blocks.entities.InfusionPedestalEntity;
import ru.betterend.blocks.entities.PedestalBlockEntity;
import ru.betterend.recipe.builders.InfusionRecipe;
public class InfusionRitual implements Inventory {
private static Point[] pedestalsMap = new Point[] {
new Point(0, 3), new Point(2, 2), new Point(3, 0), new Point(2, -2),
new Point(0, -3), new Point(-2, -2), new Point(-3, 0), new Point(-2, 2)
};
private final World world;
private final BlockPos worldPos;
private World world;
private BlockPos worldPos;
private InfusionRecipe activeRecipe;
private int progress = 0;
private int time = 0;
private InfusionPedestalEntity input;
private PedestalBlockEntity[] catalysts = new PedestalBlockEntity[8];
public InfusionRitual(World world, BlockPos pos) {
this.world = world;
this.worldPos = pos;
this.configure();
}
public void configure() {
if (world == null || world.isClient || worldPos == null) return;
BlockEntity inputEntity = world.getBlockEntity(worldPos);
if (inputEntity instanceof InfusionPedestalEntity) {
this.input = (InfusionPedestalEntity) inputEntity;
}
int i = 0;
for(Point point : pedestalsMap) {
BlockPos.Mutable checkPos = worldPos.mutableCopy().move(Direction.EAST, point.x).move(Direction.NORTH, point.y);
BlockEntity catalystEntity = world.getBlockEntity(checkPos);
if (catalystEntity instanceof PedestalBlockEntity) {
catalysts[i] = (PedestalBlockEntity) catalystEntity;
i++;
} else {
break;
}
}
}
public boolean checkRecipe() {
if (!isValid()) return false;
this.activeRecipe = this.world.getRecipeManager().getFirstMatch(InfusionRecipe.TYPE, this, world).orElse(null);
if (activeRecipe != null) {
this.time = this.activeRecipe.getInfusionTime();
return true;
}
return false;
}
public void tick() {
if (!hasRecipe()) return;
if (!isValid() || !hasRecipe()) return;
this.progress++;
if (progress == time) {
this.input.setStack(0, activeRecipe.craft(this));
for (PedestalBlockEntity catalyst : catalysts) {
catalyst.clear();
}
this.activeRecipe = null;
this.progress = 0;
this.time = 0;
}
}
@Override
public boolean isValid(int slot, ItemStack stack) {
return this.isValid();
}
public boolean isValid() {
if (world == null || world.isClient || worldPos == null || input == null) return false;
for (PedestalBlockEntity catalyst : catalysts) {
if (catalyst == null) return false;
}
return true;
}
public boolean hasRecipe() {
return this.activeRecipe != null;
}
public void setLocation(World world, BlockPos pos) {
this.world = world;
this.worldPos = pos;
this.configure();
}
@Override
public void clear() {
// TODO
if (!isValid()) return;
this.input.clear();
for (PedestalBlockEntity catalyst : catalysts) {
catalyst.clear();
}
}
@Override
@ -51,28 +123,41 @@ public class InfusionRitual implements Inventory {
@Override
public ItemStack getStack(int slot) {
return null;
if (slot > 8) return ItemStack.EMPTY;
if (slot == 0) {
return this.input.getStack(0);
} else {
return this.catalysts[slot - 1].getStack(0);
}
}
@Override
public ItemStack removeStack(int slot, int amount) {
return null;
return this.removeStack(slot);
}
@Override
public ItemStack removeStack(int slot) {
return null;
if (slot > 8) return ItemStack.EMPTY;
if (slot == 0) {
return this.input.removeStack(0);
} else {
return this.catalysts[slot - 1].getStack(0);
}
}
@Override
public void setStack(int slot, ItemStack stack) {
// TODO
if (slot > 8) return;
if (slot == 0) {
this.input.setStack(0, stack);
} else {
this.catalysts[slot - 1].setStack(0, stack);
}
}
@Override
public void markDirty() {
// TODO
}
public void markDirty() {}
@Override
public boolean canPlayerUse(PlayerEntity player) {
@ -80,9 +165,19 @@ public class InfusionRitual implements Inventory {
}
public void fromTag(CompoundTag tag) {
if (tag.contains("recipe")) {
this.activeRecipe = InfusionRecipe.fromTag(tag.getCompound("recipe"));
this.progress = tag.getInt("progress");
this.time = tag.getInt("time");
}
}
public CompoundTag toTag(CompoundTag tag) {
if (hasRecipe()) {
tag.put("recipe", activeRecipe.toTag(new CompoundTag()));
tag.putInt("progress", progress);
tag.putInt("time", time);
}
return tag;
}
}