#2775 Adding Trinket support for BatpackItem. Thanks to SimonFlapse

* #2775 Adding trinket support for BatpackItem

* Added support for wearing BatpackItems in the 'back' trinket slot on the chest. This frees up an additional space for regular armor.

* BatpackItem only output when in chest armor slot
This commit is contained in:
Simon 2022-03-30 11:21:29 +02:00 committed by GitHub
parent 4fc9352c76
commit e28c9176e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 115 additions and 18 deletions

View file

@ -280,18 +280,40 @@ public class ItemUtils {
}
for (int i = 0; i < player.getInventory().size(); i++) {
ItemStack invStack = player.getInventory().getStack(i);
if (invStack.isEmpty() || !filter.test(invStack)) {
continue;
}
EnergyStorageUtil.move(
sourceStorage,
ContainerItemContext.ofPlayerSlot(player, playerInv.getSlots().get(i)).find(EnergyStorage.ITEM),
maxOutput,
null
);
transferPower(player, i, sourceStorage, maxOutput, filter);
}
}
/**
* Output energy from EnergyStorage to other EnergyStorage at slot index.
* <br> <br>
* It is recommended to use {@link ItemUtils#distributePowerToInventory(PlayerEntity, ItemStack, long, Predicate)}
* <br> <br>
* Only outputs energy if the item in the provided slot can accept power
*
* @param player {@link PlayerEntity} Player having powered item
* @param slot {@code int} Slot of the targeted item
* @param sourceStorage {@link EnergyStorage} EnergyStorage to output from
* @param maxOutput {@code int} Maximum output rate of powered item
* @param filter {@link Predicate} Filter for items to output to
* @return The actual amount of energy transferred, can be zero (0) if the targeted item does not support power input
*/
public static long transferPower(PlayerEntity player, int slot, EnergyStorage sourceStorage, long maxOutput, Predicate<ItemStack> filter) {
PlayerInventoryStorage playerInv = PlayerInventoryStorage.of(player);
ItemStack invStack = player.getInventory().getStack(slot);
if (invStack.isEmpty() || !filter.test(invStack)) {
return 0;
}
EnergyStorage receiverStorage = ContainerItemContext.ofPlayerSlot(player, playerInv.getSlots().get(slot)).find(EnergyStorage.ITEM);
return EnergyStorageUtil.move(
sourceStorage,
receiverStorage,
maxOutput,
null
);
}
}

View file

