This repository has been archived by the owner on Aug 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add metadata support for Thermal Expansion's morbs
See #185
- Loading branch information
Showing
2 changed files
with
104 additions
and
12 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
74 changes: 74 additions & 0 deletions
74
.../java/org/squiddev/plethora/integration/thermalexpansion/IntegrationThermalExpansion.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,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); | ||
} | ||
}; | ||
} |