Skip to content

Commit

Permalink
Backport Accessories compat combined with other fixes/adjustments for…
Browse files Browse the repository at this point in the history
… erroring (#169)

The given commit is a backport of e339e4c for 1.20

Co-authored-by: Patbox <39821509+Patbox@users.noreply.github.com>
  • Loading branch information
Dragon-Seeker and Patbox authored Aug 6, 2024
1 parent fe08a33 commit 54334a5
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 19 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ repositories {
url 'https://maven.gegy.dev'
}

maven { url "https://maven.wispforest.io/releases" }

mavenCentral()
mavenLocal()
}
Expand All @@ -67,6 +69,7 @@ dependencies {

modImplementation include("me.lucko:fabric-permissions-api:0.2-SNAPSHOT")

modCompileOnly "io.wispforest:accessories-fabric:1.0.0-beta.26+1.20.1"
modCompileOnly "dev.emi:trinkets:3.0.4"
//modCompileOnly "modrinth.maven:trinkets:3.0.4"
modCompileOnly files("modsWithCompat/inventorio-1.17-fabric-1.6.2.jar")
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fabric_version=0.83.0+1.20.1


# Mod Properties
mod_version = 3.0.1+1.20.1
mod_version = 3.1.0+1.20.1
maven_group = eu.pb4
archives_base_name = graves

Expand Down
16 changes: 10 additions & 6 deletions src/main/java/eu/pb4/graves/GravesMod.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package eu.pb4.graves;

import eu.pb4.common.protection.api.CommonProtection;
import eu.pb4.graves.compat.GomlCompat;
import eu.pb4.graves.compat.InventorioCompat;
import eu.pb4.graves.compat.SaveGearOnDeathCompat;
import eu.pb4.graves.compat.TrinketsCompat;
import eu.pb4.graves.compat.*;
import eu.pb4.graves.config.ConfigManager;
import eu.pb4.graves.grave.GraveManager;
import eu.pb4.graves.other.Commands;
Expand Down Expand Up @@ -87,8 +84,11 @@ public void onInitialize() {
if (loader.isModLoaded("inventorio")) {
InventorioCompat.register();
}
if (loader.isModLoaded("accessories")) {
AccessoriesCompat.register();
}
if (loader.isModLoaded("trinkets")) {
TrinketsCompat.register();
TrinketsCompat.register(loader.isModLoaded("tclayer"));
}
if (loader.isModLoaded("sgod")) {
SaveGearOnDeathCompat.register();
Expand All @@ -113,7 +113,11 @@ public void onInitialize() {
var copied = new ArrayList<>(DO_ON_NEXT_TICK);
DO_ON_NEXT_TICK.clear();
for (var c : copied) {
c.run();
try {
c.run();
} catch (Throwable e) {
GravesMod.LOGGER.error("Error occurred while executing delayed task!", e);
}
}
});
}
Expand Down
135 changes: 135 additions & 0 deletions src/main/java/eu/pb4/graves/compat/AccessoriesCompat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package eu.pb4.graves.compat;

import eu.pb4.graves.GravesApi;
import eu.pb4.graves.grave.GraveInventoryMask;
import eu.pb4.graves.other.VanillaInventoryMask;
import io.wispforest.accessories.api.AccessoriesAPI;
import io.wispforest.accessories.api.AccessoriesCapability;
import io.wispforest.accessories.api.DropRule;
import io.wispforest.accessories.api.events.OnDropCallback;
import io.wispforest.accessories.api.slot.SlotReference;
import io.wispforest.accessories.impl.ExpandedSimpleContainer;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

public class AccessoriesCompat implements GraveInventoryMask {
private static final String TYPE_TAG = "Type";
private static final String SLOT_TAG = "Slot";

public static void register() {
GravesApi.registerInventoryMask(Identifier.of("universal_graves", "accessories"), new AccessoriesCompat());
}

@Override
public void addToGrave(ServerPlayerEntity player, ItemConsumer consumer) {
var cap = AccessoriesCapability.get(player);
if (cap == null) {
return;
}

cap.getContainers().forEach((s, accessoriesContainer) -> {
var defRule = accessoriesContainer.slotType() != null ? Objects.requireNonNull(accessoriesContainer.slotType()).dropRule() : DropRule.DEFAULT;
addToGrave(player, consumer, s, accessoriesContainer.getAccessories(), "", defRule);
addToGrave(player, consumer, s, accessoriesContainer.getCosmeticAccessories(), "cosmetic", defRule);
});
}

private void addToGrave(ServerPlayerEntity player, ItemConsumer consumer, String slotName, ExpandedSimpleContainer accessories, String type, DropRule defaultDropRule) {
var dmg = player.getRecentDamageSource();
if (dmg == null) {
dmg = player.getDamageSources().generic();
}
for (var i = 0; i < accessories.size(); i++) {
var stack = accessories.getStack(i);
if (stack.isEmpty() || !GravesApi.canAddItem(player, stack)) {
return;
}

var ref = SlotReference.of(player, slotName, i);

var dropRule = AccessoriesAPI.getOrDefaultAccessory(stack).getDropRule(stack, ref, dmg);

dropRule = OnDropCallback.EVENT.invoker().onDrop(dropRule, stack, ref, dmg);

if (dropRule == DropRule.DEFAULT) {
dropRule = defaultDropRule;
}

if (dropRule == DropRule.DEFAULT) {
if (EnchantmentHelper.hasVanishingCurse(stack)) {
dropRule = DropRule.DESTROY;
} else {
dropRule = DropRule.DROP;
}
}

if (dropRule == DropRule.DROP) {
var nbt = new NbtCompound();
nbt.putString(TYPE_TAG, type);
nbt.putString(SLOT_TAG, slotName);

consumer.addItem(stack.copy(), i, nbt);
accessories.setStack(i, ItemStack.EMPTY);
}
}
}

@Override
public boolean moveToPlayerExactly(ServerPlayerEntity player, ItemStack stack, int slot, NbtElement extraData) {
var typeId = ((NbtCompound) extraData).getString(TYPE_TAG);
var slotId = ((NbtCompound) extraData).getString(SLOT_TAG);

var inventory = getInventory(player, typeId, slotId);

if (inventory != null) {
if (inventory.getStack(slot).isEmpty()) {
inventory.setStack(slot, stack.copyAndEmpty());
return true;
}
}
return false;
}

@Override
public boolean moveToPlayerClosest(ServerPlayerEntity player, ItemStack stack, int slot, NbtElement data) {
var type = ((NbtCompound) data).getString(TYPE_TAG);
var slotId = ((NbtCompound) data).getString(SLOT_TAG);

var inventory = getInventory(player, type, slotId);

if (inventory != null) {
int size = inventory.size();

for (int i = 0; i < size; i++) {
if (inventory.getStack(i).isEmpty()) {
inventory.setStack(i, stack.copyAndEmpty());
return true;
}
}
}
return false;
}

@Nullable
private ExpandedSimpleContainer getInventory(ServerPlayerEntity player, String type, String slotId) {
var cap = AccessoriesCapability.get(player);
if (cap == null) {
return null;
}

var slot = cap.getContainers().get(slotId);

if(slot == null) return null;

return ("cosmetic".equals(type))
? slot.getCosmeticAccessories()
: slot.getAccessories();
}
}
17 changes: 9 additions & 8 deletions src/main/java/eu/pb4/graves/compat/TrinketsCompat.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;

public class TrinketsCompat extends VanillaInventoryMask {
public static final GraveInventoryMask INSTANCE = new TrinketsCompat();
public record TrinketsCompat(boolean preventAddition) implements GraveInventoryMask {
private static final String GROUP_TAG = "Group";
private static final String SLOT_TAG = "Slot";

public static void register() {
GravesApi.registerInventoryMask(new Identifier("universal_graves", "trinkets"), INSTANCE);
public static void register(boolean preventAddition) {
GravesApi.registerInventoryMask(new Identifier("universal_graves", "trinkets"), new TrinketsCompat(preventAddition));
}

@Override
public void addToGrave(ServerPlayerEntity player, ItemConsumer consumer) {
if (preventAddition) {
return;
}

TrinketsApi.getTrinketComponent(player).ifPresent(trinkets -> trinkets.forEach((ref, stack) -> {
if (stack.isEmpty() || !GravesApi.canAddItem(player, stack)) {
return;
Expand Down Expand Up @@ -68,8 +71,7 @@ public boolean moveToPlayerExactly(ServerPlayerEntity player, ItemStack stack, i

if (inventory != null) {
if (inventory.getStack(slot).isEmpty()) {
inventory.setStack(slot, stack.copy());
stack.setCount(0);
inventory.setStack(slot, stack.copyAndEmpty());
return true;
}
}
Expand All @@ -88,8 +90,7 @@ public boolean moveToPlayerClosest(ServerPlayerEntity player, ItemStack stack, i

for (int i = 0; i < size; i++) {
if (inventory.getStack(i).isEmpty()) {
inventory.setStack(i, stack.copy());
stack.setCount(0);
inventory.setStack(i, stack.copyAndEmpty());
return true;
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/eu/pb4/graves/mixin/LivingEntityMixin.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package eu.pb4.graves.mixin;

import eu.pb4.graves.GravesMod;
import eu.pb4.graves.other.GraveUtils;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
Expand All @@ -18,8 +21,10 @@ private void replaceWithGrave(DamageSource source, CallbackInfo ci) {
if (((Object) this) instanceof ServerPlayerEntity player) {
try {
GraveUtils.createGrave(player, source);
} catch (Exception e) {
e.printStackTrace();
} catch (Throwable e) {
player.sendMessage(Text.literal("Failed to create a grave due to an exception! See logs for more information and report it!").formatted(Formatting.RED));
player.sendMessage(Text.literal(e.toString()).formatted(Formatting.RED));
GravesMod.LOGGER.error("Failed to create a grave due to an exception!", e);
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/eu/pb4/graves/other/GraveUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,11 @@ public static void createGrave(ServerPlayerEntity player, DamageSource source) {
List<PositionedItemStack> items = new ArrayList<>();

for (var mask : GravesApi.getAllInventoryMasks()) {
mask.addToGrave(player, (stack, slot, nbt, tags) -> items.add(new PositionedItemStack(stack, slot, mask, nbt, Set.of(tags))));
try {
mask.addToGrave(player, (stack, slot, nbt, tags) -> items.add(new PositionedItemStack(stack, slot, mask, nbt, Set.of(tags))));
} catch (Throwable e) {
GravesMod.LOGGER.error("Failed to add items from '{}'!", mask.getId(), e);
}
}

int experience = 0;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/eu/pb4/graves/registry/GraveBlock.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package eu.pb4.graves.registry;

import eu.pb4.graves.GravesMod;
import eu.pb4.graves.config.ConfigManager;
import eu.pb4.graves.grave.Grave;
import eu.pb4.graves.other.VisualGraveData;
Expand Down Expand Up @@ -72,7 +73,7 @@ public void onBreak(World world, BlockPos pos, BlockState state, PlayerEntity pl
graveBlockEntity.breakBlock();
}
} catch (Exception e) {
e.printStackTrace();
GravesMod.LOGGER.error("Exception occurred while breaking grave!", e);
}
}
super.onBreak(world, pos, state, player);
Expand Down

0 comments on commit 54334a5

Please sign in to comment.