Search in sources :

Example 31 with SubscribeEvent

use of cpw.mods.fml.common.eventhandler.SubscribeEvent in project BetterStorage by copygirl.

the class BackpackHandler method onSpecialSpawn.

@SubscribeEvent
public void onSpecialSpawn(SpecialSpawn event) {
    // When a mob spawns naturally, see if it has a chance to spawn with a backpack.
    EntityLivingBase entity = event.entityLiving;
    World world = entity.worldObj;
    double probability = 0.0;
    for (BetterStorageBackpack.BackpackSpawnEntry entry : BetterStorageBackpack.spawnWithBackpack) {
        if (!entity.getClass().equals(entry.entityClass))
            continue;
        probability = entry.probability;
        break;
    }
    if (!RandomUtils.getBoolean(probability) || entity.isChild())
        return;
    // If entity is a vanilla enderman, replace it with a friendly one.
    if (entity.getClass().equals(EntityEnderman.class)) {
        if ((BetterStorageTiles.enderBackpack != null) && // Don't spawn friendly endermen in the end or end biome, would make them too easy to get.
        (world.provider.dimensionId != 1) && (world.getBiomeGenForCoords((int) entity.posX, (int) entity.posZ) != BiomeGenBase.sky)) {
            EntityFrienderman frienderman = new EntityFrienderman(world);
            frienderman.setPositionAndRotation(entity.posX, entity.posY, entity.posZ, entity.rotationYaw, 0);
            world.spawnEntityInWorld(frienderman);
            ItemBackpack.getBackpackData(frienderman).spawnsWithBackpack = true;
            entity.setDead();
        }
    // Otherwise, just mark it to spawn with a backpack.
    } else if (BetterStorageTiles.backpack != null)
        ItemBackpack.getBackpackData(entity).spawnsWithBackpack = true;
}
Also used : EntityFrienderman(net.mcft.copy.betterstorage.entity.EntityFrienderman) EntityLivingBase(net.minecraft.entity.EntityLivingBase) World(net.minecraft.world.World) BetterStorageBackpack(net.mcft.copy.betterstorage.api.BetterStorageBackpack) SubscribeEvent(cpw.mods.fml.common.eventhandler.SubscribeEvent)

Example 32 with SubscribeEvent

use of cpw.mods.fml.common.eventhandler.SubscribeEvent in project BetterStorage by copygirl.

the class BackpackHandler method onLivingUpdate.

