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

@ -10,6 +10,7 @@ import ru.betterend.effects.EndPotions;
import ru.betterend.recipe.AlloyingRecipes; import ru.betterend.recipe.AlloyingRecipes;
import ru.betterend.recipe.CraftingRecipes; import ru.betterend.recipe.CraftingRecipes;
import ru.betterend.recipe.FurnaceRecipes; import ru.betterend.recipe.FurnaceRecipes;
import ru.betterend.recipe.InfusionRecipes;
import ru.betterend.recipe.SmithingRecipes; import ru.betterend.recipe.SmithingRecipes;
import ru.betterend.registry.EndBiomes; import ru.betterend.registry.EndBiomes;
import ru.betterend.registry.EndBlockEntities; import ru.betterend.registry.EndBlockEntities;
@ -45,6 +46,7 @@ public class BetterEnd implements ModInitializer {
FurnaceRecipes.register(); FurnaceRecipes.register();
AlloyingRecipes.register(); AlloyingRecipes.register();
SmithingRecipes.register(); SmithingRecipes.register();
InfusionRecipes.register();
EndStructures.register(); EndStructures.register();
FabricLoader.getInstance().getEntrypoints("betterend", BetterEndPlugin.class).forEach(BetterEndPlugin::register); FabricLoader.getInstance().getEntrypoints("betterend", BetterEndPlugin.class).forEach(BetterEndPlugin::register);

View file

@ -16,6 +16,7 @@ import net.minecraft.world.BlockView;
import net.minecraft.world.World; import net.minecraft.world.World;
import ru.betterend.blocks.basis.BlockPedestal; import ru.betterend.blocks.basis.BlockPedestal;
import ru.betterend.blocks.entities.InfusionPedestalEntity; import ru.betterend.blocks.entities.InfusionPedestalEntity;
import ru.betterend.rituals.InfusionRitual;
public class InfusionPedestal extends BlockPedestal { public class InfusionPedestal extends BlockPedestal {
private static final VoxelShape SHAPE_DEFAULT; private static final VoxelShape SHAPE_DEFAULT;
@ -28,7 +29,28 @@ public class InfusionPedestal extends BlockPedestal {
@Override @Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
BlockEntity blockEntity = world.getBlockEntity(pos);
InfusionPedestalEntity pedestal = null;
if (blockEntity instanceof InfusionPedestalEntity) {
pedestal = (InfusionPedestalEntity) blockEntity;
if (!pedestal.isEmpty() && pedestal.hasRitual()) {
if (pedestal.getRitual().checkRecipe()) {
return ActionResult.SUCCESS;
}
}
}
ActionResult result = super.onUse(state, world, pos, player, hand, hit); ActionResult result = super.onUse(state, world, pos, player, hand, hit);
if (result == ActionResult.SUCCESS) {
if (pedestal != null) {
if (pedestal.hasRitual()) {
pedestal.getRitual().checkRecipe();
} else {
InfusionRitual ritual = new InfusionRitual(world, pos);
pedestal.linkRitual(ritual);
ritual.checkRecipe();
}
}
}
return result; return result;
} }

View file

@ -2,23 +2,57 @@ package ru.betterend.blocks.entities;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import ru.betterend.rituals.InfusionRitual; import ru.betterend.rituals.InfusionRitual;
public class InfusionPedestalEntity extends PedestalBlockEntity { public class InfusionPedestalEntity extends PedestalBlockEntity {
private InfusionRitual activeRitual; private InfusionRitual linkedRitual;
@Override
public void setLocation(World world, BlockPos pos) {
super.setLocation(world, pos);
if (hasRitual()) {
this.linkedRitual.setLocation(world, pos);
}
}
public void linkRitual(InfusionRitual ritual) {
this.linkedRitual = ritual;
}
public InfusionRitual getRitual() {
return this.linkedRitual;
}
public boolean hasRitual() { public boolean hasRitual() {
return this.activeRitual != null; return this.linkedRitual != null;
}
@Override
public void tick() {
if (hasRitual()) {
this.linkedRitual.tick();
}
super.tick();
} }
@Override @Override
public void fromTag(BlockState state, CompoundTag tag) { public void fromTag(BlockState state, CompoundTag tag) {
if (tag.contains("ritual")) {
this.linkedRitual = new InfusionRitual(world, pos);
this.linkedRitual.fromTag(tag.getCompound("ritual"));
}
super.fromTag(state, tag); super.fromTag(state, tag);
} }
@Override @Override
public CompoundTag toTag(CompoundTag tag) { public CompoundTag toTag(CompoundTag tag) {
if (hasRitual()) {
tag.put("ritual", linkedRitual.toTag(new CompoundTag()));
}
return super.toTag(tag); return super.toTag(tag);
} }
} }

View file

@ -0,0 +1,18 @@
package ru.betterend.recipe;
import ru.betterend.recipe.builders.InfusionRecipe;
import ru.betterend.registry.EndBlocks;
import ru.betterend.registry.EndItems;
public class InfusionRecipes {
public static void register() {
InfusionRecipe.Builder.create("runed_flavolite")
.setInput(EndBlocks.FLAVOLITE.polished)
.setOutput(EndBlocks.FLAVOLITE_RUNED)
.addCatalyst(0, EndItems.CRYSTAL_SHARDS)
.addCatalyst(2, EndItems.CRYSTAL_SHARDS)
.addCatalyst(4, EndItems.CRYSTAL_SHARDS)
.addCatalyst(6, EndItems.CRYSTAL_SHARDS)
.build();
}
}

View file

@ -55,8 +55,8 @@ public class InfusionRecipe implements Recipe<InfusionRitual> {
public boolean matches(InfusionRitual inv, World world) { public boolean matches(InfusionRitual inv, World world) {
boolean valid = this.input.test(inv.getStack(0)); boolean valid = this.input.test(inv.getStack(0));
if (!valid) return false; if (!valid) return false;
for (int i = 1; i < 9; i++) { for (int i = 0; i < 8; i++) {
valid &= this.catalysts[i].test(inv.getStack(i)); valid &= this.catalysts[i].test(inv.getStack(i + 1));
} }
return valid; return valid;
} }
@ -91,7 +91,7 @@ public class InfusionRecipe implements Recipe<InfusionRitual> {
return TYPE; return TYPE;
} }
public InfusionRecipe fromTag(CompoundTag tag) { public static InfusionRecipe fromTag(CompoundTag tag) {
return SERIALIZER.fromTag(tag); return SERIALIZER.fromTag(tag);
} }
@ -132,8 +132,8 @@ public class InfusionRecipe implements Recipe<InfusionRitual> {
return this; return this;
} }
public Builder setOutput(ItemStack output) { public Builder setOutput(ItemConvertible output) {
this.output = output; this.output = new ItemStack(output);
this.output.setCount(1); this.output.setCount(1);
return this; return this;
} }

View file

@ -1,42 +1,114 @@
package ru.betterend.rituals; 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.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventory; import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.World; import net.minecraft.world.World;
import ru.betterend.blocks.entities.InfusionPedestalEntity;
import ru.betterend.blocks.entities.PedestalBlockEntity;
import ru.betterend.recipe.builders.InfusionRecipe; import ru.betterend.recipe.builders.InfusionRecipe;
public class InfusionRitual implements Inventory { 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 World world;
private final BlockPos worldPos; private BlockPos worldPos;
private InfusionRecipe activeRecipe; private InfusionRecipe activeRecipe;
private int progress = 0; private int progress = 0;
private int time = 0; private int time = 0;
private InfusionPedestalEntity input;
private PedestalBlockEntity[] catalysts = new PedestalBlockEntity[8];
public InfusionRitual(World world, BlockPos pos) { public InfusionRitual(World world, BlockPos pos) {
this.world = world; this.world = world;
this.worldPos = pos; 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() { public void tick() {
if (!hasRecipe()) return; if (!isValid() || !hasRecipe()) return;
this.progress++; this.progress++;
if (progress == time) { 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() { public boolean hasRecipe() {
return this.activeRecipe != null; return this.activeRecipe != null;
} }
public void setLocation(World world, BlockPos pos) {
this.world = world;
this.worldPos = pos;
this.configure();
}
@Override @Override
public void clear() { public void clear() {
// TODO if (!isValid()) return;
this.input.clear();
for (PedestalBlockEntity catalyst : catalysts) {
catalyst.clear();
}
} }
@Override @Override
@ -51,28 +123,41 @@ public class InfusionRitual implements Inventory {
@Override @Override
public ItemStack getStack(int slot) { 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 @Override
public ItemStack removeStack(int slot, int amount) { public ItemStack removeStack(int slot, int amount) {
return null; return this.removeStack(slot);
} }
@Override @Override
public ItemStack removeStack(int slot) { 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 @Override
public void setStack(int slot, ItemStack stack) { 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 @Override
public void markDirty() { public void markDirty() {}
// TODO
}
@Override @Override
public boolean canPlayerUse(PlayerEntity player) { public boolean canPlayerUse(PlayerEntity player) {
@ -80,9 +165,19 @@ public class InfusionRitual implements Inventory {
} }
public void fromTag(CompoundTag tag) { 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) { public CompoundTag toTag(CompoundTag tag) {
if (hasRecipe()) {
tag.put("recipe", activeRecipe.toTag(new CompoundTag()));
tag.putInt("progress", progress);
tag.putInt("time", time);
}
return tag; return tag;
} }
} }