Skip to content

Commit

Permalink
Crystal growth chamber now functional
Browse files Browse the repository at this point in the history
  • Loading branch information
ProjectET committed Dec 18, 2023
1 parent 95038b3 commit 22b53ce
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 116 deletions.
16 changes: 12 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ dependencies {
mappings loom.officialMojangMappings()
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

modLocalRuntime("dev.architectury:architectury:${project.arch_version}")

// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

Expand All @@ -63,13 +61,23 @@ dependencies {
exclude(group: "net.fabricmc.fabric-api")
}

modCompileOnly("me.shedaniel:RoughlyEnoughItems-fabric:${project.rei_version}")
modCompileOnly("me.shedaniel:RoughlyEnoughItems-api-fabric:${project.rei_version}") {
exclude group: "net.fabricmc.fabric-api"
exclude group: "org.yaml" // snakeyaml
exclude group: "blue.endless" // jankson
}
modCompileOnly("me.shedaniel:RoughlyEnoughItems-default-plugin-fabric:${project.rei_version}") {
exclude group: "net.fabricmc.fabric-api"
exclude group: "org.yaml" // snakeyaml
exclude group: "blue.endless" // jankson
}

modCompileOnly("mcp.mobius.waila:wthit-api:fabric-${project.wthit_version}") {
exclude group: "net.fabricmc.fabric-api"
}

modLocalRuntime("teamreborn:energy:${project.tr_energy_version}") {
modLocalRuntime "dev.architectury:architectury-fabric:6.2.43"
modLocalRuntime modCompileOnly("me.shedaniel:RoughlyEnoughItems-fabric:${project.rei_version}") {
exclude group: "net.fabricmc.fabric-api"
}

Expand Down
7 changes: 3 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ archives_base_name=AE2Things
fabric_version=0.76.1+1.19.2
ae2_version=12.9.8
cloth_config_version=8.3.103
arch_version=6.5.85
rei_version=9.1.668
wthit_version=5.9.0
tr_energy_version=2.0.0-beta1
arch_version=6.2.43
rei_version=9.1.530
wthit_version=5.9.0
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
import io.github.projectet.ae2things.recipe.CrystalGrowthRecipe;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.Registry;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.crafting.RecipeManager;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable;
Expand All @@ -51,14 +53,16 @@ public class BECrystalGrowth extends AENetworkPowerBlockEntity implements IGridT
private final CombinedInternalInventory combInv = new CombinedInternalInventory(topRow, midRow, botRow);

private final Map<InternalInventory, Integer> progress = new IdentityHashMap<>(Map.of(topRow, 0, midRow, 0, botRow, 0));
private final Map<InternalInventory, CrystalGrowthRecipe> cachedRecipes = new WeakHashMap<>();
private final Map<InternalInventory, CrystalGrowthRecipe> cachedRecipes = new IdentityHashMap<>();

private final Set<InternalInventory> toRemove = new HashSet<>();

private boolean hasInitialised = false;

private IUpgradeInventory upgrades;

private boolean isWorking;

private final Set<Integer> cachedGrowable = new HashSet<>();

public BECrystalGrowth(BlockPos pos, BlockState state) {
super(AE2Things.CRYSTAL_GROWTH_BE, pos, state);
upgrades = UpgradeInventories.forMachine(AE2Things.CRYSTAL_GROWTH, 3, this::saveChanges);
Expand Down Expand Up @@ -94,7 +98,11 @@ public boolean isWorking() {
}

public boolean hasWork() {
return cachedRecipes.size() > 0;
if(!hasInitialised) {
initCache();
hasInitialised = true;
}
return !cachedRecipes.isEmpty();
}

private ItemStack multiplyYield(ItemStack stack) {
Expand All @@ -104,7 +112,7 @@ private ItemStack multiplyYield(ItemStack stack) {
}

private boolean outputIsEmpty() {
for (InternalInventory inv: cachedRecipes.keySet()) {
for (InternalInventory inv: progress.keySet()) {
if(inv.getStackInSlot(3).getItem() != Items.AIR) {
return false;
}
Expand Down Expand Up @@ -145,7 +153,6 @@ public TickRateModulation tickingRequest(IGridNode node, int ticksSinceLastCall)
}
return TickRateModulation.IDLE;
}
//TODO: FIX THIS
cachedRecipes.forEach((inventory, recipe) -> {
if(recipe != null) {
int d = progress.get(inventory);
Expand All @@ -160,7 +167,7 @@ public TickRateModulation tickingRequest(IGridNode node, int ticksSinceLastCall)
ItemStack stack = inventory.getStackInSlot(i);
if(stack.getItem() != Items.AIR) {
Item item = recipe.nextStage(stack);
if(r.nextInt(15) == 0) {
if(r.nextInt(15) == 0 && !recipe.isFlawless(stack)) {
inventory.getStackInSlot(i).shrink(1);
if(item != Items.AIR && i != 2) {
inventory.setItemDirect(i + 1, new ItemStack(item));
Expand All @@ -171,49 +178,27 @@ public TickRateModulation tickingRequest(IGridNode node, int ticksSinceLastCall)
}
inventory.insertItem(3, resultItem, false);
progress.put(inventory, 0);
saveChanges();
}
else {
progress.put(inventory, d + speedFactor);
}
}
});
/*
for (Integer slot : cachedGrowable.stream().toList()) {
ItemStack crystal = inventory.getStackInSlot(slot);
if (!(crystal.getItem() instanceof IGrowableCrystal)) {
cachedGrowable.remove(slot);
continue;
}
inventory.setItemDirect(slot, triggerGrowth(crystal, 20, speedFactor));
this.saveChanges();
}
if (hasFluixIngredients()) {
inventory.removeItems(1, new ItemStack(Items.REDSTONE), null);
inventory.removeItems(1, new ItemStack(AEItems.CERTUS_QUARTZ_CRYSTAL_CHARGED.asItem()), null);
inventory.removeItems(1, new ItemStack(Items.QUARTZ), null);
inventory.addItems(new ItemStack(AEItems.FLUIX_DUST, 2));
this.saveChanges();
}
if (cachedGrowable.isEmpty() && !hasFluixIngredients()) {
isWorking = false;
markForUpdate();
}*/
}
if (!outputIsEmpty()) {
MEStorage gridStorage = getMainNode().getGrid().getStorageService().getInventory();
for (ItemStack stack: combInv) {
if (stack.equals(ItemStack.EMPTY) || stack.getItem().equals(Items.AIR))
for (InternalInventory inv: progress.keySet()) {
if (inv.getStackInSlot(3).isEmpty())
continue;
if (!FilteredInventory.canTransfer(stack.getItem())) {
AEItemKey item = AEItemKey.of(stack);
long inserted = gridStorage.insert(item, stack.getCount(), Actionable.MODULATE, new MachineSource(this));
stack.shrink((int) inserted);
}
AEItemKey item = AEItemKey.of(inv.getStackInSlot(3));
long inserted = gridStorage.insert(item, inv.getStackInSlot(3).getCount(), Actionable.MODULATE, new MachineSource(this));
inv.getStackInSlot(3).shrink((int) inserted);
}
}

clearEmptyCache();
matchWork();
return hasWork() ? TickRateModulation.URGENT : !combInv.isEmpty() ? TickRateModulation.SLOWER : TickRateModulation.SLEEP;
return hasWork() ? TickRateModulation.URGENT : !outputIsEmpty() ? TickRateModulation.SLOWER : TickRateModulation.SLEEP;
}

@Override
Expand All @@ -227,6 +212,26 @@ public void onReady() {
super.onReady();
}

private void clearEmptyCache() {
for (InternalInventory inv: toRemove) {
cachedRecipes.remove(inv);
}
toRemove.clear();
}

private void initCache() {
for (InternalInventory inv: progress.keySet()) {
if(hasIngredients(inv)) {
for (ItemStack stack: inv) {
if(stack.isEmpty())
continue;
cachedRecipes.put(inv, CrystalGrowthRecipe.getRecipefromStack(getLevel(), stack));
break;
}
}
}
}

@Override
protected boolean readFromStream(FriendlyByteBuf data) {
var c = super.readFromStream(data);
Expand All @@ -238,10 +243,6 @@ protected boolean readFromStream(FriendlyByteBuf data) {
isWorking = true;
}

progress.put(topRow, data.readInt());
progress.put(midRow, data.readInt());
progress.put(botRow, data.readInt());

for (int i = 0; i < this.combInv.size(); i++) {
this.combInv.setItemDirect(i, data.readItem());
}
Expand All @@ -255,10 +256,6 @@ protected void writeToStream(FriendlyByteBuf data) {

data.writeBoolean(isWorking());

data.writeInt(getTopProg());
data.writeInt(getMidProg());
data.writeInt(getBotProg());

for (int i = 0; i < this.combInv.size(); i++) {
data.writeItem(combInv.getStackInSlot(i));
}
Expand All @@ -280,15 +277,13 @@ public InternalInventory getSubInventory(ResourceLocation id) {
public void saveAdditional(CompoundTag data) {
super.saveAdditional(data);
this.upgrades.writeToNBT(data, "upgrades");
data.putIntArray("cache", this.cachedGrowable.stream().toList());
data.putBoolean("working", isWorking);
}

@Override
public void loadTag(CompoundTag data) {
super.loadTag(data);
this.upgrades.readFromNBT(data, "upgrades");
this.cachedGrowable.addAll(Arrays.stream(data.getIntArray("cache")).boxed().toList());
this.isWorking = data.getBoolean("working");
}

Expand Down Expand Up @@ -318,7 +313,7 @@ public void onChangeInventory(InternalInventory inv, int slot) {
cachedRecipes.put(inv, recipe);
}
else if(!hasIngredients(inv)) {
cachedRecipes.remove(inv);
toRemove.add(inv);
progress.put(inv, 0);
}

Expand All @@ -331,7 +326,7 @@ else if(!hasIngredients(inv)) {
}

public boolean hasIngredients(InternalInventory inv) {
for(int i = 0; i < 3; i++) {
for(int i = 0; i <= 3; i++) {
if(inv.getStackInSlot(i).getItem() != Items.AIR)
return true;
}
Expand All @@ -351,19 +346,31 @@ public IUpgradeInventory getUpgrades() {
}

public class FilteredInventory implements IAEItemFilter {
public static boolean canTransfer(Item item) {
return true;
//return CrystalGrowthSlot.validItems.contains(item) || (item instanceof CrystalSeedItem);
}

@Override
public boolean allowExtract(InternalInventory inv, int slot, int amount) {
return slot == 3;
}

@Override
public boolean allowInsert(InternalInventory inv, int slot, ItemStack stack) {
return !(slot == 3) && CrystalGrowthRecipe.getRecipefromStack(getLevel(), stack) != null;
CrystalGrowthRecipe recipe = CrystalGrowthRecipe.getRecipefromStack(getLevel(), stack);
if(recipe == null) {
return false;
}
switch(slot) {
case 0 -> {
return recipe.getFlawlessCrystal().test(stack) || recipe.getFlawedCrystal().test(stack);
}
case 1 -> {
return recipe.getChippedCrystal().test(stack);
}
case 2 -> {
return recipe.getDamagedCrystal().test(stack);
}
default -> {
return false;
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.github.projectet.ae2things.AE2Things;
import io.github.projectet.ae2things.block.entity.BECrystalGrowth;
import io.github.projectet.ae2things.inventory.CrystalGrowthSlot;
import io.github.projectet.ae2things.recipe.CrystalGrowthRecipe;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.MenuType;
Expand All @@ -21,17 +22,17 @@

public class CrystalGrowthMenu extends UpgradeableMenu<BECrystalGrowth> {

public static MenuType<CrystalGrowthMenu> CRYSTAL_GROWTH_SHT = MenuTypeBuilder.create(CrystalGrowthMenu::new, BECrystalGrowth.class).build("crystal_growth");;
public static MenuType<CrystalGrowthMenu> CRYSTAL_GROWTH_SHT = MenuTypeBuilder.create(CrystalGrowthMenu::new, BECrystalGrowth.class).build("crystal_growth");

private final InternalInventory inventory;
private final Level world;

@GuiSync(2)
public int topRowProg = -1;
public int topRowProg = 0;
@GuiSync(3)
public int midRowProg = -1;
public int midRowProg = 0;
@GuiSync(4)
public int botRowProg = -1;
public int botRowProg = 0;

public final IProgressProvider topRow = new IProgressProvider() {
@Override
Expand Down Expand Up @@ -76,46 +77,13 @@ public CrystalGrowthMenu(int id, Inventory ip, BECrystalGrowth crystalGrowth) {
for(int x = 0; x < 4; x++) {
int xx = 26 + (x * 36);
int yy = 17 + (y * 18);
CrystalGrowthSlot slot = new CrystalGrowthSlot(inventory, i, xx, yy);
CrystalGrowthSlot slot = new CrystalGrowthSlot(inventory, i, xx, yy, x, world);
this.addSlot(slot, AE2Things.CG_SEMANTIC);
i++;
}
}
}

/* @Override
public boolean isValidForSlot(Slot s, ItemStack is) {
final ItemStack top = inventory.getStackInSlot(0);
final ItemStack bot = inventory.getStackInSlot(1);
if (s == this.middle) {
ItemDefinition<?> press = AEItems.NAME_PRESS;
if (press.isSameAs(top) || press.isSameAs(bot)) {
return !press.isSameAs(is);
}
return InscriberRecipes.findRecipe(world, is, top, bot, false) != null;
} else if (s == this.top && !bot.isEmpty() || s == this.bottom && !top.isEmpty()) {
ItemStack otherSlot;
if (s == this.top) {
otherSlot = this.bottom.getItem();
} else {
otherSlot = this.top.getItem();
}
// name presses
ItemDefinition<?> namePress = AEItems.NAME_PRESS;
if (namePress.isSameAs(otherSlot)) {
return namePress.isSameAs(is);
}
// everything else
// test for a partial recipe match (ignoring the middle slot)
return InscriberRecipes.isValidOptionalIngredientCombination(world, is, otherSlot);
}
return true;
}*/

@Override
protected void standardDetectAndSendChanges() {
if(isServerSide()) {
Expand Down
Loading

0 comments on commit 22b53ce

Please sign in to comment.