com.builtbroken.icbm.content.items.ItemRadarGun.java Source code

Java tutorial

Introduction

Here is the source code for com.builtbroken.icbm.content.items.ItemRadarGun.java

Source

package com.builtbroken.icbm.content.items;

import com.builtbroken.icbm.ICBM;
import com.builtbroken.icbm.api.launcher.ILauncher;
import com.builtbroken.icbm.content.launcher.TileAbstractLauncher;
import com.builtbroken.mc.api.items.tools.IWorldPosItem;
import com.builtbroken.mc.api.tile.multiblock.IMultiTile;
import com.builtbroken.mc.api.tile.multiblock.IMultiTileHost;
import com.builtbroken.mc.core.Engine;
import com.builtbroken.mc.core.network.IPacketReceiver;
import com.builtbroken.mc.core.network.packet.PacketPlayerItem;
import com.builtbroken.mc.core.network.packet.PacketType;
import com.builtbroken.mc.core.registry.implement.IPostInit;
import com.builtbroken.mc.lib.helper.LanguageUtility;
import com.builtbroken.mc.lib.helper.recipe.OreNames;
import com.builtbroken.mc.lib.helper.recipe.UniversalRecipe;
import com.builtbroken.mc.lib.transform.vector.Location;
import com.builtbroken.mc.prefab.items.ItemWorldPos;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.IIcon;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;
import net.minecraftforge.oredict.ShapedOreRecipe;

import java.util.List;

/**
 * @see <a href="https://github.com/BuiltBrokenModding/VoltzEngine/blob/development/license.md">License</a> for what you can and can't do with the code.
 * Created by Dark(DarkGuardsman, Robert) on 6/13/2016.
 */
public class ItemRadarGun extends ItemWorldPos implements IWorldPosItem, IPostInit, IPacketReceiver {
    IIcon linked_icon;

    public ItemRadarGun() {
        this.setMaxStackSize(1);
        this.setHasSubtypes(true);
        this.setUnlocalizedName(ICBM.PREFIX + "radarGun");
    }

    @Override
    public void onPostInit() {
        GameRegistry.addRecipe(new ShapedOreRecipe(new ItemStack(this), "RGR", "WCB", "WIB", 'I', ICBM.itemGPSTool,
                'B', OreNames.ROD_IRON, 'C', UniversalRecipe.CIRCUIT_T2.get(), 'G', Items.glass_bottle, 'W',
                OreNames.WIRE_GOLD, 'R', OreNames.ROD_COPPER));
    }

    @SideOnly(Side.CLIENT)
    @Override
    public void addInformation(ItemStack stack, EntityPlayer player, List lines, boolean b) {
        String localization = LanguageUtility.getLocal(getUnlocalizedName() + ".info");
        if (localization != null && !localization.isEmpty()) {
            String[] split = localization.split(",");
            for (String line : split) {
                lines.add(line.trim());
            }
        }
    }

    @SideOnly(Side.CLIENT)
    @Override
    public void registerIcons(IIconRegister reg) {
        this.itemIcon = reg.registerIcon(ICBM.PREFIX + "radargun.unlinked");
        this.linked_icon = reg.registerIcon(ICBM.PREFIX + "radargun.linked");
    }

    @Override
    public IIcon getIconFromDamage(int meta) {
        if (meta == 1) {
            return this.linked_icon;
        }
        return this.itemIcon;
    }

    @Override
    public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player) {
        if (world.isRemote) {
            MovingObjectPosition objectMouseOver = player.rayTrace(200, 1);
            TileEntity tileEntity = world.getTileEntity(objectMouseOver.blockX, objectMouseOver.blockY,
                    objectMouseOver.blockZ);
            if (!(tileEntity instanceof ILauncher)) {
                Engine.instance.packetHandler.sendToServer(new PacketPlayerItem(player, objectMouseOver.blockX,
                        objectMouseOver.blockY, objectMouseOver.blockZ));
            }
        }
        return stack;
    }

    @Override
    public boolean onItemUse(ItemStack stack, EntityPlayer player, World world, int x, int y, int z, int side,
            float hit_x, float hit_y, float hit_z) {
        if (world.isRemote) {
            return true;
        }

        Location location = new Location(world, x, y, z);
        TileEntity tile = location.getTileEntity();
        if (tile instanceof IMultiTile) {
            IMultiTileHost host = ((IMultiTile) tile).getHost();
            if (host instanceof TileEntity) {
                tile = (TileEntity) host;
            }
        }

        if (player.isSneaking()) {
            stack.setTagCompound(null);
            stack.setItemDamage(0);
            LanguageUtility.addChatToPlayer(player, "gps.cleared");
            player.inventoryContainer.detectAndSendChanges();
            return true;
        } else {
            Location storedLocation = getLocation(stack);
            if (storedLocation == null || !storedLocation.isAboveBedrock()) {
                LanguageUtility.addChatToPlayer(player, "gps.error.pos.invalid");
                return true;
            } else if (tile instanceof TileAbstractLauncher) {
                ((TileAbstractLauncher) tile).setTarget(storedLocation.toPos());
                LanguageUtility.addChatToPlayer(player, "gps.data.transferred");
                return true;
            }
        }
        return false;
    }

    @Override
    public void read(ByteBuf buf, EntityPlayer player, PacketType packet) {
        ItemStack stack = player.inventory.getCurrentItem();
        if (stack != null && stack.getItem() == this) {
            setLocation(stack, new Location(player.worldObj, buf.readInt(), buf.readInt(), buf.readInt()));
            player.addChatComponentMessage(new ChatComponentText("GPS data set"));
        }
    }
}