Skip to content

Commit

Permalink
Fix scarf jitter, finally finish Scarf Serger
Browse files Browse the repository at this point in the history
  • Loading branch information
falkreon committed Jul 27, 2023
1 parent fa03c56 commit ed01351
Show file tree
Hide file tree
Showing 22 changed files with 700 additions and 43 deletions.
18 changes: 15 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,24 @@ version = project.mod_version
group = project.maven_group

repositories {
exclusiveContent {
forRepository {
maven {
name = "Modrinth"
url = "https://api.modrinth.com/maven"
}
}
filter {
includeGroup "maven.modrinth"
}
}
maven {
name = "TerraformersMC"
url = "https://maven.terraformersmc.com/"
}
maven {
name = "Ladysnake Libs"
url = "https://ladysnake.jfrog.io/artifactory/mods"
url = "https://maven.ladysnake.org/releases"
}
maven {
name = "CottonMC"
Expand All @@ -39,11 +50,12 @@ dependencies {
exclude group: "com.terraformersmc", module: "modmenu"
}

modCompileOnly "dev.emi:emi-fabric:${emi_version}:api"
modLocalRuntime "dev.emi:emi-fabric:${emi_version}"

modImplementation include("io.github.cottonmc:LibGui:${project.libgui_version}")

modImplementation include("io.github.queerbric:pridelib:1.2.0+1.19.4")

//modRuntimeOnly "com.terraformersmc:modmenu:4.0.6"
}

processResources {
Expand Down
13 changes: 7 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_version=1.20
yarn_mappings=1.20+build.1
loader_version=0.14.21
minecraft_version = 1.20
yarn_mappings = 1.20+build.1
loader_version = 0.14.21

# Mod Properties
mod_version = 1.2.2
mod_version = 1.3.0
maven_group = blue.endless
archives_base_name = scarves

# Dependencies
fabric_version=0.83.0+1.20
libgui_version=8.0.0+1.20
fabric_version = 0.83.0+1.20
libgui_version = 8.0.0+1.20
emi_version = 1.0.15+1.20.1
11 changes: 10 additions & 1 deletion src/main/java/blue/endless/scarves/ScarfTableBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.state.StateManager.Builder;
import net.minecraft.state.property.DirectionProperty;
import net.minecraft.state.property.Properties;
Expand Down Expand Up @@ -45,7 +46,15 @@ public BlockEntity createBlockEntity(BlockPos pos, BlockState state) {

@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
player.openHandledScreen(state.createScreenHandlerFactory(world, pos));
if (!world.isClient) {
player.openHandledScreen(state.createScreenHandlerFactory(world, pos));
if (player instanceof ServerPlayerEntity serverPlayer) {
ScarfTableBlockEntity be = world.getBlockEntity(pos, ScarvesBlocks.SCARF_TABLE_ENTITY).orElse(null);
if (be != null) {
be.syncGhostItems(serverPlayer);
}
}
}
return ActionResult.SUCCESS;
}

Expand Down
90 changes: 73 additions & 17 deletions src/main/java/blue/endless/scarves/ScarfTableBlockEntity.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
package blue.endless.scarves;

import java.util.List;
import java.util.ArrayList;

import blue.endless.scarves.api.FabricSquare;
import blue.endless.scarves.api.FabricSquareRegistry;
import blue.endless.scarves.ghost.ImplementedGhostInventory;
import blue.endless.scarves.gui.ScarfTableGuiDescription;
import blue.endless.scarves.util.ArrayPropertyDelegate;
import blue.endless.scarves.util.ImplementedInventory;
import io.github.cottonmc.cotton.gui.PropertyDelegateHolder;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventories;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList;
import net.minecraft.screen.NamedScreenHandlerFactory;
import net.minecraft.screen.PropertyDelegate;
import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerContext;
import net.minecraft.text.Text;
import net.minecraft.util.Nameable;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.math.BlockPos;

public class ScarfTableBlockEntity extends BlockEntity implements ImplementedInventory, Nameable, PropertyDelegateHolder, NamedScreenHandlerFactory {
public class ScarfTableBlockEntity extends BlockEntity implements ImplementedInventory, ImplementedGhostInventory, Nameable, NamedScreenHandlerFactory {
private Text customName;
private DefaultedList<ItemStack> inventory = DefaultedList.ofSize(10, ItemStack.EMPTY);
private int[] tints = {
0xFF_FFFFFF, 0xFF_FFFFFF, 0xFF_FFFFFF, 0xFF_FFFFFF,
0xFF_FFFFFF, 0xFF_FFFFFF, 0xFF_FFFFFF, 0xFF_FFFFFF
};
private ArrayPropertyDelegate propertyDelegate = new ArrayPropertyDelegate(tints);
private DefaultedList<ItemStack> inventory = DefaultedList.ofSize(1, ItemStack.EMPTY);
private DefaultedList<ItemStack> ghostItems = DefaultedList.ofSize(8, ItemStack.EMPTY);

public static final int SLOT_INPUT = 8;
public static final int SLOT_OUTPUT = 9;
Expand All @@ -36,22 +37,82 @@ public ScarfTableBlockEntity(BlockPos pos, BlockState state) {
super(ScarvesBlocks.SCARF_TABLE_ENTITY, pos, state);
}

public List<FabricSquare> getGhostPattern(int patternLength) {
List<FabricSquare> result = new ArrayList<>();
int count = Math.max(patternLength, ghostItems.size());
for(int i=0; i<count; i++) {
ItemStack ghostItem = ghostItems.get(i);
if (ghostItem.isEmpty()) continue;
FabricSquare square = FabricSquareRegistry.forItem(ghostItem);
if (square != null) result.add(square);
}

return result;
}

public NbtList getAppliedPattern(int patternLength, int repetitions) {
List<FabricSquare> pattern = getGhostPattern(patternLength);
if (pattern.isEmpty()) return new NbtList();

NbtList list = new NbtList();
for(int i=0; i<repetitions; i++) {
for(FabricSquare square : pattern) {
list.add(square.toCompound());
}
}

return list;
}

public void applyLeft(int patternLength, int repetitions) {
ItemStack scarfStack = this.inventory.get(0);
if (scarfStack.isEmpty()) return;

NbtList appliedPattern = getAppliedPattern(patternLength, repetitions);

NbtCompound tag = scarfStack.getOrCreateNbt();
tag.put("LeftScarf", appliedPattern);

this.setStack(0, scarfStack);
this.markDirty();
}

public void applyRight(int patternLength, int repetitions) {
ItemStack scarfStack = this.inventory.get(0);
if (scarfStack.isEmpty()) return;

NbtList appliedPattern = getAppliedPattern(patternLength, repetitions);

NbtCompound tag = scarfStack.getOrCreateNbt();
tag.put("RightScarf", appliedPattern);

this.setStack(0, scarfStack);
this.markDirty();
}

@Override
public DefaultedList<ItemStack> getItems() {
return inventory;
}

@Override
public DefaultedList<ItemStack> getGhostItems() {
return ghostItems;
}

@Override
public void readNbt(NbtCompound nbt) {
Inventories.readNbt(nbt, inventory);
tints = nbt.getIntArray("Tints");
readGhostItems(nbt);

super.readNbt(nbt);
}

@Override
protected void writeNbt(NbtCompound nbt) {
Inventories.writeNbt(nbt, inventory);
nbt.putIntArray("Tints", tints);
writeGhostItems(nbt);

super.writeNbt(nbt);
}

Expand All @@ -64,11 +125,6 @@ public Text getName() {
return customName;
}

@Override
public PropertyDelegate getPropertyDelegate() {
return propertyDelegate;
}

@Override
public ScreenHandler createMenu(int syncId, PlayerInventory playerInventory, PlayerEntity player) {
return new ScarfTableGuiDescription(syncId, playerInventory, ScreenHandlerContext.create(world, pos));
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/blue/endless/scarves/ScarvesMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import blue.endless.scarves.api.ScarvesApi;
import blue.endless.scarves.api.ScarvesIntegration;
import blue.endless.scarves.ghost.GhostInventoryNetworking;
import io.github.queerbric.pride.PrideFlag;
import io.github.queerbric.pride.PrideFlags;
import net.fabricmc.api.ModInitializer;
Expand All @@ -27,6 +28,8 @@ public class ScarvesMod implements ModInitializer {
public static final String MODID = "scarves";
public static final Logger LOGGER = LoggerFactory.getLogger("Scarves");

public static final Identifier GHOST_SLOT_MESSAGE = new Identifier(MODID, "ghost");

public static ItemGroup ITEM_GROUP;

private static final List<ItemStack> creativeScarves = new ArrayList<>();
Expand All @@ -51,6 +54,9 @@ public void onInitialize() {
ScarvesBlocks.register();
ScarvesItems.register();

//TODO: I'd love to use static data to load in custom scarves squares but I can't.

GhostInventoryNetworking.init();

for (EntrypointContainer<ScarvesIntegration> entrypoint : FabricLoader.getInstance().getEntrypointContainers(MODID, ScarvesIntegration.class)) {
try {
Expand Down
123 changes: 123 additions & 0 deletions src/main/java/blue/endless/scarves/WScarfPreview.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package blue.endless.scarves;

import java.util.List;

import blue.endless.scarves.api.FabricSquare;
import blue.endless.scarves.api.FabricSquareRegistry;
import blue.endless.scarves.ghost.GhostInventory;
import io.github.cottonmc.cotton.gui.client.ScreenDrawing;
import io.github.cottonmc.cotton.gui.widget.WWidget;
import io.github.cottonmc.cotton.gui.widget.data.Axis;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.texture.AbstractTexture;
import net.minecraft.client.texture.Sprite;
import net.minecraft.client.texture.SpriteAtlasTexture;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList;
import net.minecraft.screen.PlayerScreenHandler;
import net.minecraft.util.math.Vec2f;

public class WScarfPreview extends WWidget {
protected int panelSize = 24;
protected int maxSquares = 8;

//Used if this preview is of a GhostInventory. Takes precedence.
private GhostInventory inventory = null;

//Used if this preview is of the ScarfData on an ItemStack. Left scarf takes precedence if there are two.
private Inventory exemplarInventory = null;
private int exemplarSlot = 0;
//private List<FabricSquare> cachedExemplar = null;

private Axis axis = Axis.HORIZONTAL;

public WScarfPreview(GhostInventory inventory, Axis axis) {
this.inventory = inventory;
this.axis = axis;
}

public WScarfPreview(Inventory inventory, int slot, Axis axis) {
this.exemplarInventory = inventory;
this.exemplarSlot = slot;
this.axis = axis;
}

public void setPanelSize(int value) {
this.panelSize = value;
}

public void setMaxSquares(int value) {
this.maxSquares = value;
}

@Override
public void paint(DrawContext context, int x, int y, int mouseX, int mouseY) {
int dx = (axis == Axis.HORIZONTAL) ? panelSize : 0;
int dy = (axis == Axis.HORIZONTAL) ? 0 : panelSize;

int ix = this.x + x;
int iy = this.y + y;

if (inventory != null) {
int squares = Math.min(maxSquares, inventory.getGhostInventorySize());

// Draw from ghost inventory
for(int i=0; i<squares; i++) {
ItemStack stack = inventory.getGhostItem(i);
if (!stack.isEmpty()) {
FabricSquare square = FabricSquareRegistry.forItem(stack);
if (square != null) {
paintSquare(context, ix, iy, panelSize, panelSize, square);
}
}

ix += dx;
iy += dy;
}


} else if (exemplarInventory != null) {
ItemStack exemplar = exemplarInventory.getStack(exemplarSlot);
NbtList list = FabricSquareRegistry.getStaplerData(exemplar);

int squares = Math.min(maxSquares, list.size());
for(int i=0; i<squares; i++) {
NbtCompound squareNbt = list.getCompound(i);
FabricSquare square = FabricSquare.fromCompound(squareNbt);
paintSquare(context, ix, iy, panelSize, panelSize, square);

ix += dx;
iy += dy;
}
}
}

@Environment(EnvType.CLIENT)
public void paintSquare(DrawContext context, int x, int y, int width, int height, FabricSquare square) {
AbstractTexture tex = MinecraftClient.getInstance().getTextureManager().getTexture(PlayerScreenHandler.BLOCK_ATLAS_TEXTURE);
if (tex instanceof SpriteAtlasTexture atlas) {
Sprite sprite = atlas.getSprite(square.id());
float uPx = (sprite.getMaxU() - sprite.getMinU()) / 16f;
float vPx = (sprite.getMaxV() - sprite.getMinV()) / 16f;

float uofs = uPx * square.xofs();
float vofs = vPx * square.yofs();

float minU = sprite.getMinU() + uofs;
float minV = sprite.getMinV() + vofs;
float maxU = minU + (uPx * 8);
float maxV = minV + (vPx * 8);

ScreenDrawing.texturedRect(context, x, y, width, height, PlayerScreenHandler.BLOCK_ATLAS_TEXTURE, minU, minV, maxU, maxV, square.color());
} else {
ScarvesMod.LOGGER.error("Block Atlas Texture isn't a block atlas");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,14 @@ public class FabricSquareRegistry {
register(Blocks.RED_WOOL, "minecraft:block/red_wool");
register(Blocks.BLACK_WOOL, "minecraft:block/black_wool");


register(Blocks.BEDROCK, "minecraft:block/bedrock");
register(Blocks.PRISMARINE, "minecraft:block/prismarine");
register(Blocks.PURPUR_BLOCK, "minecraft:block/purpur_block");
register(Blocks.SANDSTONE, "minecraft:block/sandstone_top");
register(Blocks.RED_SANDSTONE, "minecraft:block/red_sandstone_top");
register(Blocks.GLOWSTONE, "minecraft:block/glowstone");
register(Blocks.REDSTONE_BLOCK, new FabricSquare(new Identifier("minecraft:block/redstone_block"), 4, 4, 0xFF_FFFFFF, true));
register(Items.LAVA_BUCKET, new FabricSquare(new Identifier("minecraft:block/lava_still"), 4, 4, 0xFF_FFFFFF, true));
register(Items.WATER_BUCKET, new FabricSquare(new Identifier("minecraft:block/water_still"), 4, 4, 0xFF_4444FF, false));
}
Expand Down
Loading

0 comments on commit ed01351

Please sign in to comment.