Skip to content

Commit

Permalink
-Book has nicer pages now
Browse files Browse the repository at this point in the history
-Added mechanic to reverse engineer recipes
  • Loading branch information
Cassunshine committed Feb 26, 2024
1 parent 96ffeee commit 181482b
Show file tree
Hide file tree
Showing 20 changed files with 342 additions and 89 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cassunshine.thework.client.gui.ingame.notebook;

import cassunshine.thework.client.gui.ingame.notebook.pages.AlchemistNotebookPage;
import cassunshine.thework.client.networking.TheWorkClientNetworking;
import cassunshine.thework.rendering.items.AlchemistNotebookRenderer;
import net.minecraft.client.gui.DrawContext;
Expand Down Expand Up @@ -36,7 +37,7 @@ public void nextPage() {
var nbt = stack.getOrCreateNbt();
var page = nbt.getInt("current_page");

page = MathHelper.clamp(page + 1, 0, (pages.size() / 2));
page = MathHelper.clamp(page + 1, 0, MathHelper.ceil(pages.size() / 2.0f) - 1);

nbt.putInt("current_page", page);
syncNbt();
Expand Down Expand Up @@ -82,8 +83,8 @@ protected void init() {
int centerX = MathHelper.floor(width / 2.0f);
int centerY = MathHelper.floor(height / 2.0f);

int pageHeight = MathHelper.floor(height * 0.9f);
int pageWidth = MathHelper.floor(pageHeight * 0.5f);
int pageHeight = 64 * MathHelper.floor((height - 50) / 64.0f);
int pageWidth = MathHelper.floor(pageHeight * 0.75f);

int heightDifference = MathHelper.floor(height - pageHeight);

Expand Down Expand Up @@ -121,8 +122,8 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) {
int centerX = MathHelper.floor(width / 2.0f);
int centerY = MathHelper.floor(height / 2.0f);

int pageHeight = MathHelper.floor(height * 0.9f);
int pageWidth = MathHelper.floor(pageHeight * 0.5f);
int pageHeight = 64 * MathHelper.floor((height - 50) / 64.0f);
int pageWidth = MathHelper.floor(pageHeight * 0.75f);

int heightDifference = MathHelper.floor(height - pageHeight);

Expand All @@ -131,23 +132,23 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) {
AlchemistNotebookPage pageLeft = pages.get(currentPage);
AlchemistNotebookPage pageRight = currentPage + 1 >= pages.size() ? null : pages.get(currentPage + 1);

context.drawTexture(AlchemistNotebookRenderer.BOOK_TEXTURE, centerX - pageWidth, heightDifference / 2, pageWidth, pageHeight, 8, 16, 8, 16, 32, 32);
context.drawTexture(AlchemistNotebookRenderer.BOOK_TEXTURE, centerX, heightDifference / 2, pageWidth, pageHeight, 8, 16, 8, 16, 32, 32);
context.drawTexture(AlchemistNotebookRenderer.BOOK_TEXTURE, centerX - pageWidth, heightDifference / 2, pageWidth, pageHeight, 0, 0, 48, 64, 144, 64);
context.drawTexture(AlchemistNotebookRenderer.BOOK_TEXTURE, centerX, heightDifference / 2, pageWidth, pageHeight, 0, 0, 48, 64, 144, 64);


if (pageLeft != null && pageLeft.drawing != null) {
context.drawTexture(pageLeft.drawing, centerX - pageWidth, heightDifference / 2, pageWidth, pageHeight, 0, 0, 128, 256, 128, 256);
context.drawTexture(pageLeft.drawing, centerX - pageWidth, heightDifference / 2, pageWidth, pageHeight, 0, 0, 192, 256, 192, 256);
}

if (pageRight != null && pageRight.drawing != null) {
context.drawTexture(pageRight.drawing, centerX, heightDifference / 2, pageWidth, pageHeight, 0, 0, 128, 256, 128, 256);
context.drawTexture(pageRight.drawing, centerX, heightDifference / 2, pageWidth, pageHeight, 0, 0, 192, 256, 192, 256);
}

if (pageLeft != null)
pageLeft.render(this, context, mouseX, mouseY, delta);
if (pageRight != null)
pageRight.render(this, context, mouseX, mouseY, delta);


super.render(context, mouseX, mouseY, delta);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cassunshine.thework.client.gui.ingame.notebook.drawables;

import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.Drawable;
import net.minecraft.client.render.DiffuseLighting;
import net.minecraft.client.render.OverlayTexture;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.client.render.model.json.ModelTransformationMode;
import net.minecraft.item.ItemStack;
import org.joml.Matrix4f;

public class ItemDisplay implements Drawable {
public int x;
public int y;

public ItemStack stack = ItemStack.EMPTY;

@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {

if (stack.isEmpty())
return;

var matrices = context.getMatrices();
var client = MinecraftClient.getInstance();

BakedModel bakedModel = client.getItemRenderer().getModel(stack, client.world, client.player, 0);
matrices.push();
matrices.translate((float) (x), (float) (y), (float) (150));

try {
matrices.multiplyPositionMatrix((new Matrix4f()).scaling(1.0F, -1.0F, 1.0F));
matrices.scale( 48 / (float) client.getWindow().getScaleFactor(), 48 / (float) client.getWindow().getScaleFactor(), 48 / (float) client.getWindow().getScaleFactor());
boolean bl = !bakedModel.isSideLit();
if (bl) {
DiffuseLighting.disableGuiDepthLighting();
}

client.getItemRenderer().renderItem(stack, ModelTransformationMode.GUI, false, matrices, context.getVertexConsumers(), 15728880, OverlayTexture.DEFAULT_UV, bakedModel);
RenderSystem.disableDepthTest();
context.getVertexConsumers().draw();
RenderSystem.enableDepthTest();
if (bl) {
DiffuseLighting.enableGuiDepthLighting();
}
} catch (Throwable var12) {
//Ignore.
}

matrices.pop();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cassunshine.thework.client.gui.ingame.notebook.drawables;

import net.minecraft.client.gui.DrawContext;
import net.minecraft.item.ItemStack;
import net.minecraft.registry.Registries;

import java.util.Random;

public class RandomItemDisplay extends ItemDisplay {

private static final Random random = new Random();
private float changeCooldown = 0f;

@Override
public void render(DrawContext context, int mouseX, int mouseY, float delta) {
changeCooldown -= delta;

if (changeCooldown <= 0) {
var randomItem = Registries.ITEM.get(random.nextInt(Registries.ITEM.size()));
stack = new ItemStack(randomItem);

changeCooldown = 10f;
}

super.render(context, mouseX, mouseY, delta);
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
package cassunshine.thework.client.gui.ingame.notebook;
package cassunshine.thework.client.gui.ingame.notebook.pages;

import cassunshine.thework.TheWorkMod;
import cassunshine.thework.alchemy.runes.TheWorkRunes;
import cassunshine.thework.client.gui.ingame.notebook.AlchemistNotebookScreen;
import cassunshine.thework.client.gui.ingame.notebook.drawables.NodeDrawer;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.ButtonTextures;
import net.minecraft.client.gui.screen.GameModeSelectionScreen;
import net.minecraft.client.gui.widget.ButtonWidget;
import net.minecraft.client.gui.widget.TextWidget;
import net.minecraft.client.gui.widget.TexturedButtonWidget;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.text.OrderedText;
import net.minecraft.text.StringVisitable;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.MathHelper;

import java.util.Objects;

public class AlchemistNotebookNodePage extends AlchemistNotebookPage {
public static final Identifier IDENTIFIER = new Identifier(TheWorkMod.ModID, "node_page");

Expand All @@ -45,7 +42,7 @@ public AlchemistNotebookNodePage() {
}

@Override
protected void init(AlchemistNotebookScreen screen, int x, int y, int width, int height) {
public void init(AlchemistNotebookScreen screen, int x, int y, int width, int height) {
super.init(screen, x, y, width, height);

var nbt = screen.stack.getOrCreateNbt();
Expand Down Expand Up @@ -123,7 +120,8 @@ protected void init(AlchemistNotebookScreen screen, int x, int y, int width, int
drawnNode.size = MathHelper.floor(width * 0.6f);

drawnNode.x = x + width / 2;
drawnNode.y = y + 100;
drawnNode.y = y + height / 3;


circleSidesLeftButton.setX(drawnNode.x - circleSidesLeftButton.getWidth());
circleSidesLeftButton.setY(drawnNode.y + drawnNode.size / 2 + 5);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cassunshine.thework.client.gui.ingame.notebook;
package cassunshine.thework.client.gui.ingame.notebook.pages;

import cassunshine.thework.TheWorkMod;
import cassunshine.thework.client.networking.TheWorkClientNetworking;
import cassunshine.thework.client.gui.ingame.notebook.AlchemistNotebookScreen;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.item.ItemStack;
Expand All @@ -13,15 +13,27 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.function.Function;
import java.util.function.Supplier;

public class AlchemistNotebookPage extends Screen {

public static final Identifier IDENTIFIER = new Identifier(TheWorkMod.ModID, "basic");
private static final HashMap<Identifier, Function<NbtCompound, AlchemistNotebookPage>> GENERATORS = new HashMap<>() {{
private static final HashMap<Identifier, Function<NbtCompound, AlchemistNotebookPage>> NBT_GENERATORS = new HashMap<>() {{
put(IDENTIFIER, AlchemistNotebookPage::new);
}};

private static final HashMap<Identifier, Supplier<AlchemistNotebookPage>> GENERATORS = new HashMap<>() {{
put(AlchemistNotebookNodePage.IDENTIFIER, AlchemistNotebookNodePage::new);
put(LegendNotebookPage.IDENTIFIER, LegendNotebookPage::new);
put(DeconstructTutorialPage.IDENTIFIER, DeconstructTutorialPage::new);
}};

public static final Identifier[] DEFAULT_PAGES = new Identifier[]{
AlchemistNotebookNodePage.IDENTIFIER,
LegendNotebookPage.IDENTIFIER,
DeconstructTutorialPage.IDENTIFIER
};

public Identifier typeId;
public Identifier drawing;

Expand All @@ -37,7 +49,7 @@ public AlchemistNotebookPage(Identifier id, Identifier drawing) {
this.drawing = drawing;
}

protected void init(AlchemistNotebookScreen screen, int x, int y, int width, int height) {
public void init(AlchemistNotebookScreen screen, int x, int y, int width, int height) {

}

Expand All @@ -61,46 +73,38 @@ public void readNbt(NbtCompound nbt) {
}

public static ArrayList<AlchemistNotebookPage> getPages(ItemStack stack) {
var nbt = stack.getOrCreateNbt();

if (!stack.hasNbt()) {
var nbt = stack.getOrCreateNbt();

nbt.putInt("current_page", 0);

var list = new ArrayList<AlchemistNotebookPage>() {{
add(new AlchemistNotebookNodePage());
add(new AlchemistNotebookPage(IDENTIFIER, new Identifier(TheWorkMod.ModID, "textures/pages/page-basics.png")));
add(new AlchemistNotebookPage(IDENTIFIER, new Identifier(TheWorkMod.ModID, "textures/pages/page-runes.png")));
}};

nbt.put("pages", pagesToNbt(list));

TheWorkClientNetworking.updateBook(nbt);
return list;
} else {
var nbt = stack.getOrCreateNbt();
var nbtList = nbt.getList("pages", NbtElement.COMPOUND_TYPE);
var list = new ArrayList<AlchemistNotebookPage>();

var nbtList = nbt.getList("pages", NbtElement.COMPOUND_TYPE);
var list = new ArrayList<AlchemistNotebookPage>();

for (int i = 0; i < nbtList.size(); i++) {
var compound = nbtList.getCompound(i);
var type = new Identifier(compound.getString("type"));
for (Identifier page : DEFAULT_PAGES) {
var factoryResult = GENERATORS.get(page).get();
list.add(factoryResult);
}

var factoryResult = GENERATORS.get(type).apply(compound);
list.add(factoryResult);
}
for (int i = 0; i < nbtList.size(); i++) {
var compound = nbtList.getCompound(i);
var type = new Identifier(compound.getString("type"));

return list;
var factoryResult = NBT_GENERATORS.get(type).apply(compound);
list.add(factoryResult);
}

return list;
}


public static NbtList pagesToNbt(ArrayList<AlchemistNotebookPage> pages) {
var list = new NbtList();

for (AlchemistNotebookPage page : pages)
for (int i = 0; i < pages.size(); i++) {
if (i < DEFAULT_PAGES.length)
continue;

AlchemistNotebookPage page = pages.get(i);
list.add(page.writeNbt(new NbtCompound()));
}

return list;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package cassunshine.thework.client.gui.ingame.notebook.pages;

import cassunshine.thework.TheWorkMod;
import cassunshine.thework.client.gui.ingame.notebook.AlchemistNotebookScreen;
import cassunshine.thework.client.gui.ingame.notebook.drawables.ItemDisplay;
import net.minecraft.block.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.MathHelper;

public class DeconstructTutorialPage extends AlchemistNotebookPage {

public static final Identifier IDENTIFIER = new Identifier(TheWorkMod.ModID, "deconstruct_tutorial");
public static final Identifier PAGE_TEXTURE = new Identifier(TheWorkMod.ModID, "textures/pages/deconstruct_tutorial.png");

public DeconstructTutorialPage(NbtCompound compound) {
super(compound);
}

public DeconstructTutorialPage() {
super(IDENTIFIER, PAGE_TEXTURE);
}

@Override
public void init(AlchemistNotebookScreen screen, int x, int y, int width, int height) {
super.init(screen, x, y, width, height);

var itemDisplay = new ItemDisplay();
itemDisplay.x = x + MathHelper.floor(width * (14.5f / 96.0f));
itemDisplay.y = y + MathHelper.floor(height * (38.5f / 128.0f));
itemDisplay.stack = new ItemStack(Blocks.DIRT.asItem());

screen.addDrawable(itemDisplay);

itemDisplay = new ItemDisplay();
itemDisplay.x = x + MathHelper.floor(width * (14.5f / 96.0f));
itemDisplay.y = y + MathHelper.floor(height * (54.5f / 128.0f));
itemDisplay.stack = new ItemStack(Blocks.STONE.asItem());

screen.addDrawable(itemDisplay);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cassunshine.thework.client.gui.ingame.notebook.pages;

import cassunshine.thework.TheWorkMod;
import cassunshine.thework.client.gui.ingame.notebook.AlchemistNotebookScreen;
import cassunshine.thework.client.gui.ingame.notebook.drawables.RandomItemDisplay;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.MathHelper;

public class LegendNotebookPage extends AlchemistNotebookPage {

public static final Identifier IDENTIFIER = new Identifier(TheWorkMod.ModID, "legend");
public static final Identifier PAGE_TEXTURE = new Identifier(TheWorkMod.ModID, "textures/pages/legend.png");

public LegendNotebookPage(NbtCompound compound) {
super(compound);
}

public LegendNotebookPage() {
super(IDENTIFIER, PAGE_TEXTURE);
}

@Override
public void init(AlchemistNotebookScreen screen, int x, int y, int width, int height) {
super.init(screen, x, y, width, height);

var randomItemDisplay = new RandomItemDisplay();
randomItemDisplay.x = x + MathHelper.floor(width * 0.61);
randomItemDisplay.y = y + MathHelper.floor(height * 0.61);

screen.addDrawable(randomItemDisplay);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public void render(AlchemyCircleBlockEntity entity, float tickDelta, MatrixStack
TheWorkMod.LOGGER.error(e.toString());
}

RenderingUtilities.setupWobble(0);
RenderingUtilities.popMat();
}
}
Loading

0 comments on commit 181482b

Please sign in to comment.