Skip to content

Commit

Permalink
Implement end-to-end scarf stapling. Fixes #3
Browse files Browse the repository at this point in the history
  • Loading branch information
falkreon committed Sep 13, 2022
1 parent f45e38b commit b5db44f
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 169 deletions.
7 changes: 6 additions & 1 deletion src/main/java/blue/endless/scarves/ScarfItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

public class ScarfItem extends TrinketItem {
public static final String ID = "scarf";
public static final int MAX_CREATIVE_SCARF_LENGTH = 8;

public ScarfItem() {
super(new FabricItemSettings().rarity(Rarity.UNCOMMON).group(ScarvesMod.ITEM_GROUP));
Expand All @@ -29,7 +30,11 @@ public void appendStacks(ItemGroup group, DefaultedList<ItemStack> stacks) {
super.appendStacks(group, stacks);
if (this.isIn(group)) {
for(PrideFlag flag : PrideFlags.getFlags()) {
ItemStack scarf = createScarf(flag, 30, flag, 15);
int reps = MAX_CREATIVE_SCARF_LENGTH / flag.getColors().size();
if (reps < 1) reps = 1;
int flagLength = flag.getColors().size() * reps;

ItemStack scarf = createScarf(flag, flagLength, flag, 0);

//Create name
String flagKey = "flag.pridelib."+flag.getId();
Expand Down
40 changes: 27 additions & 13 deletions src/main/java/blue/endless/scarves/ScarfStaplerBlockEntity.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package blue.endless.scarves;

import blue.endless.scarves.api.FabricSquare;
import blue.endless.scarves.api.FabricSquareRegistry;
import blue.endless.scarves.gui.ScarfStaplerGuiDescription;
import blue.endless.scarves.util.ImplementedInventory;
Expand All @@ -22,6 +21,8 @@
import net.minecraft.util.math.BlockPos;

public class ScarfStaplerBlockEntity extends BlockEntity implements ImplementedInventory, Nameable, NamedScreenHandlerFactory {
public static final int STAPLER_CAP = 256;

private Text customName;
public static final int SCARF_SLOT = 0;
public static final int LEFT_SLOT = 1;
Expand Down Expand Up @@ -69,27 +70,40 @@ public Text getDisplayName() {
}

public void staple() {
ItemStack toStapleLeft = this.removeStack(LEFT_SLOT, 1);
ItemStack toStapleRight = this.removeStack(RIGHT_SLOT, 1);
ItemStack scarf = this.getStack(SCARF_SLOT);
ItemStack leftSlot = this.getStack(LEFT_SLOT);
ItemStack rightSlot = this.getStack(RIGHT_SLOT);
if (leftSlot.isEmpty() && rightSlot.isEmpty()) return;

ItemStack scarf = this.getStack(SCARF_SLOT);
if (scarf.isEmpty()) return;
if (toStapleLeft.isEmpty() && toStapleRight.isEmpty()) return;

//Check for capped lengths
NbtCompound tag = scarf.getOrCreateNbt();
NbtList leftSquares = tag.getList("LeftScarf", NbtElement.COMPOUND_TYPE);
NbtList rightSquares = tag.getList("RightScarf", NbtElement.COMPOUND_TYPE);

if (!leftSlot.isEmpty() && leftSquares.size()>=STAPLER_CAP) return;
if (!rightSlot.isEmpty() && rightSquares.size()>=STAPLER_CAP) return;

ItemStack toStapleLeft = this.removeStack(LEFT_SLOT, 1);
ItemStack toStapleRight = this.removeStack(RIGHT_SLOT, 1);

if (toStapleLeft!=ItemStack.EMPTY) {
FabricSquare toAdd = FabricSquareRegistry.forItem(toStapleLeft);
NbtList squares = tag.getList("LeftScarf", NbtElement.COMPOUND_TYPE);
squares.add(toAdd.toCompound());
tag.put("LeftScarf", squares);
NbtList toAdd = FabricSquareRegistry.getStaplerData(toStapleLeft);
for(NbtElement elem : toAdd) {
if (leftSquares.size()>=512) break;
if (elem instanceof NbtCompound compound) leftSquares.add(compound);
}
tag.put("LeftScarf", leftSquares);
}

if (toStapleRight!=ItemStack.EMPTY) {
FabricSquare toAdd = FabricSquareRegistry.forItem(toStapleRight);
NbtList squares = tag.getList("RightScarf", NbtElement.COMPOUND_TYPE);
squares.add(toAdd.toCompound());
tag.put("RightScarf", squares);
NbtList toAdd = FabricSquareRegistry.getStaplerData(toStapleRight);
for(NbtElement elem : toAdd) {
if (rightSquares.size()>=512) break;
if (elem instanceof NbtCompound compound) rightSquares.add(compound);
}
tag.put("RightScarf", rightSquares);
}

this.setStack(SCARF_SLOT, scarf);
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/blue/endless/scarves/ScarvesMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ public void onInitialize() {
.create(new Identifier(MODID, "general"))
.icon(()->new ItemStack(ScarvesItems.SCARF))
.build();
//.build(
// new Identifier(MODID, "general"),
// ()->new ItemStack(ScarvesItems.SCARF_TABLE)
//);


ScarvesBlocks.register();
ScarvesItems.register();
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/blue/endless/scarves/api/FabricSquareRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList;
import net.minecraft.util.Identifier;

public class FabricSquareRegistry {
Expand Down Expand Up @@ -132,4 +134,44 @@ public static boolean isFabricSquare(ItemStack stack) {
return entries.containsKey(item);
}
}

public static boolean canBeStapled(ItemStack stack) {
if (isFabricSquare(stack)) return true;

NbtCompound tag = stack.getNbt();
if (tag==null) return false;

NbtList leftScarfTag = tag.getList("LeftScarf", NbtElement.COMPOUND_TYPE);
NbtList rightScarfTag = tag.getList("RightScarf", NbtElement.COMPOUND_TYPE);

boolean hasLeftData = (leftScarfTag==null) ? false : leftScarfTag.size()>0;
boolean hasRightData = (rightScarfTag==null) ? false : rightScarfTag.size()>0;

return hasLeftData ^ hasRightData;
}

public static NbtList getStaplerData(ItemStack stack) {
if (isFabricSquare(stack)) {
NbtList result = new NbtList();
result.add(forItem(stack).toCompound());
return result;
} else {
NbtCompound tag = stack.getNbt();
if (tag==null) return new NbtList();

NbtList leftScarfTag = tag.getList("LeftScarf", NbtElement.COMPOUND_TYPE);
NbtList rightScarfTag = tag.getList("RightScarf", NbtElement.COMPOUND_TYPE);

boolean hasLeftData = (leftScarfTag==null) ? false : leftScarfTag.size()>0;
boolean hasRightData = (rightScarfTag==null) ? false : rightScarfTag.size()>0;

if (hasLeftData) {
return leftScarfTag;
} else if (hasRightData) {
return rightScarfTag;
} else {
return new NbtList();
}
}
}
}
2 changes: 1 addition & 1 deletion src/main/java/blue/endless/scarves/api/ScarfLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public static void updateScarfAttachment(ScarfAttachment attachment, World world

ScarfNode node = nodes.get(i);
Vec3d prospectivePosition = node.getPosition().add(0, ScarvesClient.SCARF_GRAVITY, 0);
BlockPos blockInThatPosition = new BlockPos(prospectivePosition);
BlockPos blockInThatPosition = new BlockPos(prospectivePosition.add(0,-ScarfNode.FABRIC_SQUARE_WIDTH,0));
if (world!=null) {
if (!world.isTopSolid(blockInThatPosition, entity)) {
node.setPosition(prospectivePosition);
Expand Down
63 changes: 12 additions & 51 deletions src/main/java/blue/endless/scarves/client/ScarvesClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public static void beforeEntities(WorldRenderContext ctx) {
if (entity instanceof IScarfHaver scarfHaver) {
try {
scarfHaver.iScarfHaver_getAttachments(ctx.tickDelta()).forEach( it-> {
//Physics
//Physics - gravity and collisions run on the tick thread
List<ScarfNode> nodes = it.nodes();
if (nodes.isEmpty()) return;
nodes.get(0).pullTowards(it.getLocation());
Expand All @@ -56,14 +56,18 @@ public static void beforeEntities(WorldRenderContext ctx) {
cur.pullTowards(prev.position);
}

//TODO: Gravity, floor collisions

//Rendering
Vec3d prev = it.getLocation();
Vec3d prevUp = new Vec3d(0,1,0).multiply(ScarfNode.FABRIC_SQUARE_WIDTH);
for(int i=0; i<nodes.size(); i++) {
ScarfNode cur = nodes.get(i);

BlockPos curPos = new BlockPos(cur.position.add(0,0.25,0));
Vec3d forwardVec = cur.position.subtract(prev).normalize();
Vec3d tempUpVec = (forwardVec.x==0&&forwardVec.z==0) ? new Vec3d(1,0,0) : new Vec3d(0,1,0);
Vec3d rightVec = forwardVec.crossProduct(tempUpVec);
Vec3d curUp = forwardVec.crossProduct(rightVec).multiply(ScarfNode.FABRIC_SQUARE_WIDTH);

int nodeLight = (cur.square.emissive()) ?
LightmapTextureManager.pack(15,15) :

Expand All @@ -74,8 +78,10 @@ public static void beforeEntities(WorldRenderContext ctx) {

ScarfRenderer.quad(
prev,
prev.add(0,ScarfNode.FABRIC_SQUARE_WIDTH,0),
cur.position.add(0,ScarfNode.FABRIC_SQUARE_WIDTH,0),
prev.add(prevUp),
//prev.add(0,ScarfNode.FABRIC_SQUARE_WIDTH,0),
//cur.position.add(0,ScarfNode.FABRIC_SQUARE_WIDTH,0),
cur.position.add(curUp),
cur.position,

cur.square,
Expand All @@ -86,6 +92,7 @@ public static void beforeEntities(WorldRenderContext ctx) {
);

prev = cur.position;
prevUp = curUp;
}
});
} catch (Throwable t) {
Expand All @@ -95,52 +102,6 @@ public static void beforeEntities(WorldRenderContext ctx) {
}
}

/*
SpriteIdentifier spriteId = new SpriteIdentifier(new Identifier("minecraft", "stone"), PlayerScreenHandler.BLOCK_ATLAS_TEXTURE);
RenderSystem.setShaderTexture(0, spriteId.getTextureId());
AbstractTexture tex = MinecraftClient.getInstance().getTextureManager().getTexture(PlayerScreenHandler.BLOCK_ATLAS_TEXTURE);
float minU = 0;
float minV = 0;
float maxU = 0.5f;
float maxV = 0.5f;
float pxX = (maxU-minU) / 16.0f;
float pxY = (maxV-minV) / 16.0f;
if (tex instanceof SpriteAtlasTexture atlas) {
Sprite sprite = atlas.getSprite(new Identifier("minecraft", "block/white_wool"));
if (sprite!=null) {
minU = sprite.getMinU();// + (4*pxX);
minV = sprite.getMinV();// + (4*pxY);
maxU = sprite.getMaxU();// - (4*pxX);
maxV = sprite.getMaxV();// - (4*pxY);
} //sprite will never be null
}
ScarfRenderer.quad(
new Vec3d(0 , 0 , 0 ),
new Vec3d(0 , 0.5, 0 ),
new Vec3d(0.5, 0.5, 0 ),
new Vec3d(0.5, 0 , 0 ),
new Vec2f(minU, minV),
new Vec2f(minU, maxV),
new Vec2f(maxU, maxV),
new Vec2f(maxU, minV),
true,
ctx.consumers(),
ctx.matrixStack(),
0xFF_77FF77, light);
*/
//buf.vertex(ctx.matrixStack().peek().getPositionMatrix(), 0, 0, 0).color(0xFF_FFFFFF).texture(1, 0).light(light).normal(0, 1, 0).next();
//buf.vertex(ctx.matrixStack().peek().getPositionMatrix(), 1, 0, 1).color(0xFF_FFFFFF).texture(0, 0).light(light).normal(0, 1, 0).next();



ctx.matrixStack().pop();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public ScarfStaplerGuiDescription(int syncId, PlayerInventory playerInventory, S
root.add(itemSlot, 4, 2);

WItemSlot leftSlot = WItemSlot.of(blockInventory, ScarfStaplerBlockEntity.LEFT_SLOT);
leftSlot.setFilter(FabricSquareRegistry::isFabricSquare);
leftSlot.setFilter(FabricSquareRegistry::canBeStapled);
root.add(leftSlot, 2, 2);

WItemSlot rightSlot = WItemSlot.of(blockInventory, ScarfStaplerBlockEntity.RIGHT_SLOT);
rightSlot.setFilter(FabricSquareRegistry::isFabricSquare);
rightSlot.setFilter(FabricSquareRegistry::canBeStapled);
root.add(rightSlot, 6, 2);

WButton stapleButton = new WButton(Text.translatable("gui.scarves.staple"));
Expand Down
Loading

0 comments on commit b5db44f

Please sign in to comment.