Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes Trinkets #3

Open
wants to merge 3 commits into
base: 1.20.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ repositories {
}
maven {
name = 'Ladysnake Mods'
url = 'https://ladysnake.jfrog.io/artifactory/mods'
url = 'https://maven.ladysnake.org/releases'
}
maven { url "https://maven.shedaniel.me/" }
}
Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ org.gradle.jvmargs=-Xmx3G

minecraft_version=1.20.1
yarn_mappings=1.20.1+build.2
loader_version=0.14.21
loader_version=0.15.7

# Mod Properties
mod_version = v1.15
Expand All @@ -15,7 +15,7 @@ archives_base_name = gravestones

# Dependencies
# currently not on the main fabric site, check on the maven: https://maven.fabricmc.net/net/fabricmc/fabric-api/fabric-api
fabric_version=0.83.1+1.20.1
fabric_version=0.91.0+1.20.1
cloth_config_version=11.0.99

# https://modrinth.com/mod/modmenu
Expand Down
61 changes: 42 additions & 19 deletions src/main/java/net/guavy/gravestones/compat/TrinketsCompat.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import dev.emi.trinkets.api.TrinketInventory;
import dev.emi.trinkets.api.TrinketsApi;
import java.util.Collection;
import java.util.function.Consumer;
import net.guavy.gravestones.api.GravestonesApi;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.item.ItemStack;

import java.util.ArrayList;
Expand All @@ -16,29 +18,36 @@ public class TrinketsCompat implements GravestonesApi {
public List<ItemStack> getInventory(PlayerEntity entity) {
List<ItemStack> itemStacks = new ArrayList<>();

Map<String, Map<String, TrinketInventory>> inventories = TrinketsApi.getTrinketComponent(entity).get().getInventory();
for (Map<String, TrinketInventory> map : inventories.values()) {
for (TrinketInventory inventory : map.values()) {
for(int i = 0; i < inventory.size(); i++) {
itemStacks.add(inventory.getStack(i));
}
}
}
forEachTrinket(entity, itemStacks::add);
return itemStacks;
}

@Override
public void setInventory(List<ItemStack> stacks, PlayerEntity entity) {
for(ItemStack itemStack : stacks) {
Map<String, Map<String, TrinketInventory>> inventories = TrinketsApi.getTrinketComponent(entity).get().getInventory();
for (Map<String, TrinketInventory> map : inventories.values()) {
for (TrinketInventory inventory : map.values()) {
for(int i = 0; i < inventory.size(); i++) {
if (inventory.getStack(i).isEmpty()) {
inventory.setStack(i, itemStack);
PlayerInventory playerInventory = entity.getInventory();
Collection<Map<String, TrinketInventory>> inventories = getTrinketInventory(entity).values();

// Yes this is unbelievably awful, I am both sorry and not sorry... schrodinger's apology
for (int i = 0; i < stacks.size(); i++) {
ItemStack item = stacks.get(i);

int size = 0;

for (Map<String, TrinketInventory> inventoryMap : inventories) {
for (TrinketInventory inventory : inventoryMap.values()) {
for (int j = 0; j < inventory.size(); j++, size++) {
//GLFW.glfwSetInputMode(MinecraftClient.getInstance().getWindow().getHandle(), GLFW_CURSOR, GLFW_CURSOR_NORMAL);

if (i != size) {
continue;
}

if (inventory.getStack(j).isEmpty()) {
inventory.setStack(j, item);
continue;
}
entity.getInventory().insertStack(itemStack);

playerInventory.offerOrDrop(item);

}
}
Expand All @@ -48,10 +57,24 @@ public void setInventory(List<ItemStack> stacks, PlayerEntity entity) {

@Override
public int getInventorySize(PlayerEntity entity) {
return TrinketsApi.getTrinketComponent(entity).get().getInventory().size();
return getTrinketInventory(entity).size();
}

public static void dropAll(PlayerEntity entity) {
TrinketsApi.getTrinketComponent(entity).get().getInventory().clear();
getTrinketInventory(entity).clear();
}

private static void forEachTrinket(PlayerEntity entity, Consumer<ItemStack> callable) {
for (Map<String, TrinketInventory> map : getTrinketInventory(entity).values()) {
for (TrinketInventory inventory : map.values()) {
for(int i = 0; i < inventory.size(); i++) {
callable.accept(inventory.getStack(i));
}
}
}
}

private static Map<String, Map<String, TrinketInventory>> getTrinketInventory(PlayerEntity entity) {
return TrinketsApi.getTrinketComponent(entity).get().getInventory();
}
}
35 changes: 35 additions & 0 deletions src/main/java/net/guavy/gravestones/mixin/LivingEntityMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.guavy.gravestones.mixin;

import net.fabricmc.loader.api.FabricLoader;
import net.guavy.gravestones.Gravestones;
import net.guavy.gravestones.compat.TrinketsCompat;
import net.guavy.gravestones.config.GravestonesConfig;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.world.World;
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.CallbackInfo;

@Mixin(LivingEntity.class)
public abstract class LivingEntityMixin extends Entity {
protected LivingEntityMixin(EntityType<? extends LivingEntity> type, World world) {
super(type, world);
}

@Inject(at = @At(value = "HEAD"), method = "drop", cancellable = true)
private void onDrop(DamageSource damageSource, CallbackInfo ci) {
if (GravestonesConfig.getConfig().mainSettings.enableGraves && ((LivingEntity) (Object) this) instanceof PlayerEntity player) {
Gravestones.placeGrave(this.getWorld(), this.getPos(), player);

if (FabricLoader.getInstance().isModLoaded("trinkets"))
TrinketsCompat.dropAll(player);

ci.cancel();
}
}
}
39 changes: 0 additions & 39 deletions src/main/java/net/guavy/gravestones/mixin/PlayerEntityMixin.java

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/resources/gravestones.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"compatibilityLevel": "JAVA_16",
"mixins": [
"ExplosionMixin",
"PlayerEntityMixin"
"LivingEntityMixin"
],
"client": [
"ClientPlayerInteractionManagerMixin"
Expand Down