@SubscribeEvent
public void onLivingUpdate(LivingUpdateEvent event) {
    EntityLivingBase entity = event.entityLiving;
    EntityPlayer player = ((entity instanceof EntityPlayer) ? (EntityPlayer) entity : null);
    ItemStack backpack = ItemBackpack.getBackpack(entity);
    PropertiesBackpack backpackData;
    if (backpack == null) {
        backpackData = EntityUtils.getProperties(entity, PropertiesBackpack.class);
        if (backpackData == null)
            return;
        // with a backpack, equip it with one.
        if (backpackData.spawnsWithBackpack) {
            ItemStack[] contents = null;
            if (entity instanceof EntityFrienderman) {
                backpack = new ItemStack(BetterStorageItems.itemEnderBackpack);
                // Remove drop chance for the backpack.
                ((EntityLiving) entity).setEquipmentDropChance(EquipmentSlot.CHEST, 0.0F);
            } else {
                backpack = new ItemStack(BetterStorageItems.itemBackpack, 1, RandomUtils.getInt(120, 240));
                ItemBackpack backpackType = (ItemBackpack) backpack.getItem();
                if (RandomUtils.getBoolean(0.15)) {
                    // Give the backpack a random color.
                    int r = RandomUtils.getInt(32, 224);
                    int g = RandomUtils.getInt(32, 224);
                    int b = RandomUtils.getInt(32, 224);
                    int color = (r << 16) | (g << 8) | b;
                    StackUtils.set(backpack, color, "display", "color");
                }
                contents = new ItemStack[backpackType.getBackpackColumns() * backpackType.getBackpackRows()];
                // Set drop chance for the backpack to 100%.
                ((EntityLiving) entity).setEquipmentDropChance(EquipmentSlot.CHEST, 1.0F);
            }
            // If the entity spawned with enchanted armor,
            // move the enchantments over to the backpack.
            ItemStack armor = entity.getEquipmentInSlot(EquipmentSlot.CHEST);
            if (armor != null && armor.isItemEnchanted()) {
                NBTTagCompound compound = new NBTTagCompound();
                compound.setTag("ench", armor.getTagCompound().getTag("ench"));
                backpack.setTagCompound(compound);
            }
            if (contents != null) {
                // Add random items to the backpack.
                InventoryStacks inventory = new InventoryStacks(contents);
                // Add normal random backpack loot.
                WeightedRandomChestContent.generateChestContents(RandomUtils.random, randomBackpackItems, inventory, 20);
                // With a chance of 10%, add some random dungeon loot.
                if (RandomUtils.getDouble() < 0.1) {
                    ChestGenHooks info = ChestGenHooks.getInfo(ChestGenHooks.DUNGEON_CHEST);
                    WeightedRandomChestContent.generateChestContents(RandomUtils.random, info.getItems(RandomUtils.random), inventory, 5);
                }
            }
            ItemBackpack.setBackpack(entity, backpack, contents);
            backpackData.spawnsWithBackpack = false;
        } else {
            // but still has some backpack data, drop the items.
            if (backpackData.contents != null) {
                for (ItemStack stack : backpackData.contents) WorldUtils.dropStackFromEntity(entity, stack, 1.5F);
                backpackData.contents = null;
            }
        }
    }
    ItemBackpack.getBackpackData(entity).update(entity);
    if (backpack != null)
        ((ItemBackpack) backpack.getItem()).onEquippedUpdate(entity, backpack);
}
Also used : EntityFrienderman(net.mcft.copy.betterstorage.entity.EntityFrienderman) EntityLiving(net.minecraft.entity.EntityLiving) EntityLivingBase(net.minecraft.entity.EntityLivingBase) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) ChestGenHooks(net.minecraftforge.common.ChestGenHooks) EntityPlayer(net.minecraft.entity.player.EntityPlayer) PropertiesBackpack(net.mcft.copy.betterstorage.misc.PropertiesBackpack) ItemBackpack(net.mcft.copy.betterstorage.item.ItemBackpack) ItemStack(net.minecraft.item.ItemStack) InventoryStacks(net.mcft.copy.betterstorage.inventory.InventoryStacks) SubscribeEvent(cpw.mods.fml.common.eventhandler.SubscribeEvent)

Example 33 with SubscribeEvent

use of cpw.mods.fml.common.eventhandler.SubscribeEvent in project BetterStorage by copygirl.

the class BackpackHandler method onEntityInteract.

@SubscribeEvent
public void onEntityInteract(EntityInteractEvent event) {
    if (event.entity.worldObj.isRemote || !(event.entity instanceof EntityPlayerMP) || !(event.target instanceof EntityLivingBase) || (((EntityPlayerMP) event.entity).playerNetServerHandler == null) || ((event.target instanceof EntityPlayer) && !BetterStorage.globalConfig.getBoolean(GlobalConfig.enableBackpackInteraction)))
        return;
    EntityPlayerMP player = (EntityPlayerMP) event.entity;
    EntityLivingBase target = (EntityLivingBase) event.target;
    if (ItemBackpack.openBackpack(player, target))
        player.swingItem();
}
Also used : EntityLivingBase(net.minecraft.entity.EntityLivingBase) EntityPlayer(net.minecraft.entity.player.EntityPlayer) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) SubscribeEvent(cpw.mods.fml.common.eventhandler.SubscribeEvent)

Example 34 with SubscribeEvent

use of cpw.mods.fml.common.eventhandler.SubscribeEvent in project BetterStorage by copygirl.

the class BackpackHandler method onPlayerRespawn.

@SubscribeEvent
public void onPlayerRespawn(PlayerRespawnEvent event) {
    // If the player dies when when keepInventory is on and respawns,
    // retrieve the backpack items from eir persistent NBT tag.
    NBTTagCompound entityData = event.player.getEntityData();
    if (!entityData.hasKey(EntityPlayer.PERSISTED_NBT_TAG))
        return;
    NBTTagCompound persistent = entityData.getCompoundTag(EntityPlayer.PERSISTED_NBT_TAG);
    if (!persistent.hasKey("Backpack"))
        return;
    NBTTagCompound compound = persistent.getCompoundTag("Backpack");
    PropertiesBackpack backpackData = ItemBackpack.getBackpackData(event.player);
    int size = compound.getInteger("count");
    ItemStack[] contents = new ItemStack[size];
    NbtUtils.readItems(contents, compound.getTagList("Items", NBT.TAG_COMPOUND));
    backpackData.contents = contents;
    if (compound.hasKey("Stack"))
        backpackData.backpack = ItemStack.loadItemStackFromNBT(compound.getCompoundTag("Stack"));
    persistent.removeTag("Backpack");
    if (persistent.hasNoTags())
        entityData.removeTag(EntityPlayer.PERSISTED_NBT_TAG);
}
Also used : NBTTagCompound(net.minecraft.nbt.NBTTagCompound) PropertiesBackpack(net.mcft.copy.betterstorage.misc.PropertiesBackpack) ItemStack(net.minecraft.item.ItemStack) SubscribeEvent(cpw.mods.fml.common.eventhandler.SubscribeEvent)

