-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backport Accessories compat combined with other fixes/adjustments for…
… 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
1 parent
fe08a33
commit 54334a5
Showing
8 changed files
with
172 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
src/main/java/eu/pb4/graves/compat/AccessoriesCompat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters