Skip to content

Commit

Permalink
crystal growth chamber somewhat functional now
Browse files Browse the repository at this point in the history
  • Loading branch information
ProjectET committed Dec 12, 2023
1 parent 5d493ac commit 95038b3
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 77 deletions.
9 changes: 6 additions & 3 deletions src/main/java/io/github/projectet/ae2things/AE2Things.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import io.github.projectet.ae2things.gui.cell.DISKItemCellGuiHandler;
import io.github.projectet.ae2things.gui.crystalGrowth.CrystalGrowthMenu;
import io.github.projectet.ae2things.item.AETItems;
import io.github.projectet.ae2things.recipe.CrystalGrowthRecipe;
import io.github.projectet.ae2things.recipe.CrystalGrowthRecipeSerializer;
import io.github.projectet.ae2things.storage.DISKCellHandler;
import io.github.projectet.ae2things.util.StorageManager;
import net.fabricmc.fabric.api.client.itemgroup.FabricItemGroupBuilder;
Expand Down Expand Up @@ -77,11 +79,12 @@ public void onAe2Initialized() {
Upgrades.add(AEItems.SPEED_CARD, CRYSTAL_GROWTH, 3);
Upgrades.add(AETItems.FORTUNE_CARD, CRYSTAL_GROWTH, 3);

ServerTickEvents.START_WORLD_TICK.register((world -> {
STORAGE_INSTANCE = StorageManager.getInstance(world.getServer());
}));
ServerTickEvents.START_WORLD_TICK.register((world ->
STORAGE_INSTANCE = StorageManager.getInstance(world.getServer())));

Registry.register(Registry.MENU, AppEng.makeId("advanced_inscriber"),AdvancedInscriberMenu.ADVANCED_INSCRIBER_SHT);
Registry.register(Registry.MENU, AppEng.makeId("crystal_growth"),CrystalGrowthMenu.CRYSTAL_GROWTH_SHT);

Registry.register(Registry.RECIPE_SERIALIZER, CrystalGrowthRecipe.TYPE_ID, CrystalGrowthRecipeSerializer.INSTANCE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public class BECrystalGrowth extends AENetworkPowerBlockEntity implements IGridT

private final CombinedInternalInventory combInv = new CombinedInternalInventory(topRow, midRow, botRow);

private final Map<AppEngInternalInventory, Integer> progress = new IdentityHashMap<>(Map.of(topRow, 0, midRow, 0, botRow, 0));
private final Map<AppEngInternalInventory, CrystalGrowthRecipe> cachedRecipes = new WeakHashMap<>();
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 IUpgradeInventory upgrades;

Expand All @@ -74,7 +74,7 @@ public BECrystalGrowth(BlockPos pos, BlockState state) {

@Override
public TickingRequest getTickingRequest(IGridNode node) {
return new TickingRequest(1, 20, !this.hasWork() && combInv.isEmpty(), true);
return new TickingRequest(1, 20, this.combInv.isEmpty() || !this.hasWork(), true);
}

public int getTopProg() {
Expand All @@ -94,27 +94,20 @@ public boolean isWorking() {
}

public boolean hasWork() {
return hasFluixIngredients() || !cachedGrowable.isEmpty();
return cachedRecipes.size() > 0;
}

private boolean hasFluixIngredients() {
boolean hasRedstone = combInv.simulateRemove(1, new ItemStack(Items.REDSTONE), null).getCount() == 1;
boolean hasChargedCertus = combInv.simulateRemove(1, new ItemStack(AEItems.CERTUS_QUARTZ_CRYSTAL_CHARGED.asItem()), null).getCount() == 1;
boolean hasQuartz = combInv.simulateRemove(1, new ItemStack(Items.QUARTZ), null).getCount() == 1;

return hasRedstone && hasChargedCertus && hasQuartz;

}

private ItemStack multiplyYield(ItemStack output) {
output.setCount(output.getCount() * this.upgrades.getInstalledUpgrades(AETItems.FORTUNE_CARD));
return output;
private ItemStack multiplyYield(ItemStack stack) {
ItemStack copy = stack.copy();
copy.grow(this.upgrades.getInstalledUpgrades(AETItems.FORTUNE_CARD));
return copy;
}

private boolean outputIsEmpty() {
for (AppEngInternalInventory inv: progress.keySet()) {
if(inv.getStackInSlot(3) != ItemStack.EMPTY)
for (InternalInventory inv: cachedRecipes.keySet()) {
if(inv.getStackInSlot(3).getItem() != Items.AIR) {
return false;
}
}
return true;
}
Expand Down Expand Up @@ -152,6 +145,7 @@ 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,17 +154,19 @@ public TickRateModulation tickingRequest(IGridNode node, int ticksSinceLastCall)
if(inventory.insertItem(3, resultItem, true).getCount() != 0)
return;
int i = 2;
while(inventory.getStackInSlot(i) == ItemStack.EMPTY && i > 0) {
while(inventory.getStackInSlot(i).getItem() == Items.AIR && i >= 1) {
i--;
}
ItemStack stack = inventory.getStackInSlot(i);
if(stack != ItemStack.EMPTY) {
if(stack.getItem() != Items.AIR) {
Item item = recipe.nextStage(stack);
if(r.nextInt(15) == 0) {
inventory.extractItem(i, 1, false);
inventory.getStackInSlot(i).shrink(1);
if(item != Items.AIR && i != 2) {
inventory.setItemDirect(i + 1, new ItemStack(item));
}
else
inventory.getStackInSlot(i + 1).grow(1);
}
}
inventory.insertItem(3, resultItem, false);
Expand Down Expand Up @@ -215,6 +211,8 @@ public TickRateModulation tickingRequest(IGridNode node, int ticksSinceLastCall)
}
}
}

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

Expand All @@ -233,6 +231,17 @@ public void onReady() {
protected boolean readFromStream(FriendlyByteBuf data) {
var c = super.readFromStream(data);

var oldWorking = isWorking();
var newWorking = data.readBoolean();

if (oldWorking != newWorking && newWorking) {
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 @@ -244,6 +253,12 @@ protected boolean readFromStream(FriendlyByteBuf data) {
protected void writeToStream(FriendlyByteBuf data) {
super.writeToStream(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 Down Expand Up @@ -298,14 +313,38 @@ public InternalInventory getExposedInventoryForSide(Direction side) {

@Override
public void onChangeInventory(InternalInventory inv, int slot) {
CrystalGrowthRecipe recipe = CrystalGrowthRecipe.getRecipefromStack(this.level, inv.getStackInSlot(slot));
if(cachedRecipes.get(inv) != recipe && recipe != null) {
cachedRecipes.put(inv, recipe);
}
else if(!hasIngredients(inv)) {
cachedRecipes.remove(inv);
progress.put(inv, 0);
}

if (!this.isWorking()) {
this.markForUpdate();
}

//if(inv.getStackInSlot(slot).getItem() instanceof IGrowableCrystal) {
// cachedGrowable.add(slot);
//}
this.markForUpdate();
getMainNode().ifPresent((grid, node) -> grid.getTickManager().wakeDevice(node));
}

public boolean hasIngredients(InternalInventory inv) {
for(int i = 0; i < 3; i++) {
if(inv.getStackInSlot(i).getItem() != Items.AIR)
return true;
}
return false;
}

public void matchWork() {
if(isWorking() != hasWork()) {
isWorking = hasWork();
this.markForUpdate();
}
}

@Override
public IUpgradeInventory getUpgrades() {
return upgrades;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package io.github.projectet.ae2things.gui.crystalGrowth;

import appeng.api.inventories.InternalInventory;
import appeng.blockentity.misc.InscriberRecipes;
import appeng.core.definitions.AEItems;
import appeng.core.definitions.ItemDefinition;
import appeng.menu.SlotSemantics;
import appeng.menu.guisync.GuiSync;
import appeng.menu.implementations.MenuTypeBuilder;
import appeng.menu.implementations.UpgradeableMenu;
import appeng.menu.interfaces.IProgressProvider;
import io.github.projectet.ae2things.AE2Things;
import io.github.projectet.ae2things.block.entity.BECrystalGrowth;
import io.github.projectet.ae2things.inventory.CrystalGrowthSlot;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;

public class CrystalGrowthMenu extends UpgradeableMenu<BECrystalGrowth> {
Expand All @@ -19,50 +26,49 @@ public class CrystalGrowthMenu extends UpgradeableMenu<BECrystalGrowth> {
private final InternalInventory inventory;
private final Level world;

public final IProgressProvider topRow;
public final IProgressProvider midRow;
public final IProgressProvider botRow;
@GuiSync(2)
public int topRowProg = -1;
@GuiSync(3)
public int midRowProg = -1;
@GuiSync(4)
public int botRowProg = -1;

public final IProgressProvider topRow = new IProgressProvider() {
@Override
public int getCurrentProgress() {
return topRowProg;
}

public CrystalGrowthMenu(int id, Inventory ip, BECrystalGrowth crystalGrowth) {
super(CRYSTAL_GROWTH_SHT, id, ip, crystalGrowth);

topRow = new IProgressProvider() {
@Override
public int getCurrentProgress() {
return crystalGrowth.getTopProg();
}

@Override
public int getMaxProgress() {
return 100;
}
};

midRow = new IProgressProvider() {
@Override
public int getCurrentProgress() {
return crystalGrowth.getMidProg();
}

@Override
public int getMaxProgress() {
return 100;
}
};
@Override
public int getMaxProgress() {
return 100;
}
};
public final IProgressProvider midRow = new IProgressProvider() {
@Override
public int getCurrentProgress() {
return midRowProg;
}

botRow = new IProgressProvider() {
@Override
public int getCurrentProgress() {
return crystalGrowth.getBotProg();
}
@Override
public int getMaxProgress() {
return 100;
}
};
public final IProgressProvider botRow = new IProgressProvider() {
@Override
public int getCurrentProgress() {
return botRowProg;
}

@Override
public int getMaxProgress() {
return 100;
}
};
@Override
public int getMaxProgress() {
return 100;
}
};

public CrystalGrowthMenu(int id, Inventory ip, BECrystalGrowth crystalGrowth) {
super(CRYSTAL_GROWTH_SHT, id, ip, crystalGrowth);
world = ip.player.level;
inventory = crystalGrowth.getInternalInventory();
int i = 0;
Expand All @@ -76,4 +82,52 @@ public int getMaxProgress() {
}
}
}

/* @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()) {
topRowProg = getHost().getTopProg();
midRowProg = getHost().getMidProg();
botRowProg = getHost().getBotProg();
}
super.standardDetectAndSendChanges();
}

@Override
public boolean stillValid(Player PlayerEntity) {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ public static CrystalGrowthRecipe getRecipefromStack(Level level, ItemStack item
CrystalGrowthRecipe matchedRecipe = null;
for (CrystalGrowthRecipe recipe : getRecipes(level)) {
for(Ingredient ingredient : recipe.getIngredients()) {
if(ingredient.test(item))
if(ingredient.test(item)) {
matchedRecipe = recipe;
break;
}
}
if(matchedRecipe != null)
break;
Expand Down Expand Up @@ -108,7 +110,12 @@ public ItemStack getResultItem() {

@Override
public NonNullList<Ingredient> getIngredients() {
return NonNullList.of(flawlessCrystal, flawedCrystal, chippedCrystal, damagedCrystal);
NonNullList<Ingredient> ingredients = NonNullList.create();
ingredients.add(this.flawlessCrystal);
ingredients.add(this.flawedCrystal);
ingredients.add(this.chippedCrystal);
ingredients.add(this.damagedCrystal);
return ingredients;
}

@Override
Expand All @@ -125,4 +132,9 @@ public RecipeSerializer<?> getSerializer() {
public RecipeType<?> getType() {
return TYPE;
}

@Override
public boolean isSpecial() {
return true;
}
}
Loading

0 comments on commit 95038b3

Please sign in to comment.