Infusion fixes

This commit is contained in:
Aleksey 2021-06-09 17:59:40 +03:00
parent a6895004c7
commit 030ede58d3
4 changed files with 68 additions and 61 deletions

View file

@ -30,9 +30,13 @@ public class InfusionPedestal extends PedestalBlock {
if (blockEntity instanceof InfusionPedestalEntity) { if (blockEntity instanceof InfusionPedestalEntity) {
InfusionPedestalEntity pedestal = (InfusionPedestalEntity) blockEntity; InfusionPedestalEntity pedestal = (InfusionPedestalEntity) blockEntity;
if (pedestal.hasRitual()) { if (pedestal.hasRitual()) {
InfusionRitual ritual = pedestal.getRitual();
if (!ritual.isValid()) {
ritual.configure();
}
pedestal.getRitual().checkRecipe(); pedestal.getRitual().checkRecipe();
} else { } else {
InfusionRitual ritual = new InfusionRitual(world, pos); InfusionRitual ritual = new InfusionRitual(pedestal, world, pos);
pedestal.linkRitual(ritual); pedestal.linkRitual(ritual);
ritual.checkRecipe(); ritual.checkRecipe();
} }

View file

@ -19,6 +19,8 @@ public class InfusionPedestalEntity extends PedestalBlockEntity {
super.setLevelAndPosition(world, pos); super.setLevelAndPosition(world, pos);
if (hasRitual()) { if (hasRitual()) {
linkedRitual.setLocation(world, pos); linkedRitual.setLocation(world, pos);
} else {
linkRitual(new InfusionRitual(this, world, pos));
} }
} }
@ -54,7 +56,7 @@ public class InfusionPedestalEntity extends PedestalBlockEntity {
protected void fromTag(CompoundTag tag) { protected void fromTag(CompoundTag tag) {
super.fromTag(tag); super.fromTag(tag);
if (tag.contains("ritual")) { if (tag.contains("ritual")) {
linkedRitual = new InfusionRitual(level, worldPosition); linkedRitual = new InfusionRitual(this, level, worldPosition);
linkedRitual.fromTag(tag.getCompound("ritual")); linkedRitual.fromTag(tag.getCompound("ritual"));
} }
} }

View file

@ -127,13 +127,13 @@ public class InfusionRecipe implements Recipe<InfusionRitual>, BetterEndRecipe {
return INSTANCE; return INSTANCE;
} }
private final Ingredient[] catalysts = new Ingredient[8];
private ResourceLocation id; private ResourceLocation id;
private Ingredient input; private Ingredient input;
private ItemStack output; private ItemStack output;
private String group; private String group;
private int time = 1; private int time = 1;
private Ingredient[] catalysts = new Ingredient[8];
private Builder() { private Builder() {
Arrays.fill(catalysts, Ingredient.EMPTY); Arrays.fill(catalysts, Ingredient.EMPTY);
@ -212,23 +212,23 @@ public class InfusionRecipe implements Recipe<InfusionRitual>, BetterEndRecipe {
recipe.group = GsonHelper.getAsString(json, "group", GROUP); recipe.group = GsonHelper.getAsString(json, "group", GROUP);
recipe.time = GsonHelper.getAsInt(json, "time", 1); recipe.time = GsonHelper.getAsInt(json, "time", 1);
JsonObject catalysts = GsonHelper.convertToJsonObject(json, "catalysts"); JsonObject catalysts = GsonHelper.getAsJsonObject(json, "catalysts");
ItemStack catalyst = ItemUtil.fromStackString(GsonHelper.getAsString(catalysts, "north", "")); ItemStack catalyst = ItemUtil.fromStackString(GsonHelper.getAsString(catalysts, "north", ""));
recipe.catalysts[0] = Ingredient.of(Arrays.stream(new ItemStack[] { catalyst })); recipe.catalysts[0] = (catalyst != null && !catalyst.isEmpty()) ? Ingredient.of(catalyst.getItem()) : Ingredient.EMPTY;
catalyst = ItemUtil.fromStackString(GsonHelper.getAsString(catalysts, "north_east", "")); catalyst = ItemUtil.fromStackString(GsonHelper.getAsString(catalysts, "north_east", ""));
recipe.catalysts[1] = Ingredient.of(Arrays.stream(new ItemStack[] { catalyst })); recipe.catalysts[1] = (catalyst != null && !catalyst.isEmpty()) ? Ingredient.of(catalyst.getItem()) : Ingredient.EMPTY;
catalyst = ItemUtil.fromStackString(GsonHelper.getAsString(catalysts, "east", "")); catalyst = ItemUtil.fromStackString(GsonHelper.getAsString(catalysts, "east", ""));
recipe.catalysts[2] = Ingredient.of(Arrays.stream(new ItemStack[] { catalyst })); recipe.catalysts[2] = (catalyst != null && !catalyst.isEmpty()) ? Ingredient.of(catalyst.getItem()) : Ingredient.EMPTY;
catalyst = ItemUtil.fromStackString(GsonHelper.getAsString(catalysts, "south_east", "")); catalyst = ItemUtil.fromStackString(GsonHelper.getAsString(catalysts, "south_east", ""));
recipe.catalysts[3] = Ingredient.of(Arrays.stream(new ItemStack[] { catalyst })); recipe.catalysts[3] = (catalyst != null && !catalyst.isEmpty()) ? Ingredient.of(catalyst.getItem()) : Ingredient.EMPTY;
catalyst = ItemUtil.fromStackString(GsonHelper.getAsString(catalysts, "south", "")); catalyst = ItemUtil.fromStackString(GsonHelper.getAsString(catalysts, "south", ""));
recipe.catalysts[4] = Ingredient.of(Arrays.stream(new ItemStack[] { catalyst })); recipe.catalysts[4] = (catalyst != null && !catalyst.isEmpty()) ? Ingredient.of(catalyst.getItem()) : Ingredient.EMPTY;
catalyst = ItemUtil.fromStackString(GsonHelper.getAsString(catalysts, "south_west", "")); catalyst = ItemUtil.fromStackString(GsonHelper.getAsString(catalysts, "south_west", ""));
recipe.catalysts[5] = Ingredient.of(Arrays.stream(new ItemStack[] { catalyst })); recipe.catalysts[5] = (catalyst != null && !catalyst.isEmpty()) ? Ingredient.of(catalyst.getItem()) : Ingredient.EMPTY;
catalyst = ItemUtil.fromStackString(GsonHelper.getAsString(catalysts, "west", "")); catalyst = ItemUtil.fromStackString(GsonHelper.getAsString(catalysts, "west", ""));
recipe.catalysts[6] = Ingredient.of(Arrays.stream(new ItemStack[] { catalyst })); recipe.catalysts[6] = (catalyst != null && !catalyst.isEmpty()) ? Ingredient.of(catalyst.getItem()) : Ingredient.EMPTY;
catalyst = ItemUtil.fromStackString(GsonHelper.getAsString(catalysts, "north_west", "")); catalyst = ItemUtil.fromStackString(GsonHelper.getAsString(catalysts, "north_west", ""));
recipe.catalysts[7] = Ingredient.of(Arrays.stream(new ItemStack[] { catalyst })); recipe.catalysts[7] = (catalyst != null && !catalyst.isEmpty()) ? Ingredient.of(catalyst.getItem()) : Ingredient.EMPTY;
return recipe; return recipe;
} }

View file

@ -1,8 +1,7 @@
package ru.betterend.rituals; package ru.betterend.rituals;
import java.awt.Point;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.BlockPos.MutableBlockPos;
import net.minecraft.core.Direction; import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerLevel;
@ -16,10 +15,20 @@ import ru.betterend.blocks.entities.PedestalBlockEntity;
import ru.betterend.particle.InfusionParticleType; import ru.betterend.particle.InfusionParticleType;
import ru.betterend.recipe.builders.InfusionRecipe; import ru.betterend.recipe.builders.InfusionRecipe;
import java.awt.*;
import java.util.Arrays;
import java.util.Objects;
public class InfusionRitual implements Container { public class InfusionRitual implements Container {
private static final Point[] PEDESTALS_MAP = new Point[] { private static final Point[] PEDESTALS_MAP = new Point[] {
new Point(0, 3), new Point(2, 2), new Point(3, 0), new Point(2, -2), new Point(0, 3),
new Point(0, -3), new Point(-2, -2), new Point(-3, 0), new Point(-2, 2) 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 Level world; private Level world;
@ -29,35 +38,25 @@ public class InfusionRitual implements Container {
private boolean hasRecipe = false; private boolean hasRecipe = false;
private int progress = 0; private int progress = 0;
private int time = 0; private int time = 0;
private InfusionPedestalEntity input;
private final PedestalBlockEntity[] catalysts = new PedestalBlockEntity[8]; private final PedestalBlockEntity[] catalysts = new PedestalBlockEntity[8];
private final InfusionPedestalEntity input;
public InfusionRitual(Level world, BlockPos pos) { public InfusionRitual(InfusionPedestalEntity pedestal, Level world, BlockPos pos) {
this.input = pedestal;
this.world = world; this.world = world;
this.worldPos = pos; this.worldPos = pos;
this.configure(); configure();
}
public static Point[] getMap() {
return PEDESTALS_MAP;
} }
public void configure() { public void configure() {
if (world == null || world.isClientSide || worldPos == null) return; if (world == null || worldPos == null || world.isClientSide) return;
BlockEntity inputEntity = world.getBlockEntity(worldPos); for (int i = 0; i < catalysts.length; i++) {
if (inputEntity instanceof InfusionPedestalEntity) { Point point = PEDESTALS_MAP[i];
input = (InfusionPedestalEntity) inputEntity; MutableBlockPos checkPos = worldPos.mutable().move(Direction.EAST, point.x).move(Direction.NORTH, point.y);
}
int i = 0;
for(Point point : PEDESTALS_MAP) {
BlockPos.MutableBlockPos checkPos = worldPos.mutable().move(Direction.EAST, point.x).move(Direction.NORTH, point.y);
BlockEntity catalystEntity = world.getBlockEntity(checkPos); BlockEntity catalystEntity = world.getBlockEntity(checkPos);
if (catalystEntity instanceof PedestalBlockEntity) { if (catalystEntity instanceof PedestalBlockEntity) {
catalysts[i] = (PedestalBlockEntity) catalystEntity; catalysts[i] = (PedestalBlockEntity) catalystEntity;
i++;
} else {
break;
} }
} }
} }
@ -67,34 +66,36 @@ public class InfusionRitual implements Container {
InfusionRecipe recipe = world.getRecipeManager().getRecipeFor(InfusionRecipe.TYPE, this, world).orElse(null); InfusionRecipe recipe = world.getRecipeManager().getRecipeFor(InfusionRecipe.TYPE, this, world).orElse(null);
if (hasRecipe()) { if (hasRecipe()) {
if (recipe == null) { if (recipe == null) {
stop(); reset();
return false; return false;
} else if (recipe.getInfusionTime() != time) { } else if (activeRecipe == null || recipe.getInfusionTime() != time) {
activeRecipe = recipe; updateRecipe(recipe);
time = activeRecipe.getInfusionTime();
progress = 0;
setChanged();
} else if (activeRecipe == null) {
activeRecipe = recipe;
} }
return true; return true;
} }
if (recipe != null) { if (recipe != null) {
activeRecipe = recipe; updateRecipe(recipe);
time = activeRecipe.getInfusionTime();
hasRecipe = true;
progress = 0;
setChanged();
return true; return true;
} }
return false; return false;
} }
private void updateRecipe(InfusionRecipe recipe) {
activeRecipe = recipe;
hasRecipe = true;
resetTimer();
setChanged();
}
private void resetTimer() {
time = activeRecipe != null ? activeRecipe.getInfusionTime() : 0;
progress = 0;
}
public void stop() { public void reset() {
activeRecipe = null; activeRecipe = null;
hasRecipe = false; hasRecipe = false;
progress = 0; resetTimer();
time = 0;
setChanged(); setChanged();
} }
@ -103,7 +104,6 @@ public class InfusionRitual implements Container {
configure(); configure();
isDirty = false; isDirty = false;
} }
if (!isValid() || !hasRecipe()) return;
if (!checkRecipe()) return; if (!checkRecipe()) return;
progress++; progress++;
if (progress == time) { if (progress == time) {
@ -112,9 +112,9 @@ public class InfusionRitual implements Container {
for (PedestalBlockEntity catalyst : catalysts) { for (PedestalBlockEntity catalyst : catalysts) {
catalyst.removeItemNoUpdate(0); catalyst.removeItemNoUpdate(0);
} }
stop(); reset();
} else { } else {
ServerLevel world = (ServerLevel) this.world; ServerLevel serverLevel = (ServerLevel) world;
BlockPos target = worldPos.above(); BlockPos target = worldPos.above();
double tx = target.getX() + 0.5; double tx = target.getX() + 0.5;
double ty = target.getY() + 0.5; double ty = target.getY() + 0.5;
@ -126,11 +126,11 @@ public class InfusionRitual implements Container {
double sx = start.getX() + 0.5; double sx = start.getX() + 0.5;
double sy = start.getY() + 1.25; double sy = start.getY() + 1.25;
double sz = start.getZ() + 0.5; double sz = start.getZ() + 0.5;
world.sendParticles(new InfusionParticleType(stack), sx, sy, sz, 0, tx - sx, ty - sy, tz - sz, 0.5); serverLevel.sendParticles(new InfusionParticleType(stack), sx, sy, sz, 0, tx - sx, ty - sy, tz - sz, 0.5);
} }
} }
} }
} }
@Override @Override
@ -140,10 +140,7 @@ public class InfusionRitual implements Container {
public boolean isValid() { public boolean isValid() {
if (world == null || world.isClientSide || worldPos == null || input == null) return false; if (world == null || world.isClientSide || worldPos == null || input == null) return false;
for (PedestalBlockEntity catalyst : catalysts) { return Arrays.stream(catalysts).noneMatch(Objects::isNull);
if (catalyst == null) return false;
}
return true;
} }
public boolean hasRecipe() { public boolean hasRecipe() {
@ -241,4 +238,8 @@ public class InfusionRitual implements Container {
} }
return tag; return tag;
} }
public static Point[] getMap() {
return PEDESTALS_MAP;
}
} }