Skip to content
This repository has been archived by the owner on Aug 26, 2021. It is now read-only.

Commit

Permalink
Add metadata support for Thermal Expansion's morbs
Browse files Browse the repository at this point in the history
See #185
  • Loading branch information
SquidDev committed Mar 4, 2019
1 parent 2f77a63 commit adecac6
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.squiddev.plethora.integration.enderio;

import crazypants.enderio.base.EnderIO;
import crazypants.enderio.base.init.ModObject;
import crazypants.enderio.base.item.soulvial.ItemSoulVial;
import crazypants.enderio.util.CapturedMob;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityList;
import net.minecraft.entity.passive.EntityCow;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import org.squiddev.plethora.api.IWorldLocation;
import org.squiddev.plethora.api.Injects;
Expand All @@ -13,31 +17,45 @@
import org.squiddev.plethora.api.method.IPartialContext;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Injects(EnderIO.MODID)
public class IntegrationEnderIO {
public static final IMetaProvider<ItemStack> META_SOUL_VIAL = new ItemStackContextMetaProvider<ItemSoulVial>(ItemSoulVial.class) {
public static final IMetaProvider<ItemStack> META_SOUL_VIAL = new ItemStackContextMetaProvider<ItemSoulVial>(
ItemSoulVial.class,
"Provides the entity captured inside this Soul Vial."
) {
@Nonnull
@Override
public Map<Object, Object> getMeta(@Nonnull IPartialContext<ItemStack> context, @Nonnull ItemSoulVial item) {
IWorldLocation location = context.getContext(ContextKeys.ORIGIN, IWorldLocation.class);
if (location == null) return Collections.emptyMap();

CapturedMob mob = CapturedMob.create(context.getTarget());
if (mob == null) return Collections.emptyMap();

IWorldLocation location = context.getContext(ContextKeys.ORIGIN, IWorldLocation.class);
if (location == null) return getBasic(mob);

Entity entity = mob.getEntity(location.getWorld(), location.getPos(), null, false);
if (entity == null) {
Map<Object, Object> details = new HashMap<>(2);
details.put("name", mob.getTranslationName());
details.put("displayName", mob.getDisplayName());
return Collections.singletonMap("capturedEntity", details);
} else {
return Collections.singletonMap("capturedEntity", context.makePartialChild(entity).getMeta());
}
if (entity == null) return getBasic(mob);

return Collections.singletonMap("capturedEntity", context.makePartialChild(entity).getMeta());
}

private Map<Object, Object> getBasic(CapturedMob mob) {
Map<Object, Object> details = new HashMap<>(2);
details.put("name", mob.getTranslationName());
details.put("displayName", mob.getDisplayName());
return Collections.singletonMap("capturedEntity", details);
}

@Nullable
@Override
public ItemStack getExample() {
CapturedMob mob = CapturedMob.create(EntityList.getKey(EntityCow.class));
Item item = ModObject.itemSoulVial.getItem();
return mob != null && item != null ? mob.toStack(item, 1, 1) : null;
}
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package org.squiddev.plethora.integration.thermalexpansion;

import cofh.thermalexpansion.ThermalExpansion;
import cofh.thermalexpansion.item.ItemMorb;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityList;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.Vec3d;
import net.minecraftforge.common.util.Constants;
import org.squiddev.plethora.api.IWorldLocation;
import org.squiddev.plethora.api.Injects;
import org.squiddev.plethora.api.meta.IMetaProvider;
import org.squiddev.plethora.api.meta.ItemStackContextMetaProvider;
import org.squiddev.plethora.api.method.ContextKeys;
import org.squiddev.plethora.api.method.IPartialContext;
import org.squiddev.plethora.utils.Helpers;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@Injects(ThermalExpansion.MOD_ID)
public class IntegrationThermalExpansion {
public static final IMetaProvider<ItemStack> META_MORB = new ItemStackContextMetaProvider<ItemMorb>(
ItemMorb.class,
"Provides the entity captured inside this Morb."
) {

@Nonnull
@Override
public Map<Object, Object> getMeta(@Nonnull IPartialContext<ItemStack> context, @Nonnull ItemMorb item) {
NBTTagCompound entityData = context.getTarget().getTagCompound();
if (entityData == null || !entityData.hasKey("id", Constants.NBT.TAG_STRING)) return Collections.emptyMap();

IWorldLocation location = context.getContext(ContextKeys.ORIGIN, IWorldLocation.class);
if (location == null) return getBasicDetails(entityData);

Entity entity = entityData.getBoolean(ItemMorb.GENERIC)
? EntityList.createEntityByIDFromName(new ResourceLocation(entityData.getString("id")), location.getWorld())
: EntityList.createEntityFromNBT(entityData, location.getWorld());
if (entity == null) return getBasicDetails(entityData);

Vec3d loc = location.getLoc();
entity.setPositionAndRotation(loc.x, loc.y, loc.z, 0, 0);
return Collections.singletonMap("capturedEntity", context.makePartialChild(entity).getMeta());
}

private Map<Object, Object> getBasicDetails(NBTTagCompound entityData) {
String translationKey = EntityList.getTranslationName(new ResourceLocation(entityData.getString("id")));
if (translationKey == null) return Collections.emptyMap();

String translated = Helpers.translateToLocal("entity." + translationKey + ".name");

Map<Object, Object> details = new HashMap<>(2);
details.put("name", translated);
details.put("displayName",
entityData.hasKey("CustomName", Constants.NBT.TAG_STRING)
? entityData.getString("CustomName")
: translated
);
return Collections.singletonMap("capturedEntity", details);
}

@Nullable
@Override
public ItemStack getExample() {
return ItemMorb.morbList.isEmpty() ? null : ItemMorb.morbList.get(0);
}
};
}

0 comments on commit adecac6

Please sign in to comment.