Skip to content

Commit

Permalink
Stapler updates, move tick logic to public methods
Browse files Browse the repository at this point in the history
  • Loading branch information
falkreon committed Aug 20, 2022
1 parent 4c69d42 commit 634a960
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 30 deletions.
5 changes: 4 additions & 1 deletion src/main/java/blue/endless/scarves/ScarfStaplerBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Wearable;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.state.StateManager.Builder;
import net.minecraft.state.property.DirectionProperty;
Expand All @@ -30,7 +31,7 @@
import net.minecraft.world.BlockView;
import net.minecraft.world.World;

public class ScarfStaplerBlock extends BlockWithEntity {
public class ScarfStaplerBlock extends BlockWithEntity implements Wearable {
public static final String ID = "scarf_stapler";
public static final DirectionProperty FACING = Properties.HORIZONTAL_FACING;
private static final float PX = 1/16f;
Expand Down Expand Up @@ -111,4 +112,6 @@ protected void appendProperties(Builder<Block, BlockState> builder) {
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
return SHAPE;
}


}
62 changes: 62 additions & 0 deletions src/main/java/blue/endless/scarves/api/ScarfLogic.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package blue.endless.scarves.api;

import java.util.List;

import blue.endless.scarves.client.ScarfAttachment;
import blue.endless.scarves.client.ScarfNode;
import blue.endless.scarves.client.ScarvesClient;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.NbtList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

public class ScarfLogic {

/**
* Updates the ScarfAttachment with new scarf data and applies gravity. Should be called each tick by an IScarfHaver.
*/
public static void updateScarfAttachment(ScarfAttachment attachment, World world, Entity entity, Vec3d anchorPosition, NbtList data) {
List<ScarfNode> nodes = attachment.nodes();
while(nodes.size()>data.size()) nodes.remove(nodes.size()-1);

Vec3d lastPos = anchorPosition;
for(int i=0; i<data.size(); i++) {
FabricSquare square = FabricSquare.fromCompound(data.getCompound(i));
if (nodes.size()<=i) {
ScarfNode node = new ScarfNode(lastPos, square);
nodes.add(node);
} else {
nodes.get(i).setSquare(square);
lastPos = nodes.get(i).getPosition();
}

ScarfNode node = nodes.get(i);
Vec3d prospectivePosition = node.getPosition().add(0, ScarvesClient.SCARF_GRAVITY, 0);
BlockPos blockInThatPosition = new BlockPos(prospectivePosition);
if (world!=null) {
if (!world.isTopSolid(blockInThatPosition, entity)) {
node.setPosition(prospectivePosition);
}
}
}
}

/**
* Only updates gravity on the scarf. Should be called each tick by an IScarfHaver if their scarf never changes color, length, or patterns based on item data.
*/
public static void updateGravityOnly(ScarfAttachment attachment, World world, Entity entity) {
List<ScarfNode> nodes = attachment.nodes();

for(int i=0; i<nodes.size(); i++) {
ScarfNode node = nodes.get(i);
Vec3d prospectivePosition = node.getPosition().add(0, ScarvesClient.SCARF_GRAVITY, 0);
BlockPos blockInThatPosition = new BlockPos(prospectivePosition);
if (world!=null) {
if (!world.isTopSolid(blockInThatPosition, entity)) {
node.setPosition(prospectivePosition);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import blue.endless.scarves.ScarvesItems;
import blue.endless.scarves.api.FabricSquare;
import blue.endless.scarves.api.ScarfLogic;
import blue.endless.scarves.client.IScarfHaver;
import blue.endless.scarves.client.ScarfAttachment;
import blue.endless.scarves.client.ScarfNode;
Expand All @@ -21,6 +22,7 @@
import dev.emi.trinkets.api.TrinketsApi;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.network.OtherClientPlayerEntity;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
Expand Down Expand Up @@ -113,42 +115,16 @@ private void scarves_updateScarfAttachments() {
NbtList leftScarfTag = tag.getList("LeftScarf", NbtElement.COMPOUND_TYPE);
if (leftScarfTag!=null) {
if (scarves_leftScarf==null) scarves_leftScarf = new SimpleScarfAttachment();
scarves_updateScarfAttachment(leftScarfTag, scarves_leftScarf);
ScarfLogic.updateScarfAttachment(scarves_leftScarf, this.world, (Entity)(Object)this, this.getPos(), leftScarfTag);
}

NbtList rightScarfTag = tag.getList("RightScarf", NbtElement.COMPOUND_TYPE);
if (rightScarfTag!=null) {
if (scarves_rightScarf==null) scarves_rightScarf = new SimpleScarfAttachment();
scarves_updateScarfAttachment(rightScarfTag, scarves_rightScarf);
ScarfLogic.updateScarfAttachment(scarves_rightScarf, this.world, (Entity)(Object)this, this.getPos(), rightScarfTag);
}
}
}
}
}

private void scarves_updateScarfAttachment(NbtList data, ScarfAttachment attachment) {
List<ScarfNode> nodes = attachment.nodes();
while(nodes.size()>data.size()) nodes.remove(nodes.size()-1);

Vec3d lastPos = this.getPos();
for(int i=0; i<data.size(); i++) {
FabricSquare square = FabricSquare.fromCompound(data.getCompound(i));
if (nodes.size()<=i) {
ScarfNode node = new ScarfNode(lastPos, square);
nodes.add(node);
} else {
nodes.get(i).setSquare(square);
lastPos = nodes.get(i).getPosition();
}

ScarfNode node = nodes.get(i);
Vec3d prospectivePosition = node.getPosition().add(0, ScarvesClient.SCARF_GRAVITY, 0);
BlockPos blockInThatPosition = new BlockPos(prospectivePosition);
if (this.world!=null) {
if (!this.world.isTopSolid(blockInThatPosition, this)) {
node.setPosition(prospectivePosition);
}
}
}
}
}
23 changes: 23 additions & 0 deletions src/main/java/blue/endless/scarves/mixin/EntityEquipmentMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package blue.endless.scarves.mixin;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import blue.endless.scarves.ScarvesItems;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;

@Mixin(LivingEntity.class)
public class EntityEquipmentMixin {


@Inject(at = { @At("HEAD") }, method = "getPreferredEquipmentSlot", cancellable = true)
private static EquipmentSlot getPreferredEquipmentSlot(ItemStack stack, CallbackInfoReturnable<EquipmentSlot> ci) {
if (stack.isOf(ScarvesItems.SCARF_STAPLER)) ci.setReturnValue(EquipmentSlot.HEAD);

return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"textures": {
"1": "scarves:block/scarf_stapler_body"
"1": "scarves:block/scarf_stapler_body",
"particle": "scarves:block/scarf_stapler_body"
},
"elements": [
{
Expand Down Expand Up @@ -78,6 +79,42 @@
}
}
],
"display": {
"thirdperson_righthand": {
"rotation": [75, 45, 0],
"translation": [0, 2.5, 0],
"scale": [0.375, 0.375, 0.375]
},
"thirdperson_lefthand": {
"rotation": [75, 45, 0],
"translation": [0, 2.5, 0],
"scale": [0.375, 0.375, 0.375]
},
"firstperson_righthand": {
"rotation": [0, 45, 0],
"scale": [0.4, 0.4, 0.4]
},
"firstperson_lefthand": {
"rotation": [0, 225, 0],
"scale": [0.4, 0.4, 0.4]
},
"ground": {
"translation": [0, 3, 0],
"scale": [0.25, 0.25, 0.25]
},
"gui": {
"rotation": [30, 225, 0],
"scale": [0.625, 0.625, 0.625]
},
"head": {
"translation": [0, 1.75, 0],
"scale": [1.37, 1.37, 1.37]
},
"fixed": {
"translation": [0, 0, -2],
"scale": [0.75, 0.75, 0.75]
}
},
"groups": [
0,
1,
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/scarves.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"package": "blue.endless.scarves.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"EntityEquipmentMixin"
],
"client": [
"ClientPlayerEntityMixin"
Expand Down

0 comments on commit 634a960

Please sign in to comment.