Skip to content

Commit

Permalink
Fix snail collision box and improve inventory accessibility.
Browse files Browse the repository at this point in the history
  • Loading branch information
LambdAurora committed Aug 11, 2021
1 parent ebeb8c0 commit 67c7002
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 16 deletions.
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,10 @@

### 1.0.2

- Fix shift-click behavior in snail inventory.
- Fix chests being transferable to baby snails.
- Fixed shift-click behavior in snail inventory.
- Fixed chests being transferable to baby snails.

### 1.0.3

- Added inventory page buttons to improve accessibility.
- Fixed baby snails collision box.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'fabric-loom' version '0.9.+'
id 'fabric-loom' version '0.9.25'
id 'io.github.juuxel.loom-quiltflower' version '1.1.1'
id 'java-library'
id 'maven-publish'
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ org.gradle.jvmargs=-Xmx1G

# Quilt properties
minecraft_version=1.17.1
yarn_mappings=1
yarn_mappings=37
loader_version=0.11.6
fabric_api_version=0.36.1+1.17

# Mod properties
mod_version=1.0.2
mod_version=1.0.3
maven_group=dev.lambdaurora
archives_base_name=lovely_snails
modrinth_id=hBVVhStr
Expand Down
Binary file modified images/snail_inventory.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pluginManagement {
url 'https://maven.fabricmc.net/'
}
maven {
name = 'Cotton'
url = 'https://server.bbkr.space/artifactory/libs-release/'
name 'Cotton'
url 'https://server.bbkr.space/artifactory/libs-release/'
}
gradlePluginPortal()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* Represents the snail inventory screen.
*
* @author LambdAurora
* @version 1.0.0
* @version 1.0.3
* @since 1.0.0
*/
@Environment(EnvType.CLIENT)
Expand All @@ -53,41 +53,59 @@ public class SnailInventoryScreen extends HandledScreen<SnailScreenHandler> {
private float mouseX;
private float mouseY;
private EnderChestButton enderChestButton;
private PageButton[] pageButtons = new PageButton[3];

public SnailInventoryScreen(SnailScreenHandler handler, PlayerInventory inventory, Text title) {
super(handler, inventory, handler.snail().getDisplayName());
this.backgroundWidth += 19;
this.entity = handler.snail();
}

private void clearEnderChestListener() {
private void clearListeners() {
if (this.enderChestButton != null) {
this.getScreenHandler().getInventory().removeListener(this.enderChestButton);
}
this.enderChestButton = null;

for (int page = 0; page < 3; page++) {
if (this.pageButtons[page] != null) {
this.getScreenHandler().getInventory().removeListener(this.pageButtons[page]);
this.getScreenHandler().removePageChangeListener(this.pageButtons[page]);
}

this.pageButtons[page] = null;
}
}

@Override
protected void init() {
super.init();
this.clearEnderChestListener();
this.clearListeners();

int x = (this.width - this.backgroundWidth) / 2;
int y = (this.height - this.backgroundHeight) / 2;
this.addDrawableChild(this.enderChestButton = new EnderChestButton(x + 7 + 18, y + 35 + 18));
this.getScreenHandler().getInventory().addListener(this.enderChestButton);

int buttonX = x + this.backgroundWidth - 3;
int buttonY = y + 17;
for (int page = 0; page < 3; page++) {
this.addDrawableChild(this.pageButtons[page] = new PageButton(buttonX, buttonY, page));
this.getScreenHandler().getInventory().addListener(this.pageButtons[page]);
this.getScreenHandler().addPageChangeListener(this.pageButtons[page]);
}
}

@Override
public void removed() {
super.removed();
this.clearEnderChestListener();
this.clearListeners();
}

@Override
public void onClose() {
super.onClose();
this.clearEnderChestListener();
this.clearListeners();
}

/* Input */
Expand Down Expand Up @@ -180,4 +198,31 @@ public void onInventoryChanged(Inventory sender) {
this.visible = this.active = SnailInventoryScreen.this.getScreenHandler().hasEnderChest();
}
}

private class PageButton extends TexturedButtonWidget implements InventoryChangedListener, SnailScreenHandler.InventoryPageChangeListener {
private final int page;

public PageButton(int x, int y, int page) {
super(x, y + page * 18 + 1, 15, 16, 211 + page * 15, 0, 16, TEXTURE,
256, 256,
btn -> {
SnailInventoryScreen.this.getScreenHandler().requestStoragePage(page);
});
this.page = page;

this.visible = SnailInventoryScreen.this.getScreenHandler().hasChest(this.page);
this.onCurrentPageSet(SnailInventoryScreen.this.getScreenHandler().getCurrentStoragePage());
}

@Override
public void onInventoryChanged(Inventory sender) {
this.visible = SnailInventoryScreen.this.getScreenHandler().hasChest(page);
}

@Override
public void onCurrentPageSet(int page) {
this.active = this.page != page;
this.setFocused(this.page == page);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
import net.minecraft.util.DyeColor;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.*;
Expand All @@ -83,7 +84,7 @@
* Represents the snail entity.
*
* @author LambdAurora
* @version 1.0.0
* @version 1.0.3
* @since 1.0.0
*/
public class SnailEntity extends TameableEntity implements InventoryChangedListener, Saddleable {
Expand Down Expand Up @@ -192,8 +193,27 @@ public void setSatisfaction(int satisfaction) {
* @param baseSatisfaction the base satisfaction amount
*/
public void satisfies(int baseSatisfaction) {
this.setSatisfaction(this.getSatisfaction() + baseSatisfaction + this.random.nextInt(10));
this.putInteractionOnCooldown();
if (this.isBaby()) {
this.putInteractionOnCooldown();
int newSatisfaction = this.getSatisfaction() + baseSatisfaction + this.random.nextInt(10);

if (newSatisfaction >= 0) {
var adultDimensions = this.getType().getDimensions();
float width = adultDimensions.width * .8f;
float eyeHeight = this.getEyeHeight(EntityPose.STANDING, adultDimensions);
var box = Box.of(new Vec3d(this.getX(), this.getY() + eyeHeight, this.getZ()), width, 1.0E-6, width);

// Adult form will suffocate, so we must prevent the growth until the player moves the snail.
boolean willSuffocate = this.world.getBlockCollisions(this, box, (state, pos) -> state.shouldSuffocate(this.world, pos))
.findAny().isPresent();
if (willSuffocate) {
this.world.sendEntityStatus(this, (byte) 10);
return;
}
}

this.setSatisfaction(newSatisfaction);
}

this.world.sendEntityStatus(this, (byte) 8);
}
Expand Down Expand Up @@ -267,6 +287,15 @@ public void handleStatus(byte status) {
this.getParticleX(1.0), this.getRandomBodyY() + 0.5, this.getParticleZ(1.0),
xOffset, yOffset, zOffset);
}
} else if (status == 10) {
for (int i = 0; i < 7; ++i) {
double xOffset = this.random.nextGaussian() * 0.02;
double yOffset = this.random.nextGaussian() * 0.02;
double zOffset = this.random.nextGaussian() * 0.02;
this.world.addParticle(this.random.nextBoolean() ? ParticleTypes.ANGRY_VILLAGER : ParticleTypes.SMOKE,
this.getParticleX(1.0), this.getRandomBodyY() + 0.5, this.getParticleZ(1.0),
xOffset, yOffset, zOffset);
}
} else
super.handleStatus(status);
}
Expand Down Expand Up @@ -770,6 +799,7 @@ public boolean isBaby() {
public void setBaby(boolean baby) {
var wasBaby = this.dataTracker.get(CHILD);
this.dataTracker.set(CHILD, baby);
this.calculateDimensions();

if (wasBaby && !baby && !this.reading) {
this.onGrowUp();
Expand All @@ -782,7 +812,7 @@ public void setBaby(boolean baby) {
public PassiveEntity createChild(ServerWorld world, PassiveEntity otherParent) {
var child = LovelySnailsRegistry.SNAIL_ENTITY_TYPE.create(world);

if (otherParent instanceof SnailEntity otherSnail) {
if (otherParent instanceof SnailEntity) {
if (this.isTamed()) {
child.setOwnerUuid(this.getOwnerUuid());
child.setTamed(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@
import net.minecraft.screen.slot.Slot;
import net.minecraft.server.network.ServerPlayerEntity;

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

public class SnailScreenHandler extends ScreenHandler implements InventoryChangedListener {
private final PlayerEntity player;
private final SimpleInventory inventory;
private final SnailEntity entity;
private final List<InventoryPageChangeListener> pageChangeListeners = new ArrayList<>();
private int currentStoragePage;

public SnailScreenHandler(int syncId, PlayerInventory playerInventory, PacketByteBuf buf) {
Expand Down Expand Up @@ -171,6 +175,10 @@ public void setCurrentStoragePage(int page) {
buffer.writeByte(page);
ServerPlayNetworking.send(serverPlayerEntity, LovelySnailsRegistry.SNAIL_SET_STORAGE_PAGE, buffer);
}

for (var listener : this.pageChangeListeners) {
listener.onCurrentPageSet(page);
}
}

/**
Expand Down Expand Up @@ -201,6 +209,14 @@ public static int getOpeningStoragePage(Inventory inventory) {
return 0;
}

public void addPageChangeListener(InventoryPageChangeListener listener) {
this.pageChangeListeners.add(listener);
}

public void removePageChangeListener(InventoryPageChangeListener listener) {
this.pageChangeListeners.remove(listener);
}

@Override
public boolean canUse(PlayerEntity player) {
return !this.entity.isInventoryDifferent(this.inventory)
Expand Down Expand Up @@ -308,9 +324,17 @@ public void onInventoryChanged(Inventory sender) {
}
default -> getOpeningStoragePage(this.getInventory());
};

for (var listener : this.pageChangeListeners) {
listener.onCurrentPageSet(this.currentStoragePage);
}
}
}

public interface InventoryPageChangeListener {
void onCurrentPageSet(int page);
}

private static class SaddleSlot extends Slot {
private final SnailEntity snail;

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 67c7002

Please sign in to comment.