@ -45,6 +45,16 @@ repositories {
content {
// Trinkets dependency
includeGroup "io.github.onyxstudios.Cardinal-Components-API"
includeGroup "dev.onyxstudios.cardinal-components-api"
}
}
// Needed because of an error in the POM file in the Trinkets implementation
// See https://github.com/emilyploszaj/trinkets/issues/159
maven {
name = "TerraformersMC"
url = "https://maven.terraformersmc.com/"
content {
includeGroup("com.terraformersmc")
}
}
maven {
@ -184,7 +194,7 @@ dependencies {
include project(":RebornCore")
optionalDependency "me.shedaniel:RoughlyEnoughItems-fabric:${project.rei_version}"
disabledOptionalDependency "com.github.emilyploszaj:trinkets:${project.trinkets_version}"
optionalDependency "com.github.emilyploszaj:trinkets:${project.trinkets_version}"
disabledOptionalDependency "net.oskarstrom:DashLoader:${project.dashloader_version}"
// Use groovy for datagen/gametest, if you are copying this you prob dont want it.
@ -304,8 +314,10 @@ task renameCrowdin(type: Copy, dependsOn: ['crowdin', 'cleanCrowdin']){
}
}
import groovy.json.JsonSlurper
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import groovy.util.XmlSlurper
task fixTranslations(dependsOn: ['renameCrowdin']) {
description "Remove all translations that do not have an entry, ensures that minecraft falls back to EN_US over writing out an empty string"
@ -321,7 +333,8 @@ task fixTranslations(dependsOn: ['renameCrowdin']) {
}
}
import groovy.util.XmlSlurper
import org.kohsuke.github.GHReleaseBuilder
curseforge {
if (ENV.CURSEFORGE_API_KEY) {
@ -360,7 +373,7 @@ def getBranch() {
return "unknown"
}
import org.kohsuke.github.GHReleaseBuilder
import org.kohsuke.github.GitHub
task github(dependsOn: remapJar) {

View file

@ -14,5 +14,5 @@ org.gradle.jvmargs=-Xmx2G
# Dependencies
energy_version=2.2.0
rei_version=8.0.438
trinkets_version=3.0.2
trinkets_version=3.3.0
dashloader_version=2.0

View file

@ -24,22 +24,30 @@
package techreborn.items.armor;
import dev.emi.trinkets.api.SlotReference;
import dev.emi.trinkets.api.Trinket;
import dev.emi.trinkets.api.TrinketsApi;
import net.fabricmc.fabric.api.transfer.v1.context.ContainerItemContext;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ArmorItem;
import net.minecraft.item.ArmorMaterial;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.ItemStack;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.world.World;
import reborncore.common.powerSystem.RcEnergyItem;
import reborncore.common.powerSystem.RcEnergyTier;
import reborncore.common.util.ItemUtils;
import team.reborn.energy.api.EnergyStorage;
import techreborn.TechReborn;
import techreborn.utils.InitUtils;
public class BatpackItem extends ArmorItem implements RcEnergyItem {
public class BatpackItem extends ArmorItem implements RcEnergyItem, Trinket {
public final int maxCharge;
public final RcEnergyTier tier;
@ -48,6 +56,32 @@ public class BatpackItem extends ArmorItem implements RcEnergyItem {
super(material, EquipmentSlot.CHEST, new Settings().group(TechReborn.ITEMGROUP).maxCount(1).maxDamage(-1));
this.maxCharge = maxCharge;
this.tier = tier;
registerTrinket();
}
//Trinket
@Override
public void tick(ItemStack stack, SlotReference slot, LivingEntity entity) {
if (entity instanceof ServerPlayerEntity) {
EnergyStorage sourceStorage = ContainerItemContext.withInitial(stack).find(EnergyStorage.ITEM);
ServerPlayerEntity player = (ServerPlayerEntity) entity;
for (int i = 0; i < player.getInventory().size(); i++) {
long amountBefore = sourceStorage.getAmount();
long accepted = ItemUtils.transferPower(player, i, sourceStorage, tier.getMaxOutput(), (itemStack) -> true );
long expectedAfter = amountBefore - accepted;
if (amountBefore != expectedAfter) {
((BatpackItem) stack.getItem()).setStoredEnergy(stack, expectedAfter);
}
}
}
}
// Item
@ -56,6 +90,12 @@ public class BatpackItem extends ArmorItem implements RcEnergyItem {
if (worldIn.isClient) {
return;
}
// vanilla default for chest inventory slot
if (itemSlot != 2) {
return;
}
if (entityIn instanceof PlayerEntity) {
ItemUtils.distributePowerToInventory((PlayerEntity) entityIn, stack, tier.getMaxOutput());
}
@ -104,4 +144,11 @@ public class BatpackItem extends ArmorItem implements RcEnergyItem {
public int getItemBarStep(ItemStack stack) {
return ItemUtils.getPowerForDurabilityBar(stack);
}
private void registerTrinket() {
// Ensures that trinkets are only registered if the mod is installed
if (FabricLoader.getInstance().isModLoaded("trinkets")) {
TrinketsApi.registerTrinket(this, this);
}
}
}

View file

@ -0,0 +1,8 @@
{
"entities": [
"player"
],
"slots": [
"chest/back"
]
}

View file

@ -0,0 +1,7 @@
{
"replace": false,
"values": [
"techreborn:lithium_ion_batpack",
"techreborn:lapotronic_orbpack"
]
}