Example 35 with SubscribeEvent

use of cpw.mods.fml.common.eventhandler.SubscribeEvent in project Engine by VoltzEngine-Project.

the class ClientProxy method clientUpdate.

@SubscribeEvent
public void clientUpdate(TickEvent.WorldTickEvent event) {
    if (///TODO reduce check to every 10 ticks
    Engine.enableExtendedMetaPacketSync) {
        try {
            if (event.side == Side.CLIENT) {
                Minecraft mc = Minecraft.getMinecraft();
                if (mc != null) {
                    EntityPlayer player = mc.thePlayer;
                    World world = mc.theWorld;
                    if (player != null && world != null) {
                        if (ExtendedBlockDataManager.CLIENT.dimID != world.provider.dimensionId) {
                            ExtendedBlockDataManager.CLIENT.clear();
                            ExtendedBlockDataManager.CLIENT.dimID = world.provider.dimensionId;
                        }
                        int renderDistance = mc.gameSettings.renderDistanceChunks + 2;
                        int centerX = ((int) Math.floor(player.posX)) >> 4;
                        int centerZ = ((int) Math.floor(player.posZ)) >> 4;
                        //Clear out chunks outside of render distance
                        List<ChunkData> chunksToRemove = new ArrayList();
                        for (ChunkData data : ExtendedBlockDataManager.CLIENT.chunks.values()) {
                            if (Math.abs(data.position.chunkXPos - centerX) > renderDistance || Math.abs(data.position.chunkZPos - centerZ) > renderDistance) {
                                chunksToRemove.add(data);
                            }
                        }
                        for (ChunkData data : chunksToRemove) {
                            ExtendedBlockDataManager.CLIENT.chunks.remove(data.position);
                        }
                        renderDistance = mc.gameSettings.renderDistanceChunks;
                        for (int x = centerX - renderDistance; x < centerX + renderDistance; x++) {
                            for (int z = centerZ - renderDistance; z < centerZ + renderDistance; z++) {
                                ChunkData chunkData = ExtendedBlockDataManager.CLIENT.getChunk(x, z);
                                if (chunkData == null) {
                                    Engine.instance.packetHandler.sendToServer(new PacketRequestData(world.provider.dimensionId, x, z, 0));
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            Engine.logger().error("Unexpected error while updating client chunk data state", e);
        }
    }
}
Also used : ChunkData(com.builtbroken.mc.lib.world.map.data.ChunkData) PacketRequestData(com.builtbroken.mc.core.network.packet.callback.chunk.PacketRequestData) ArrayList(java.util.ArrayList) EntityPlayer(net.minecraft.entity.player.EntityPlayer) World(net.minecraft.world.World) Minecraft(net.minecraft.client.Minecraft) SubscribeEvent(cpw.mods.fml.common.eventhandler.SubscribeEvent)

Aggregations

SubscribeEvent (cpw.mods.fml.common.eventhandler.SubscribeEvent)109 EntityPlayer (net.minecraft.entity.player.EntityPlayer)45 ItemStack (net.minecraft.item.ItemStack)45 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)14 EntityLivingBase (net.minecraft.entity.EntityLivingBase)11 World (net.minecraft.world.World)11 Minecraft (net.minecraft.client.Minecraft)10 ChunkPosition (net.minecraft.world.ChunkPosition)9 EntityItem (net.minecraft.entity.item.EntityItem)8 ChatComponentText (net.minecraft.util.ChatComponentText)8 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)6 AffinityData (am2.playerextensions.AffinityData)5 SideOnly (cpw.mods.fml.relauncher.SideOnly)5 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 Block (net.minecraft.block.Block)5 EntityEnderman (net.minecraft.entity.monster.EntityEnderman)5 ChunkCoordIntPair (net.minecraft.world.ChunkCoordIntPair)5 ExtendedProperties (am2.playerextensions.ExtendedProperties)4