Skip to content

Commit

Permalink
Merge pull request #4638 from LeoBeliik/TotemCurioCompat
Browse files Browse the repository at this point in the history
  • Loading branch information
IThundxr authored Feb 4, 2024
2 parents 8d21412 + 4944d69 commit b710bc6
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
import net.minecraft.world.item.enchantment.EnchantmentHelper;
import net.minecraft.world.level.Level;
import net.minecraftforge.network.NetworkHooks;

import org.jetbrains.annotations.NotNull;

import org.violetmoon.quark.addons.oddities.item.BackpackItem;
import org.violetmoon.quark.addons.oddities.module.TotemOfHoldingModule;
import org.violetmoon.quark.base.Quark;
import org.violetmoon.quark.content.tweaks.compat.TotemOfHoldingCuriosCompat;

import java.util.LinkedList;
import java.util.List;
Expand All @@ -34,6 +34,7 @@
*/
public class TotemOfHoldingEntity extends Entity {
private static final String TAG_ITEMS = "storedItems";
private static final String TAG_CURIOS = "curiosList";
private static final String TAG_DYING = "dying";
private static final String TAG_OWNER = "owner";

Expand All @@ -44,6 +45,7 @@ public class TotemOfHoldingEntity extends Entity {
private int deathTicks = 0;
private String owner;
private List<ItemStack> storedItems = new LinkedList<>();
private List<ItemStack> equipedCurios = new LinkedList<>();

public TotemOfHoldingEntity(EntityType<? extends TotemOfHoldingEntity> entityType, Level worldIn) {
super(entityType, worldIn);
Expand All @@ -58,6 +60,10 @@ public void addItem(ItemStack stack) {
storedItems.add(stack);
}

public void addCurios(ItemStack stack) {
equipedCurios.add(stack);
}

public void setOwner(Player player) {
owner = player.getUUID().toString();
}
Expand Down Expand Up @@ -106,6 +112,8 @@ public boolean skipAttackInteraction(@NotNull Entity e) {
player.setItemSlot(EquipmentSlot.OFFHAND, stack);
stack = null;
}
} else if (Quark.ZETA.isModLoaded("curios")) {
stack = TotemOfHoldingCuriosCompat.equipCurios(player, equipedCurios, stack);
}

if(stack != null)
Expand Down Expand Up @@ -163,6 +171,7 @@ private void dropEverythingAndDie() {
spawnAtLocation(storedItem, 0);

storedItems.clear();
equipedCurios.clear();

discard();
}
Expand All @@ -178,14 +187,22 @@ public boolean isDying() {
@Override
public void readAdditionalSaveData(@NotNull CompoundTag compound) {
ListTag list = compound.getList(TAG_ITEMS, 10);
ListTag curiosList = compound.getList(TAG_CURIOS, 10);
storedItems = new LinkedList<>();
equipedCurios = new LinkedList<>();

for(int i = 0; i < list.size(); i++) {
CompoundTag cmp = list.getCompound(i);
ItemStack stack = ItemStack.of(cmp);
storedItems.add(stack);
}

for (int i = 0; i < curiosList.size(); i++) {
CompoundTag cmp = curiosList.getCompound(i);
ItemStack stack = ItemStack.of(cmp);
equipedCurios.add(stack);
}

boolean dying = compound.getBoolean(TAG_DYING);
entityData.set(DYING, dying);

Expand All @@ -195,11 +212,18 @@ public void readAdditionalSaveData(@NotNull CompoundTag compound) {
@Override
protected void addAdditionalSaveData(@NotNull CompoundTag compound) {
ListTag list = new ListTag();
ListTag curiosList = new ListTag();

for(ItemStack stack : storedItems) {
list.add(stack.serializeNBT());
}

for (ItemStack equipedCurio : equipedCurios) {
curiosList.add(equipedCurio.serializeNBT());
}

compound.put(TAG_ITEMS, list);
compound.put(TAG_CURIOS, curiosList);
compound.putBoolean(TAG_DYING, isDying());
if(owner != null)
compound.putString(TAG_OWNER, owner);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.violetmoon.quark.addons.oddities.client.render.entity.TotemOfHoldingRenderer;
import org.violetmoon.quark.addons.oddities.entity.TotemOfHoldingEntity;
import org.violetmoon.quark.base.Quark;
import org.violetmoon.quark.content.tweaks.compat.TotemOfHoldingCuriosCompat;
import org.violetmoon.zeta.client.event.load.ZAddModels;
import org.violetmoon.zeta.client.event.load.ZClientSetup;
import org.violetmoon.zeta.config.Config;
Expand Down Expand Up @@ -90,6 +91,8 @@ public void onPlayerDrops(ZLivingDrops.Lowest event) {
.map(ItemEntity::getItem)
.filter(stack -> !stack.isEmpty())
.forEach(totem::addItem);
if (zeta.isModLoaded("curios"))
TotemOfHoldingCuriosCompat.saveCurios(player, totem);
if(!player.level().isClientSide)
player.level().addFreshEntity(totem);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class QuarkGeneralConfig {
"appeng.client.gui.implementations.SkyChestScreen",
"com.progwml6.ironchest.client.screen.IronChestScreen",
"net.mehvahdjukaar.supplementaries.client.screens.SackScreen",
"top.theillusivec4.curios.client.gui.CuriosScreen",
"org.violetmoon.quark.addons.oddities.client.screen.CrateScreen",
"org.violetmoon.quark.addons.oddities.client.screen.BackpackInventoryScreen"
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.violetmoon.quark.content.tweaks.compat;

import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import org.violetmoon.quark.addons.oddities.entity.TotemOfHoldingEntity;
import top.theillusivec4.curios.api.CuriosApi;
import top.theillusivec4.curios.api.type.capability.ICuriosItemHandler;

import java.util.List;
import java.util.Optional;
import java.util.stream.IntStream;

public class TotemOfHoldingCuriosCompat {

public static ItemStack equipCurios(Player player, List<ItemStack> equipedCurios, ItemStack stack) {
Optional<ICuriosItemHandler> curiosApi = CuriosApi.getCuriosInventory(player).resolve();
if (curiosApi.isPresent()) {
for (int j = 0; j < equipedCurios.size(); j++) {
ItemStack curiosItem = equipedCurios.get(j);
if (stack.is(curiosItem.getItem())) {
curiosApi.get().getEquippedCurios().setStackInSlot(j, stack);
return null;
}
}
}
return stack;
}

public static void saveCurios(Player player, TotemOfHoldingEntity totem) {
Optional<ICuriosItemHandler> curiosApi = CuriosApi.getCuriosInventory(player).resolve();
curiosApi.ifPresent(iCuriosItemHandler -> iCuriosItemHandler.getCurios().forEach((a, b) ->
IntStream.range(0, b.getStacks().getSlots()).mapToObj(i ->
b.getStacks().getPreviousStackInSlot(i)).forEach(totem::addCurios)));
}
}

0 comments on commit b710bc6

Please sign in to comment.