Skip to content

Commit

Permalink
rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
Leclowndu93150 committed Dec 29, 2024
1 parent 1bb95d7 commit 39f74fe
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 97 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod_name=Corpse Curios Compatibility
# The license of the mod. Review your options at https://choosealicense.com/. All Rights Reserved is the default.
mod_license=MIT
# The mod version. See https://semver.org/
mod_version=1.2.0
mod_version=2.0.0
# The group ID for the mod. It is only important when publishing as an artifact to a Maven repository.
# This should match the base package used for the mod sources.
# See https://maven.apache.org/guides/mini/guide-naming-conventions.html
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import top.theillusivec4.curios.api.type.capability.ICuriosItemHandler;
import top.theillusivec4.curios.api.type.inventory.ICurioStacksHandler;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

Expand All @@ -28,72 +30,67 @@ private void onInit(int id, Inventory playerInventory, CorpseEntity corpse, bool
}
}

@Inject(method = "transferItems", at = @At("HEAD"))
@Inject(method = "transferItems", at = @At("HEAD"), cancellable = true)
private void transferItemsToCurios(CallbackInfo ci) {
if (this.cachedPlayer == null) {
return;
}
if (this.cachedPlayer == null) return;

de.maxhenkel.corpse.gui.CorpseAdditionalContainer container =
(de.maxhenkel.corpse.gui.CorpseAdditionalContainer) (Object) this;

CorpseAdditionalContainer container = (CorpseAdditionalContainer) (Object) this;
if (container == null) {
if (!container.isEditable()) {
ci.cancel();
return;
}

Optional<ICuriosItemHandler> curiosOpt = CuriosApi.getCuriosHelper().getCuriosHandler(this.cachedPlayer);
if (!curiosOpt.isPresent()) return;

if (curiosOpt.isPresent()) {
ICuriosItemHandler curiosHandler = curiosOpt.get();
ICuriosItemHandler curiosHandler = curiosOpt.get();
Map<String, ICurioStacksHandler> curios = curiosHandler.getCurios();

if (container.getItems() == null || container.getItems().isEmpty()) {
return;
List<Integer> processedSlots = new ArrayList<>();
for (int i = 0; i < container.getItems().size(); i++) {
ItemStack stack = container.getSlot(i).getItem();
if (stack.isEmpty() || CuriosApi.getCuriosHelper().getCurioTags(stack.getItem()).isEmpty()) {
continue;
}

for (int i = 0; i < container.getItems().size(); i++) {
if (container.getSlot(i) == null) {
continue;
}

ItemStack stack = container.getSlot(i).getItem();

if (stack == null || stack.isEmpty()) {
continue;
}

if (stack.getItem() == null || CuriosApi.getCuriosHelper().getCurioTags(stack.getItem()).isEmpty()) {
continue;
}

boolean itemTransferred = false;

Map<String, ICurioStacksHandler> curios = curiosHandler.getCurios();
if (curios == null) {
continue;
}

for (Map.Entry<String, ICurioStacksHandler> entry : curios.entrySet()) {
if (entry == null || entry.getValue() == null || entry.getKey() == null) {
continue;
}

ICurioStacksHandler handler = entry.getValue();
String slotType = entry.getKey();

if (CuriosApi.getCuriosHelper().getCurioTags(stack.getItem()).contains(slotType)) {
for (int slot = 0; slot < handler.getSlots(); slot++) {
ItemStack currentSlotItem = handler.getStacks().getStackInSlot(slot);

if (currentSlotItem == null || currentSlotItem.isEmpty()) {
handler.getStacks().setStackInSlot(slot, stack.copy());
boolean itemTransferred = false;
for (Map.Entry<String, ICurioStacksHandler> entry : curios.entrySet()) {
if (entry == null || entry.getValue() == null) continue;

ICurioStacksHandler handler = entry.getValue();
String slotType = entry.getKey();

if (CuriosApi.getCuriosHelper().getCurioTags(stack.getItem()).contains(slotType)) {
for (int slot = 0; slot < handler.getSlots(); slot++) {
ItemStack currentSlot = handler.getStacks().getStackInSlot(slot);

if (currentSlot.isEmpty()) {
handler.getStacks().setStackInSlot(slot, stack.copy());
container.setItem(i, 1, ItemStack.EMPTY);
itemTransferred = true;
processedSlots.add(i);
break;
} else if (currentSlot.getItem() == stack.getItem()
&& ItemStack.isSameItemSameComponents(currentSlot, stack)
&& currentSlot.getCount() < currentSlot.getMaxStackSize()) {
int canAdd = currentSlot.getMaxStackSize() - currentSlot.getCount();
int toAdd = Math.min(canAdd, stack.getCount());

currentSlot.grow(toAdd);
stack.shrink(toAdd);

if (stack.isEmpty()) {
container.setItem(i, 1, ItemStack.EMPTY);
itemTransferred = true;
processedSlots.add(i);
break;
}
}
}
if (itemTransferred) {
break;
}
}
if (itemTransferred) break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import top.theillusivec4.curios.api.type.capability.ICuriosItemHandler;
import top.theillusivec4.curios.api.type.inventory.ICurioStacksHandler;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

Expand All @@ -28,69 +30,63 @@ private void onInit(int id, Inventory playerInventory, CorpseEntity corpse, bool
}
}

@Inject(method = "transferItems", at = @At("TAIL"))
@Inject(method = "transferItems", at = @At("HEAD"))
private void transferItemsToCurios(CallbackInfo ci) {
if (this.cachedPlayer == null) {
return;
}
if (this.cachedPlayer == null) return;

CorpseInventoryContainer container = (CorpseInventoryContainer) (Object) this;
if (container == null) {
return;
}
if (!container.isEditable()) return;

Optional<ICuriosItemHandler> curiosOpt = CuriosApi.getCuriosHelper().getCuriosHandler(this.cachedPlayer);
if (!curiosOpt.isPresent()) return;

if (curiosOpt.isPresent()) {
ICuriosItemHandler curiosHandler = curiosOpt.get();
ICuriosItemHandler curiosHandler = curiosOpt.get();
Map<String, ICurioStacksHandler> curios = curiosHandler.getCurios();

if (this.cachedPlayer.getInventory() == null) {
return;
List<Integer> processedSlots = new ArrayList<>();
for (int i = 0; i < this.cachedPlayer.getInventory().getContainerSize(); i++) {
ItemStack stack = this.cachedPlayer.getInventory().getItem(i);
if (stack.isEmpty() || CuriosApi.getCuriosHelper().getCurioTags(stack.getItem()).isEmpty()) {
continue;
}

for (int i = 0; i < this.cachedPlayer.getInventory().getContainerSize(); i++) {
ItemStack stack = this.cachedPlayer.getInventory().getItem(i);

if (stack == null || stack.isEmpty()) {
continue;
}

boolean itemTransferred = false;

Map<String, ICurioStacksHandler> curios = curiosHandler.getCurios();
if (curios == null) {
continue;
}

for (Map.Entry<String, ICurioStacksHandler> entry : curios.entrySet()) {
if (entry == null || entry.getValue() == null || entry.getKey() == null) {
continue;
}

ICurioStacksHandler handler = entry.getValue();
String slotType = entry.getKey();

if (stack.getItem() == null) {
continue;
}

if (CuriosApi.getCuriosHelper().getCurioTags(stack.getItem()).contains(slotType)) {
for (int slot = 0; slot < handler.getSlots(); slot++) {
ItemStack currentSlotItem = handler.getStacks().getStackInSlot(slot);

if (currentSlotItem == null || currentSlotItem.isEmpty()) {
handler.getStacks().setStackInSlot(slot, stack.copy());
boolean itemTransferred = false;
for (Map.Entry<String, ICurioStacksHandler> entry : curios.entrySet()) {
if (entry == null || entry.getValue() == null) continue;

ICurioStacksHandler handler = entry.getValue();
String slotType = entry.getKey();

if (CuriosApi.getCuriosHelper().getCurioTags(stack.getItem()).contains(slotType)) {
for (int slot = 0; slot < handler.getSlots(); slot++) {
ItemStack currentSlot = handler.getStacks().getStackInSlot(slot);

if (currentSlot.isEmpty()) {
handler.getStacks().setStackInSlot(slot, stack.copy());
this.cachedPlayer.getInventory().setItem(i, ItemStack.EMPTY);
itemTransferred = true;
processedSlots.add(i);
break;
} else if (currentSlot.getItem() == stack.getItem()
&& ItemStack.isSameItemSameComponents(currentSlot, stack)
&& currentSlot.getCount() < currentSlot.getMaxStackSize()) {
int canAdd = currentSlot.getMaxStackSize() - currentSlot.getCount();
int toAdd = Math.min(canAdd, stack.getCount());

currentSlot.grow(toAdd);
stack.shrink(toAdd);

if (stack.isEmpty()) {
this.cachedPlayer.getInventory().setItem(i, ItemStack.EMPTY);
itemTransferred = true;
processedSlots.add(i);
break;
}
}
}
if (itemTransferred) {
break;
}
}
if (itemTransferred) break;
}
}
}
}
}

0 comments on commit 39f74fe

Please sign in to comment.