Skip to content

Commit

Permalink
Backpack Crafting
Browse files Browse the repository at this point in the history
  • Loading branch information
hammy275 committed May 7, 2022
1 parent 4291d41 commit f11a8a7
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 29 deletions.
1 change: 1 addition & 0 deletions model/Crafting.bbmodel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"meta":{"format_version":"4.0","creation_time":1651947086,"model_format":"modded_entity","box_uv":true},"name":"BackpackCrafting","geometry_name":"","modded_entity_version":"1.15_mojmaps","visible_box":[1,1,0],"variable_placeholders":"","variable_placeholder_buttons":[],"resolution":{"width":32,"height":32},"elements":[{"name":"table","rescale":false,"locked":false,"from":[-4,-4,-4],"to":[4,4,4],"autouv":0,"color":0,"origin":[0,0,0],"faces":{"north":{"uv":[8,8,16,16],"texture":0},"east":{"uv":[0,8,8,16],"texture":0},"south":{"uv":[24,8,32,16],"texture":0},"west":{"uv":[16,8,24,16],"texture":0},"up":{"uv":[16,8,8,0],"texture":0},"down":{"uv":[24,0,16,8],"texture":0}},"type":"cube","uuid":"55203d27-0b71-1cef-1b4c-a7995d3b3b20"}],"outliner":[{"name":"table","origin":[0,0,0],"color":0,"uuid":"e15e2c02-2573-f919-6f81-55536df2b501","export":true,"isOpen":true,"locked":false,"visibility":true,"autouv":0,"children":["55203d27-0b71-1cef-1b4c-a7995d3b3b20"]}],"textures":[{"path":"C:\\Users\\hammy3502\\Desktop\\Forge\\immersive-mc\\src\\main\\resources\\assets\\immersivemc\\crafting.png","name":"crafting.png","folder":"block","namespace":"","id":"0","particle":false,"render_mode":"default","visible":true,"mode":"bitmap","saved":true,"uuid":"c8c866b2-f2e8-9229-266e-60f81c6e90ec","relative_path":"../../src/main/resources/assets/immersivemc/crafting.png","source":"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAYxJREFUWEdjZCAAtlx5+B+fkvm9k/GasHZ+DyM+BXglQRpBDrBQkWEQ4WBmePPjL4pZJ+48YQA5QFpcAqsdT1++YKCLA9bO78HqgODEEuo6ANkWUGjAQgDkgLwKiCMmdZSA2SB6+DkAVxqgWxQMWCKkaTZ0ftGLN59LXEnHm89nnpqEV54l/A1eeUaQAyzExRhaGWNRFFb/X8xw4uUrBpADFCS44XKtOpCio/rKf4YHL74ygBzArSgHFmOMjIGr+798CcPX+48YQA7gUO7DcMT/HzMZfj69yYDiAJClyACXA0CWgwCyA0CWgyxFBsgOAFmIDjAcAPYZkiNIdQBIP7IjYA5gl1ZnYORIZ0B3BIoDYK4DRQXMEeRGAcwRyA4ARxEHJD3BHAJ3wF6JYoz4BwlgcwBIHJQOkNMAT1UVin50B8DSAMhi5JDAGgLoaQDdcTB5bIkQPY7RowAmjxEC+PIJKBcsdeFhiN7zBasyirMhPsvpIUewPUBrR4w6YDQERkNgNARGQ2A0BEZDAAAY10NgBpvC7AAAAABJRU5ErkJggg=="}]}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class BackpackConfigScreen extends Screen {
protected final Screen parentScreen;
protected OptionsRowList list;

protected static int BUTTON_WIDTH = 256;
protected static int BUTTON_WIDTH = 128;
protected static int BUTTON_HEIGHT = 20;

public BackpackConfigScreen(Screen lastScreen) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ImmersivesConfigScreen extends Screen {
protected OptionsRowList list;
protected boolean notInWorld;

protected static int BUTTON_WIDTH = 256;
protected static int BUTTON_WIDTH = 128;
protected static int BUTTON_HEIGHT = 20;

public ImmersivesConfigScreen(Screen screen) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
import net.blf02.immersivemc.client.config.ClientConstants;
import net.blf02.immersivemc.client.immersive.info.AbstractImmersiveInfo;
import net.blf02.immersivemc.client.immersive.info.BackpackInfo;
import net.blf02.immersivemc.client.model.BackpackCraftingModel;
import net.blf02.immersivemc.client.model.BackpackModel;
import net.blf02.immersivemc.common.config.ActiveConfig;
import net.blf02.immersivemc.common.network.Network;
import net.blf02.immersivemc.common.network.packet.CraftPacket;
import net.blf02.immersivemc.common.network.packet.InventorySwapPacket;
import net.blf02.immersivemc.common.util.Util;
import net.blf02.immersivemc.common.vr.VRPlugin;
import net.blf02.immersivemc.common.vr.VRPluginVerify;
import net.blf02.immersivemc.server.swap.Swap;
import net.blf02.vrapi.api.data.IVRData;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.ActiveRenderInfo;
Expand All @@ -28,6 +33,8 @@ public class BackpackImmersive extends AbstractImmersive<BackpackInfo> {
public static final BackpackImmersive singleton = new BackpackImmersive();

public static final BackpackModel model = new BackpackModel();
public static final BackpackCraftingModel craftingModel = new BackpackCraftingModel();

private final double spacing = 3d/8d;

public BackpackImmersive() {
Expand Down Expand Up @@ -110,6 +117,36 @@ protected void doTick(BackpackInfo info, boolean isInVR) {
info.setPosition(i, slotPos);
info.setHitbox(i, createHitbox(info.getPosition(i), 0.05f));
}

Vector3d upVec = info.downVec.multiply(-1, -1, -1);

double upMult = 0.05;

// Multiply these by 4 since rightVec is multiplied by 0.25 above
Vector3d leftCraftingPos = info.centerTopPos.add(rightVec.multiply(0.3125*4, 0.3125*4, 0.3125*4))
.add(upVec.multiply(upMult, upMult, upMult));
Vector3d rightCraftingPos = info.centerTopPos.add(rightVec.multiply(0.4375*4, 0.4375*4, 0.4375*4))
.add(upVec.multiply(upMult, upMult, upMult));
Vector3d centerCraftingPos = info.centerTopPos.add(rightVec.multiply(0.375*4, 0.375*4, 0.375*4))
.add(upVec.multiply(upMult, upMult, upMult));

double craftingOffset = 0.625;
Vector3d[] craftingPositions = new Vector3d[]{
leftCraftingPos.add(topOffset.multiply(craftingOffset, craftingOffset, craftingOffset)),
rightCraftingPos.add(topOffset.multiply(craftingOffset, craftingOffset, craftingOffset)),
leftCraftingPos.add(botOffset.multiply(craftingOffset, craftingOffset, craftingOffset)),
rightCraftingPos.add(botOffset.multiply(craftingOffset, craftingOffset, craftingOffset))
};

for (int i = 27; i <= 30; i++) {
info.setPosition(i, craftingPositions[i - 27]);
info.setHitbox(i, createHitbox(info.getPosition(i), 0.05f));
}

info.setPosition(31, centerCraftingPos.add(upVec.multiply(0.125, 0.125, 0.125)));
info.setHitbox(31, createHitbox(info.getPosition(31), 0.05f));


Optional<Integer> hitboxIntersect = Util.getFirstIntersect(handController.position(),
info.getAllHitboxes());
if (hitboxIntersect.isPresent()) {
Expand All @@ -119,6 +156,20 @@ protected void doTick(BackpackInfo info, boolean isInVR) {
}
}

public static void onHitboxInteract(PlayerEntity player, BackpackInfo info, int slot) {
if (slot <= 26) { // Inventory handle
Network.INSTANCE.sendToServer(new InventorySwapPacket(slot + 9));
Swap.handleInventorySwap(player, slot + 9, Hand.MAIN_HAND); // Do swap on both sides
} else if (slot <= 30) {
info.craftingInput[slot - 27] = player.getItemInHand(Hand.MAIN_HAND);
Network.INSTANCE.sendToServer(new CraftPacket(info.craftingInput, player.blockPosition(),
true));
} else if (slot == 31) {
Network.INSTANCE.sendToServer(new CraftPacket(info.craftingInput, player.blockPosition(),
false));
}
}

protected boolean inRange(int num, int start, int end) {
return start <= num && num <= end;
}
Expand All @@ -133,7 +184,7 @@ public boolean shouldRender(BackpackInfo info, boolean isInVR) {

@Override
protected void render(BackpackInfo info, MatrixStack stack, boolean isInVR) {
for (int i = 0; i <= 26; i++) {
for (int i = 0; i <= 31; i++) {
AxisAlignedBB hitbox = info.getHibtox(i);
renderHitbox(stack, hitbox, info.getPosition(i));
}
Expand All @@ -147,6 +198,13 @@ protected void render(BackpackInfo info, MatrixStack stack, boolean isInVR) {
}
}

for (int i = 27; i <= 31; i++) {
ItemStack item = i == 31 ? info.craftingOutput : info.craftingInput[i - 27];
if (!item.isEmpty() && info.getPosition(i) != null) {
renderItem(item, stack, info.getPosition(i), ClientConstants.itemScaleSizeBackpack, null, info.getHibtox(i), i == 31);
}
}

stack.pushPose();
Vector3d pos = info.renderPos;

Expand Down Expand Up @@ -174,6 +232,15 @@ protected void render(BackpackInfo info, MatrixStack stack, boolean isInVR) {
15728880, OverlayTexture.NO_OVERLAY,
info.rgb.x(), info.rgb.y(), info.rgb.z(), 1);

// Translate and render the crafting on the side of the backpack and down a bit
// (yes, positive y in this context moves it down lol)
stack.translate(ActiveConfig.leftHandedBackpack ? -0.75 : 0.75, 0.25, 0);
craftingModel.renderToBuffer(stack,
Minecraft.getInstance().renderBuffers().bufferSource()
.getBuffer(RenderType.entityCutout(BackpackCraftingModel.textureLocation)),
15728880, OverlayTexture.NO_OVERLAY,
1, 1, 1, 1);

Minecraft.getInstance().renderBuffers().bufferSource().endBatch();
stack.popPose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

import net.blf02.immersivemc.client.config.ClientConstants;
import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.math.vector.Vector3f;

public class BackpackInfo extends AbstractImmersiveInfo {

protected final Vector3d[] itemPositions = new Vector3d[27];
protected final AxisAlignedBB[] itemHitboxes = new AxisAlignedBB[27];
protected final Vector3d[] positions = new Vector3d[32];
protected final AxisAlignedBB[] hitboxes = new AxisAlignedBB[32];
// 0-26: Inventory
// 27-31: Input crafting
// 32: Output crafting

public Vector3d handPos = Vector3d.ZERO;
public Vector3d lookVec = Vector3d.ZERO;
Expand All @@ -23,49 +27,51 @@ public class BackpackInfo extends AbstractImmersiveInfo {
public Vector3f rgb = new Vector3f(0, 0, 0);
public int topRow = 0;
public int slotHovered = -1;
public ItemStack[] craftingInput = new ItemStack[]{ItemStack.EMPTY, ItemStack.EMPTY, ItemStack.EMPTY, ItemStack.EMPTY};
public ItemStack craftingOutput = ItemStack.EMPTY;

public BackpackInfo() {
super(ClientConstants.ticksToRenderBackpack);
}

@Override
public AxisAlignedBB getHibtox(int slot) {
return itemHitboxes[slot];
return hitboxes[slot];
}

@Override
public AxisAlignedBB[] getAllHitboxes() {
return itemHitboxes;
return hitboxes;
}

@Override
public void setHitbox(int slot, AxisAlignedBB hitbox) {
itemHitboxes[slot] = hitbox;
hitboxes[slot] = hitbox;
}

@Override
public boolean hasHitboxes() {
return itemHitboxes[26] != null;
return hitboxes[26] != null;
}

@Override
public Vector3d getPosition(int slot) {
return itemPositions[slot];
return positions[slot];
}

@Override
public Vector3d[] getAllPositions() {
return itemPositions;
return positions;
}

@Override
public void setPosition(int slot, Vector3d position) {
itemPositions[slot] = position;
positions[slot] = position;
}

@Override
public boolean hasPositions() {
return itemPositions[26] != null;
return positions[26] != null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package net.blf02.immersivemc.client.model;

import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.vertex.IVertexBuilder;
import net.blf02.immersivemc.ImmersiveMC;
import net.minecraft.client.renderer.entity.model.EntityModel;
import net.minecraft.client.renderer.model.ModelRenderer;
import net.minecraft.entity.Entity;
import net.minecraft.util.ResourceLocation;

// 99% exported from BlockBench
public class BackpackCraftingModel extends EntityModel<Entity> {

public static final ResourceLocation textureLocation = new ResourceLocation(ImmersiveMC.MOD_ID, "crafting.png");
private final ModelRenderer table;

public BackpackCraftingModel() {
texWidth = 32;
texHeight = 32;

table = new ModelRenderer(this);
table.setPos(0.0F, 24.0F, 0.0F);
table.texOffs(0, 0).addBox(-4.0F, -4.0F, -4.0F, 8.0F, 8.0F, 8.0F, 0.0F, false);
}

@Override
public void setupAnim(Entity entity, float limbSwing, float limbSwingAmount, float ageInTicks, float netHeadYaw, float headPitch){
//previously the render function, render code was moved to a method below
}

@Override
public void renderToBuffer(MatrixStack matrixStack, IVertexBuilder buffer, int packedLight, int packedOverlay, float red, float green, float blue, float alpha){
table.render(matrixStack, buffer, packedLight, packedOverlay, red, green, blue, alpha);
}

public void setRotationAngle(ModelRenderer modelRenderer, float x, float y, float z) {
modelRenderer.xRot = x;
modelRenderer.yRot = y;
modelRenderer.zRot = z;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@
import net.blf02.immersivemc.client.immersive.info.InfoTriggerHitboxes;
import net.blf02.immersivemc.client.tracker.ClientTrackerInit;
import net.blf02.immersivemc.common.config.ActiveConfig;
import net.blf02.immersivemc.common.network.Network;
import net.blf02.immersivemc.common.network.packet.InventorySwapPacket;
import net.blf02.immersivemc.common.tracker.AbstractTracker;
import net.blf02.immersivemc.common.util.Util;
import net.blf02.immersivemc.common.vr.VRPlugin;
import net.blf02.immersivemc.common.vr.VRPluginVerify;
import net.blf02.immersivemc.server.swap.Swap;
import net.blf02.vrapi.api.data.IVRData;
import net.minecraft.block.AbstractChestBlock;
import net.minecraft.block.AnvilBlock;
Expand Down Expand Up @@ -201,8 +198,7 @@ public static boolean handleLeftClick(PlayerEntity player) {
BackpackImmersive.singleton.getTrackedObjects().get(0) : null;
// Move to next row on left click if backpack is out
if (backpackInfo != null && backpackInfo.slotHovered > -1) {
Network.INSTANCE.sendToServer(new InventorySwapPacket(backpackInfo.slotHovered + 9));
Swap.handleInventorySwap(player, backpackInfo.slotHovered + 9, Hand.MAIN_HAND); // Do swap on both sides
BackpackImmersive.onHitboxInteract(player, backpackInfo, backpackInfo.slotHovered);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import net.blf02.immersivemc.client.immersive.AbstractImmersive;
import net.blf02.immersivemc.client.immersive.AbstractTileEntityImmersive;
import net.blf02.immersivemc.client.immersive.BackpackImmersive;
import net.blf02.immersivemc.client.immersive.Immersives;
import net.blf02.immersivemc.client.immersive.info.AbstractImmersiveInfo;
import net.blf02.immersivemc.client.immersive.info.AbstractTileEntityImmersiveInfo;
import net.blf02.immersivemc.client.immersive.info.BackpackInfo;
import net.blf02.immersivemc.client.immersive.info.ChestInfo;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
Expand All @@ -13,6 +15,13 @@

public class NetworkClientHandlers {

public static void setBackpackOutput(ItemStack output) {
if (BackpackImmersive.singleton.getTrackedObjects().size() > 0) {
BackpackInfo info = BackpackImmersive.singleton.getTrackedObjects().get(0);
info.craftingOutput = output;
}
}

public static void handleReceiveInvData(ItemStack[] stacks, BlockPos pos) {
Objects.requireNonNull(stacks);
for (AbstractImmersive<? extends AbstractImmersiveInfo> singleton : Immersives.IMMERSIVES) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.blf02.immersivemc.client.storage.ClientStorage;
import net.blf02.immersivemc.common.config.ActiveConfig;
import net.blf02.immersivemc.common.network.Network;
import net.blf02.immersivemc.common.network.NetworkClientHandlers;
import net.blf02.immersivemc.common.network.NetworkUtil;
import net.blf02.immersivemc.server.swap.Swap;
import net.minecraft.entity.player.ServerPlayerEntity;
Expand All @@ -22,16 +23,19 @@ public class CraftPacket {
protected final BlockPos tablePos;

protected final ItemStack resItem;
protected final boolean is2x2;

public CraftPacket(ItemStack[] inv, BlockPos tablePos, boolean retrieveRecipe) {
this.inv = inv;
this.tablePos = tablePos;
this.isAskingForRecipe = retrieveRecipe;
this.resItem = null;
this.is2x2 = false;
}

protected CraftPacket(ItemStack resItem) {
protected CraftPacket(ItemStack resItem, boolean is2x2) {
this.resItem = resItem;
this.is2x2 = is2x2;
this.isAskingForRecipe = false;
this.inv = null;
this.tablePos = null;
Expand All @@ -40,26 +44,29 @@ protected CraftPacket(ItemStack resItem) {
public static void encode(CraftPacket packet, PacketBuffer buffer) {
buffer.writeBoolean(packet.resItem == null);
if (packet.resItem == null) {
for (int i = 0; i < 9; i++) {
buffer.writeInt(packet.inv.length);
for (int i = 0; i < packet.inv.length; i++) {
buffer.writeItem(packet.inv[i]);
}
buffer.writeBlockPos(packet.tablePos);
buffer.writeBoolean(packet.isAskingForRecipe);
} else {
buffer.writeItem(packet.resItem);
buffer.writeBoolean(packet.is2x2);
}

}

public static CraftPacket decode(PacketBuffer buffer) {
if (buffer.readBoolean()) {
ItemStack[] inv = new ItemStack[9];
for (int i = 0; i < 9; i++) {
int numItems = buffer.readInt();
ItemStack[] inv = new ItemStack[numItems];
for (int i = 0; i < numItems; i++) {
inv[i] = buffer.readItem();
}
return new CraftPacket(inv, buffer.readBlockPos(), buffer.readBoolean());
} else {
return new CraftPacket(buffer.readItem());
return new CraftPacket(buffer.readItem(), buffer.readBoolean());
}


Expand All @@ -74,12 +81,16 @@ public static void handle(final CraftPacket message, Supplier<NetworkEvent.Conte
ICraftingRecipe recipe = Swap.getReecipe(player, message.inv);
ItemStack result = recipe == null ? ItemStack.EMPTY : recipe.getResultItem();
Network.INSTANCE.send(PacketDistributor.PLAYER.with(() -> player),
new CraftPacket(result));
new CraftPacket(result, message.inv.length == 4));
} else {
Swap.handleCrafting(player, message.inv, message.tablePos);
}
} else if (player == null) {
ClientStorage.craftingOutput = message.resItem;
if (message.is2x2) {
NetworkClientHandlers.setBackpackOutput(message.resItem);
} else {
ClientStorage.craftingOutput = message.resItem;
}
}
});

Expand Down
Loading

0 comments on commit f11a8a7

Please sign in to comment.