Skip to content

Commit

Permalink
Providers from TOPExtras
Browse files Browse the repository at this point in the history
  • Loading branch information
strubium committed Feb 17, 2024
1 parent 8926464 commit 7d0368c
Show file tree
Hide file tree
Showing 4 changed files with 219 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package mcjty.theoneprobe.apiimpl.providers;

import mcjty.theoneprobe.api.IProbeHitEntityData;
import mcjty.theoneprobe.api.IProbeInfo;
import mcjty.theoneprobe.api.IProbeInfoEntityProvider;
import mcjty.theoneprobe.api.ProbeMode;
import net.minecraft.entity.Entity;
import net.minecraft.entity.passive.AbstractChestHorse;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.World;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import topextras.Utilities;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ChestHorseInfoProvider implements IProbeInfoEntityProvider {

private final List<ItemStack> stacks = new ArrayList<>();
private final Set<Item> foundItems = new HashSet<>();

@Override
public String getID() {
return Utilities.getProviderId("chest_horse");
}

@Override
public void addProbeEntityInfo(ProbeMode mode, IProbeInfo probeInfo, EntityPlayer player, World world, Entity entity, IProbeHitEntityData data) {
// hopper and chest minecarts
if (entity instanceof AbstractChestHorse && ((AbstractChestHorse) entity).hasChest()) {
int maxSlots;
if (entity.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)) {
IItemHandler capability = entity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
if (capability == null) {
stacks.clear();
foundItems.clear();
return;
}

maxSlots = capability.getSlots();
// start at 3 to ignore armor/saddle
for (int i = 3; i < maxSlots; i++) {
Utilities.addItemStack(stacks, foundItems, capability.getStackInSlot(i));
}
} else {
NBTTagCompound compound = entity.writeToNBT(new NBTTagCompound());
if (!compound.hasKey("Items")) {
stacks.clear();
foundItems.clear();
return;
}

NBTTagList list = compound.getTagList("Items", 10);
for (int i = 0; i < list.tagCount(); i++) {
NBTTagCompound tagCompound = list.getCompoundTagAt(i);
int slot = tagCompound.getByte("Slot") & 255;

// start at 3 to ignore armor/saddle
if (slot > 2) Utilities.addItemStack(stacks, foundItems, new ItemStack(tagCompound));
}
}
if (!stacks.isEmpty()) Utilities.showChestContents(probeInfo, stacks, mode);
stacks.clear();
foundItems.clear();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package mcjty.theoneprobe.apiimpl.providers;

import mcjty.theoneprobe.api.*;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntityEnchantmentTable;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeHooks;
import topextras.Utilities;

import javax.annotation.Nonnull;

public class EnchantingPowerInfoProvider implements IProbeInfoProvider {

private static final ItemStack ENCHANTED_BOOK = new ItemStack(Items.ENCHANTED_BOOK);

@Override
public String getID() {
return Utilities.getProviderId("enchanting_power");
}

@Override
public void addProbeInfo(ProbeMode mode, @Nonnull IProbeInfo probeInfo, EntityPlayer player, @Nonnull World world, @Nonnull IBlockState blockState, @Nonnull IProbeHitData data) {
float enchantingPower = ForgeHooks.getEnchantPower(world, data.getPos());
if (blockState.getBlock().hasTileEntity(blockState) && world.getTileEntity(data.getPos()) instanceof TileEntityEnchantmentTable) {
BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos(data.getPos());
enchantingPower = 0.0F;
for (int x = -1; x <= 1; x++) {
for (int z = -1; z <= 1; z++) {
if ((x != 0 || z != 0) && world.isAirBlock(pos.add(z, 0, x)) && world.isAirBlock(pos.add(z, 1, x))) {
enchantingPower += ForgeHooks.getEnchantPower(world, pos.add(z * 2, 0, x * 2));
enchantingPower += ForgeHooks.getEnchantPower(world, pos.add(z * 2, 1, x * 2));
if (z != 0 && x != 0) {
enchantingPower += ForgeHooks.getEnchantPower(world, pos.add(z * 2, 0, x));
enchantingPower += ForgeHooks.getEnchantPower(world, pos.add(z * 2, 1, x));
enchantingPower += ForgeHooks.getEnchantPower(world, pos.add(z, 0, x * 2));
enchantingPower += ForgeHooks.getEnchantPower(world, pos.add(z, 1, x * 2));
}
}
}
}
}
if (enchantingPower > 0.0F) {
probeInfo.horizontal(probeInfo.defaultLayoutStyle().alignment(ElementAlignment.ALIGN_CENTER))
.item(ENCHANTED_BOOK)
.text(TextStyleClass.LABEL + "{*topextras.top.enchanting_power*} " + TextFormatting.LIGHT_PURPLE + Utilities.FORMAT.format(enchantingPower));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package mcjty.theoneprobe.apiimpl.providers;

import mcjty.theoneprobe.api.IProbeHitEntityData;
import mcjty.theoneprobe.api.IProbeInfo;
import mcjty.theoneprobe.api.IProbeInfoEntityProvider;
import mcjty.theoneprobe.api.ProbeMode;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityMinecartContainer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import topextras.Utilities;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class MinecartInfoProvider implements IProbeInfoEntityProvider {

private final List<ItemStack> stacks = new ArrayList<>();
private final Set<Item> foundItems = new HashSet<>();

@Override
public String getID() {
return Utilities.getProviderId("minecart");
}

@Override
public void addProbeEntityInfo(ProbeMode mode, IProbeInfo probeInfo, EntityPlayer player, World world, Entity entity, IProbeHitEntityData data) {
// hopper and chest minecarts
if (entity instanceof EntityMinecartContainer) {
int maxSlots;
if (entity.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null)) {
IItemHandler capability = entity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, null);
if (capability == null) {
stacks.clear();
foundItems.clear();
return;
}

maxSlots = capability.getSlots();
for (int i = 0; i < maxSlots; i++) {
Utilities.addItemStack(stacks, foundItems, capability.getStackInSlot(i));
}
} else {
IInventory inventory = (IInventory) entity;
maxSlots = inventory.getSizeInventory();
for (int i = 0; i < maxSlots; i++) {
Utilities.addItemStack(stacks, foundItems, inventory.getStackInSlot(i));
}
}
if (!stacks.isEmpty()) Utilities.showChestContents(probeInfo, stacks, mode);
stacks.clear();
foundItems.clear();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package mcjty.theoneprobe.apiimpl.providers;

import mcjty.theoneprobe.api.*;
import net.minecraft.entity.Entity;
import net.minecraft.entity.item.EntityPainting;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
import org.apache.commons.lang3.StringUtils;
import topextras.Utilities;

public class PaintingInfoProvider implements IProbeInfoEntityProvider {

@Override
public String getID() {
return Utilities.getProviderId("painting");
}

@Override
public void addProbeEntityInfo(ProbeMode mode, IProbeInfo probeInfo, EntityPlayer player, World world, Entity entity, IProbeHitEntityData data) {
if (entity instanceof EntityPainting) {
NBTTagCompound compound = entity.writeToNBT(new NBTTagCompound());
if (compound.hasKey("Motive")) {
probeInfo.text(TextStyleClass.INFO + TextFormatting.ITALIC.toString() +
StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(compound.getString("Motive")), ' '));
}
}
}
}

0 comments on commit 7d0368c

Please sign in to comment.